2a44e1c115d218eccf867ab1143c8cdded1b57b0
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * feature-test-transaction
4  * ================================================================================
5  * Copyright (C) 2017 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.PolicyProperties;
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         /**
44          * Test JUnit Controller Name
45          */
46         public static final String TEST_CONTROLLER_NAME = "unnamed";
47         /**
48          * Controller Configuration File
49          */
50         public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
51         
52         /**
53          * Controller Configuration Backup File
54          */
55         public static final String TEST_CONTROLLER_FILE_BAK = TEST_CONTROLLER_NAME + "-controller.properties.bak";
56         
57         
58         /**
59          * logger
60          */
61         private static Logger logger = LoggerFactory.getLogger(TestTransactionTest.class);
62         
63         
64
65         @BeforeClass
66         public static void startUp() throws IOException {
67                 logger.info("enter");
68                 
69                 cleanUpWorkingDir();
70                 
71                 /* ensure presence of config directory */
72                 Path configDir = Paths.get(SystemPersistence.CONFIG_DIR_NAME);
73                 if (Files.notExists(configDir))
74                         Files.createDirectories(configDir);
75         }
76         
77         @Test
78         public void registerUnregisterTest() {
79                 Properties controllerProperties = new Properties();
80                 controllerProperties.put(PolicyProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
81                 PolicyController controller = PolicyEngine.manager.createPolicyController(TEST_CONTROLLER_NAME, controllerProperties);
82                 Thread ttThread = null;
83                 
84                 TestTransaction.manager.register(controller);
85                 assertNotNull(TestTransaction.manager);
86                 
87                         /* 
88                          * If the controller was successfully registered
89                          * it will have a thread created. 
90                          */
91                 ttThread = getThread("tt-controller-task-" + TEST_CONTROLLER_NAME);
92                 assertNotNull(ttThread);
93
94                         /* 
95                          * Unregistering the controller should
96                          * terminate its TestTransaction thread
97                          * if it hasn't already been terminated
98                          */
99                 TestTransaction.manager.unregister(controller);
100                 
101                         /*
102                          * Put this thread to sleep so the TestTransaction
103                          * thread has enough time to terminate before
104                          * we check.
105                          */
106                 try {
107                         Thread.sleep(2000);
108                 } catch (InterruptedException e) {
109                         
110                 }
111                 ttThread = getThread("tt-controller-task-" + TEST_CONTROLLER_NAME);
112                 assertEquals(null, ttThread);
113                 
114                 
115         }
116
117                 /*
118                  * Returns thread object based on
119                  * String name
120                  */
121         public Thread getThread(String threadName) {
122                 
123                 Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
124                 for (Thread thread : threadSet) {
125                         if (thread.getName().equals(threadName)) {
126                                 return thread;
127                         }
128                         
129                 }
130                 return null;
131         }
132         
133         /**
134          * clean up working directory
135          */
136         protected static void cleanUpWorkingDir() {
137                 Path testControllerPath = Paths.get(SystemPersistence.CONFIG_DIR_NAME, TEST_CONTROLLER_FILE);
138                 try {
139                         Files.deleteIfExists(testControllerPath);
140                 } catch (Exception e) {
141                         logger.info("Problem cleaning {}", testControllerPath, e);
142                 }
143                 
144                 Path testControllerBakPath = Paths.get(SystemPersistence.CONFIG_DIR_NAME, TEST_CONTROLLER_FILE_BAK);
145                 try {
146                         Files.deleteIfExists(testControllerBakPath);
147                 } catch (Exception e) {
148                         logger.info("Problem cleaning {}", testControllerBakPath, e);
149                 }
150         }
151         
152 }