2 * ============LICENSE_START=======================================================
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
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.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;
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;
37 public class TestTransactionFeatureTest {
39 private AtomicInteger regCount;
40 private AtomicInteger unregCount;
41 private TestTransaction mgr;
42 private DroolsController drools;
43 private PolicyController ctlr;
44 private TestTransactionFeature feat;
47 * Initialize objects for each test.
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);
57 feat = new TestTransactionFeature() {
59 protected TestTransaction getTestTransMgr() {
64 when(ctlr.getDrools()).thenReturn(drools);
67 regCount.incrementAndGet();
69 }).when(mgr).register(ctlr);
72 unregCount.incrementAndGet();
74 }).when(mgr).unregister(ctlr);
78 public void testAfterStart() {
79 // try each combination of alive, locked, and brained
80 checkCombos(regCount, ctlr -> feat.afterStart(ctlr));
84 public void testAfterLock() {
85 checkSimple(unregCount, ctlr -> feat.afterLock(ctlr));
89 public void testAfterUnlock() {
90 // try each combination of alive, locked, and brained
91 checkCombos(regCount, ctlr -> feat.afterUnlock(ctlr));
95 public void testBeforeStop() {
96 checkSimple(unregCount, ctlr -> feat.beforeStop(ctlr));
100 public void testGetSequenceNumber() {
101 assertEquals(1000, feat.getSequenceNumber());
105 public void testGetTestTransMgr() {
106 assertNotNull(new TestTransactionFeature().getTestTransMgr());
110 * Try each combination of alive, locked, and brained.
112 * @param counter counter to check after each invocation
113 * @param method method to invoke
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));
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));
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));
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));
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));
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));
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));
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));
167 * Check the simple case that doesn't depend on the controller state.
169 * @param counter counter to check after each invocation
170 * @param method method to invoke
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));
177 when(ctlr.isAlive()).thenReturn(false);
178 assertFalse(method.apply(ctlr));
179 assertEquals(1, counter.getAndSet(0));