Upgrade Java 17 in policy-drools-apps
[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  * 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.eventmanager;
23
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;
36
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;
45
46 class LockDataTest {
47
48     private static final String ENTITY = "my-entity";
49     private static final UUID REQ_ID = UUID.randomUUID();
50
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();
55
56     private LockData data;
57
58     /**
59      * Sets up.
60      */
61     @BeforeEach
62     public void setUp() {
63         data = new LockData(ENTITY, REQ_ID);
64     }
65
66     @Test
67     void testGetFuture() {
68         var future = data.getFuture();
69         assertNotNull(future);
70         assertFalse(future.isDone());
71     }
72
73     @Test
74     void testAddUnavailableCallback() {
75         data.addUnavailableCallback(callback1);
76         data.addUnavailableCallback(callback2);
77
78         data.lockAvailable(lock);
79         verify(callback1, never()).accept(any());
80         verify(callback2, never()).accept(any());
81
82         data.lockUnavailable(lock);
83         verify(callback1).accept(any());
84         verify(callback2).accept(any());
85     }
86
87     /**
88      * Tests addUnavailableCallback() when the lock never becomes available.
89      */
90     @Test
91     void testAddUnavailableCallbackNeverAvailable() {
92         data.addUnavailableCallback(callback1);
93         data.addUnavailableCallback(callback2);
94
95         data.lockUnavailable(lock);
96         verify(callback1).accept(any());
97         verify(callback2).accept(any());
98
99         data.addUnavailableCallback(callback3);
100         verify(callback3).accept(any());
101     }
102
103     @Test
104     void testFree() {
105         // no lock yet
106         assertThatCode(() -> data.free()).doesNotThrowAnyException();
107
108         // no with a lock
109         data.lockAvailable(lock);
110         data.free();
111         verify(lock).free();
112     }
113
114     @Test
115     void testLockAvailable() throws Exception {
116         data.addUnavailableCallback(callback1);
117         data.addUnavailableCallback(callback2);
118
119         var future = data.getFuture();
120         data.lockAvailable(lock);
121
122         assertSame(future, data.getFuture());
123
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());
131
132         var start = outcome.getStart();
133         assertNotNull(start);
134
135         var end = outcome.getEnd();
136         assertNotNull(end);
137         assertTrue(start.compareTo(end) <= 0);
138
139         verify(callback1, never()).accept(any());
140         verify(callback2, never()).accept(any());
141     }
142
143     @Test
144     void testLockUnavailable() throws Exception {
145         data.addUnavailableCallback(callback1);
146         data.addUnavailableCallback(callback2);
147         data.addUnavailableCallback(callback3);
148
149         // arrange for callback2 to throw an exception
150         doThrow(new IllegalStateException("expected exception")).when(callback2).accept(any());
151
152         var future = data.getFuture();
153         assertNotNull(future);
154         data.lockUnavailable(lock);
155
156         var future2 = data.getFuture();
157         assertNotNull(future2);
158
159         assertNotSame(future, future2);
160
161         assertTrue(future.isDone());
162         var outcome = future.get();
163
164         assertTrue(future2.isDone());
165         assertSame(outcome, future2.get());
166
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());
172
173         var start = outcome.getStart();
174         assertNotNull(start);
175
176         var end = outcome.getEnd();
177         assertNotNull(end);
178         assertTrue(start.compareTo(end) <= 0);
179
180         verify(callback1).accept(outcome);
181         verify(callback2).accept(outcome);
182         verify(callback3).accept(outcome);
183     }
184 }