diff options
author | adheli.tavares <adheli.tavares@est.tech> | 2024-07-19 11:51:37 +0100 |
---|---|---|
committer | adheli.tavares <adheli.tavares@est.tech> | 2024-07-19 11:55:19 +0100 |
commit | b8c49fd30e2e660198d29e599a0ccecf184b2071 (patch) | |
tree | 3a45c42aabee26b5b4abcca5e5922896ce4bcf79 /plugins | |
parent | 9551484946f5c53f9a0ea37c3b0a7810b5d77fab (diff) |
Uplift of json schema validator library
Issue-ID: POLICY-5084
Change-Id: Ie24719570c43a9f7b0fdac28973cc50d8eb7ed2c
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'plugins')
4 files changed, 44 insertions, 25 deletions
diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/pom.xml b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/pom.xml index c46b26271..99864b304 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/pom.xml +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/pom.xml @@ -1,7 +1,7 @@ <!-- ============LICENSE_START======================================================= Copyright (C) 2022 Bell Canada. All rights reserved. - Modifications Copyright (C) 2023 Nordix Foundation. + Modifications Copyright (C) 2023-2024 Nordix Foundation. ================================================================================ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -33,8 +33,8 @@ <dependencies> <dependency> - <groupId>com.worldturner.medeia</groupId> - <artifactId>medeia-validator-gson</artifactId> + <groupId>com.networknt</groupId> + <artifactId>json-schema-validator</artifactId> </dependency> </dependencies> </project> diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/main/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelper.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/main/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelper.java index 4896e5d4a..0e8d5dfc8 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/main/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelper.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/main/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelper.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2022 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,18 +23,18 @@ package org.onap.policy.apex.plugins.context.schema.json; import com.google.gson.Gson; import com.google.gson.JsonElement; -import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; -import com.worldturner.medeia.api.SchemaSource; -import com.worldturner.medeia.api.StringInputSource; -import com.worldturner.medeia.api.StringSchemaSource; -import com.worldturner.medeia.api.gson.MedeiaGsonApi; -import com.worldturner.medeia.schema.validation.SchemaValidator; +import com.networknt.schema.InputFormat; +import com.networknt.schema.JsonSchema; +import com.networknt.schema.JsonSchemaFactory; +import com.networknt.schema.SpecVersionDetector; +import java.io.StringReader; import java.io.StringWriter; import java.util.List; import java.util.Map; import org.onap.policy.apex.context.ContextRuntimeException; import org.onap.policy.apex.context.impl.schema.AbstractSchemaHelper; +import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; import org.onap.policy.apex.model.basicmodel.concepts.AxKey; import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema; @@ -44,16 +45,14 @@ public class JsonSchemaHelper extends AbstractSchemaHelper { private static final Gson gson = new Gson(); - private MedeiaGsonApi api = new MedeiaGsonApi(); - private SchemaValidator validator; + private JsonSchema jsonSchema; @Override public void init(final AxKey userKey, final AxContextSchema schema) { super.init(userKey, schema); try { - SchemaSource source = new StringSchemaSource(schema.getSchema()); - validator = api.loadSchema(source); + this.jsonSchema = getJsonSchema(schema.getSchema()); } catch (final Exception e) { final String resultSting = userKey.getId() + ": json context schema \"" + schema.getId() + "\" schema is invalid, schema: " + schema.getSchema(); @@ -86,8 +85,8 @@ public class JsonSchemaHelper extends AbstractSchemaHelper { return object; } var objectString = (String) object; - JsonReader reader = api.createJsonReader(validator, new StringInputSource(objectString)); - return gson.fromJson(reader, Object.class); + validate(objectString); + return gson.fromJson(new StringReader(objectString), Object.class); } @Override @@ -103,7 +102,8 @@ public class JsonSchemaHelper extends AbstractSchemaHelper { } private JsonElement validateAndDecode(Object schemaObject, StringWriter stringWriter) { - JsonWriter jsonWriter = api.createJsonWriter(validator, stringWriter); + validate(schemaObject); + JsonWriter jsonWriter = new JsonWriter(stringWriter); jsonWriter.setIndent(" "); // to enable pretty print JsonElement jsonObj = gson.toJsonTree(schemaObject); gson.toJson(jsonObj, jsonWriter); @@ -119,4 +119,27 @@ public class JsonSchemaHelper extends AbstractSchemaHelper { private boolean passThroughObject(final Object object) { return (object instanceof JsonElement || object instanceof Map || object instanceof List); } + + private JsonSchema getJsonSchema(String jsonSchema) { + var schemaMap = new Gson().fromJson(jsonSchema, Map.class); + if (schemaMap != null && schemaMap.containsKey("$schema")) { + var flag = SpecVersionDetector.detectOptionalVersion(schemaMap.get("$schema").toString()).orElse(null); + return JsonSchemaFactory.getInstance(flag).getSchema(jsonSchema); + } else { + throw new ApexRuntimeException("Schema is invalid"); + } + } + + private void validate(String json) { + var validations = this.jsonSchema.validate(json, InputFormat.JSON); + if (!validations.isEmpty()) { + StringBuilder errors = new StringBuilder(); + validations.forEach(m -> errors.append(m.toString()).append("\n")); + throw new ApexRuntimeException(errors.toString()); + } + } + + private void validate(Object object) { + validate(gson.toJson(object)); + } } diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/test/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelperMarshalTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/test/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelperMarshalTest.java index b129330ce..413037b97 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/test/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelperMarshalTest.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/test/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelperMarshalTest.java @@ -26,7 +26,6 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import com.worldturner.medeia.api.ValidationFailedException; import java.util.ArrayList; import java.util.Map; import org.junit.jupiter.api.Test; @@ -103,8 +102,7 @@ class JsonSchemaHelperMarshalTest extends CommonTestData { var dataAsObject = coder.decode(COMMONHEADER, Map.class); dataAsObject.remove(TEST_ID); assertThatThrownBy(() -> validateAndMarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject, true)) - .isInstanceOf(ValidationFailedException.class) - .hasMessageContaining("Required property testId is missing from object"); + .hasMessageContaining("required property 'testId' not found"); } /** diff --git a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/test/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelperUnmarshalTest.java b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/test/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelperUnmarshalTest.java index d0059f26f..f633b2d0f 100644 --- a/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/test/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelperUnmarshalTest.java +++ b/plugins/plugins-context/plugins-context-schema/plugins-context-schema-json/src/test/java/org/onap/policy/apex/plugins/context/schema/json/JsonSchemaHelperUnmarshalTest.java @@ -25,7 +25,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.google.gson.JsonObject; -import com.worldturner.medeia.api.ValidationFailedException; import java.util.ArrayList; import java.util.Map; import org.junit.jupiter.api.Test; @@ -74,7 +73,8 @@ class JsonSchemaHelperUnmarshalTest extends CommonTestData { new AxContextSchema(new AxArtifactKey("JsonObject", VERSION), JSON, schemaDef); var jsonSchemaHelper = new JsonSchemaHelper(); assertThatThrownBy(() -> jsonSchemaHelper.init(testKey, jsonSchema)) - .isInstanceOf(ContextRuntimeException.class).hasMessageContaining("schema is invalid"); + .isInstanceOf(ContextRuntimeException.class) + .hasMessageContaining("schema is invalid"); } /** @@ -111,8 +111,7 @@ class JsonSchemaHelperUnmarshalTest extends CommonTestData { var dataAsObject = coder.decode(COMMONHEADER, JsonObject.class); dataAsObject.addProperty("requestId", "abcd"); assertThatThrownBy(() -> validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject)) - .isInstanceOf(ValidationFailedException.class) - .hasMessageContaining("Pattern ^[0-9]*-[0-9]*$ is not contained in text"); + .hasMessageContaining("does not match the regex pattern ^[0-9]*-[0-9]*$"); } /** @@ -137,8 +136,7 @@ class JsonSchemaHelperUnmarshalTest extends CommonTestData { var dataAsObject = coder.decode(COMMONHEADER, JsonObject.class); dataAsObject.remove(TEST_ID); assertThatThrownBy(() -> validateAndUnmarshal(COMMONHEADERTYPE_DRAFT07, dataAsObject)) - .isInstanceOf(ValidationFailedException.class) - .hasMessageContaining("Required property testId is missing from object"); + .hasMessageContaining("required property 'testId' not found"); } /** |