Isolating the usage of json-schema validator to drools-pdp 64/138564/1
authoradheli.tavares <adheli.tavares@est.tech>
Wed, 24 Jul 2024 13:01:48 +0000 (14:01 +0100)
committeradheli.tavares <adheli.tavares@est.tech>
Wed, 24 Jul 2024 13:04:40 +0000 (14:04 +0100)
- only drools-pdp have this validation, so moving the class and
its dependencies to there to avoid loading dependencies where it's
not used.

Issue-ID: POLICY-5084
Change-Id: I9ec6b06f4a8b56c844725b7da8993eb3da078574
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
utils/pom.xml
utils/src/main/java/org/onap/policy/common/utils/coder/CoderRuntimeException.java [deleted file]
utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java [deleted file]
utils/src/test/java/org/onap/policy/common/utils/coder/StandardValCoderTest.java [deleted file]
utils/src/test/resources/org/onap/policy/common/utils/coder/bad-regex.json [deleted file]
utils/src/test/resources/org/onap/policy/common/utils/coder/missing-required.json [deleted file]
utils/src/test/resources/org/onap/policy/common/utils/coder/test.schema.json [deleted file]
utils/src/test/resources/org/onap/policy/common/utils/coder/valid.json [deleted file]

index 699ed40..791a048 100644 (file)
             <artifactId>gson</artifactId>
             <version>${project.version}</version>
         </dependency>
-        <dependency>
-            <groupId>net.jimblackler.jsonschemafriend</groupId>
-            <artifactId>core</artifactId>
-        </dependency>
         <dependency>
             <groupId>commons-net</groupId>
             <artifactId>commons-net</artifactId>
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/CoderRuntimeException.java b/utils/src/main/java/org/onap/policy/common/utils/coder/CoderRuntimeException.java
deleted file mode 100644 (file)
index 0ffd607..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- * 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.
- * 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.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.common.utils.coder;
-
-import java.io.Serial;
-
-/**
- * Exceptions generated by coders.
- */
-public class CoderRuntimeException extends RuntimeException {
-
-    @Serial
-    private static final long serialVersionUID = 1L;
-
-    public CoderRuntimeException(Throwable cause) {
-        super(cause);
-    }
-
-}
diff --git a/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java b/utils/src/main/java/org/onap/policy/common/utils/coder/StandardValCoder.java
deleted file mode 100644 (file)
index 408ae81..0000000
+++ /dev/null
@@ -1,138 +0,0 @@
-/*--
- * ============LICENSE_START=======================================================
- *  Copyright (C) 2020-2021 AT&T Intellectual Property. 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.
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.common.utils.coder;
-
-import java.io.Reader;
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.io.Writer;
-import lombok.NonNull;
-import lombok.ToString;
-import net.jimblackler.jsonschemafriend.GenerationException;
-import net.jimblackler.jsonschemafriend.Schema;
-import net.jimblackler.jsonschemafriend.SchemaStore;
-import net.jimblackler.jsonschemafriend.ValidationException;
-import net.jimblackler.jsonschemafriend.Validator;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Extension to the StandardCoder to support streaming validation against a Draft-07 Json schema specification.
- */
-
-@ToString
-public class StandardValCoder extends StandardCoder {
-
-    private static final Logger logger = LoggerFactory.getLogger(StandardValCoder.class);
-
-    private final Schema schema;
-
-    /**
-     * StandardCoder with validation.
-     */
-    public StandardValCoder(@NonNull String jsonSchema) {
-        try {
-            SchemaStore store = new SchemaStore();
-            this.schema = store.loadSchemaJson(jsonSchema);
-        } catch (GenerationException e) {
-            throw new CoderRuntimeException(e);
-        }
-    }
-
-    @Override
-    protected String toPrettyJson(Object object) {
-        try {
-            validate(gsonPretty.toJson(object));
-        } catch (CoderException e) {
-            throw new CoderRuntimeException(e);
-        }
-        return super.toPrettyJson(object);
-    }
-
-    @Override
-    protected String toJson(@NonNull Object object) {
-        var output = new StringWriter();
-        toJson(output, object);
-        return output.toString();
-    }
-
-    @Override
-    protected void toJson(@NonNull Writer target, @NonNull Object object) {
-        try {
-            validate(gson.toJson(object));
-        } catch (CoderException e) {
-            throw new CoderRuntimeException(e);
-        }
-        gson.toJson(object, object.getClass(), target);
-    }
-
-    @Override
-    protected <T> T fromJson(@NonNull Reader source, @NonNull Class<T> clazz) {
-        return convertFromDouble(clazz, gson.fromJson(source, clazz));
-    }
-
-    @Override
-    protected <T> T fromJson(String json, Class<T> clazz) {
-        try {
-            validate(json);
-        } catch (CoderException e) {
-            throw new CoderRuntimeException(e);
-        }
-        var reader = new StringReader(json);
-        return convertFromDouble(clazz, gson.fromJson(reader, clazz));
-    }
-
-    /**
-     * Is the json conformant?.
-     */
-    public boolean isConformant(@NonNull String json) {
-        try {
-            conformance(json);
-            return true;
-        } catch (Exception e) {
-            logger.error("JSON is not conformant to schema", e);
-            return false;
-        }
-    }
-
-    /**
-     * Check a json string for conformance against its schema definition.
-     */
-    public void conformance(@NonNull String json) throws CoderException {
-        validate(json);
-    }
-
-    private void validate(Object object) throws CoderException {
-        try {
-            final var validator = new Validator();
-            validator.validate(schema, object);
-        } catch (ValidationException exception) {
-            var error = String.format("JSON validation failed: %s", exception.getMessage());
-            logger.error(error);
-            throw new CoderException(error);
-        }
-    }
-
-    private void validate(String json) throws CoderException {
-        validate(gson.fromJson(json, Object.class));
-    }
-}
diff --git a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardValCoderTest.java b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardValCoderTest.java
deleted file mode 100644 (file)
index 8b5f406..0000000
+++ /dev/null
@@ -1,174 +0,0 @@
-/*
- * ============LICENSE_START=======================================================
- *  Copyright (C) 2020 AT&T Intellectual Property. 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.
- * 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.policy.common.utils.coder;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertNotEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
-
-import java.io.StringReader;
-import java.io.StringWriter;
-import java.util.List;
-import lombok.Data;
-import lombok.NoArgsConstructor;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.onap.policy.common.utils.resources.ResourceUtils;
-
-class StandardValCoderTest {
-    private String jsonSchema;
-    private String validJson;
-    private String missingReqJson;
-    private String badRegexJson;
-
-    @Data
-    @NoArgsConstructor
-    public static class ValOuter {
-        @Data
-        @NoArgsConstructor
-        public static class ValInner {
-            public String subItemString;
-            public Integer subItemInteger;
-        }
-
-        public String aaString;
-        public int anInteger;
-        public boolean aaBoolean;
-        public List<ValInner> aaCollection;
-    }
-
-    @BeforeEach
-    public void testSetUp() {
-        jsonSchema = getJson("src/test/resources/org/onap/policy/common/utils/coder/test.schema.json");
-        validJson = getJson("src/test/resources/org/onap/policy/common/utils/coder/valid.json");
-        missingReqJson = getJson("src/test/resources/org/onap/policy/common/utils/coder/missing-required.json");
-        badRegexJson = getJson("src/test/resources/org/onap/policy/common/utils/coder/bad-regex.json");
-    }
-
-    @Test
-    void testDecode() throws CoderException {
-        StandardValCoder valCoder = new StandardValCoder(jsonSchema);
-
-        ValOuter valOuter = valCoder.decode(validJson, ValOuter.class);
-        assertValidJson(valOuter);
-
-        StringReader reader = new StringReader(validJson);
-        valOuter = valCoder.decode(reader, ValOuter.class);
-        assertValidJson(valOuter);
-
-        try {
-            valCoder.decode(missingReqJson, ValOuter.class);
-            fail("missing required field should have been flagged by the schema validation");
-        } catch (CoderException e) {
-            assertThat(e.getMessage()).contains("Missing property aaCollection");
-        }
-
-        try {
-            valCoder.decode(badRegexJson, ValOuter.class);
-            fail("bad regex should have been flagged by the schema validation");
-        } catch (CoderException e) {
-            assertThat(e.getMessage())
-                .contains("Validation errors: \"abc123\" at #/aaString failed")
-                .contains("Did not match pattern: ^([a-z]*)$");
-        }
-    }
-
-    @Test
-    void testEncode() throws CoderException {
-        StandardValCoder valCoder = new StandardValCoder(jsonSchema);
-        ValOuter valOuter = valCoder.decode(validJson, ValOuter.class);
-
-        String valOuterJson = valCoder.encode(valOuter);
-        assertEquals(valOuter, valCoder.decode(valOuterJson, ValOuter.class));
-        assertValidJson(valOuter);
-
-        StringWriter writer = new StringWriter();
-        valCoder.encode(writer, valOuter);
-        assertEquals(valOuterJson, writer.toString());
-
-        // test exception case with an empty object
-        assertThatThrownBy(() -> valCoder.encode(new ValOuter())).isInstanceOf(CoderException.class);
-    }
-
-    @Test
-    void testPretty() throws CoderException {
-        StandardValCoder valCoder = new StandardValCoder(jsonSchema);
-        ValOuter valOuter = valCoder.decode(validJson, ValOuter.class);
-
-        String valOuterJson = valCoder.encode(valOuter);
-        assertEquals(valOuterJson, valCoder.encode(valOuter, false));
-        String prettyValOuterJson = valCoder.encode(valOuter, true);
-        assertNotEquals(valOuterJson, prettyValOuterJson);
-
-        assertEquals(valOuter, valCoder.decode(prettyValOuterJson, ValOuter.class));
-
-        // test exception cases with an empty object
-        assertThatThrownBy(() -> valCoder.encode(new ValOuter(), false)).isInstanceOf(CoderException.class);
-        assertThatThrownBy(() -> valCoder.encode(new ValOuter(), true)).isInstanceOf(CoderException.class);
-    }
-
-    @Test
-    void testConformance() {
-        StandardValCoder valCoder = new StandardValCoder(jsonSchema);
-        assertTrue(valCoder.isConformant(validJson));
-        assertFalse(valCoder.isConformant(missingReqJson));
-        assertFalse(valCoder.isConformant(badRegexJson));
-    }
-
-    @Test
-    void testNullValues() throws CoderException {
-        StandardValCoder valCoder = new StandardValCoder(jsonSchema);
-        assertThrows(NullPointerException.class, () -> valCoder.toJson(null));
-        assertThrows(NullPointerException.class, () -> valCoder.isConformant(null));
-        assertThrows(NullPointerException.class, () -> valCoder.conformance(null));
-
-        assertThrows(NullPointerException.class, () -> valCoder.toJson(null, null));
-        var writer = new StringWriter();
-        assertThrows(NullPointerException.class, () -> valCoder.toJson(writer, null));
-        ValOuter valOuter = valCoder.decode(validJson, ValOuter.class);
-        assertThrows(NullPointerException.class, () -> valCoder.toJson(null, valOuter));
-    }
-
-    @Test
-    void testConstructor() {
-        assertThrows(NullPointerException.class, () -> new StandardValCoder(null));
-
-        assertThrows(CoderRuntimeException.class, () -> new StandardValCoder("$schema"));
-    }
-
-    private void assertValidJson(ValOuter valOuter) {
-        assertEquals("abcd", valOuter.getAaString());
-        assertEquals(90, valOuter.getAnInteger());
-        assertTrue(valOuter.isAaBoolean());
-        assertEquals("defg", valOuter.getAaCollection().get(0).getSubItemString());
-        assertEquals(Integer.valueOf(1200), valOuter.getAaCollection().get(0).getSubItemInteger());
-    }
-
-    private String getJson(String filePath) {
-        return ResourceUtils.getResourceAsString(filePath);
-    }
-}
\ No newline at end of file
diff --git a/utils/src/test/resources/org/onap/policy/common/utils/coder/bad-regex.json b/utils/src/test/resources/org/onap/policy/common/utils/coder/bad-regex.json
deleted file mode 100644 (file)
index 049de2c..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-{
-    "aaString": "abc123",
-    "anInteger": 90,
-    "aaBoolean": true,
-    "aaCollection": [ {
-        "subItemString": "defg",
-        "subItemInteger": 1200
-    }]
-}
\ No newline at end of file
diff --git a/utils/src/test/resources/org/onap/policy/common/utils/coder/missing-required.json b/utils/src/test/resources/org/onap/policy/common/utils/coder/missing-required.json
deleted file mode 100644 (file)
index e19db9d..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-{
-    "aaString": "abcd",
-    "anInteger": 90,
-    "aaBoolean": true
-}
\ No newline at end of file
diff --git a/utils/src/test/resources/org/onap/policy/common/utils/coder/test.schema.json b/utils/src/test/resources/org/onap/policy/common/utils/coder/test.schema.json
deleted file mode 100644 (file)
index 60e70fb..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-{
-  "definitions": {},
-  "$schema": "http://json-schema.org/draft-07/schema#",
-  "$id": "http://onap.org/policy/common/coders/root.json",
-  "type": "object",
-  "title": "Test Schema",
-  "required": [
-    "aaString",
-    "anInteger",
-    "aaBoolean",
-    "aaCollection"
-  ],
-  "properties": {
-    "aaString": {
-      "$id": "#/properties/aaString",
-      "type": "string",
-      "title": "an alphabetical string",
-      "default": "",
-      "examples": [
-        "abcdef"
-      ],
-      "pattern": "^([a-z]*)$"
-    },
-    "anInteger": {
-      "$id": "#/properties/anInteger",
-      "type": "integer",
-      "title": "a bounded integer",
-      "default": 5,
-      "examples": [
-        98
-      ],
-      "minimum": 10,
-      "maximum": 100
-    },
-    "aaBoolean": {
-      "$id": "#/properties/aaBoolean",
-      "type": "boolean",
-      "title": "a boolean",
-      "default": false,
-      "examples": [
-        true
-      ]
-    },
-    "aaCollection": {
-      "$id": "#/properties/aaCollection",
-      "type": "array",
-      "title": "a collection",
-      "items": {
-        "$id": "#/properties/aaCollection/items",
-        "type": "object",
-        "title": "the collection items",
-        "required": [
-          "subItemString"
-        ],
-        "properties": {
-          "subItemString": {
-            "$id": "#/properties/aaCollection/items/properties/subItemString",
-            "type": "string",
-            "title": "the subitem string",
-            "default": "blah",
-            "pattern": "^(.*)$"
-          },
-          "subItemInteger": {
-            "$id": "#/properties/aaCollection/items/properties/subItemInteger",
-            "type": "integer"
-          }
-        }
-      }
-    }
-  }
-}
\ No newline at end of file
diff --git a/utils/src/test/resources/org/onap/policy/common/utils/coder/valid.json b/utils/src/test/resources/org/onap/policy/common/utils/coder/valid.json
deleted file mode 100644 (file)
index a2cdb93..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-{
-  "aaString": "abcd",
-  "anInteger": 90,
-  "aaBoolean": true,
-  "aaCollection": [
-    {
-      "subItemString": "defg",
-      "subItemInteger": 1200
-    }
-  ]
-}
\ No newline at end of file