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