Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-test-transaction / src / test / java / org / onap / policy / drools / testtransaction / TestTransactionTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-test-transaction
4  * ================================================================================
5  * Copyright (C) 2017-2020 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.testtransaction;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.Properties;
33 import java.util.Set;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
39 import org.onap.policy.drools.properties.DroolsPropertyConstants;
40 import org.onap.policy.drools.system.PolicyController;
41 import org.onap.policy.drools.system.PolicyControllerConstants;
42 import org.onap.policy.drools.system.PolicyEngineConstants;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class TestTransactionTest {
47     /** Test JUnit Controller Name. */
48     public static final String TEST_CONTROLLER_NAME = "unnamed";
49     /** Controller Configuration File. */
50     public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
51
52     /** Controller Configuration Backup File. */
53     public static final String TEST_CONTROLLER_FILE_BAK =
54             TEST_CONTROLLER_NAME + "-controller.properties.bak";
55
56     /** logger. */
57     private static final Logger logger = LoggerFactory.getLogger(TestTransactionTest.class);
58
59     /**
60      * Start up.
61      *
62      */
63     @BeforeAll
64     public static void startUp() {
65         logger.info("enter");
66
67         cleanUpWorkingDir();
68
69         /* ensure presence of config directory */
70         SystemPersistenceConstants.getManager().setConfigurationDir(null);
71     }
72
73     @Test
74     void testRegisterUnregister() throws InterruptedException {
75         final Properties controllerProperties = new Properties();
76         controllerProperties.put(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
77         final PolicyController controller =
78                 PolicyEngineConstants.getManager().createPolicyController(TEST_CONTROLLER_NAME, controllerProperties);
79         assertNotNull(PolicyControllerConstants.getFactory().get(TEST_CONTROLLER_NAME));
80         logger.info(controller.toString());
81
82         CountDownLatch latch = new CountDownLatch(1);
83
84         // use our own impl, so we can decrement the latch when run() completes
85         TtImpl impl = new TtImpl() {
86             @Override
87             protected TtControllerTask makeControllerTask(PolicyController controller) {
88                 return new TtControllerTask(controller) {
89                     @Override
90                     public void run() {
91                         super.run();
92                         latch.countDown();
93                     }
94                 };
95             }
96         };
97
98         impl.register(controller);
99         assertNotNull(TestTransactionConstants.getManager());
100
101         /*
102          * Unregistering the controller should terminate its TestTransaction thread if it hasn't already
103          * been terminated
104          */
105         impl.unregister(controller);
106
107         Thread ttThread = getThread(latch, "tt-controller-task-" + TEST_CONTROLLER_NAME);
108         assertNull(ttThread);
109     }
110
111     /**
112      * Returns thread object based on String name.
113      * @param latch indicates when the thread has finished running
114      * @param threadName thread name
115      * @return the thread
116      * @throws InterruptedException exception
117      */
118     public Thread getThread(CountDownLatch latch, String threadName) throws InterruptedException {
119         // give a chance to the transaction thread to be spawned/destroyed
120         latch.await(5, TimeUnit.SECONDS);
121
122         final Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
123         for (final Thread thread : threadSet) {
124             if (thread.getName().equals(threadName)) {
125                 return thread;
126             }
127         }
128         return null;
129     }
130
131     /** clean up working directory. */
132     protected static void cleanUpWorkingDir() {
133         final Path testControllerPath =
134                         Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
135                                         TEST_CONTROLLER_FILE);
136         try {
137             Files.deleteIfExists(testControllerPath);
138         } catch (final Exception e) {
139             logger.info("Problem cleaning {}", testControllerPath, e);
140         }
141
142         final Path testControllerBakPath =
143                         Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
144                                         TEST_CONTROLLER_FILE_BAK);
145         try {
146             Files.deleteIfExists(testControllerBakPath);
147         } catch (final Exception e) {
148             logger.info("Problem cleaning {}", testControllerBakPath, e);
149         }
150     }
151 }