Fix checkstyle for features submodules.
[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-2018 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
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.drools.persistence.SystemPersistence;
36 import org.onap.policy.drools.properties.DroolsProperties;
37 import org.onap.policy.drools.system.PolicyController;
38 import org.onap.policy.drools.system.PolicyEngine;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class TestTransactionTest {
43     /** Test JUnit Controller Name. */
44     public static final String TEST_CONTROLLER_NAME = "unnamed";
45     /** Controller Configuration File. */
46     public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
47
48     /** Controller Configuration Backup File. */
49     public static final String TEST_CONTROLLER_FILE_BAK =
50             TEST_CONTROLLER_NAME + "-controller.properties.bak";
51
52     /** logger. */
53     private static Logger logger = LoggerFactory.getLogger(TestTransactionTest.class);
54
55     /**
56      * Start up.
57      * 
58      * @throws IOException exception
59      */
60     @BeforeClass
61     public static void startUp() throws IOException {
62         logger.info("enter");
63
64         cleanUpWorkingDir();
65
66         /* ensure presence of config directory */
67         SystemPersistence.manager.setConfigurationDir(null);
68     }
69
70     @Test
71     public void registerUnregisterTest() throws InterruptedException {
72         final Properties controllerProperties = new Properties();
73         controllerProperties.put(DroolsProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
74         final PolicyController controller =
75                 PolicyEngine.manager.createPolicyController(TEST_CONTROLLER_NAME, controllerProperties);
76         assertNotNull(PolicyController.factory.get(TEST_CONTROLLER_NAME));
77         logger.info(controller.toString());
78
79         TestTransaction.manager.register(controller);
80         assertNotNull(TestTransaction.manager);
81
82         /*
83          * Unregistering the controller should terminate its TestTransaction thread if it hasn't already
84          * been terminated
85          */
86         TestTransaction.manager.unregister(controller);
87
88         Thread ttThread = this.getThread("tt-controller-task-" + TEST_CONTROLLER_NAME);
89         assertEquals(null, ttThread);
90     }
91
92     /**
93      * Returns thread object based on String name.
94      * 
95      * @param threadName thread name
96      * @return the thread
97      * @throws InterruptedException exception
98      */
99     public Thread getThread(String threadName) throws InterruptedException {
100         // give a chance to the transaction thread to be spawned/destroyed
101         Thread.sleep(5000L);
102
103         final Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
104         for (final Thread thread : threadSet) {
105             if (thread.getName().equals(threadName)) {
106                 return thread;
107             }
108         }
109         return null;
110     }
111
112     /** clean up working directory. */
113     protected static void cleanUpWorkingDir() {
114         final Path testControllerPath =
115                 Paths.get(
116                         SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
117         try {
118             Files.deleteIfExists(testControllerPath);
119         } catch (final Exception e) {
120             logger.info("Problem cleaning {}", testControllerPath, e);
121         }
122
123         final Path testControllerBakPath =
124                 Paths.get(
125                         SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK);
126         try {
127             Files.deleteIfExists(testControllerBakPath);
128         } catch (final Exception e) {
129             logger.info("Problem cleaning {}", testControllerBakPath, e);
130         }
131     }
132 }