summaryrefslogtreecommitdiffstats
path: root/policy-utils
diff options
context:
space:
mode:
authorjhh <jorge.hernandez-herrero@att.com>2020-01-30 18:57:13 -0600
committerjhh <jorge.hernandez-herrero@att.com>2020-02-04 00:26:57 -0600
commitbaa4e928207f5930fcd620bddddcd493aa7a4122 (patch)
treeb4832c661bc05bcb457e65a73c06f0a960ac7632 /policy-utils
parenta54a2373f1b48cdb64d4920ec57ffeab4f228814 (diff)
domain native and operational policies support
- moved DomainMaker to policy-utils/ project. - support for native drools and controller drools policy types. Issue-ID: POLICY-2345 Signed-off-by: jhh <jorge.hernandez-herrero@att.com> Change-Id: I46b9816eadceb7ef280e0e6789f07b4d4ca51519 Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Diffstat (limited to 'policy-utils')
-rw-r--r--policy-utils/pom.xml20
-rw-r--r--policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java233
-rw-r--r--policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/DomainAPolicy.java49
-rw-r--r--policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Metadata.java35
-rw-r--r--policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Nested.java41
-rw-r--r--policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Properties.java35
-rw-r--r--policy-utils/src/test/java/org/onap/policy/drools/policies/DomainMakerTest.java194
-rw-r--r--policy-utils/src/test/resources/policy.type.external-9.9.9.schema.json7
-rw-r--r--policy-utils/src/test/resources/policyA-no-policy-type.json16
-rw-r--r--policy-utils/src/test/resources/policyA.json16
-rw-r--r--policy-utils/src/test/resources/schemas/policy.type.A-1.0.0.schema.json89
11 files changed, 735 insertions, 0 deletions
diff --git a/policy-utils/pom.xml b/policy-utils/pom.xml
index 1bc680ba..f7bbff67 100644
--- a/policy-utils/pom.xml
+++ b/policy-utils/pom.xml
@@ -42,6 +42,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.onap.policy.common</groupId>
<artifactId>utils</artifactId>
<version>${policy.common.version}</version>
@@ -51,6 +56,21 @@
<artifactId>commons-configuration2</artifactId>
<version>2.4</version>
</dependency>
+ <dependency>
+ <groupId>org.onap.policy.common</groupId>
+ <artifactId>gson</artifactId>
+ <version>${policy.common.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.projectlombok</groupId>
+ <artifactId>lombok</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.onap.policy.models</groupId>
+ <artifactId>policy-models-tosca</artifactId>
+ <version>${policy.models.version}</version>
+ </dependency>
</dependencies>
<build>
<plugins>
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
new file mode 100644
index 00000000..6e1e0881
--- /dev/null
+++ b/policy-utils/src/main/java/org/onap/policy/drools/policies/DomainMaker.java
@@ -0,0 +1,233 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.drools.policies;
+
+import com.worldturner.medeia.api.ValidationFailedException;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import lombok.NoArgsConstructor;
+import lombok.NonNull;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardValCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Converts Tosca Policies into Domain policies.
+ *
+ * <p>
+ * A Domain Policy is a specialized version of the Tosca Policy that
+ * conform with a particular domain specification in json schema format.
+ * A ToscaPolicy is a generic data structure where domain data is contained
+ * in a Map[String, Object]. This class contains that generic information
+ * into a concrete domain specific data model for the ToscaPolicy.
+ * </p>
+ */
+
+@NoArgsConstructor
+public class DomainMaker {
+
+ private static final Logger logger = LoggerFactory.getLogger(DomainMaker.class);
+
+ /**
+ * policy-type -> schema validator map.
+ */
+ private final Map<ToscaPolicyTypeIdentifier, StandardValCoder> validators = new ConcurrentHashMap<>();
+
+ /**
+ * non-validation serialization coder.
+ */
+ private final StandardCoder nonValCoder = new StandardCoder();
+
+ /**
+ * Does this json conform to a registered policy type schema?.
+ */
+ public boolean isConformant(@NonNull ToscaPolicyTypeIdentifier policyType, @NonNull String json) {
+ if (!isRegistered(policyType)) {
+ return false;
+ }
+
+ return validators.get(policyType).isConformant(json);
+ }
+
+ /**
+ * Does this policy conform to its domain specification?.
+ */
+ public boolean isConformant(@NonNull ToscaPolicy policy) {
+ String rawPolicy = serialize(policy);
+ if (StringUtils.isBlank(rawPolicy)) {
+ return false;
+ }
+
+ return isConformant(policy.getTypeIdentifier(), rawPolicy);
+ }
+
+ /**
+ * Does this domain policy conforms to its schema definition?.
+ */
+ public <T> boolean isDomainConformant(@NonNull ToscaPolicyTypeIdentifier policyType, @NonNull T domainPolicy) {
+ if (!isRegistered(policyType)) {
+ return false;
+ }
+
+ try {
+ return validators.get(policyType).encode(domainPolicy) != null;
+ } catch (CoderException e) {
+ logger.info("policy {}:{} is not conformant", policyType, domainPolicy.getClass().getName(), e);
+ return false;
+ }
+ }
+
+ /**
+ * Check policy conformance to its specification providing a list of errors
+ * in a ValidationFailedException.
+ */
+ public boolean conformance(@NonNull ToscaPolicy policy) throws ValidationFailedException {
+ if (!isRegistered(policy.getTypeIdentifier())) {
+ return false;
+ }
+
+ String rawPolicy = serialize(policy);
+ if (StringUtils.isBlank(rawPolicy)) {
+ return false;
+ }
+
+ 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();
+ }
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Checks a domain policy conformance to its specification providing a list of errors
+ * in a ValidationFailedException.
+ */
+ public <T> boolean conformance(@NonNull ToscaPolicyTypeIdentifier policyType, T domainPolicy)
+ throws ValidationFailedException {
+
+ if (!isRegistered(policyType)) {
+ return false;
+ }
+
+ 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();
+ }
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Registers a known schema resource for validation.
+ */
+ public boolean registerValidator(@NonNull ToscaPolicyTypeIdentifier policyType) {
+ //
+ // A known schema is an schema embedded in a .jar in the classpath under the schemas/
+ // resource directory matching the following syntax: schemas/<policy-type>.schema.json.
+ //
+ String schema =
+ ResourceUtils
+ .getResourceAsString("schemas/"
+ + policyType.getName() + "-" + policyType.getVersion() + ".schema.json");
+ if (schema == null) {
+ return false;
+ }
+
+ return registerValidator(policyType, schema);
+ }
+
+ /**
+ * Registers/Overrides a new/known schema for a policy type.
+ */
+ public boolean registerValidator(@NonNull ToscaPolicyTypeIdentifier policyType, @NonNull String schema) {
+ try {
+ validators.put(policyType, new StandardValCoder(schema, policyType.toString()));
+ } catch (RuntimeException r) {
+ logger.info("schema for {} is not valid", policyType, r);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Converts a ToscaPolicy into a Domain Policy.
+ */
+ public <T> T convertTo(@NonNull ToscaPolicy toscaPolicy, @NonNull Class<T> clazz) throws CoderException {
+ return convertTo(toscaPolicy.getTypeIdentifier(), nonValCoder.encode(toscaPolicy), clazz);
+ }
+
+ /**
+ * Converts a JSON policy into a Domain Policy.
+ */
+ public <T> T convertTo(@NonNull ToscaPolicyTypeIdentifier policyType, @NonNull String json, @NonNull Class<T> clazz)
+ throws CoderException {
+ if (isRegistered(policyType)) {
+ return validators.get(policyType).decode(json, clazz);
+ } else {
+ return nonValCoder.decode(json, clazz);
+ }
+ }
+
+ /**
+ * Converts a Tosca Policy Type specification to a domain-specific json specification.
+ */
+ public String convertToSchema(@NonNull ToscaPolicyType policyType) {
+ //
+ // TODO:
+ // 1. Convert Tosca Policy Type definition schema to suitable json schema.
+ // 2. Call registerValidator to register
+ throw new UnsupportedOperationException("schema generation from policy type is not supported");
+ }
+
+ public boolean isRegistered(@NonNull ToscaPolicyTypeIdentifier policyType) {
+ return validators.containsKey(policyType) || registerValidator(policyType);
+ }
+
+
+ private String serialize(@NonNull ToscaPolicy policy) {
+ String rawPolicy = null;
+ try {
+ rawPolicy = nonValCoder.encode(policy);
+ } catch (CoderException e) {
+ logger.debug("policy {}:{} is invalid json", policy.getTypeIdentifier(), policy.getIdentifier(), e);
+ }
+ return rawPolicy;
+ }
+}
diff --git a/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/DomainAPolicy.java b/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/DomainAPolicy.java
new file mode 100644
index 00000000..04f20b2a
--- /dev/null
+++ b/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/DomainAPolicy.java
@@ -0,0 +1,49 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.drools.models.domains.a;
+
+import com.google.gson.annotations.SerializedName;
+import lombok.Builder;
+import lombok.Data;
+
+@Data
+@Builder
+public class DomainAPolicy {
+ private static final long serialVersionUID = -8942432000554391455L;
+
+ @SerializedName("type")
+ public String type;
+
+ @SerializedName("type_version")
+ public String typeVersion;
+
+ @SerializedName("version")
+ public String version;
+
+ @SerializedName("metadata")
+ public Metadata metadata;
+
+ @SerializedName("properties")
+ public Properties properties;
+
+ @SerializedName("name")
+ public String name;
+}
diff --git a/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Metadata.java b/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Metadata.java
new file mode 100644
index 00000000..0cad4ff0
--- /dev/null
+++ b/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Metadata.java
@@ -0,0 +1,35 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.drools.models.domains.a;
+
+import com.google.gson.annotations.SerializedName;
+import java.io.Serializable;
+import lombok.Builder;
+import lombok.Data;
+
+@Data
+@Builder
+public class Metadata implements Serializable {
+ private static final long serialVersionUID = 476541531265304644L;
+
+ @SerializedName("policy-id")
+ public String policyId;
+}
diff --git a/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Nested.java b/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Nested.java
new file mode 100644
index 00000000..eedadeff
--- /dev/null
+++ b/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Nested.java
@@ -0,0 +1,41 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.drools.models.domains.a;
+
+import com.google.gson.annotations.SerializedName;
+import java.io.Serializable;
+import lombok.Builder;
+import lombok.Data;
+
+@Data
+@Builder
+public class Nested implements Serializable {
+ private static final long serialVersionUID = -1203143214137354429L;
+
+ @SerializedName("nested1")
+ public String nested1;
+
+ @SerializedName("nested2")
+ public boolean nested2;
+
+ @SerializedName("nested3")
+ public int nested3;
+}
diff --git a/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Properties.java b/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Properties.java
new file mode 100644
index 00000000..3ba66ce9
--- /dev/null
+++ b/policy-utils/src/test/java/org/onap/policy/drools/models/domains/a/Properties.java
@@ -0,0 +1,35 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.drools.models.domains.a;
+
+import com.google.gson.annotations.SerializedName;
+import java.io.Serializable;
+import lombok.Builder;
+import lombok.Data;
+
+@Data
+@Builder
+public class Properties implements Serializable {
+ private static final long serialVersionUID = 8476625479607243095L;
+
+ @SerializedName("nested")
+ public Nested nested;
+}
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
new file mode 100644
index 00000000..cfacc7ab
--- /dev/null
+++ b/policy-utils/src/test/java/org/onap/policy/drools/policies/DomainMakerTest.java
@@ -0,0 +1,194 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * 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.drools.policies;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import com.worldturner.medeia.api.ValidationFailedException;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.drools.models.domains.a.DomainAPolicy;
+import org.onap.policy.drools.models.domains.a.Metadata;
+import org.onap.policy.drools.models.domains.a.Nested;
+import org.onap.policy.drools.models.domains.a.Properties;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
+
+public class DomainMakerTest {
+
+ private DomainMaker domainMaker;
+ private StandardCoder nonValCoder;
+
+ @Before
+ public void setUp() throws Exception {
+ domainMaker = new DomainMaker();
+ nonValCoder = new StandardCoder();
+ }
+
+ @Test
+ public void testIsConformantString() throws IOException {
+ ToscaPolicyTypeIdentifier policyTypeId =
+ new ToscaPolicyTypeIdentifier("policy.type.A", "1.0.0");
+ String rawJsonPolicyType =
+ getJsonFromFile("src/test/resources/policyA.json");
+
+ assertTrue(domainMaker.isConformant(policyTypeId, rawJsonPolicyType));
+
+ policyTypeId.setVersion("2.0.0");
+ assertFalse(domainMaker.isConformant(policyTypeId, rawJsonPolicyType));
+ }
+
+ @Test
+ public void testIsConformantToscaPolicy() throws IOException, CoderException {
+ ToscaPolicy policy = getToscaPolicy("src/test/resources/policyA.json");
+ assertTrue(domainMaker.isConformant(policy));
+
+ policy.setType("policy.type.Z");
+ assertFalse(domainMaker.isConformant(policy));
+ }
+
+ @Test
+ public void testIsDomainConformant() {
+ ToscaPolicyTypeIdentifier policyTypeId =
+ new ToscaPolicyTypeIdentifier("policy.type.A", "1.0.0");
+
+ DomainAPolicy domainAPolicy = createDomainPolicy();
+
+ assertTrue(domainMaker.isDomainConformant(policyTypeId, domainAPolicy));
+
+ // integer exceeding max. value
+ domainAPolicy.getProperties().getNested().setNested3(999);
+ assertFalse(domainMaker.isDomainConformant(policyTypeId, domainAPolicy));
+ domainAPolicy.getProperties().getNested().setNested3(33); // restore good valude
+
+ // not registered schema for policy type
+ policyTypeId.setVersion("2.0.0");
+ assertFalse(domainMaker.isDomainConformant(policyTypeId, domainAPolicy));
+ }
+
+
+ @Test
+ public void testConformance() throws IOException, CoderException {
+ ToscaPolicy policy1 = getToscaPolicy("src/test/resources/policyA.json");
+ assertTrue(domainMaker.conformance(policy1));
+
+ policy1.getProperties().remove("nested");
+ assertThatThrownBy(() -> domainMaker.conformance(policy1))
+ .isInstanceOf(ValidationFailedException.class)
+ .hasMessageContaining("Required property nested is missing from object");
+
+ DomainAPolicy domainAPolicy = createDomainPolicy();
+ assertTrue(domainMaker.conformance(policy1.getTypeIdentifier(), domainAPolicy));
+ assertDomainPolicy(domainAPolicy);
+
+ domainAPolicy.getProperties().getNested().setNested1("");
+ assertThatThrownBy(() -> domainMaker.conformance(policy1.getTypeIdentifier(), domainAPolicy))
+ .isInstanceOf(ValidationFailedException.class)
+ .hasMessageContaining("Pattern ^(.+)$ is not contained in text");
+ }
+
+ @Test
+ public void testRegisterValidator() throws IOException, CoderException {
+ ToscaPolicyTypeIdentifier policyTypeId =
+ new ToscaPolicyTypeIdentifier("policy.type.external", "9.9.9");
+
+ assertTrue(domainMaker.registerValidator(policyTypeId,
+ getJsonFromFile("src/test/resources/policy.type.external-9.9.9.schema.json")));
+
+ ToscaPolicy policy = getToscaPolicy("src/test/resources/policyA.json");
+ policy.setType("policy.type.external");
+ policy.setTypeVersion("9.9.9");
+ assertTrue(domainMaker.isConformant(policy));
+
+ policy.setTypeVersion("1.0.0");
+ assertFalse(domainMaker.isConformant(policy));
+ }
+
+ @Test
+ public void testConvertToDomainPolicy() throws IOException, CoderException {
+ DomainAPolicy domainAPolicy =
+ 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));
+ }
+
+ @Test
+ public void testConvertToSchema() {
+ assertThatThrownBy(() -> domainMaker
+ .convertToSchema(new ToscaPolicyType()))
+ .isInstanceOf(UnsupportedOperationException.class);
+ }
+
+ @Test
+ public void testIsRegistered() {
+ ToscaPolicyTypeIdentifier policyTypeId1 =
+ new ToscaPolicyTypeIdentifier("policy.type.A", "1.0.0");
+ assertTrue(domainMaker.isRegistered(policyTypeId1));
+
+ ToscaPolicyTypeIdentifier policyTypeId2 =
+ new ToscaPolicyTypeIdentifier("policy.type.external", "7.7.9");
+ assertFalse(domainMaker.isRegistered(policyTypeId2));
+
+ }
+
+ private String getJsonFromFile(String filePath) throws IOException {
+ return new String(Files.readAllBytes(Paths.get(filePath)));
+ }
+
+ private ToscaPolicy getToscaPolicy(String filePath) throws CoderException, IOException {
+ String policyJson = getJsonFromFile(filePath);
+ return new StandardCoder().decode(policyJson, ToscaPolicy.class);
+ }
+
+ 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();
+ }
+
+ private void assertDomainPolicy(DomainAPolicy domainAPolicy) {
+ assertEquals("A", domainAPolicy.getName());
+ assertEquals("1.0.0", domainAPolicy.getVersion());
+ assertEquals("1.0.0", domainAPolicy.getTypeVersion());
+ assertEquals("policy.type.A", domainAPolicy.getType());
+ assertEquals("A", domainAPolicy.getMetadata().getPolicyId());
+ assertEquals("nested1", domainAPolicy.getProperties().getNested().getNested1());
+ assertEquals(true, domainAPolicy.getProperties().getNested().isNested2());
+ assertEquals(50, domainAPolicy.getProperties().getNested().getNested3());
+ }
+} \ No newline at end of file
diff --git a/policy-utils/src/test/resources/policy.type.external-9.9.9.schema.json b/policy-utils/src/test/resources/policy.type.external-9.9.9.schema.json
new file mode 100644
index 00000000..d8651eb5
--- /dev/null
+++ b/policy-utils/src/test/resources/policy.type.external-9.9.9.schema.json
@@ -0,0 +1,7 @@
+{
+ "definitions": {},
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "$id": "http://www.onap.org/policy/models/schemas/policy.type.external",
+ "type": "object",
+ "title": "Domain policy.type.external Policy root schema"
+}
diff --git a/policy-utils/src/test/resources/policyA-no-policy-type.json b/policy-utils/src/test/resources/policyA-no-policy-type.json
new file mode 100644
index 00000000..c92b736c
--- /dev/null
+++ b/policy-utils/src/test/resources/policyA-no-policy-type.json
@@ -0,0 +1,16 @@
+{
+ "type": "policy.type.Z",
+ "type_version": "1.0.0",
+ "version": "1.0.0",
+ "metadata": {
+ "policy-id": "A"
+ },
+ "properties": {
+ "nested": {
+ "nested1": "nested1",
+ "nested2": true,
+ "nested3": 50
+ }
+ },
+ "name": "A"
+} \ No newline at end of file
diff --git a/policy-utils/src/test/resources/policyA.json b/policy-utils/src/test/resources/policyA.json
new file mode 100644
index 00000000..7afa79d3
--- /dev/null
+++ b/policy-utils/src/test/resources/policyA.json
@@ -0,0 +1,16 @@
+{
+ "type": "policy.type.A",
+ "type_version": "1.0.0",
+ "version": "1.0.0",
+ "metadata": {
+ "policy-id": "A"
+ },
+ "properties": {
+ "nested": {
+ "nested1": "nested1",
+ "nested2": true,
+ "nested3": 50
+ }
+ },
+ "name": "A"
+} \ No newline at end of file
diff --git a/policy-utils/src/test/resources/schemas/policy.type.A-1.0.0.schema.json b/policy-utils/src/test/resources/schemas/policy.type.A-1.0.0.schema.json
new file mode 100644
index 00000000..089ec44f
--- /dev/null
+++ b/policy-utils/src/test/resources/schemas/policy.type.A-1.0.0.schema.json
@@ -0,0 +1,89 @@
+{
+ "definitions": {},
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "$id": "http://www.onap.org/policy/models/schemas/policy.type.A",
+ "type": "object",
+ "title": "Domain policy.type.A Policy root schema",
+ "required": [
+ "type",
+ "type_version",
+ "version",
+ "metadata",
+ "properties"
+ ],
+ "properties": {
+ "type": {
+ "$id": "#/properties/type",
+ "type": "string",
+ "pattern": "^(.+)$"
+ },
+ "type_version": {
+ "$id": "#/properties/type_version",
+ "type": "string",
+ "pattern": "^(.+)$"
+ },
+ "version": {
+ "$id": "#/properties/version",
+ "type": "string",
+ "title": "Version",
+ "pattern": "^(.+)$"
+ },
+ "name": {
+ "$id": "#/properties/name",
+ "type": "string",
+ "pattern": "^(.+)$"
+ },
+ "metadata": {
+ "$id": "#/properties/metadata",
+ "type": "object",
+ "title": "Metadata",
+ "required": [
+ "policy-id"
+ ],
+ "properties": {
+ "policy-id": {
+ "$id": "#/properties/metadata/properties/policy-id",
+ "type": "string",
+ "pattern": "^(.+)$"
+ }
+ }
+ },
+ "properties": {
+ "$id": "#/properties/properties",
+ "type": "object",
+ "required": [
+ "nested"
+ ],
+ "properties": {
+ "nested": {
+ "$id": "#/properties/properties/properties/nested",
+ "type": "object",
+ "required": [
+ "nested1",
+ "nested2",
+ "nested3"
+ ],
+ "properties": {
+ "nested1": {
+ "$id": "#/properties/properties/properties/nested/properties/nested1",
+ "type": "string",
+ "pattern": "^(.+)$"
+ },
+ "nested2": {
+ "$id": "#/properties/properties/properties/nested/properties/nested2",
+ "type": "boolean",
+ "default": true
+ },
+ "nested3": {
+ "$id": "#/properties/properties/properties/nested/properties/nested3",
+ "type": "integer",
+ "minimum": 1,
+ "maximum": 100,
+ "default": 30
+ }
+ }
+ }
+ }
+ }
+ }
+}