6bb09903bae14b6019e4eeb213b68da76aedffd2
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.testtransaction;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.mockito.Mockito.doAnswer;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.util.concurrent.atomic.AtomicInteger;
31 import java.util.function.Function;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.drools.controller.DroolsController;
35 import org.onap.policy.drools.system.PolicyController;
36
37 public class TestTransactionFeatureTest {
38
39     private AtomicInteger regCount;
40     private AtomicInteger unregCount;
41     private TestTransaction mgr;
42     private DroolsController drools;
43     private PolicyController ctlr;
44     private TestTransactionFeature feat;
45
46     /**
47      * Initialize objects for each test.
48      */
49     @Before
50     public void setUp() {
51         regCount = new AtomicInteger(0);
52         unregCount = new AtomicInteger(0);
53         mgr = mock(TestTransaction.class);
54         drools = mock(DroolsController.class);
55         ctlr = mock(PolicyController.class);
56
57         feat = new TestTransactionFeature() {
58             @Override
59             protected TestTransaction getTestTransMgr() {
60                 return mgr;
61             }
62         };
63
64         when(ctlr.getDrools()).thenReturn(drools);
65
66         doAnswer(args -> {
67             regCount.incrementAndGet();
68             return null;
69         }).when(mgr).register(ctlr);
70
71         doAnswer(args -> {
72             unregCount.incrementAndGet();
73             return null;
74         }).when(mgr).unregister(ctlr);
75     }
76
77     @Test
78     public void testAfterStart() {
79         // try each combination of alive, locked, and brained
80         checkCombos(regCount, ctlr -> feat.afterStart(ctlr));
81     }
82
83     @Test
84     public void testAfterLock() {
85         checkSimple(unregCount, ctlr -> feat.afterLock(ctlr));
86     }
87
88     @Test
89     public void testAfterUnlock() {
90         // try each combination of alive, locked, and brained
91         checkCombos(regCount, ctlr -> feat.afterUnlock(ctlr));
92     }
93
94     @Test
95     public void testBeforeStop() {
96         checkSimple(unregCount, ctlr -> feat.beforeStop(ctlr));
97     }
98
99     @Test
100     public void testGetSequenceNumber() {
101         assertEquals(1000, feat.getSequenceNumber());
102     }
103
104     @Test
105     public void testGetTestTransMgr() {
106         assertNotNull(new TestTransactionFeature().getTestTransMgr());
107     }
108
109     /**
110      * Try each combination of alive, locked, and brained.
111      *
112      * @param counter counter to check after each invocation
113      * @param method method to invoke
114      */
115     private void checkCombos(AtomicInteger counter, Function<PolicyController, Boolean> method) {
116         when(ctlr.isAlive()).thenReturn(true);
117         when(ctlr.isLocked()).thenReturn(true);
118         when(drools.isBrained()).thenReturn(true);
119         assertFalse(method.apply(ctlr));
120         assertEquals(0, counter.getAndSet(0));
121
122         when(ctlr.isAlive()).thenReturn(true);
123         when(ctlr.isLocked()).thenReturn(true);
124         when(drools.isBrained()).thenReturn(false);
125         assertFalse(method.apply(ctlr));
126         assertEquals(0, counter.getAndSet(0));
127
128         // this is the only one that should cause it to register
129         when(ctlr.isAlive()).thenReturn(true);
130         when(ctlr.isLocked()).thenReturn(false);
131         when(drools.isBrained()).thenReturn(true);
132         assertFalse(method.apply(ctlr));
133         assertEquals(1, counter.getAndSet(0));
134
135         when(ctlr.isAlive()).thenReturn(true);
136         when(ctlr.isLocked()).thenReturn(false);
137         when(drools.isBrained()).thenReturn(false);
138         assertFalse(method.apply(ctlr));
139         assertEquals(0, counter.getAndSet(0));
140
141         when(ctlr.isAlive()).thenReturn(false);
142         when(ctlr.isLocked()).thenReturn(true);
143         when(drools.isBrained()).thenReturn(true);
144         assertFalse(method.apply(ctlr));
145         assertEquals(0, counter.getAndSet(0));
146
147         when(ctlr.isAlive()).thenReturn(false);
148         when(ctlr.isLocked()).thenReturn(true);
149         when(drools.isBrained()).thenReturn(false);
150         assertFalse(method.apply(ctlr));
151         assertEquals(0, counter.getAndSet(0));
152
153         when(ctlr.isAlive()).thenReturn(false);
154         when(ctlr.isLocked()).thenReturn(false);
155         when(drools.isBrained()).thenReturn(true);
156         assertFalse(method.apply(ctlr));
157         assertEquals(0, counter.getAndSet(0));
158
159         when(ctlr.isAlive()).thenReturn(false);
160         when(ctlr.isLocked()).thenReturn(false);
161         when(drools.isBrained()).thenReturn(false);
162         assertFalse(method.apply(ctlr));
163         assertEquals(0, counter.getAndSet(0));
164     }
165
166     /**
167      * Check the simple case that doesn't depend on the controller state.
168      *
169      * @param counter counter to check after each invocation
170      * @param method method to invoke
171      */
172     private void checkSimple(AtomicInteger counter, Function<PolicyController, Boolean> method) {
173         when(ctlr.isAlive()).thenReturn(true);
174         assertFalse(method.apply(ctlr));
175         assertEquals(1, counter.getAndSet(0));
176
177         when(ctlr.isAlive()).thenReturn(false);
178         assertFalse(method.apply(ctlr));
179         assertEquals(1, counter.getAndSet(0));
180     }
181 }