2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020,2022 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021, 2024 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.domain.models;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
29 import com.worldturner.medeia.api.ValidationFailedException;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.ResourceUtils;
38 import org.onap.policy.drools.domain.models.artifact.NativeArtifactController;
39 import org.onap.policy.drools.domain.models.artifact.NativeArtifactPolicy;
40 import org.onap.policy.drools.domain.models.artifact.NativeArtifactProperties;
41 import org.onap.policy.drools.domain.models.artifact.NativeArtifactRulesArtifact;
42 import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
43 import org.onap.policy.drools.policies.DomainMaker;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 class DomainPolicyTypesTest {
51 private static final String NATIVE_DROOLS_POLICY_TYPE = "onap.policies.native.drools.Artifact";
53 // Native Drools Policy
54 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example";
55 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
56 "src/test/resources/tosca-policy-native-artifact-example.json";
58 // Controller Drools Policy
59 private static final String EXAMPLE_CONTROLLER_DROOLS_POLICY_NAME = "example";
60 private static final String EXAMPLE_CONTROLLER_DROOLS_POLICY_JSON =
61 "src/test/resources/tosca-policy-native-controller-example.json";
63 private DomainMaker domainMaker;
64 private StandardCoder nonValCoder;
68 domainMaker = new DomainMaker();
69 nonValCoder = new StandardCoder();
73 void testToscaNativeDroolsPolicy() throws CoderException, IOException {
74 String rawNativeDroolsPolicy =
75 getPolicyFromFileString();
76 ToscaPolicy toscaPolicy =
77 getExamplesPolicy(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME);
79 ToscaConceptIdentifier policyTypeId =
80 new ToscaConceptIdentifier(NATIVE_DROOLS_POLICY_TYPE, "1.0.0");
81 domainMaker.isConformant(policyTypeId, rawNativeDroolsPolicy);
83 assertTrue(domainMaker.isConformant(toscaPolicy));
84 NativeArtifactPolicy domainDroolsPolicy = domainMaker.convertTo(toscaPolicy, NativeArtifactPolicy.class);
85 assertEquals("org.onap.policy.drools.test", domainDroolsPolicy.getProperties().getRulesArtifact().getGroupId());
86 assertEquals("lifecycle", domainDroolsPolicy.getProperties().getRulesArtifact().getArtifactId());
87 assertEquals("1.0.0", domainDroolsPolicy.getProperties().getRulesArtifact().getVersion());
89 String policyId = "" + toscaPolicy.getMetadata().remove("policy-id");
90 assertThatThrownBy(() -> domainMaker.convertTo(toscaPolicy, NativeArtifactPolicy.class))
91 .isInstanceOf(CoderException.class).hasCauseInstanceOf(ValidationFailedException.class);
93 toscaPolicy.getMetadata().put("policy-id", policyId);
95 assertTrue(domainMaker.isDomainConformant(policyTypeId, domainDroolsPolicy));
96 assertTrue(domainMaker.conformance(policyTypeId, domainDroolsPolicy));
98 domainDroolsPolicy.setName("");
99 assertFalse(domainMaker.isDomainConformant(policyTypeId, domainDroolsPolicy));
100 assertThatThrownBy(() -> domainMaker.conformance(policyTypeId, domainDroolsPolicy))
101 .isInstanceOf(ValidationFailedException.class)
102 .hasMessageContaining("Pattern ^(.+)$ is not contained in text");
105 NativeArtifactPolicy domainDroolsPolicy2 =
106 NativeArtifactPolicy.builder().metadata(Metadata.builder().policyId("policy-id").build())
108 .version("1.0.0").properties(
109 NativeArtifactProperties.builder().controller(
110 NativeArtifactController.builder().name("example").build())
112 NativeArtifactRulesArtifact.builder().groupId("org.onap.policy.controlloop")
113 .artifactId("example").version("example").build()).build())
114 .type("onap.policies.native.drools.Artifact")
115 .typeVersion("1.0.0").build();
118 assertTrue(domainMaker
120 new ToscaConceptIdentifier(domainDroolsPolicy2.getType(), domainDroolsPolicy2.getTypeVersion()),
121 domainDroolsPolicy2));
125 void testToscaControllerPolicy() throws CoderException {
126 ToscaPolicy toscaPolicy =
127 getExamplesPolicy(EXAMPLE_CONTROLLER_DROOLS_POLICY_JSON, EXAMPLE_CONTROLLER_DROOLS_POLICY_NAME);
129 assertTrue(domainMaker.isConformant(toscaPolicy));
130 ControllerPolicy controllerPolicy = domainMaker.convertTo(toscaPolicy, ControllerPolicy.class);
132 assertEquals("example", controllerPolicy.getName());
133 assertEquals("1.0.0", controllerPolicy.getVersion());
134 assertEquals("onap.policies.native.drools.Controller", controllerPolicy.getType());
135 assertEquals("1.0.0", controllerPolicy.getTypeVersion());
136 assertEquals("example", controllerPolicy.getMetadata().getPolicyId());
137 assertEquals("lifecycle", controllerPolicy.getProperties().getControllerName());
138 assertEquals("dcae_topic", controllerPolicy.getProperties().getSourceTopics().get(0).getTopicName());
139 assertEquals("org.onap.policy.controlloop.CanonicalOnset",
140 controllerPolicy.getProperties().getSourceTopics().get(0).getEvents().get(0).getEventClass());
141 assertEquals("[?($.closedLoopEventStatus == 'ONSET')]",
142 controllerPolicy.getProperties().getSourceTopics().get(0).getEvents().get(0).getEventFilter());
143 assertEquals("org.onap.policy.controlloop.util.Serialization",
144 controllerPolicy.getProperties().getSourceTopics().get(0).getEvents().get(0)
145 .getCustomSerialization().getCustomSerializerClass());
147 controllerPolicy.getProperties().getSourceTopics().get(0).getEvents().get(0)
148 .getCustomSerialization().getJsonParser());
149 assertEquals("appc-cl", controllerPolicy.getProperties().getSinkTopics().get(0).getTopicName());
150 assertEquals("org.onap.policy.appc.Response",
151 controllerPolicy.getProperties().getSinkTopics().get(0).getEvents().get(0).getEventClass());
152 assertEquals("[?($.CommonHeader && $.Status)]",
153 controllerPolicy.getProperties().getSinkTopics().get(0).getEvents().get(0).getEventFilter());
154 assertEquals("org.onap.policy.appc.util.Serialization",
155 controllerPolicy.getProperties().getSinkTopics().get(0).getEvents().get(0)
156 .getCustomSerialization().getCustomSerializerClass());
157 assertEquals("gsonPretty",
158 controllerPolicy.getProperties().getSinkTopics().get(0).getEvents().get(0)
159 .getCustomSerialization().getJsonParser());
160 assertEquals("value1", controllerPolicy.getProperties().getCustomConfig().get("field1"));
163 private String getJsonFromFile() throws IOException {
164 return Files.readString(Paths.get(DomainPolicyTypesTest.EXAMPLE_NATIVE_DROOLS_POLICY_JSON));
167 private String getJsonFromResource(String resourcePath) {
168 return ResourceUtils.getResourceAsString(resourcePath);
171 private String getPolicyFromFileString() throws CoderException, IOException {
172 String policyJson = getJsonFromFile();
173 ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
174 return nonValCoder.encode(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(
175 DomainPolicyTypesTest.EXAMPLE_NATIVE_DROOLS_POLICY_NAME));
178 private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
179 String policyJson = getJsonFromResource(resourcePath);
180 ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
181 return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);