2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 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.controlloop.eventmanager;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNotSame;
29 import static org.junit.jupiter.api.Assertions.assertSame;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Mockito.doThrow;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.never;
35 import static org.mockito.Mockito.verify;
37 import java.util.UUID;
38 import java.util.function.Consumer;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.onap.policy.controlloop.ControlLoopOperation;
42 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
43 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
44 import org.onap.policy.drools.core.lock.Lock;
48 private static final String ENTITY = "my-entity";
49 private static final UUID REQ_ID = UUID.randomUUID();
51 private final Lock lock = mock(Lock.class);
52 private final Consumer<OperationOutcome> callback1 = mock();
53 private final Consumer<OperationOutcome> callback2 = mock();
54 private final Consumer<OperationOutcome> callback3 = mock();
56 private LockData data;
63 data = new LockData(ENTITY, REQ_ID);
67 void testGetFuture() {
68 var future = data.getFuture();
69 assertNotNull(future);
70 assertFalse(future.isDone());
74 void testAddUnavailableCallback() {
75 data.addUnavailableCallback(callback1);
76 data.addUnavailableCallback(callback2);
78 data.lockAvailable(lock);
79 verify(callback1, never()).accept(any());
80 verify(callback2, never()).accept(any());
82 data.lockUnavailable(lock);
83 verify(callback1).accept(any());
84 verify(callback2).accept(any());
88 * Tests addUnavailableCallback() when the lock never becomes available.
91 void testAddUnavailableCallbackNeverAvailable() {
92 data.addUnavailableCallback(callback1);
93 data.addUnavailableCallback(callback2);
95 data.lockUnavailable(lock);
96 verify(callback1).accept(any());
97 verify(callback2).accept(any());
99 data.addUnavailableCallback(callback3);
100 verify(callback3).accept(any());
106 assertThatCode(() -> data.free()).doesNotThrowAnyException();
109 data.lockAvailable(lock);
115 void testLockAvailable() throws Exception {
116 data.addUnavailableCallback(callback1);
117 data.addUnavailableCallback(callback2);
119 var future = data.getFuture();
120 data.lockAvailable(lock);
122 assertSame(future, data.getFuture());
124 assertTrue(future.isDone());
125 var outcome = future.get();
126 assertEquals(ActorConstants.LOCK_ACTOR, outcome.getActor());
127 assertEquals(ActorConstants.LOCK_OPERATION, outcome.getOperation());
128 assertEquals(ENTITY, outcome.getTarget());
129 assertEquals(OperationResult.SUCCESS, outcome.getResult());
130 assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
132 var start = outcome.getStart();
133 assertNotNull(start);
135 var end = outcome.getEnd();
137 assertTrue(start.compareTo(end) <= 0);
139 verify(callback1, never()).accept(any());
140 verify(callback2, never()).accept(any());
144 void testLockUnavailable() throws Exception {
145 data.addUnavailableCallback(callback1);
146 data.addUnavailableCallback(callback2);
147 data.addUnavailableCallback(callback3);
149 // arrange for callback2 to throw an exception
150 doThrow(new IllegalStateException("expected exception")).when(callback2).accept(any());
152 var future = data.getFuture();
153 assertNotNull(future);
154 data.lockUnavailable(lock);
156 var future2 = data.getFuture();
157 assertNotNull(future2);
159 assertNotSame(future, future2);
161 assertTrue(future.isDone());
162 var outcome = future.get();
164 assertTrue(future2.isDone());
165 assertSame(outcome, future2.get());
167 assertEquals(ActorConstants.LOCK_ACTOR, outcome.getActor());
168 assertEquals(ActorConstants.LOCK_OPERATION, outcome.getOperation());
169 assertEquals(ENTITY, outcome.getTarget());
170 assertEquals(OperationResult.FAILURE, outcome.getResult());
171 assertEquals(ControlLoopOperation.FAILED_MSG, outcome.getMessage());
173 var start = outcome.getStart();
174 assertNotNull(start);
176 var end = outcome.getEnd();
178 assertTrue(start.compareTo(end) <= 0);
180 verify(callback1).accept(outcome);
181 verify(callback2).accept(outcome);
182 verify(callback3).accept(outcome);