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
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;
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;
42 public class TestTransactionTest {
44 * Test JUnit Controller Name
46 public static final String TEST_CONTROLLER_NAME = "unnamed";
48 * Controller Configuration File
50 public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
53 * Controller Configuration Backup File
55 public static final String TEST_CONTROLLER_FILE_BAK =
56 TEST_CONTROLLER_NAME + "-controller.properties.bak";
62 private static Logger logger = LoggerFactory.getLogger(TestTransactionTest.class);
67 public static void startUp() throws IOException {
72 /* ensure presence of config directory */
73 SystemPersistence.manager.setConfigurationDir(null);
77 public void registerUnregisterTest() {
78 final Properties controllerProperties = new Properties();
79 controllerProperties.put(PolicyProperties.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
80 final PolicyController controller =
81 PolicyEngine.manager.createPolicyController(TEST_CONTROLLER_NAME, controllerProperties);
82 Thread ttThread = null;
84 TestTransaction.manager.register(controller);
85 assertNotNull(TestTransaction.manager);
88 * If the controller was successfully registered it will have a thread created.
90 ttThread = this.getThread("tt-controller-task-" + TEST_CONTROLLER_NAME);
91 assertNotNull(ttThread);
94 * Unregistering the controller should terminate its TestTransaction thread if it hasn't already
97 TestTransaction.manager.unregister(controller);
100 * Put this thread to sleep so the TestTransaction thread has enough time to terminate before we
105 } catch (final InterruptedException e) {
108 ttThread = this.getThread("tt-controller-task-" + TEST_CONTROLLER_NAME);
109 assertEquals(null, ttThread);
115 * Returns thread object based on String name
117 public Thread getThread(String threadName) {
119 final Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
120 for (final Thread thread : threadSet) {
121 if (thread.getName().equals(threadName)) {
130 * clean up working directory
132 protected static void cleanUpWorkingDir() {
133 final Path testControllerPath = Paths
134 .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE);
136 Files.deleteIfExists(testControllerPath);
137 } catch (final Exception e) {
138 logger.info("Problem cleaning {}", testControllerPath, e);
141 final Path testControllerBakPath = Paths
142 .get(SystemPersistence.manager.getConfigurationPath().toString(), TEST_CONTROLLER_FILE_BAK);
144 Files.deleteIfExists(testControllerBakPath);
145 } catch (final Exception e) {
146 logger.info("Problem cleaning {}", testControllerBakPath, e);