Commit 8629c671 authored by Rasko Leinonen's avatar Rasko Leinonen

Moved validator reporting functionality to webin-cli-validator 1.0.6.

parent f6ae7e5a
......@@ -110,7 +110,7 @@ dependencies {
implementation( 'info.picocli:picocli:3.9.1' )
implementation( group: 'uk.ac.ebi.ena.sequence', name: 'sequencetools', version: '2.0.37-snapshot' )
implementation( "org.jdom:jdom:2.0.2" )
implementation( group: 'uk.ac.ebi.ena.webin-cli', name: 'webin-cli-validator', version: '1.0.5' )
implementation( group: 'uk.ac.ebi.ena.webin-cli', name: 'webin-cli-validator', version: '1.0.6' )
implementation( 'de.vandermeer:asciitable:0.3.2' )
// implementation(group: 'com.opencsv', name: 'opencsv', version: '4.5')
......
package uk.ac.ebi.ena.webin.cli;
import uk.ac.ebi.ena.webin.cli.message.source.MessageFormatSource;
import uk.ac.ebi.ena.webin.cli.validator.message.source.MessageFormatSource;
public enum WebinCliMessage implements MessageFormatSource {
......
......@@ -10,7 +10,7 @@
*/
package uk.ac.ebi.ena.webin.cli.manifest;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
public interface
ManifestFieldProcessor {
......
......@@ -10,7 +10,7 @@
*/
package uk.ac.ebi.ena.webin.cli.manifest;
import uk.ac.ebi.ena.webin.cli.message.ValidationOrigin;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationOrigin;
import java.util.ArrayList;
import java.util.List;
......
......@@ -27,11 +27,11 @@ import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.lang.StringUtils;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationOrigin;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationOrigin;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.WebinCliMessage;
import uk.ac.ebi.ena.webin.cli.message.listener.MessageListener;
import uk.ac.ebi.ena.webin.cli.validator.message.listener.MessageListener;
import uk.ac.ebi.ena.webin.cli.validator.manifest.Manifest;
public abstract class
......
......@@ -15,7 +15,7 @@ import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
public class
ManifestReaderResult
......
......@@ -12,8 +12,8 @@ package uk.ac.ebi.ena.webin.cli.manifest.processor;
import java.util.regex.Pattern;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
import uk.ac.ebi.ena.webin.cli.WebinCliMessage;
......
......@@ -12,7 +12,7 @@ package uk.ac.ebi.ena.webin.cli.manifest.processor;
import java.util.regex.Pattern;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
......
......@@ -12,8 +12,8 @@ package uk.ac.ebi.ena.webin.cli.manifest.processor;
import java.util.List;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestCVList;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
......
......@@ -12,8 +12,8 @@ package uk.ac.ebi.ena.webin.cli.manifest.processor;
import java.util.List;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
import uk.ac.ebi.ena.webin.cli.WebinCliMessage;
......
......@@ -17,8 +17,8 @@ import java.util.Set;
import java.util.stream.Collectors;
import uk.ac.ebi.ena.webin.cli.WebinCliException;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
import uk.ac.ebi.ena.webin.cli.manifest.processor.MetadataProcessorParameters;
......
......@@ -14,8 +14,8 @@ import java.util.*;
import java.util.stream.Collectors;
import uk.ac.ebi.ena.webin.cli.WebinCliException;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
import uk.ac.ebi.ena.webin.cli.manifest.processor.MetadataProcessorParameters;
......
......@@ -11,8 +11,8 @@
package uk.ac.ebi.ena.webin.cli.manifest.processor.metadata;
import uk.ac.ebi.ena.webin.cli.WebinCliException;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
import uk.ac.ebi.ena.webin.cli.manifest.processor.MetadataProcessorParameters;
......
......@@ -11,8 +11,8 @@
package uk.ac.ebi.ena.webin.cli.manifest.processor.metadata;
import uk.ac.ebi.ena.webin.cli.WebinCliException;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
import uk.ac.ebi.ena.webin.cli.manifest.processor.MetadataProcessorParameters;
......
......@@ -11,8 +11,8 @@
package uk.ac.ebi.ena.webin.cli.manifest.processor.metadata;
import uk.ac.ebi.ena.webin.cli.WebinCliException;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.validator.message.ValidationResult;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldProcessor;
import uk.ac.ebi.ena.webin.cli.manifest.ManifestFieldValue;
import uk.ac.ebi.ena.webin.cli.manifest.processor.MetadataProcessorParameters;
......
/*
* Copyright 2018-2019 EMBL - European Bioinformatics Institute
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package uk.ac.ebi.ena.webin.cli.message;
import uk.ac.ebi.ena.webin.cli.message.source.MessageSource;
import java.text.MessageFormat;
import java.util.*;
public class ValidationMessage {
public enum Severity {
ERROR,
INFO
}
private final Severity severity;
private final String message;
private LinkedList<ValidationOrigin> origin = new LinkedList<>();
private final Exception ex;
public ValidationMessage(Severity severity, MessageSource message, Object... arguments) {
this.severity = severity;
this.message = message.format(arguments);
this.ex = null;
}
public ValidationMessage(Severity severity, String message) {
this.severity = severity;
this.message = message;
this.ex = null;
}
public ValidationMessage(Severity severity, Exception ex) {
this.severity = severity;
this.message = ex.getMessage();
this.ex = ex;
}
public ValidationMessage appendOrigin(ValidationOrigin ... origin) {
this.origin.addAll(Arrays.asList(origin));
return this;
}
public ValidationMessage appendOrigin(List<ValidationOrigin> origin) {
this.origin.addAll(origin);
return this;
}
public ValidationMessage prependOrigin(ValidationOrigin ... origin) {
for (int i = origin.length - 1; i >= 0; i--) {
this.origin.addFirst(origin[i]);
}
return this;
}
public ValidationMessage prependOrigin(List<ValidationOrigin> origin) {
for (int i = origin.size() - 1; i >= 0; i--) {
this.origin.addFirst(origin.get(i));
}
return this;
}
public Severity getSeverity() {
return severity;
}
public String getMessage() {
return message;
}
public List<ValidationOrigin> getOrigin() {
return origin;
}
public Exception getException() {
return ex;
}
public static ValidationMessage error(MessageSource message, Object... arguments) {
return new ValidationMessage(Severity.ERROR, message, arguments);
}
public static ValidationMessage error(String message, Object... arguments) {
return new ValidationMessage(Severity.ERROR, MessageFormat.format(message, arguments));
}
public static ValidationMessage error(String message) {
return new ValidationMessage(Severity.ERROR, message);
}
public static ValidationMessage error(Exception ex) {
return new ValidationMessage(Severity.ERROR, ex);
}
public static ValidationMessage info(MessageSource message, Object... arguments) {
return new ValidationMessage(Severity.INFO, message, arguments);
}
public static ValidationMessage info(String message, Object... arguments) {
return new ValidationMessage(Severity.INFO, MessageFormat.format(message, arguments));
}
public static ValidationMessage info(String message) {
return new ValidationMessage(Severity.INFO, message);
}
}
package uk.ac.ebi.ena.webin.cli.message;
public class ValidationOrigin {
private final String key;
private final String value;
public ValidationOrigin(String key, Object value) {
this.key = key;
this.value = value.toString();
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public String toString() {
String str = getKey();
if (getValue() != null && !getValue().isEmpty()) {
str += ": " + getValue();
}
return str;
}
}
package uk.ac.ebi.ena.webin.cli.message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage.Severity;
import uk.ac.ebi.ena.webin.cli.message.listener.MessageListener;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ValidationResult implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(ValidationResult.class);
private final OutputStream strm;
private final ValidationResult parentResult;
private final List<ValidationOrigin> origin = new ArrayList<>();
private final List<MessageListener> listener = new ArrayList<>();
private boolean log = true;
private int infoCount = 0;
private int errorCount = 0;
/** Creates a new validation result that logs messages.
*/
public ValidationResult() {
this.strm = null;
this.parentResult = null;
}
/** Creates a new validation result that logs messages.
*/
public ValidationResult(ValidationOrigin ... origin) {
this();
this.origin.addAll(Arrays.asList(origin));
}
/** Creates a new validation result that logs messages.
*/
public ValidationResult(List<ValidationOrigin> origin) {
this();
this.origin.addAll(origin);
}
/** Creates a new validation result that writes messages
* to a file. By default also logs messages.
*/
public ValidationResult(File reportFile) {
try {
if (reportFile != null) {
this.strm = Files.newOutputStream(
reportFile.toPath(),
StandardOpenOption.CREATE,
StandardOpenOption.APPEND,
StandardOpenOption.SYNC);
}
else {
this.strm = null;
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
this.parentResult = null;
}
/** Creates a new validation result that writes messages
* to a file. By default also logs messages.
*/
public ValidationResult(File reportFile, ValidationOrigin ... origin) {
this(reportFile);
this.origin.addAll(Arrays.asList(origin));
}
/** Creates a new validation result that writes messages
* to a file. By default also logs messages.
*/
public ValidationResult(File reportFile, List<ValidationOrigin> origin) {
this(reportFile);
this.origin.addAll(origin);
}
private ValidationResult(ValidationResult parentResult) {
this.strm = null;
this.parentResult = parentResult;
}
private ValidationResult(ValidationResult parentResult, ValidationOrigin ... origin) {
this(parentResult);
this.origin.addAll(Arrays.asList(origin));
}
private ValidationResult(ValidationResult parentResult, List<ValidationOrigin> origin) {
this(parentResult);
this.origin.addAll(origin);
}
/** Creates a new validation result that is linked to this validation result.
*/
public ValidationResult create() {
return new ValidationResult(this);
}
/** Creates a new validation result that is linked to this validation result.
*/
public ValidationResult create(ValidationOrigin ... origin) {
return new ValidationResult(this, origin);
}
/** Creates a new validation result that is linked to this validation result.
*/
public ValidationResult create(List<ValidationOrigin> origin) {
return new ValidationResult(this, origin);
}
public boolean isLog() {
return log;
}
/** Enables or disables message logging.
*/
public void setLog(boolean log) {
this.log = log;
}
/** Adds a new validation message to the validation result.
* The validation message will be included in all linked
* validation results and will contain all the origins
* associated with them.
*/
public ValidationResult add(ValidationMessage message) {
if (Severity.ERROR.equals(message.getSeverity())) {
errorCount++;
}
else {
infoCount++;
}
listener.forEach(l -> l.listen(message));
message.prependOrigin(origin);
if (parentResult != null) {
parentResult.add(message);
}
// Write messages only once.
if (this.parentResult == null) {
if (strm != null) {
report(message);
}
if (this.log) {
log(message);
}
}
return this;
}
private void report(ValidationMessage message) {
try {
String str = formatForReport(message) + "\n";
strm.write(str.getBytes(StandardCharsets.UTF_8));
} catch (IOException ex) {
if (!this.log) {
log(message);
}
}
}
private void log(ValidationMessage message) {
String str = formatForLog(message);
if (ValidationMessage.Severity.ERROR.equals(message.getSeverity())) {
LOGGER.error(str);
} else {
LOGGER.info(str);
}
}
public static String formatForLog(ValidationMessage message) {
String originStr = "";
if (!message.getOrigin().isEmpty()) {
originStr = " " + message.getOrigin().stream()
.map(origin -> origin.toString())
.collect(Collectors.joining(", ", "[", "]"));
}
return String.format("%s%s",
message.getMessage(),
originStr);
}
public static String formatForReport(ValidationMessage message) {
return String.format("%s: %s", message.getSeverity(),
formatForLog(message));
}
/** Adds a new validation listener to the validation result.
*/
public ValidationResult add(MessageListener listener) {
this.listener.add(listener);
return this;
}
/** Returns true if this validation result does not contain
* any validation messages with ERROR severity.
*/
public boolean isValid() {
return errorCount == 0;
}
/** Returns the number of validation messages in this
* validation result.
*/
public long count() {
return infoCount + errorCount;
}
/** Returns the number of validation messages in this
* validation result for a given severity.
*/
public long count(Severity severity) {
if (Severity.ERROR.equals(severity)) {
return errorCount;
}
else {
return infoCount;
}
}
@Override
public void close() {
if (strm != null) {
try {
strm.flush();
strm.close();
} catch (IOException ex) {
}
}
}
}
package uk.ac.ebi.ena.webin.cli.message.listener;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage;
import uk.ac.ebi.ena.webin.cli.message.ValidationMessage.Severity;
import java.util.function.Predicate;
public class MessageCounter implements MessageListener {
private final Severity severity;
private final Predicate<String> messagePredicate;
private int count = 0;
public MessageCounter(Severity severity, Predicate<String> messagePredicate) {
this.severity = severity;
this.messagePredicate = messagePredicate;
}
@Override
public void listen(ValidationMessage message) {
if (severity.equals(message.getSeverity()) &&
messagePredicate.test(message.getMessage())) {
count++;
}
}
public int getCount() {
return count;
}
public static MessageCounter text(Severity severity, String text) {
return new MessageCounter(severity, m -> m.equals(text));
}
public static MessageCounter regex(Severity severity, String regex) {
return new MessageCounter(severity, m -> m.matches(regex));
}
}