7b0213107d747dd97f50d4a15182d28518f4af57
[policy/drools-applications.git] / controlloop / common / eventmanager / src / test / java / org / onap / policy / controlloop / eventmanager / LockDataTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.controlloop.eventmanager;
22
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.Mockito.doThrow;
32 import static org.mockito.Mockito.never;
33 import static org.mockito.Mockito.verify;
34
35 import java.time.Instant;
36 import java.util.UUID;
37 import java.util.concurrent.CompletableFuture;
38 import java.util.function.Consumer;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnitRunner;
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;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class LockDataTest {
51
52     private static final String ENTITY = "my-entity";
53     private static final UUID REQ_ID = UUID.randomUUID();
54
55     @Mock
56     private Lock lock;
57     @Mock
58     private Consumer<OperationOutcome> callback1;
59     @Mock
60     private Consumer<OperationOutcome> callback2;
61     @Mock
62     private Consumer<OperationOutcome> callback3;
63
64     private LockData data;
65
66     /**
67      * Sets up.
68      */
69     @Before
70     public void setUp() {
71         data = new LockData(ENTITY, REQ_ID);
72     }
73
74     @Test
75     public void testGetFuture() {
76         CompletableFuture<OperationOutcome> future = data.getFuture();
77         assertNotNull(future);
78         assertFalse(future.isDone());
79     }
80
81     @Test
82     public void testAddUnavailableCallback() {
83         data.addUnavailableCallback(callback1);
84         data.addUnavailableCallback(callback2);
85
86         data.lockAvailable(lock);
87         verify(callback1, never()).accept(any());
88         verify(callback2, never()).accept(any());
89
90         data.lockUnavailable(lock);
91         verify(callback1).accept(any());
92         verify(callback2).accept(any());
93     }
94
95     /**
96      * Tests addUnavailableCallback() when the lock never becomes available.
97      */
98     @Test
99     public void testAddUnavailableCallbackNeverAvailable() {
100         data.addUnavailableCallback(callback1);
101         data.addUnavailableCallback(callback2);
102
103         data.lockUnavailable(lock);
104         verify(callback1).accept(any());
105         verify(callback2).accept(any());
106
107         data.addUnavailableCallback(callback3);
108         verify(callback3).accept(any());
109     }
110
111     @Test
112     public void testFree() {
113         // no lock yet
114         assertThatCode(() -> data.free()).doesNotThrowAnyException();
115
116         // no with a lock
117         data.lockAvailable(lock);
118         data.free();
119         verify(lock).free();
120     }
121
122     @Test
123     public void testLockAvailable() throws Exception {
124         data.addUnavailableCallback(callback1);
125         data.addUnavailableCallback(callback2);
126
127         CompletableFuture<OperationOutcome> future = data.getFuture();
128         data.lockAvailable(lock);
129
130         assertSame(future, data.getFuture());
131
132         assertTrue(future.isDone());
133         OperationOutcome outcome = future.get();
134         assertEquals(ActorConstants.LOCK_ACTOR, outcome.getActor());
135         assertEquals(ActorConstants.LOCK_OPERATION, outcome.getOperation());
136         assertEquals(ENTITY, outcome.getTarget());
137         assertEquals(OperationResult.SUCCESS, outcome.getResult());
138         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
139
140         Instant start = outcome.getStart();
141         assertNotNull(start);
142
143         Instant end = outcome.getEnd();
144         assertNotNull(end);
145         assertTrue(start.compareTo(end) <= 0);
146
147         verify(callback1, never()).accept(any());
148         verify(callback2, never()).accept(any());
149     }
150
151     @Test
152     public void testLockUnavailable() throws Exception {
153         data.addUnavailableCallback(callback1);
154         data.addUnavailableCallback(callback2);
155         data.addUnavailableCallback(callback3);
156
157         // arrange for callback2 to throw an exception
158         doThrow(new IllegalStateException("expected exception")).when(callback2).accept(any());
159
160         CompletableFuture<OperationOutcome> future = data.getFuture();
161         assertNotNull(future);
162         data.lockUnavailable(lock);
163
164         CompletableFuture<OperationOutcome> future2 = data.getFuture();
165         assertNotNull(future2);
166
167         assertNotSame(future, future2);
168
169         assertTrue(future.isDone());
170         OperationOutcome outcome = future.get();
171
172         assertTrue(future2.isDone());
173         assertSame(outcome, future2.get());
174
175         assertEquals(ActorConstants.LOCK_ACTOR, outcome.getActor());
176         assertEquals(ActorConstants.LOCK_OPERATION, outcome.getOperation());
177         assertEquals(ENTITY, outcome.getTarget());
178         assertEquals(OperationResult.FAILURE, outcome.getResult());
179         assertEquals(ControlLoopOperation.FAILED_MSG, outcome.getMessage());
180
181         Instant start = outcome.getStart();
182         assertNotNull(start);
183
184         Instant end = outcome.getEnd();
185         assertNotNull(end);
186         assertTrue(start.compareTo(end) <= 0);
187
188         verify(callback1).accept(outcome);
189         verify(callback2).accept(outcome);
190         verify(callback3).accept(outcome);
191     }
192 }