Skip to content

Commit

Permalink
GEN-6: replace the json schema validator with the network nt validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aravinda Baliga B committed Feb 29, 2024
1 parent 6cd5664 commit 644ab6b
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 114 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@
<scope>provided</scope>
</dependency>

<!-- To validate generated JSON event against JSON Schema-->
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
78 changes: 36 additions & 42 deletions src/main/java/io/openepcis/convert/validator/EventValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,47 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.google.common.io.Resources;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import io.openepcis.constants.EPCIS;
import io.openepcis.convert.exception.FormatConverterException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import jakarta.ws.rs.ProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import lombok.extern.slf4j.Slf4j;
import org.xml.sax.SAXException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Set;

/**
* Class to validate each of the converted event against respective events XSD or JSON-Schema file
* which is stored in resources folder. If EPCIS event does not adhere to XSD/JSON-Schema then
* respective information's are shown in Log but the information will be added to final
* OutputStream. If EPCIS event adheres to XSD/JSON-Schema then no information will be logged.
* Class to validate each of the converted event against respective events XSD or JSON-Schema file which is stored in resources folder. If EPCIS event does not adhere to XSD/JSON-Schema then respective information's are shown in Log but the
* information will be added to final OutputStream. If EPCIS event adheres to XSD/JSON-Schema then no information will be logged.
*/
@Slf4j
public class EventValidator implements EPCISEventValidator {

private final Schema xsdSchema;

private final ObjectMapper objectMapper = new ObjectMapper();
private final JsonSchemaFactory validatorFactory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(objectMapper).build();

public EventValidator() {
try {
xsdSchema =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(
new StreamSource(
EventValidator.class
.getClassLoader()
.getResourceAsStream("eventSchemas/EPCISEventXSD.xsd")));
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(
new StreamSource(
EventValidator.class
.getClassLoader()
.getResourceAsStream("eventSchemas/EPCISEventXSD.xsd")));

} catch (SAXException e) {
throw new FormatConverterException(e);
Expand Down Expand Up @@ -89,7 +90,7 @@ public void validate(Object event) {

try {
// Get the JSONNode from the Event and what type of event
final JsonNode parent = new ObjectMapper().readTree(convertedEvent).get(EPCIS.TYPE);
final JsonNode parent = objectMapper.readTree(convertedEvent).get(EPCIS.TYPE);

// If the epcisEvent is not null then continue with validation
if (parent != null && parent.textValue() != null) {
Expand All @@ -101,31 +102,24 @@ public void validate(Object event) {

// Based on eventType choose different Schema file for the validation
switch (epcisEvent) {
case EPCIS.OBJECT_EVENT -> schemaFile = "eventSchemas/ObjectEventSchema.json";
case EPCIS.AGGREGATION_EVENT -> schemaFile = "eventSchemas/AggregationEventSchema.json";
case EPCIS.TRANSACTION_EVENT -> schemaFile = "eventSchemas/TransactionEventSchema.json";
case EPCIS.TRANSFORMATION_EVENT -> schemaFile =
"eventSchemas/TransformationEventSchema.json";
case EPCIS.ASSOCIATION_EVENT -> schemaFile = "eventSchemas/AssociationEventSchema.json";
case EPCIS.OBJECT_EVENT -> schemaFile = "/eventSchemas/ObjectEventSchema.json";
case EPCIS.AGGREGATION_EVENT -> schemaFile = "/eventSchemas/AggregationEventSchema.json";
case EPCIS.TRANSACTION_EVENT -> schemaFile = "/eventSchemas/TransactionEventSchema.json";
case EPCIS.TRANSFORMATION_EVENT -> schemaFile = "/eventSchemas/TransformationEventSchema.json";
case EPCIS.ASSOCIATION_EVENT -> schemaFile = "/eventSchemas/AssociationEventSchema.json";
default ->
// If NONE of the EPCIS event type matches
log.error(
"JSON event does not match any of EPCIS event. However, proceeding to next event from EventList");
// If NONE of the EPCIS event type matches
log.error("JSON event does not match any of EPCIS event. However, proceeding to next event from EventList");
}

// Get the schema file based on different schema
final String schemaString =
Resources.toString(Resources.getResource(schemaFile), StandardCharsets.UTF_8);
final JsonSchema jsonSchema =
JsonSchemaFactory.byDefault().getJsonSchema(JsonLoader.fromString(schemaString));
final ProcessingReport report =
jsonSchema.validate(JsonLoader.fromString((String) event));
// Get the schema file based on different schema and validate them
final JsonSchema jsonSchema = validatorFactory.getSchema(getClass().getResourceAsStream(schemaFile));
final Set<ValidationMessage> validationErrors = jsonSchema.validate(objectMapper.readValue((String) event, JsonNode.class));

if (report.isSuccess()) {
if (validationErrors.isEmpty()) {
log.debug("Event adheres to EPCIS Standard JSON-LD Schema");
} else {
log.warn(
"Event Does NOT adhere to EPCIS Standard JSON-LD Schema. However, proceeding to next event from EventList");
log.warn("Event Does NOT adhere to EPCIS Standard JSON-LD Schema. However, proceeding to next event from EventList");
}
} else {
log.error(
Expand Down
28 changes: 14 additions & 14 deletions src/main/resources/eventSchemas/AggregationEventSchema.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:jaxb:AggregationEvent",
"ref": "urn:jsonschema:io:openepcis:model:jaxb:AggregationEvent",
"required": [
"isA",
"type",
"eventTime",
"eventTimeZoneOffset",
"action"
],
"properties": {
"isA": {
"type": {
"type": "string"
},
"eventTime": {
Expand All @@ -28,7 +28,7 @@
},
"errorDeclaration": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:jaxb:ErrorDeclaration",
"ref": "urn:jsonschema:io:openepcis:model:jaxb:ErrorDeclaration",
"properties": {
"declarationTime": {
"type": "string",
Expand Down Expand Up @@ -72,7 +72,7 @@
},
"readPoint": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:ReadPoint",
"ref": "urn:jsonschema:io:openepcis:model:core:ReadPoint",
"properties": {
"id": {
"type": "string"
Expand All @@ -81,7 +81,7 @@
},
"bizLocation": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:BizLocation",
"ref": "urn:jsonschema:io:openepcis:model:core:BizLocation",
"properties": {
"id": {
"type": "string"
Expand All @@ -92,7 +92,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:BizTransactionList",
"ref": "urn:jsonschema:io:openepcis:model:core:BizTransactionList",
"properties": {
"type": {
"type": "string"
Expand All @@ -107,7 +107,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:QuantityList",
"ref": "urn:jsonschema:io:openepcis:model:core:QuantityList",
"properties": {
"epcClass": {
"type": "string"
Expand All @@ -125,7 +125,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:SourceList",
"ref": "urn:jsonschema:io:openepcis:model:core:SourceList",
"properties": {
"type": {
"type": "string"
Expand All @@ -140,7 +140,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:DestinationList",
"ref": "urn:jsonschema:io:openepcis:model:core:DestinationList",
"properties": {
"type": {
"type": "string"
Expand All @@ -155,11 +155,11 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:SensorElementList",
"ref": "urn:jsonschema:io:openepcis:model:core:SensorElementList",
"properties": {
"sensorMetadata": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:SensorMetadata",
"ref": "urn:jsonschema:io:openepcis:model:core:SensorMetadata",
"properties": {
"time": {
"type": "string",
Expand Down Expand Up @@ -199,7 +199,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:SensorReport",
"ref": "urn:jsonschema:io:openepcis:model:core:SensorReport",
"properties": {
"type": {
"type": "string",
Expand Down Expand Up @@ -282,7 +282,7 @@
},
"persistentDisposition": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:PersistentDisposition",
"ref": "urn:jsonschema:io:openepcis:model:core:PersistentDisposition",
"properties": {
"set": {
"type": "array",
Expand Down
28 changes: 14 additions & 14 deletions src/main/resources/eventSchemas/AssociationEventSchema.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:jaxb:AssociationEvent",
"ref": "urn:jsonschema:io:openepcis:model:jaxb:AssociationEvent",
"required": [
"isA",
"type",
"eventTime",
"eventTimeZoneOffset",
"action",
"parentID"
],
"properties": {
"isA": {
"type": {
"type": "string"
},
"eventTime": {
Expand All @@ -29,7 +29,7 @@
},
"errorDeclaration": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:jaxb:ErrorDeclaration",
"ref": "urn:jsonschema:io:openepcis:model:jaxb:ErrorDeclaration",
"properties": {
"declarationTime": {
"type": "string",
Expand Down Expand Up @@ -61,7 +61,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:QuantityList",
"ref": "urn:jsonschema:io:openepcis:model:core:QuantityList",
"properties": {
"epcClass": {
"type": "string"
Expand Down Expand Up @@ -91,7 +91,7 @@
},
"readPoint": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:ReadPoint",
"ref": "urn:jsonschema:io:openepcis:model:core:ReadPoint",
"properties": {
"id": {
"type": "string"
Expand All @@ -100,7 +100,7 @@
},
"bizLocation": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:BizLocation",
"ref": "urn:jsonschema:io:openepcis:model:core:BizLocation",
"properties": {
"id": {
"type": "string"
Expand All @@ -111,7 +111,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:BizTransactionList",
"ref": "urn:jsonschema:io:openepcis:model:core:BizTransactionList",
"properties": {
"type": {
"type": "string"
Expand All @@ -126,7 +126,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:SourceList",
"ref": "urn:jsonschema:io:openepcis:model:core:SourceList",
"properties": {
"type": {
"type": "string"
Expand All @@ -141,7 +141,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:DestinationList",
"ref": "urn:jsonschema:io:openepcis:model:core:DestinationList",
"properties": {
"type": {
"type": "string"
Expand All @@ -156,11 +156,11 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:SensorElementList",
"ref": "urn:jsonschema:io:openepcis:model:core:SensorElementList",
"properties": {
"sensorMetadata": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:SensorMetadata",
"ref": "urn:jsonschema:io:openepcis:model:core:SensorMetadata",
"properties": {
"time": {
"type": "string",
Expand Down Expand Up @@ -200,7 +200,7 @@
"type": "array",
"items": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:SensorReport",
"ref": "urn:jsonschema:io:openepcis:model:core:SensorReport",
"properties": {
"type": {
"type": "string",
Expand Down Expand Up @@ -283,7 +283,7 @@
},
"persistentDisposition": {
"type": "object",
"id": "urn:jsonschema:io:openepcis:model:core:PersistentDisposition",
"ref": "urn:jsonschema:io:openepcis:model:core:PersistentDisposition",
"properties": {
"set": {
"type": "array",
Expand Down
Loading

0 comments on commit 644ab6b

Please sign in to comment.