Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-test-transaction / src / test / java / org / onap / policy / drools / testtransaction / TestTransactionFeatureTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.testtransaction;
23
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;
30
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;
37
38 class TestTransactionFeatureTest {
39
40     private AtomicInteger regCount;
41     private AtomicInteger unregCount;
42     private TestTransaction mgr;
43     private DroolsController drools;
44     private PolicyController ctlr;
45     private TestTransactionFeature feat;
46
47     /**
48      * Initialize objects for each test.
49      */
50     @BeforeEach
51     public void setUp() {
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);
57
58         feat = new TestTransactionFeature() {
59             @Override
60             protected TestTransaction getTestTransMgr() {
61                 return mgr;
62             }
63         };
64
65         when(ctlr.getDrools()).thenReturn(drools);
66
67         doAnswer(args -> {
68             regCount.incrementAndGet();
69             return null;
70         }).when(mgr).register(ctlr);
71
72         doAnswer(args -> {
73             unregCount.incrementAndGet();
74             return null;
75         }).when(mgr).unregister(ctlr);
76     }
77
78     @Test
79     void testAfterStart() {
80         // try each combination of alive, locked, and brained
81         checkCombos(regCount, ctlr -> feat.afterStart(ctlr));
82     }
83
84     @Test
85     void testAfterLock() {
86         checkSimple(unregCount, ctlr -> feat.afterLock(ctlr));
87     }
88
89     @Test
90     void testAfterUnlock() {
91         // try each combination of alive, locked, and brained
92         checkCombos(regCount, ctlr -> feat.afterUnlock(ctlr));
93     }
94
95     @Test
96     void testBeforeStop() {
97         checkSimple(unregCount, ctlr -> feat.beforeStop(ctlr));
98     }
99
100     @Test
101     void testGetSequenceNumber() {
102         assertEquals(1000, feat.getSequenceNumber());
103     }
104
105     @Test
106     void testGetTestTransMgr() {
107         assertNotNull(new TestTransactionFeature().getTestTransMgr());
108     }
109
110     /**
111      * Try each combination of alive, locked, and brained.
112      *
113      * @param counter counter to check after each invocation
114      * @param method method to invoke
115      */
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));
122
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));
128
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));
135
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));
141
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));
147
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));
153
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));
159
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));
165     }
166
167     /**
168      * Check the simple case that doesn't depend on the controller state.
169      *
170      * @param counter counter to check after each invocation
171      * @param method method to invoke
172      */
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));
177
178         when(ctlr.isAlive()).thenReturn(false);
179         assertFalse(method.apply(ctlr));
180         assertEquals(1, counter.getAndSet(0));
181     }
182 }