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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.testtransaction;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
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;
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;
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";
50 /** Controller Configuration Backup File. */
51 public static final String TEST_CONTROLLER_FILE_BAK =
52 TEST_CONTROLLER_NAME + "-controller.properties.bak";
55 private static Logger logger = LoggerFactory.getLogger(TestTransactionTest.class);
60 * @throws IOException exception
63 public static void startUp() throws IOException {
68 /* ensure presence of config directory */
69 SystemPersistenceConstants.getManager().setConfigurationDir(null);
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());
81 CountDownLatch latch = new CountDownLatch(1);
83 // use our own impl so we can decrement the latch when run() completes
84 TtImpl impl = new TtImpl() {
86 protected TtControllerTask makeControllerTask(PolicyController controller) {
87 return new TtControllerTask(controller) {
97 impl.register(controller);
98 assertNotNull(TestTransactionConstants.getManager());
101 * Unregistering the controller should terminate its TestTransaction thread if it hasn't already
104 impl.unregister(controller);
106 Thread ttThread = getThread(latch, "tt-controller-task-" + TEST_CONTROLLER_NAME);
107 assertEquals(null, ttThread);
111 * Returns thread object based on String name.
112 * @param latch indicates when the thread has finished running
113 * @param threadName thread name
115 * @throws InterruptedException exception
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);
121 final Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
122 for (final Thread thread : threadSet) {
123 if (thread.getName().equals(threadName)) {
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);
136 Files.deleteIfExists(testControllerPath);
137 } catch (final Exception e) {
138 logger.info("Problem cleaning {}", testControllerPath, e);
141 final Path testControllerBakPath =
142 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
143 TEST_CONTROLLER_FILE_BAK);
145 Files.deleteIfExists(testControllerBakPath);
146 } catch (final Exception e) {
147 logger.info("Problem cleaning {}", testControllerBakPath, e);