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