diff options
author | adheli.tavares <adheli.tavares@est.tech> | 2024-07-19 12:04:46 +0100 |
---|---|---|
committer | adheli.tavares <adheli.tavares@est.tech> | 2024-07-19 12:05:20 +0100 |
commit | 9133dabc8566458899d39a7223fcc9c43788febd (patch) | |
tree | 509cc81d37da3e31bb606383f4b21d68747899c6 /policy-utils | |
parent | c295ebcf3e6c15510e9c0d9a1ae1dd206b403d73 (diff) |
Uplift json schema validator library
Issue-ID: POLICY-5084
Change-Id: I426cd992895d4ce4b840ac1ab479152f954cceaf
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'policy-utils')
3 files changed, 30 insertions, 43 deletions
diff --git a/policy-utils/pom.xml b/policy-utils/pom.xml index eb821b84..048c8c26 100644 --- a/policy-utils/pom.xml +++ b/policy-utils/pom.xml @@ -4,7 +4,7 @@ ONAP Policy Engine - Drools PDP ================================================================================ Copyright (C) 2017, 2019-2021 AT&T Intellectual Property. 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. @@ -67,8 +67,8 @@ <artifactId>guava</artifactId> </dependency> <dependency> - <groupId>com.worldturner.medeia</groupId> - <artifactId>medeia-validator-core</artifactId> + <groupId>com.networknt</groupId> + <artifactId>json-schema-validator</artifactId> </dependency> </dependencies> </project> diff --git a/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java b/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java index 6266f171..506b0977 100644 --- a/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java +++ b/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021, 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. @@ -21,7 +21,6 @@ package org.onap.policy.drools.policies; -import com.worldturner.medeia.api.ValidationFailedException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import lombok.NoArgsConstructor; @@ -120,11 +119,8 @@ public class DomainMaker { try { validators.get(policy.getTypeIdentifier()).conformance(rawPolicy); } catch (CoderException e) { - logger.info("policy {}:{}:{} is not conformant", - policy.getTypeIdentifier(), policy.getName(), policy.getVersion(), e); - if (e.getCause() instanceof ValidationFailedException) { - throw (ValidationFailedException) e.getCause(); - } + logger.error("policy {}:{}:{} is not conformant", + policy.getTypeIdentifier(), policy.getName(), policy.getVersion(), e); return false; } @@ -144,10 +140,7 @@ public class DomainMaker { try { validators.get(policyType).encode(domainPolicy); } catch (CoderException e) { - logger.info("policy {}:{} is not conformant", policyType, domainPolicy.getClass().getName(), e); - if (e.getCause() instanceof ValidationFailedException) { - throw (ValidationFailedException) e.getCause(); - } + logger.error("policy {}:{} is not conformant", policyType, domainPolicy.getClass().getName(), e); return false; } @@ -162,11 +155,10 @@ public class DomainMaker { // A known schema is one that embedded in a .jar in the classpath as a resource // matching the following syntax: <policy-type-name>-<policy-type-version>.schema.json. // - var schema = - ResourceUtils - .getResourceAsString("schemas/" - + policyType.getName() + "-" + policyType.getVersion() + ".schema.json"); + var schemaPath = "schemas/" + policyType.getName() + "-" + policyType.getVersion() + ".schema.json"; + var schema = ResourceUtils.getResourceAsString(schemaPath); if (schema == null) { + logger.error("Couldn't find a matching schema for type {}", policyType); return false; } @@ -178,9 +170,9 @@ public class DomainMaker { */ public boolean registerValidator(@NonNull ToscaConceptIdentifier policyType, @NonNull String schema) { try { - validators.put(policyType, new StandardValCoder(schema, policyType.toString())); + validators.put(policyType, new StandardValCoder(schema)); } catch (RuntimeException r) { - logger.info("schema for {} is not valid", policyType, r); + logger.error("schema for {} is not valid", policyType, r); return false; } return true; @@ -197,7 +189,7 @@ public class DomainMaker { * Converts a JSON policy into a Domain Policy. */ public <T> T convertTo(@NonNull ToscaConceptIdentifier policyType, @NonNull String json, @NonNull Class<T> clazz) - throws CoderException { + throws CoderException { if (isRegistered(policyType)) { return validators.get(policyType).decode(json, clazz); } else { diff --git a/policy-utils/src/test/java/org/onap/policy/drools/policies/DomainMakerTest.java b/policy-utils/src/test/java/org/onap/policy/drools/policies/DomainMakerTest.java index 5563d5ea..ee32cb2b 100644 --- a/policy-utils/src/test/java/org/onap/policy/drools/policies/DomainMakerTest.java +++ b/policy-utils/src/test/java/org/onap/policy/drools/policies/DomainMakerTest.java @@ -27,7 +27,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import com.worldturner.medeia.api.ValidationFailedException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -48,7 +47,7 @@ class DomainMakerTest { private DomainMaker domainMaker; @BeforeEach - public void setUp() throws Exception { + public void setUp() { domainMaker = new DomainMaker(); } @@ -77,7 +76,7 @@ class DomainMakerTest { @Test void testIsDomainConformant() { ToscaConceptIdentifier policyTypeId = - new ToscaConceptIdentifier("policy.type.A", "1.0.0"); + new ToscaConceptIdentifier("policy.type.A", "1.0.0"); DomainAPolicy domainAPolicy = createDomainPolicy(); @@ -86,7 +85,7 @@ class DomainMakerTest { // integer exceeding max. value domainAPolicy.getProperties().getNested().setNested3(999); assertFalse(domainMaker.isDomainConformant(policyTypeId, domainAPolicy)); - domainAPolicy.getProperties().getNested().setNested3(33); // restore good valude + domainAPolicy.getProperties().getNested().setNested3(33); // restore good value // not registered schema for policy type policyTypeId.setVersion("2.0.0"); @@ -100,9 +99,7 @@ class DomainMakerTest { assertTrue(domainMaker.conformance(policy1)); policy1.getProperties().remove("nested"); - assertThatThrownBy(() -> domainMaker.conformance(policy1)) - .isInstanceOf(ValidationFailedException.class) - .hasMessageContaining("Required property nested is missing from object"); + assertFalse(domainMaker.conformance(policy1)); DomainAPolicy domainAPolicy = createDomainPolicy(); assertTrue(domainMaker.conformance(policy1.getTypeIdentifier(), domainAPolicy)); @@ -110,15 +107,13 @@ class DomainMakerTest { domainAPolicy.getProperties().getNested().setNested1(""); ToscaConceptIdentifier ident1 = policy1.getTypeIdentifier(); - assertThatThrownBy(() -> domainMaker.conformance(ident1, domainAPolicy)) - .isInstanceOf(ValidationFailedException.class) - .hasMessageContaining("Pattern ^(.+)$ is not contained in text"); + assertFalse(domainMaker.conformance(ident1, domainAPolicy)); } @Test void testRegisterValidator() throws IOException, CoderException { ToscaConceptIdentifier policyTypeId = - new ToscaConceptIdentifier("policy.type.external", "9.9.9"); + new ToscaConceptIdentifier("policy.type.external", "9.9.9"); assertTrue(domainMaker.registerValidator(policyTypeId, getJsonFromFile("src/test/resources/policy.type.external-9.9.9.schema.json"))); @@ -135,11 +130,11 @@ class DomainMakerTest { @Test void testConvertToDomainPolicy() throws IOException, CoderException { DomainAPolicy domainAPolicy = - domainMaker.convertTo(getToscaPolicy("src/test/resources/policyA.json"), DomainAPolicy.class); + domainMaker.convertTo(getToscaPolicy("src/test/resources/policyA.json"), DomainAPolicy.class); assertDomainPolicy(domainAPolicy); assertNotNull(domainMaker.convertTo(getToscaPolicy("src/test/resources/policyA-no-policy-type.json"), - DomainAPolicy.class)); + DomainAPolicy.class)); } @Test @@ -153,11 +148,11 @@ class DomainMakerTest { @Test void testIsRegistered() { ToscaConceptIdentifier policyTypeId1 = - new ToscaConceptIdentifier("policy.type.A", "1.0.0"); + new ToscaConceptIdentifier("policy.type.A", "1.0.0"); assertTrue(domainMaker.isRegistered(policyTypeId1)); ToscaConceptIdentifier policyTypeId2 = - new ToscaConceptIdentifier("policy.type.external", "7.7.9"); + new ToscaConceptIdentifier("policy.type.external", "7.7.9"); assertFalse(domainMaker.isRegistered(policyTypeId2)); } @@ -173,13 +168,13 @@ class DomainMakerTest { private DomainAPolicy createDomainPolicy() { return DomainAPolicy.builder().metadata(Metadata.builder().policyId("A").build()) - .name("A") - .version("1.0.0") - .type("policy.type.A") - .typeVersion("1.0.0") - .properties(Properties.builder() - .nested(Nested.builder().nested1("nested1").nested2(true).nested3(50).build()) - .build()).build(); + .name("A") + .version("1.0.0") + .type("policy.type.A") + .typeVersion("1.0.0") + .properties(Properties.builder() + .nested(Nested.builder().nested1("nested1").nested2(true).nested3(50).build()) + .build()).build(); } private void assertDomainPolicy(DomainAPolicy domainAPolicy) { |