2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2022 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 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.lifecycle;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
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.assertNull;
28 import static org.junit.jupiter.api.Assertions.assertSame;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import java.io.IOException;
32 import java.util.Properties;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
36 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
39 import org.onap.policy.drools.server.restful.TestConstants;
40 import org.onap.policy.drools.system.PolicyControllerConstants;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 * Native Controller Policy Test.
46 class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningTest {
47 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example.controller";
48 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
49 "src/test/resources/tosca-policy-native-controller-example.json";
52 * Test initialization.
56 fsm = makeFsmWithPseudoTime();
60 void testDeployUndeploy() throws IOException, CoderException {
61 fsm = makeFsmWithPseudoTime();
63 assertTrue(controllerSupport.getController().getDrools().isBrained());
64 assertFalse(controllerSupport.getController().isAlive());
65 assertFalse(controllerSupport.getController().getDrools().isAlive());
66 assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
68 assertTrue(controllerSupport.getController().start());
70 assertTrue(controllerSupport.getController().isAlive());
71 assertTrue(controllerSupport.getController().getDrools().isAlive());
72 assertTrue(controllerSupport.getController().getDrools().isBrained());
73 assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
75 ToscaPolicy policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME);
76 ControllerPolicy controllerPolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
77 PolicyTypeNativeDroolsController controller =
78 new PolicyTypeNativeDroolsController(policy.getTypeIdentifier(), fsm);
79 assertTrue(controller.undeploy(policy));
80 assertThatIllegalArgumentException().isThrownBy(
81 () -> PolicyControllerConstants.getFactory().get(controllerPolicy.getName()));
83 Properties noopTopicProperties = new Properties();
84 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, TestConstants.DCAE_TOPIC);
85 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, TestConstants.APPC_CL_TOPIC);
86 TopicEndpointManager.getManager().addTopics(noopTopicProperties);
88 assertTrue(controller.deploy(policy));
89 /* this should be ok too */
90 assertTrue(controller.deploy(policy));
94 void testControllerProperties() throws CoderException {
95 Properties noopTopicProperties = new Properties();
96 String noopSources = String.join(",", TestConstants.DCAE_TOPIC, TestConstants.APPC_CL_TOPIC,
97 TestConstants.APPC_LCM_WRITE_TOPIC, TestConstants.SDNR_CL_RSP_TOPIC);
98 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, noopSources);
100 String noopSinks = String.join(",", TestConstants.APPC_CL_TOPIC, TestConstants.APPC_LCM_READ_TOPIC,
101 TestConstants.POLICY_CL_MGT_TOPIC, TestConstants.DCAE_CL_RSP_TOPIC);
102 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, noopSinks);
103 TopicEndpointManager.getManager().addTopics(noopTopicProperties);
105 ToscaPolicy nativeControllerPolicy =
106 getExamplesPolicy("policies/usecases.native.controller.policy.input.tosca.json", "usecases");
107 PolicyTypeNativeDroolsController controller =
108 new PolicyTypeNativeDroolsController(nativeControllerPolicy.getTypeIdentifier(), fsm);
109 assertTrue(controller.deploy(nativeControllerPolicy));
110 Properties properties = PolicyControllerConstants.getFactory().get("usecases").getProperties();
112 assertEquals("usecases", properties.getProperty("controller.name"));
114 assertNull(properties.getProperty("rules.groupId"));
115 assertNull(properties.getProperty("rules.artifactId"));
116 assertNull(properties.getProperty("rules.version"));
118 assertEquals("dcae_topic,appc-cl,appc-lcm-write,sdnr-cl-rsp",
119 properties.getProperty("noop.source.topics"));
120 assertEquals("appc-cl,appc-lcm-read,policy-cl-mgt,dcae_cl_rsp",
121 properties.getProperty("noop.sink.topics"));
123 assertEquals("org.onap.policy.controlloop.CanonicalOnset,org.onap.policy.controlloop.CanonicalAbated",
124 properties.getProperty("noop.source.topics.dcae_topic.events"));
125 assertEquals("[?($.closedLoopEventStatus == 'ONSET')]",
127 .getProperty("noop.source.topics.dcae_topic.events.org.onap.policy.controlloop.CanonicalOnset.filter"));
128 assertEquals("[?($.closedLoopEventStatus == 'ABATED')]",
130 .getProperty("noop.source.topics.dcae_topic.events."
131 + "org.onap.policy.controlloop.CanonicalAbated.filter"));
132 assertEquals("org.onap.policy.controlloop.util.Serialization,gson",
133 properties.getProperty("noop.source.topics.dcae_topic.events.custom.gson"));
135 assertEquals("org.onap.policy.appc.Response", properties.getProperty("noop.source.topics.appc-cl.events"));
136 assertEquals("[?($.CommonHeader && $.Status)]",
138 .getProperty("noop.source.topics.appc-cl.events.org.onap.policy.appc.Response.filter"));
139 assertEquals("org.onap.policy.appc.util.Serialization,gsonPretty",
140 properties.getProperty("noop.source.topics.appc-cl.events.custom.gson"));
142 assertEquals("org.onap.policy.appclcm.AppcLcmMessageWrapper",
143 properties.getProperty("noop.source.topics.appc-lcm-write.events"));
144 assertEquals("[?($.type == 'response')]",
146 .getProperty("noop.source.topics.appc-lcm-write.events."
147 + "org.onap.policy.appclcm.AppcLcmMessageWrapper.filter"));
148 assertEquals("org.onap.policy.appclcm.util.Serialization,gson",
149 properties.getProperty("noop.source.topics.appc-lcm-write.events.custom.gson"));
151 assertEquals("org.onap.policy.sdnr.PciResponseWrapper",
152 properties.getProperty("noop.source.topics.sdnr-cl-rsp.events"));
153 assertEquals("[?($.type == 'response')]",
155 .getProperty("noop.source.topics.sdnr-cl-rsp.events."
156 + "org.onap.policy.sdnr.PciResponseWrapper.filter"));
157 assertEquals("org.onap.policy.sdnr.util.Serialization,gson",
158 properties.getProperty("noop.source.topics.sdnr-cl-rsp.events.custom.gson"));
160 assertEquals("org.onap.policy.appc.Request", properties.getProperty("noop.sink.topics.appc-cl.events"));
161 assertEquals("org.onap.policy.appc.util.Serialization,gsonPretty",
162 properties.getProperty("noop.sink.topics.appc-cl.events.custom.gson"));
164 assertEquals("org.onap.policy.appclcm.AppcLcmMessageWrapper",
165 properties.getProperty("noop.sink.topics.appc-lcm-read.events"));
166 assertEquals("org.onap.policy.appclcm.util.Serialization,gson",
167 properties.getProperty("noop.sink.topics.appc-lcm-read.events.custom.gson"));
169 assertEquals("org.onap.policy.controlloop.VirtualControlLoopNotification",
170 properties.getProperty("noop.sink.topics.policy-cl-mgt.events"));
171 assertEquals("org.onap.policy.controlloop.util.Serialization,gsonPretty",
172 properties.getProperty("noop.sink.topics.policy-cl-mgt.events.custom.gson"));
174 assertEquals("org.onap.policy.controlloop.ControlLoopResponse",
175 properties.getProperty("noop.sink.topics.dcae_cl_rsp.events"));
176 assertEquals("org.onap.policy.controlloop.util.Serialization,gsonPretty",
177 properties.getProperty("noop.sink.topics.dcae_cl_rsp.events.custom.gson"));
179 assertEquals("test", properties.getProperty("notes"));
180 assertEquals("auto", properties.getProperty("persistence.type"));
182 assertTrue(controller.undeploy(nativeControllerPolicy));