policy/drools-pdp jdk11 upgrades
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.testtransaction;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.Properties;
31 import java.util.Set;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.TimeUnit;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
37 import org.onap.policy.drools.properties.DroolsPropertyConstants;
38 import org.onap.policy.drools.system.PolicyController;
39 import org.onap.policy.drools.system.PolicyControllerConstants;
40 import org.onap.policy.drools.system.PolicyEngineConstants;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class TestTransactionTest {
45     /** Test JUnit Controller Name. */
46     public static final String TEST_CONTROLLER_NAME = "unnamed";
47     /** Controller Configuration File. */
48     public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
49
50     /** Controller Configuration Backup File. */
51     public static final String TEST_CONTROLLER_FILE_BAK =
52             TEST_CONTROLLER_NAME + "-controller.properties.bak";
53
54     /** logger. */
55     private static Logger logger = LoggerFactory.getLogger(TestTransactionTest.class);
56
57     /**
58      * Start up.
59      *
60      * @throws IOException exception
61      */
62     @BeforeClass
63     public static void startUp() throws IOException {
64         logger.info("enter");
65
66         cleanUpWorkingDir();
67
68         /* ensure presence of config directory */
69         SystemPersistenceConstants.getManager().setConfigurationDir(null);
70     }
71
72     @Test
73     public void testRegisterUnregister() throws InterruptedException {
74         final Properties controllerProperties = new Properties();
75         controllerProperties.put(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
76         final PolicyController controller =
77                 PolicyEngineConstants.getManager().createPolicyController(TEST_CONTROLLER_NAME, controllerProperties);
78         assertNotNull(PolicyControllerConstants.getFactory().get(TEST_CONTROLLER_NAME));
79         logger.info(controller.toString());
80
81         CountDownLatch latch = new CountDownLatch(1);
82
83         // use our own impl so we can decrement the latch when run() completes
84         TtImpl impl = new TtImpl() {
85             @Override
86             protected TtControllerTask makeControllerTask(PolicyController controller) {
87                 return new TtControllerTask(controller) {
88                     @Override
89                     public void run() {
90                         super.run();
91                         latch.countDown();
92                     }
93                 };
94             }
95         };
96
97         impl.register(controller);
98         assertNotNull(TestTransactionConstants.getManager());
99
100         /*
101          * Unregistering the controller should terminate its TestTransaction thread if it hasn't already
102          * been terminated
103          */
104         impl.unregister(controller);
105
106         Thread ttThread = getThread(latch, "tt-controller-task-" + TEST_CONTROLLER_NAME);
107         assertEquals(null, ttThread);
108     }
109
110     /**
111      * Returns thread object based on String name.
112      * @param latch indicates when the thread has finished running
113      * @param threadName thread name
114      * @return the thread
115      * @throws InterruptedException exception
116      */
117     public Thread getThread(CountDownLatch latch, String threadName) throws InterruptedException {
118         // give a chance to the transaction thread to be spawned/destroyed
119         latch.await(5, TimeUnit.SECONDS);
120
121         final Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
122         for (final Thread thread : threadSet) {
123             if (thread.getName().equals(threadName)) {
124                 return thread;
125             }
126         }
127         return null;
128     }
129
130     /** clean up working directory. */
131     protected static void cleanUpWorkingDir() {
132         final Path testControllerPath =
133                         Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
134                                         TEST_CONTROLLER_FILE);
135         try {
136             Files.deleteIfExists(testControllerPath);
137         } catch (final Exception e) {
138             logger.info("Problem cleaning {}", testControllerPath, e);
139         }
140
141         final Path testControllerBakPath =
142                         Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
143                                         TEST_CONTROLLER_FILE_BAK);
144         try {
145             Files.deleteIfExists(testControllerBakPath);
146         } catch (final Exception e) {
147             logger.info("Problem cleaning {}", testControllerBakPath, e);
148         }
149     }
150 }