diff options
Diffstat (limited to 'policy-utils/src/test')
9 files changed, 482 insertions, 0 deletions
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 + } + } + } + } + } + } +} |