fc36c12ba44f1ffd0e6242cd14e8f16b5e5bc5d9
[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).timeoutSec(TIMEOUT)
116                         .startCallback(starter).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         outcome = params.makeOutcome();
143         assertNull(outcome.getTarget());
144         checkRemainingFields("with actor");
145     }
146
147     @Test
148     public void testMakeOutcomeString() {
149         assertEquals(TARGET_ENTITY, outcome.getTarget());
150         checkRemainingFields("with actor");
151     }
152
153     protected void checkRemainingFields(String testName) {
154         assertEquals(ACTOR, outcome.getActor());
155         assertEquals(OPERATION, outcome.getOperation());
156         assertNull(testName, outcome.getStart());
157         assertNull(testName, outcome.getEnd());
158         assertNull(testName, outcome.getSubRequestId());
159         assertNotNull(testName, outcome.getResult());
160         assertNull(testName, outcome.getMessage());
161     }
162
163     @Test
164     public void testCallbackStarted() {
165         params.callbackStarted(outcome);
166         verify(starter).accept(outcome);
167
168         // modify starter to throw an exception
169         AtomicInteger count = new AtomicInteger();
170         doAnswer(args -> {
171             count.incrementAndGet();
172             throw new IllegalStateException(EXPECTED_EXCEPTION);
173         }).when(starter).accept(outcome);
174
175         params.callbackStarted(outcome);
176         verify(starter, times(2)).accept(outcome);
177         assertEquals(1, count.get());
178
179         // repeat with no start-callback - no additional calls expected
180         params.toBuilder().startCallback(null).build().callbackStarted(outcome);
181         verify(starter, times(2)).accept(outcome);
182         assertEquals(1, count.get());
183
184         // should not call complete-callback
185         verify(completer, never()).accept(any());
186     }
187
188     @Test
189     public void testCallbackCompleted() {
190         params.callbackCompleted(outcome);
191         verify(completer).accept(outcome);
192
193         // modify completer to throw an exception
194         AtomicInteger count = new AtomicInteger();
195         doAnswer(args -> {
196             count.incrementAndGet();
197             throw new IllegalStateException(EXPECTED_EXCEPTION);
198         }).when(completer).accept(outcome);
199
200         params.callbackCompleted(outcome);
201         verify(completer, times(2)).accept(outcome);
202         assertEquals(1, count.get());
203
204         // repeat with no complete-callback - no additional calls expected
205         params.toBuilder().completeCallback(null).build().callbackCompleted(outcome);
206         verify(completer, times(2)).accept(outcome);
207         assertEquals(1, count.get());
208
209         // should not call start-callback
210         verify(starter, never()).accept(any());
211     }
212
213     @Test
214     public void testValidateFields() {
215         testValidate("actor", NULL_MSG, bldr -> bldr.actor(null));
216         testValidate("actorService", NULL_MSG, bldr -> bldr.actorService(null));
217         testValidate("executor", NULL_MSG, bldr -> bldr.executor(null));
218         testValidate("operation", NULL_MSG, bldr -> bldr.operation(null));
219         testValidate("requestId", NULL_MSG, bldr -> bldr.requestId(null));
220
221         // has no target entity
222         BeanValidationResult result = params.toBuilder().build().validate();
223         assertTrue(result.isValid());
224
225         // check edge cases
226         assertTrue(params.toBuilder().build().validate().isValid());
227
228         // these can be null
229         assertTrue(params.toBuilder().payload(null).retry(null).timeoutSec(null).startCallback(null)
230                         .completeCallback(null).build().validate().isValid());
231
232         // test with minimal fields
233         assertTrue(ControlLoopOperationParams.builder().actorService(actorService).requestId(REQ_ID).actor(ACTOR)
234                         .operation(OPERATION).build().validate().isValid());
235     }
236
237     private void testValidate(String fieldName, String expected,
238                     Function<ControlLoopOperationParamsBuilder, ControlLoopOperationParamsBuilder> makeInvalid) {
239
240         // original params should be valid
241         BeanValidationResult result = params.validate();
242         assertTrue(fieldName, result.isValid());
243
244         // make invalid params
245         result = makeInvalid.apply(params.toBuilder()).build().validate();
246         assertFalse(fieldName, result.isValid());
247         assertThat(result.getResult()).contains(fieldName).contains(expected);
248     }
249
250     @Test
251     public void testBuilder_testToBuilder() {
252         assertEquals(params, params.toBuilder().build());
253     }
254
255     @Test
256     public void testGetActor() {
257         assertSame(ACTOR, params.getActor());
258     }
259
260     @Test
261     public void testGetActorService() {
262         assertSame(actorService, params.getActorService());
263     }
264
265     @Test
266     public void testGetExecutor() {
267         assertSame(executor, params.getExecutor());
268
269         // should use default when unspecified
270         assertSame(ForkJoinPool.commonPool(), ControlLoopOperationParams.builder().build().getExecutor());
271     }
272
273     @Test
274     public void testGetOperation() {
275         assertSame(OPERATION, params.getOperation());
276     }
277
278     @Test
279     public void testGetPayload() {
280         assertSame(payload, params.getPayload());
281
282         // should be null when unspecified
283         assertNull(ControlLoopOperationParams.builder().build().getPayload());
284     }
285
286     @Test
287     public void testGetRetry() {
288         assertSame(RETRY, params.getRetry());
289
290         // should be null when unspecified
291         assertNull(ControlLoopOperationParams.builder().build().getRetry());
292     }
293
294     @Test
295     public void testGetTimeoutSec() {
296         assertSame(TIMEOUT, params.getTimeoutSec());
297
298         // should be 300 when unspecified
299         assertEquals(Integer.valueOf(300), ControlLoopOperationParams.builder().build().getTimeoutSec());
300
301         // null should be ok too
302         assertNull(ControlLoopOperationParams.builder().timeoutSec(null).build().getTimeoutSec());
303     }
304
305     @Test
306     public void testGetStartCallback() {
307         assertSame(starter, params.getStartCallback());
308     }
309
310     @Test
311     public void testGetCompleteCallback() {
312         assertSame(completer, params.getCompleteCallback());
313     }
314 }