2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.system;
24 import static org.awaitility.Awaitility.await;
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;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.Properties;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.jupiter.api.AfterAll;
37 import org.junit.jupiter.api.BeforeAll;
38 import org.junit.jupiter.api.MethodOrderer;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.api.TestMethodOrder;
41 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
42 import org.onap.policy.common.endpoints.event.comm.TopicSink;
43 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
44 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
45 import org.onap.policy.common.utils.gson.GsonTestUtils;
46 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
47 import org.onap.policy.drools.properties.DroolsPropertyConstants;
48 import org.onap.policy.drools.protocol.coders.EventProtocolCoderConstants;
49 import org.onap.policy.drools.protocol.coders.EventProtocolParams;
50 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
51 import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * PolicyEngine unit tests.
59 @TestMethodOrder(MethodOrderer.DisplayName.class)
60 class PolicyEngineTest {
62 * Default Telemetry port for JUnits.
64 public static final int DEFAULT_TELEMETRY_PORT = 9698;
67 * Test JUnit Controller Name.
69 public static final String TEST_CONTROLLER_NAME = "foo";
72 * Controller Configuration File.
74 public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
77 * Controller Configuration Backup File.
79 public static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_FILE + ".bak";
84 private static final String ENCODER_GROUP = "foo";
89 private static final String ENCODER_ARTIFACT = "bar";
94 private static final String ENCODER_VERSION = null;
99 private static final String NOOP_TOPIC = "JUNIT";
104 private static final Logger logger = LoggerFactory.getLogger(PolicyEngineTest.class);
106 private static GsonTestUtils gson;
109 * clean up working directory.
111 protected static void cleanUpWorkingDir() {
112 final Path testControllerPath =
113 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
114 TEST_CONTROLLER_FILE);
116 Files.deleteIfExists(testControllerPath);
117 } catch (final Exception e) {
118 logger.info("Problem cleaning {}", testControllerPath, e);
121 final Path testControllerBakPath =
122 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
123 TEST_CONTROLLER_FILE_BAK);
125 Files.deleteIfExists(testControllerBakPath);
126 } catch (final Exception e) {
127 logger.info("Problem cleaning {}", testControllerBakPath, e);
134 * @throws IOException throws IO exception
137 static void startUp() throws IOException {
138 logger.info("startUp");
140 gson = new GsonTestUtils();
144 /* ensure presence of config directory */
145 final Path configDir = Paths.get(SystemPersistenceConstants.DEFAULT_CONFIGURATION_DIR);
146 if (Files.notExists(configDir)) {
147 Files.createDirectories(configDir);
152 void test100Configure() {
153 var manager = (PolicyEngineManager) PolicyEngineConstants.getManager();
154 var engineProps = manager.defaultTelemetryConfig();
156 /* override default port */
157 engineProps.put(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "."
158 + PolicyEngineConstants.TELEMETRY_SERVER_DEFAULT_NAME
159 + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "" + DEFAULT_TELEMETRY_PORT);
161 assertFalse(manager.isAlive());
162 manager.setHostName("foo");
163 manager.setClusterName("0");
164 manager.configure(engineProps);
165 assertFalse(PolicyEngineConstants.getManager().isAlive());
167 logger.info("engine {} has configuration {}", PolicyEngineConstants.getManager(), engineProps);
169 PolicyEngineConstants.getManager().getStats().getGroupStat().setBirthTime(0L);
170 gson.compareGson(PolicyEngineConstants.getManager(),
171 new File(PolicyEngineTest.class.getSimpleName() + "Config.json"));
175 void test200Start() {
176 logger.info("enter");
178 PolicyEngineConstants.getManager().start();
180 assertTrue(PolicyEngineConstants.getManager().isAlive());
181 assertFalse(PolicyEngineConstants.getManager().isLocked());
182 assertFalse(PolicyEngineConstants.getManager().getHttpServers().isEmpty());
183 assertTrue(PolicyEngineConstants.getManager().getHttpServers().get(0).isAlive());
188 logger.info("enter");
190 PolicyEngineConstants.getManager().lock();
192 assertTrue(PolicyEngineConstants.getManager().isAlive());
193 assertTrue(PolicyEngineConstants.getManager().isLocked());
194 assertFalse(PolicyEngineConstants.getManager().getHttpServers().isEmpty());
195 assertTrue(PolicyEngineConstants.getManager().getHttpServers().get(0).isAlive());
199 void test301Unlock() {
200 logger.info("enter");
202 PolicyEngineConstants.getManager().unlock();
204 assertTrue(PolicyEngineConstants.getManager().isAlive());
205 assertFalse(PolicyEngineConstants.getManager().isLocked());
206 assertFalse(PolicyEngineConstants.getManager().getHttpServers().isEmpty());
207 assertTrue(PolicyEngineConstants.getManager().getHttpServers().get(0).isAlive());
211 void test350TopicDeliver() {
212 final Properties noopSinkProperties = new Properties();
213 noopSinkProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, NOOP_TOPIC);
215 TopicEndpointManager.getManager().addTopicSinks(noopSinkProperties).get(0).start();
217 EventProtocolCoderConstants.getManager().addEncoder(
218 EventProtocolParams.builder().groupId(ENCODER_GROUP).artifactId(ENCODER_ARTIFACT)
219 .topic(NOOP_TOPIC).eventClass(DroolsConfiguration.class.getName())
220 .protocolFilter(new JsonProtocolFilter()).customGsonCoder(null)
221 .modelClassLoaderHash(DroolsConfiguration.class.getName().hashCode()).build());
223 assertTrue(PolicyEngineConstants.getManager().deliver(NOOP_TOPIC,
224 new DroolsConfiguration(ENCODER_ARTIFACT, ENCODER_GROUP, ENCODER_VERSION)));
226 final TopicSink sink = NoopTopicFactories.getSinkFactory().get(NOOP_TOPIC);
227 assertTrue(sink.getRecentEvents()[0].contains(ENCODER_GROUP));
228 assertTrue(sink.getRecentEvents()[0].contains(ENCODER_ARTIFACT));
230 EventProtocolCoderConstants.getManager().removeEncoders(ENCODER_GROUP, ENCODER_ARTIFACT, NOOP_TOPIC);
234 void test400ControllerAdd() {
235 logger.info("enter");
237 final Properties controllerProperties = new Properties();
238 controllerProperties.put(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
239 PolicyEngineConstants.getManager().createPolicyController(TEST_CONTROLLER_NAME, controllerProperties);
241 assertEquals(1, PolicyControllerConstants.getFactory().inventory().size());
243 PolicyEngineConstants.getManager().getStats().getGroupStat().setBirthTime(0L);
244 gson.compareGson(PolicyEngineConstants.getManager(),
245 new File(PolicyEngineTest.class.getSimpleName() + "Add.json"));
249 void test401ControllerVerify() {
250 logger.info("enter");
252 final PolicyController testController = PolicyControllerConstants.getFactory().get(TEST_CONTROLLER_NAME);
254 assertFalse(testController.isAlive());
255 assertFalse(testController.isLocked());
257 testController.start();
259 assertTrue(testController.isAlive());
260 assertFalse(testController.isLocked());
264 void test500Deactivate() {
265 logger.info("enter");
267 PolicyEngineConstants.getManager().deactivate();
269 final PolicyController testController = PolicyControllerConstants.getFactory().get(TEST_CONTROLLER_NAME);
270 assertFalse(testController.isAlive());
271 assertTrue(testController.isLocked());
272 assertTrue(PolicyEngineConstants.getManager().isLocked());
273 assertTrue(PolicyEngineConstants.getManager().isAlive());
277 void test501Activate() {
278 logger.info("enter");
280 PolicyEngineConstants.getManager().activate();
282 final PolicyController testController = PolicyControllerConstants.getFactory().get(TEST_CONTROLLER_NAME);
283 assertTrue(testController.isAlive());
284 assertFalse(testController.isLocked());
285 assertFalse(PolicyEngineConstants.getManager().isLocked());
286 assertTrue(PolicyEngineConstants.getManager().isAlive());
290 void test900ControllerRemove() {
291 logger.info("enter");
293 PolicyEngineConstants.getManager().removePolicyController(TEST_CONTROLLER_NAME);
294 assertTrue(PolicyControllerConstants.getFactory().inventory().isEmpty());
299 logger.info("enter");
301 /* Shutdown managed resources */
302 PolicyControllerConstants.getFactory().shutdown();
303 TopicEndpointManager.getManager().shutdown();
304 PolicyEngineConstants.getManager().stop();
306 await().atMost(10, TimeUnit.SECONDS).until(() -> !PolicyEngineConstants.getManager().isAlive());
307 assertFalse(PolicyEngineConstants.getManager().isAlive());