Skip to content
This repository was archived by the owner on Jan 7, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.phenopackets.validator.core.except.PhenopacketValidatorRuntimeException;
import org.phenopackets.validator.jsonschema.JsonSchemaValidators;
import org.phenopackets.validator.jsonschema.JsonSchemaValidator;
import org.phenopackets.validator.ontology.HpoValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface PhenopacketValidator {

ValidatorInfo info();

List<ValidationItem> validate(InputStream inputStream);
List<ValidationItem> validate(String content); ;

// -----------------------------------------------------------------------------------------------------------------

Expand All @@ -29,8 +29,8 @@ default List<ValidationItem> validate(File phenopacket) throws IOException {
}
}

default List<ValidationItem> validate(String content) {
return validate(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
default List<ValidationItem> validate(byte[] bytes){
return validate(new String(bytes));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public ValidatorInfo info() {
* @return List of {@link ValidationItem} objects (empty list if there were no errors)
*/
@Override
public List<ValidationItem> validate(InputStream inputStream) {
public List<ValidationItem> validate(String jsonString) {
List<ValidationItem> errors = new ArrayList<>();
try {
JsonNode json = objectMapper.readTree(inputStream);
JsonNode json = objectMapper.readTree(jsonString);
jsonSchema.validate(json)
.forEach(e -> errors.add(ValidationItem.of(validatorInfo, stringToErrorType(e.getType()), e.getMessage())));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public HpoValidator(Ontology hpoOntology) {
super(hpoOntology, ValidatorInfo.of(ONTOLOGY_VALIDATOR, "Human Phenotype Ontology"));
}

private static Phenopacket readPhenopacket(InputStream inputStream) throws IOException {
String phenopacketJsonString = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
private static Phenopacket readPhenopacket(String phenopacketJsonString) throws IOException {
Phenopacket.Builder phenoPacketBuilder = Phenopacket.newBuilder();
JsonFormat.parser().merge(phenopacketJsonString, phenoPacketBuilder);
return phenoPacketBuilder.build();
Expand All @@ -56,10 +55,10 @@ public ValidatorInfo info() {
}

@Override
public List<ValidationItem> validate(InputStream inputStream) {
public List<ValidationItem> validate(String jsonString) {
Phenopacket phenopacket;
try {
phenopacket = readPhenopacket(inputStream);
phenopacket = readPhenopacket(jsonString);
} catch (IOException e) {
LOGGER.warn("Error while validating: {}", e.getMessage(), e);
return List.of(ValidationItem.of(validatorInfo, ValidationItemTypes.syntaxError(), e.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class HpoValidatorTest {

public HpoValidator validator;

private static byte[] wrapInPhenopacketAndGetJsonBytes(List<PhenotypicFeature> phenotypicFeatures) throws InvalidProtocolBufferException {
private static String wrapInPhenopacketAndGetJsonString(List<PhenotypicFeature> phenotypicFeatures) throws InvalidProtocolBufferException {
Phenopacket phenopacket = Phenopacket.newBuilder()
.addAllPhenotypicFeatures(phenotypicFeatures)
.build();
return JsonFormat.printer().print(phenopacket).getBytes(StandardCharsets.UTF_8);
return JsonFormat.printer().print(phenopacket);
}

private static PhenotypicFeature makePhenotypicFeature(String termId, boolean excluded) {
Expand Down Expand Up @@ -85,9 +85,9 @@ public void disjointTerms() throws Exception {
List<PhenotypicFeature> phenotypicFeatures = List.of(
makePhenotypicFeature("HP:0000002", false),
makePhenotypicFeature("HP:0000005", false));
byte[] bytes = wrapInPhenopacketAndGetJsonBytes(phenotypicFeatures);
String json = wrapInPhenopacketAndGetJsonString(phenotypicFeatures);

List<ValidationItem> items = validator.validate(new ByteArrayInputStream(bytes));
List<ValidationItem> items = validator.validate(json);

assertThat(items, hasSize(0));
}
Expand All @@ -99,10 +99,8 @@ public void parentIsPresentWhileChildrenAreExcluded() throws Exception {
makePhenotypicFeature("HP:0000004", true),
makePhenotypicFeature("HP:0000005", true)
);
byte[] bytes = wrapInPhenopacketAndGetJsonBytes(phenotypicFeatures);

List<ValidationItem> items = validator.validate(new ByteArrayInputStream(bytes));

String json = wrapInPhenopacketAndGetJsonString(phenotypicFeatures);
List<ValidationItem> items = validator.validate(json);
assertThat(items, hasSize(0));
}

Expand All @@ -114,10 +112,8 @@ public class ValidationFails {
@Test
public void termIdIsAbsentFromOntology() throws Exception {
List<PhenotypicFeature> phenotypicFeatures = List.of(makePhenotypicFeature("HP:9999999", false));
byte[] bytes = wrapInPhenopacketAndGetJsonBytes(phenotypicFeatures);

List<ValidationItem> items = validator.validate(new ByteArrayInputStream(bytes));

String json = wrapInPhenopacketAndGetJsonString(phenotypicFeatures);
List<ValidationItem> items = validator.validate(json);
assertThat(items, hasSize(1));
ValidationItem expected = ValidationItem.of(validator.info(), OntologyValidationItemTypes.ONTOLOGY_INVALID_ID, "Could not find term id: HP:9999999");
assertThat(items.get(0), equalTo(expected));
Expand All @@ -126,13 +122,10 @@ public void termIdIsAbsentFromOntology() throws Exception {
@Test
public void nonPrimaryTermIdIsUsed() throws Exception {
List<PhenotypicFeature> phenotypicFeatures = List.of(makePhenotypicFeature("HP:9999999", false));
byte[] bytes = wrapInPhenopacketAndGetJsonBytes(phenotypicFeatures);

String json = wrapInPhenopacketAndGetJsonString(phenotypicFeatures);
when(ontology.containsTerm(TermId.of("HP:9999999"))).thenReturn(true);
when(ontology.getPrimaryTermId(TermId.of("HP:9999999"))).thenReturn(TermId.of("HP:0000001"));

List<ValidationItem> items = validator.validate(new ByteArrayInputStream(bytes));

List<ValidationItem> items = validator.validate(json);
assertThat(items, hasSize(1));
ValidationItem expected = ValidationItem.of(validator.info(),
OntologyValidationItemTypes.ONTOLOGY_TERM_WITH_ALTERNATE_ID,
Expand All @@ -145,10 +138,8 @@ public void usingTermAlongWithItsAncestor() throws Exception {
List<PhenotypicFeature> phenotypicFeatures = List.of(
makePhenotypicFeature("HP:0000003", false),
makePhenotypicFeature("HP:0000004", false));
byte[] bytes = wrapInPhenopacketAndGetJsonBytes(phenotypicFeatures);


List<ValidationItem> items = validator.validate(new ByteArrayInputStream(bytes));
String json = wrapInPhenopacketAndGetJsonString(phenotypicFeatures);
List<ValidationItem> items = validator.validate(json);

assertThat(items, hasSize(1));
ValidationItem expected = ValidationItem.of(validator.info(),
Expand All @@ -162,10 +153,8 @@ public void usingTermWhileParentTermIsExcluded() throws Exception {
List<PhenotypicFeature> phenotypicFeatures = List.of(
makePhenotypicFeature("HP:0000003", true),
makePhenotypicFeature("HP:0000004", false));
byte[] bytes = wrapInPhenopacketAndGetJsonBytes(phenotypicFeatures);


List<ValidationItem> items = validator.validate(new ByteArrayInputStream(bytes));
String json = wrapInPhenopacketAndGetJsonString(phenotypicFeatures);
List<ValidationItem> items = validator.validate(json);

assertThat(items, hasSize(1));
ValidationItem expected = ValidationItem.of(validator.info(),
Expand Down