b6bd50c7eb85e697f18a21e9917550b6fe91f098
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / parameters / ControlLoopOperationParamsTest.java
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.actorserviceprovider.parameters;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Mockito.doAnswer;
33 import static org.mockito.Mockito.never;
34 import static org.mockito.Mockito.times;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
37
38 import java.util.Map;
39 import java.util.TreeMap;
40 import java.util.UUID;
41 import java.util.concurrent.CompletableFuture;
42 import java.util.concurrent.Executor;
43 import java.util.concurrent.ForkJoinPool;
44 import java.util.concurrent.atomic.AtomicInteger;
45 import java.util.function.Consumer;
46 import java.util.function.Function;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 import org.onap.policy.common.parameters.BeanValidationResult;
52 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
53 import org.onap.policy.controlloop.actorserviceprovider.Operation;
54 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
55 import org.onap.policy.controlloop.actorserviceprovider.Operator;
56 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams.ControlLoopOperationParamsBuilder;
57 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
58
59 public class ControlLoopOperationParamsTest {
60     private static final String NULL_MSG = "null";
61     private static final String EXPECTED_EXCEPTION = "expected exception";
62     private static final String ACTOR = "my-actor";
63     private static final String OPERATION = "my-operation";
64     private static final String TARGET_ENTITY = "my-target";
65     private static final Integer RETRY = 3;
66     private static final Integer TIMEOUT = 100;
67     private static final UUID REQ_ID = UUID.randomUUID();
68
69     @Mock
70     private Actor actor;
71
72     @Mock
73     private ActorService actorService;
74
75     @Mock
76     private Consumer<OperationOutcome> completer;
77
78     @Mock
79     private Executor executor;
80
81     @Mock
82     private CompletableFuture<OperationOutcome> operFuture;
83
84     @Mock
85     private Operator operator;
86
87     @Mock
88     private Operation operation;
89
90     @Mock
91     private Consumer<OperationOutcome> starter;
92
93     private Map<String, Object> payload;
94
95     private ControlLoopOperationParams params;
96     private OperationOutcome outcome;
97
98
99     /**
100      * Initializes mocks and sets {@link #params} to a fully-loaded set of parameters.
101      */
102     @Before
103     public void setUp() {
104         MockitoAnnotations.initMocks(this);
105
106         when(actorService.getActor(ACTOR)).thenReturn(actor);
107         when(actor.getOperator(OPERATION)).thenReturn(operator);
108         when(operator.buildOperation(any())).thenReturn(operation);
109         when(operation.start()).thenReturn(operFuture);
110
111         payload = new TreeMap<>();
112
113         params = ControlLoopOperationParams.builder().actorService(actorService).completeCallback(completer)
114                         .requestId(REQ_ID).executor(executor).actor(ACTOR).operation(OPERATION).payload(payload)
115                         .retry(RETRY).targetEntity(TARGET_ENTITY).timeoutSec(TIMEOUT)
116                         .startCallback(starter).preprocessed(true).build();
117
118         outcome = params.makeOutcome(TARGET_ENTITY);
119     }
120
121     @Test
122     public void testStart() {
123         assertThatIllegalArgumentException().isThrownBy(() -> params.toBuilder().requestId(null).build().start());
124
125         assertSame(operFuture, params.start());
126     }
127
128     @Test
129     public void testBuild() {
130         assertThatIllegalArgumentException().isThrownBy(() -> params.toBuilder().requestId(null).build().build());
131
132         assertSame(operation, params.build());
133     }
134
135     @Test
136     public void testGetRequestId() {
137         assertSame(REQ_ID, params.getRequestId());
138     }
139
140     @Test
141     public void testMakeOutcome() {
142         assertEquals(ACTOR, outcome.getActor());
143         assertEquals(OPERATION, outcome.getOperation());
144         checkRemainingFields("with actor");
145     }
146
147     protected void checkRemainingFields(String testName) {
148         assertEquals(testName, TARGET_ENTITY, outcome.getTarget());
149         assertNull(testName, outcome.getStart());
150         assertNull(testName, outcome.getEnd());
151         assertNull(testName, outcome.getSubRequestId());
152         assertNotNull(testName, outcome.getResult());
153         assertNull(testName, outcome.getMessage());
154     }
155
156     @Test
157     public void testCallbackStarted() {
158         params.callbackStarted(outcome);
159         verify(starter).accept(outcome);
160
161         // modify starter to throw an exception
162         AtomicInteger count = new AtomicInteger();
163         doAnswer(args -> {
164             count.incrementAndGet();
165             throw new IllegalStateException(EXPECTED_EXCEPTION);
166         }).when(starter).accept(outcome);
167
168         params.callbackStarted(outcome);
169         verify(starter, times(2)).accept(outcome);
170         assertEquals(1, count.get());
171
172         // repeat with no start-callback - no additional calls expected
173         params.toBuilder().startCallback(null).build().callbackStarted(outcome);
174         verify(starter, times(2)).accept(outcome);
175         assertEquals(1, count.get());
176
177         // should not call complete-callback
178         verify(completer, never()).accept(any());
179     }
180
181     @Test
182     public void testCallbackCompleted() {
183         params.callbackCompleted(outcome);
184         verify(completer).accept(outcome);
185
186         // modify completer to throw an exception
187         AtomicInteger count = new AtomicInteger();
188         doAnswer(args -> {
189             count.incrementAndGet();
190             throw new IllegalStateException(EXPECTED_EXCEPTION);
191         }).when(completer).accept(outcome);
192
193         params.callbackCompleted(outcome);
194         verify(completer, times(2)).accept(outcome);
195         assertEquals(1, count.get());
196
197         // repeat with no complete-callback - no additional calls expected
198         params.toBuilder().completeCallback(null).build().callbackCompleted(outcome);
199         verify(completer, times(2)).accept(outcome);
200         assertEquals(1, count.get());
201
202         // should not call start-callback
203         verify(starter, never()).accept(any());
204     }
205
206     @Test
207     public void testValidateFields() {
208         testValidate("actor", NULL_MSG, bldr -> bldr.actor(null));
209         testValidate("actorService", NULL_MSG, bldr -> bldr.actorService(null));
210         testValidate("executor", NULL_MSG, bldr -> bldr.executor(null));
211         testValidate("operation", NULL_MSG, bldr -> bldr.operation(null));
212         testValidate("requestId", NULL_MSG, bldr -> bldr.requestId(null));
213
214         // has no target entity
215         BeanValidationResult result = params.toBuilder().targetEntity(null).build().validate();
216         assertTrue(result.isValid());
217
218         // check edge cases
219         assertTrue(params.toBuilder().build().validate().isValid());
220
221         // these can be null
222         assertTrue(params.toBuilder().payload(null).retry(null).timeoutSec(null).startCallback(null)
223                         .completeCallback(null).build().validate().isValid());
224
225         // test with minimal fields
226         assertTrue(ControlLoopOperationParams.builder().actorService(actorService).requestId(REQ_ID).actor(ACTOR)
227                         .operation(OPERATION).targetEntity(TARGET_ENTITY).build().validate().isValid());
228     }
229
230     private void testValidate(String fieldName, String expected,
231                     Function<ControlLoopOperationParamsBuilder, ControlLoopOperationParamsBuilder> makeInvalid) {
232
233         // original params should be valid
234         BeanValidationResult result = params.validate();
235         assertTrue(fieldName, result.isValid());
236
237         // make invalid params
238         result = makeInvalid.apply(params.toBuilder()).build().validate();
239         assertFalse(fieldName, result.isValid());
240         assertThat(result.getResult()).contains(fieldName).contains(expected);
241     }
242
243     @Test
244     public void testBuilder_testToBuilder() {
245         assertEquals(params, params.toBuilder().build());
246     }
247
248     @Test
249     public void testGetActor() {
250         assertSame(ACTOR, params.getActor());
251     }
252
253     @Test
254     public void testGetActorService() {
255         assertSame(actorService, params.getActorService());
256     }
257
258     @Test
259     public void testGetExecutor() {
260         assertSame(executor, params.getExecutor());
261
262         // should use default when unspecified
263         assertSame(ForkJoinPool.commonPool(), ControlLoopOperationParams.builder().build().getExecutor());
264     }
265
266     @Test
267     public void testGetOperation() {
268         assertSame(OPERATION, params.getOperation());
269     }
270
271     @Test
272     public void testGetPayload() {
273         assertSame(payload, params.getPayload());
274
275         // should be null when unspecified
276         assertNull(ControlLoopOperationParams.builder().build().getPayload());
277     }
278
279     @Test
280     public void test() {
281         assertTrue(params.isPreprocessed());
282
283         // should be false when unspecified
284         assertFalse(ControlLoopOperationParams.builder().build().isPreprocessed());
285     }
286
287     @Test
288     public void testGetRetry() {
289         assertSame(RETRY, params.getRetry());
290
291         // should be null when unspecified
292         assertNull(ControlLoopOperationParams.builder().build().getRetry());
293     }
294
295     @Test
296     public void testGetTimeoutSec() {
297         assertSame(TIMEOUT, params.getTimeoutSec());
298
299         // should be 300 when unspecified
300         assertEquals(Integer.valueOf(300), ControlLoopOperationParams.builder().build().getTimeoutSec());
301
302         // null should be ok too
303         assertNull(ControlLoopOperationParams.builder().timeoutSec(null).build().getTimeoutSec());
304     }
305
306     @Test
307     public void testGetStartCallback() {
308         assertSame(starter, params.getStartCallback());
309     }
310
311     @Test
312     public void testGetCompleteCallback() {
313         assertSame(completer, params.getCompleteCallback());
314     }
315
316     @Test
317     public void testGetTargetEntity() {
318         assertEquals(TARGET_ENTITY, params.getTargetEntity());
319     }
320 }