2 * ============LICENSE_START=======================================================
3 * feature-test-transaction
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.testtransaction;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.Properties;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
39 import org.onap.policy.drools.properties.DroolsPropertyConstants;
40 import org.onap.policy.drools.system.PolicyController;
41 import org.onap.policy.drools.system.PolicyControllerConstants;
42 import org.onap.policy.drools.system.PolicyEngineConstants;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class TestTransactionTest {
47 /** Test JUnit Controller Name. */
48 public static final String TEST_CONTROLLER_NAME = "unnamed";
49 /** Controller Configuration File. */
50 public static final String TEST_CONTROLLER_FILE = TEST_CONTROLLER_NAME + "-controller.properties";
52 /** Controller Configuration Backup File. */
53 public static final String TEST_CONTROLLER_FILE_BAK =
54 TEST_CONTROLLER_NAME + "-controller.properties.bak";
57 private static final Logger logger = LoggerFactory.getLogger(TestTransactionTest.class);
64 public static void startUp() {
69 /* ensure presence of config directory */
70 SystemPersistenceConstants.getManager().setConfigurationDir(null);
74 void testRegisterUnregister() throws InterruptedException {
75 final Properties controllerProperties = new Properties();
76 controllerProperties.put(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
77 final PolicyController controller =
78 PolicyEngineConstants.getManager().createPolicyController(TEST_CONTROLLER_NAME, controllerProperties);
79 assertNotNull(PolicyControllerConstants.getFactory().get(TEST_CONTROLLER_NAME));
80 logger.info(controller.toString());
82 CountDownLatch latch = new CountDownLatch(1);
84 // use our own impl, so we can decrement the latch when run() completes
85 TtImpl impl = new TtImpl() {
87 protected TtControllerTask makeControllerTask(PolicyController controller) {
88 return new TtControllerTask(controller) {
98 impl.register(controller);
99 assertNotNull(TestTransactionConstants.getManager());
102 * Unregistering the controller should terminate its TestTransaction thread if it hasn't already
105 impl.unregister(controller);
107 Thread ttThread = getThread(latch, "tt-controller-task-" + TEST_CONTROLLER_NAME);
108 assertNull(ttThread);
112 * Returns thread object based on String name.
113 * @param latch indicates when the thread has finished running
114 * @param threadName thread name
116 * @throws InterruptedException exception
118 public Thread getThread(CountDownLatch latch, String threadName) throws InterruptedException {
119 // give a chance to the transaction thread to be spawned/destroyed
120 latch.await(5, TimeUnit.SECONDS);
122 final Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
123 for (final Thread thread : threadSet) {
124 if (thread.getName().equals(threadName)) {
131 /** clean up working directory. */
132 protected static void cleanUpWorkingDir() {
133 final Path testControllerPath =
134 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
135 TEST_CONTROLLER_FILE);
137 Files.deleteIfExists(testControllerPath);
138 } catch (final Exception e) {
139 logger.info("Problem cleaning {}", testControllerPath, e);
142 final Path testControllerBakPath =
143 Paths.get(SystemPersistenceConstants.getManager().getConfigurationPath().toString(),
144 TEST_CONTROLLER_FILE_BAK);
146 Files.deleteIfExists(testControllerBakPath);
147 } catch (final Exception e) {
148 logger.info("Problem cleaning {}", testControllerBakPath, e);