2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.mockito.Mockito.doAnswer;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.when;
31 import java.util.concurrent.atomic.AtomicInteger;
32 import java.util.function.Function;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.onap.policy.drools.controller.DroolsController;
36 import org.onap.policy.drools.system.PolicyController;
38 class TestTransactionFeatureTest {
40 private AtomicInteger regCount;
41 private AtomicInteger unregCount;
42 private TestTransaction mgr;
43 private DroolsController drools;
44 private PolicyController ctlr;
45 private TestTransactionFeature feat;
48 * Initialize objects for each test.
52 regCount = new AtomicInteger(0);
53 unregCount = new AtomicInteger(0);
54 mgr = mock(TestTransaction.class);
55 drools = mock(DroolsController.class);
56 ctlr = mock(PolicyController.class);
58 feat = new TestTransactionFeature() {
60 protected TestTransaction getTestTransMgr() {
65 when(ctlr.getDrools()).thenReturn(drools);
68 regCount.incrementAndGet();
70 }).when(mgr).register(ctlr);
73 unregCount.incrementAndGet();
75 }).when(mgr).unregister(ctlr);
79 void testAfterStart() {
80 // try each combination of alive, locked, and brained
81 checkCombos(regCount, ctlr -> feat.afterStart(ctlr));
85 void testAfterLock() {
86 checkSimple(unregCount, ctlr -> feat.afterLock(ctlr));
90 void testAfterUnlock() {
91 // try each combination of alive, locked, and brained
92 checkCombos(regCount, ctlr -> feat.afterUnlock(ctlr));
96 void testBeforeStop() {
97 checkSimple(unregCount, ctlr -> feat.beforeStop(ctlr));
101 void testGetSequenceNumber() {
102 assertEquals(1000, feat.getSequenceNumber());
106 void testGetTestTransMgr() {
107 assertNotNull(new TestTransactionFeature().getTestTransMgr());
111 * Try each combination of alive, locked, and brained.
113 * @param counter counter to check after each invocation
114 * @param method method to invoke
116 private void checkCombos(AtomicInteger counter, Function<PolicyController, Boolean> method) {
117 when(ctlr.isAlive()).thenReturn(true);
118 when(ctlr.isLocked()).thenReturn(true);
119 when(drools.isBrained()).thenReturn(true);
120 assertFalse(method.apply(ctlr));
121 assertEquals(0, counter.getAndSet(0));
123 when(ctlr.isAlive()).thenReturn(true);
124 when(ctlr.isLocked()).thenReturn(true);
125 when(drools.isBrained()).thenReturn(false);
126 assertFalse(method.apply(ctlr));
127 assertEquals(0, counter.getAndSet(0));
129 // this is the only one that should cause it to register
130 when(ctlr.isAlive()).thenReturn(true);
131 when(ctlr.isLocked()).thenReturn(false);
132 when(drools.isBrained()).thenReturn(true);
133 assertFalse(method.apply(ctlr));
134 assertEquals(1, counter.getAndSet(0));
136 when(ctlr.isAlive()).thenReturn(true);
137 when(ctlr.isLocked()).thenReturn(false);
138 when(drools.isBrained()).thenReturn(false);
139 assertFalse(method.apply(ctlr));
140 assertEquals(0, counter.getAndSet(0));
142 when(ctlr.isAlive()).thenReturn(false);
143 when(ctlr.isLocked()).thenReturn(true);
144 when(drools.isBrained()).thenReturn(true);
145 assertFalse(method.apply(ctlr));
146 assertEquals(0, counter.getAndSet(0));
148 when(ctlr.isAlive()).thenReturn(false);
149 when(ctlr.isLocked()).thenReturn(true);
150 when(drools.isBrained()).thenReturn(false);
151 assertFalse(method.apply(ctlr));
152 assertEquals(0, counter.getAndSet(0));
154 when(ctlr.isAlive()).thenReturn(false);
155 when(ctlr.isLocked()).thenReturn(false);
156 when(drools.isBrained()).thenReturn(true);
157 assertFalse(method.apply(ctlr));
158 assertEquals(0, counter.getAndSet(0));
160 when(ctlr.isAlive()).thenReturn(false);
161 when(ctlr.isLocked()).thenReturn(false);
162 when(drools.isBrained()).thenReturn(false);
163 assertFalse(method.apply(ctlr));
164 assertEquals(0, counter.getAndSet(0));
168 * Check the simple case that doesn't depend on the controller state.
170 * @param counter counter to check after each invocation
171 * @param method method to invoke
173 private void checkSimple(AtomicInteger counter, Function<PolicyController, Boolean> method) {
174 when(ctlr.isAlive()).thenReturn(true);
175 assertFalse(method.apply(ctlr));
176 assertEquals(1, counter.getAndSet(0));
178 when(ctlr.isAlive()).thenReturn(false);
179 assertFalse(method.apply(ctlr));
180 assertEquals(1, counter.getAndSet(0));