2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.controlloop.eventmanager;
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNotSame;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.doThrow;
33 import static org.mockito.Mockito.never;
34 import static org.mockito.Mockito.verify;
36 import java.time.Instant;
37 import java.util.UUID;
38 import java.util.concurrent.CompletableFuture;
39 import java.util.function.Consumer;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.policy.controlloop.ControlLoopOperation;
45 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
46 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
47 import org.onap.policy.drools.core.lock.Lock;
49 public class LockDataTest {
51 private static final String ENTITY = "my-entity";
52 private static final UUID REQ_ID = UUID.randomUUID();
57 private Consumer<OperationOutcome> callback1;
59 private Consumer<OperationOutcome> callback2;
61 private Consumer<OperationOutcome> callback3;
63 private LockData data;
70 MockitoAnnotations.initMocks(this);
72 data = new LockData(ENTITY, REQ_ID);
76 public void testGetFuture() {
77 CompletableFuture<OperationOutcome> future = data.getFuture();
78 assertNotNull(future);
79 assertFalse(future.isDone());
83 public void testAddUnavailableCallback() {
84 data.addUnavailableCallback(callback1);
85 data.addUnavailableCallback(callback2);
87 data.lockAvailable(lock);
88 verify(callback1, never()).accept(any());
89 verify(callback2, never()).accept(any());
91 data.lockUnavailable(lock);
92 verify(callback1).accept(any());
93 verify(callback2).accept(any());
97 * Tests addUnavailableCallback() when the lock never becomes available.
100 public void testAddUnavailableCallbackNeverAvailable() {
101 data.addUnavailableCallback(callback1);
102 data.addUnavailableCallback(callback2);
104 data.lockUnavailable(lock);
105 verify(callback1).accept(any());
106 verify(callback2).accept(any());
108 data.addUnavailableCallback(callback3);
109 verify(callback3).accept(any());
113 public void testFree() {
115 assertThatCode(() -> data.free()).doesNotThrowAnyException();
118 data.lockAvailable(lock);
124 public void testLockAvailable() throws Exception {
125 data.addUnavailableCallback(callback1);
126 data.addUnavailableCallback(callback2);
128 CompletableFuture<OperationOutcome> future = data.getFuture();
129 data.lockAvailable(lock);
131 assertSame(future, data.getFuture());
133 assertTrue(future.isDone());
134 OperationOutcome outcome = future.get();
135 assertEquals(ControlLoopOperationManager2.LOCK_ACTOR, outcome.getActor());
136 assertEquals(ControlLoopOperationManager2.LOCK_OPERATION, outcome.getOperation());
137 assertEquals(ENTITY, outcome.getTarget());
138 assertEquals(OperationResult.SUCCESS, outcome.getResult());
139 assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
141 Instant start = outcome.getStart();
142 assertNotNull(start);
144 Instant end = outcome.getEnd();
146 assertTrue(start.compareTo(end) <= 0);
148 verify(callback1, never()).accept(any());
149 verify(callback2, never()).accept(any());
153 public void testLockUnavailable() throws Exception {
154 data.addUnavailableCallback(callback1);
155 data.addUnavailableCallback(callback2);
156 data.addUnavailableCallback(callback3);
158 // arrange for callback2 to throw an exception
159 doThrow(new IllegalStateException("expected exception")).when(callback2).accept(any());
161 CompletableFuture<OperationOutcome> future = data.getFuture();
162 assertNotNull(future);
163 data.lockUnavailable(lock);
165 CompletableFuture<OperationOutcome> future2 = data.getFuture();
166 assertNotNull(future2);
168 assertNotSame(future, future2);
170 assertTrue(future.isDone());
171 OperationOutcome outcome = future.get();
173 assertTrue(future2.isDone());
174 assertSame(outcome, future2.get());
176 assertEquals(ControlLoopOperationManager2.LOCK_ACTOR, outcome.getActor());
177 assertEquals(ControlLoopOperationManager2.LOCK_OPERATION, outcome.getOperation());
178 assertEquals(ENTITY, outcome.getTarget());
179 assertEquals(OperationResult.FAILURE, outcome.getResult());
180 assertEquals(ControlLoopOperation.FAILED_MSG, outcome.getMessage());
182 Instant start = outcome.getStart();
183 assertNotNull(start);
185 Instant end = outcome.getEnd();
187 assertTrue(start.compareTo(end) <= 0);
189 verify(callback1).accept(eq(outcome));
190 verify(callback2).accept(eq(outcome));
191 verify(callback3).accept(eq(outcome));