Set sub request ID before start callback
[policy/models.git] / models-interactions / model-actors / actor.appclcm / src / test / java / org / onap / policy / controlloop / actor / appclcm / AppcLcmOperationTest.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.actor.appclcm;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
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.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31
32 import java.util.Arrays;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.concurrent.CompletableFuture;
36 import java.util.concurrent.atomic.AtomicBoolean;
37 import java.util.stream.Collectors;
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.onap.policy.appclcm.AppcLcmBody;
44 import org.onap.policy.appclcm.AppcLcmCommonHeader;
45 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
46 import org.onap.policy.appclcm.AppcLcmOutput;
47 import org.onap.policy.appclcm.AppcLcmResponseStatus;
48 import org.onap.policy.common.endpoints.event.comm.TopicSink;
49 import org.onap.policy.common.endpoints.event.comm.TopicSource;
50 import org.onap.policy.common.utils.coder.Coder;
51 import org.onap.policy.common.utils.coder.CoderException;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.controlloop.ControlLoopOperation;
54 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
55 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
56 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
57 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
58 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
59 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
60 import org.onap.policy.controlloop.policy.PolicyResult;
61 import org.onap.policy.controlloop.policy.Target;
62 import org.onap.policy.simulators.AppcLcmTopicServer;
63 import org.onap.policy.simulators.TopicServer;
64
65 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
66
67     private static final String EXPECTED_EXCEPTION = "expected exception";
68     private static final String PAYLOAD_KEY1 = "key-A";
69     private static final String PAYLOAD_VALUE1 = "value-A";
70     private static final String MY_MESSAGE = "my-message";
71     protected static final String MY_VNF = "my-vnf";
72     protected static final String RESOURCE_ID = "my-resource";
73     private static final int SUCCESS_CODE = 400;
74
75     private AppcLcmDmaapWrapper response;
76     private AppcLcmOperation oper;
77
78     @BeforeClass
79     public static void setUpBeforeClass() throws Exception {
80         initBeforeClass(MY_SINK, MY_SOURCE);
81     }
82
83     @AfterClass
84     public static void tearDownAfterClass() {
85         destroyAfterClass();
86     }
87
88     /**
89      * Sets up.
90      */
91     @Before
92     public void setUp() {
93         super.setUpBasic();
94
95         response = makeResponse();
96
97         oper = new AppcLcmOperation(params, config);
98     }
99
100     @After
101     public void tearDown() {
102         super.tearDownBasic();
103     }
104
105     @SuppressWarnings("rawtypes")
106     protected TopicServer makeServer(TopicSink sink, TopicSource source) {
107         return new AppcLcmTopicServer(sink, source);
108     }
109
110     /**
111      * Tests "success" case with simulator.
112      */
113     @Test
114     public void testSuccess() throws Exception {
115         BidirectionalTopicParams opParams =
116                         BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build();
117         config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcLcmOperation.SELECTOR_KEYS);
118
119         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
120
121         oper = new AppcLcmOperation(params, config) {
122             @Override
123             protected CompletableFuture<OperationOutcome> startGuardAsync() {
124                 return null;
125             }
126         };
127
128         outcome = oper.start().get();
129         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
130     }
131
132     @Test
133     public void testConstructor() {
134         assertEquals(DEFAULT_ACTOR, oper.getActorName());
135         assertEquals(DEFAULT_OPERATION, oper.getName());
136
137         // missing target entity
138         params = params.toBuilder().targetEntity("").build();
139         assertThatIllegalArgumentException().isThrownBy(() -> new AppcLcmOperation(params, config))
140                         .withMessage("missing targetEntity");
141     }
142
143     @Test
144     public void testStartPreprocessorAsync() throws Exception {
145         context = mock(ControlLoopEventContext.class);
146         when(context.getEvent()).thenReturn(event);
147         params = params.toBuilder().context(context).build();
148
149         AtomicBoolean guardStarted = new AtomicBoolean();
150
151         oper = new AppcLcmOperation(params, config) {
152             @Override
153             protected CompletableFuture<OperationOutcome> startGuardAsync() {
154                 guardStarted.set(true);
155                 return super.startGuardAsync();
156             }
157         };
158
159         CompletableFuture<OperationOutcome> future2 = oper.startPreprocessorAsync();
160         assertNotNull(future2);
161         assertFalse(future.isDone());
162         assertTrue(guardStarted.get());
163
164         assertTrue(executor.runAll(100));
165         assertTrue(future2.isDone());
166         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
167     }
168
169     @Test
170     public void testMakeRequest() {
171         oper.generateSubRequestId(2);
172         String subreq = oper.getSubRequestId();
173         assertNotNull(subreq);
174
175         AppcLcmDmaapWrapper request = oper.makeRequest(2);
176         assertEquals("DefaultOperation", request.getBody().getInput().getAction());
177
178         AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
179         assertNotNull(header);
180         assertEquals(params.getRequestId(), header.getRequestId());
181
182         assertEquals(subreq, header.getSubRequestId());
183
184         assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
185
186         request = oper.makeRequest(2);
187         assertEquals(subreq, request.getBody().getInput().getCommonHeader().getSubRequestId());
188     }
189
190     @Test
191     public void testConvertPayload() {
192         // only builds a payload for ConfigModify
193         params = params.toBuilder().operation(AppcLcmConstants.OPERATION_CONFIG_MODIFY).build();
194         oper = new AppcLcmOperation(params, config);
195
196         oper.generateSubRequestId(2);
197         AppcLcmDmaapWrapper req = oper.makeRequest(2);
198         assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
199
200         // coder exception
201         oper = new AppcLcmOperation(params, config) {
202             @Override
203             protected Coder makeCoder() {
204                 return new StandardCoder() {
205                     @Override
206                     public String encode(Object object) throws CoderException {
207                         throw new CoderException(EXPECTED_EXCEPTION);
208                     }
209                 };
210             }
211         };
212
213         oper.generateSubRequestId(2);
214
215         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
216                         .withMessage("Cannot convert payload");
217     }
218
219     @Test
220     public void testGetExpectedKeyValues() {
221         oper.generateSubRequestId(2);
222         AppcLcmDmaapWrapper request = oper.makeRequest(2);
223         assertEquals(Arrays.asList(request.getBody().getInput().getCommonHeader().getSubRequestId()),
224                         oper.getExpectedKeyValues(50, request));
225     }
226
227     @Test
228     public void testDetmStatus() {
229         assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
230
231         // failure
232         response.getBody().getOutput().getStatus().setCode(405);
233         assertEquals(Status.FAILURE, oper.detmStatus(null, response));
234
235         // error
236         response.getBody().getOutput().getStatus().setCode(200);
237         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
238
239         // reject
240         response.getBody().getOutput().getStatus().setCode(305);
241         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
242
243         // accepted
244         response.getBody().getOutput().getStatus().setCode(100);
245         assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
246
247         // other
248         response.getBody().getOutput().getStatus().setCode(-1);
249         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
250
251         // null status
252         response.getBody().getOutput().setStatus(null);
253         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
254     }
255
256     @Test
257     public void testSetOutcome() {
258         oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
259         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
260         assertEquals(MY_MESSAGE, outcome.getMessage());
261
262         // failure
263         oper.setOutcome(outcome, PolicyResult.FAILURE, response);
264         assertEquals(PolicyResult.FAILURE, outcome.getResult());
265         assertEquals(MY_MESSAGE, outcome.getMessage());
266
267         // null message
268         response.getBody().getOutput().getStatus().setMessage(null);
269         oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
270         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
271
272         // null status
273         response.getBody().getOutput().setStatus(null);
274         oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
275         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
276     }
277
278     @Test
279     public void testGetStatus() {
280         assertNotNull(oper.getStatus(response));
281
282         // null status
283         response.getBody().getOutput().setStatus(null);
284         assertNull(oper.getStatus(response));
285
286         // null outcome
287         response.getBody().setOutput(null);
288         assertNull(oper.getStatus(response));
289
290         // null body
291         response.setBody(null);
292         assertNull(oper.getStatus(response));
293
294         // null response
295         assertNull(oper.getStatus(null));
296     }
297
298     @Test
299     public void testOperationSupportsPayload() {
300         // these should support a payload
301         Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
302
303         for (String name : supported) {
304             params = params.toBuilder().operation(name).build();
305             oper = new AppcLcmOperation(params, config);
306             assertTrue(name, oper.operationSupportsPayload());
307         }
308
309         // these should NOT support a payload
310         Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
311                         .collect(Collectors.toSet());
312
313         for (String name : unsupported) {
314             params = params.toBuilder().operation(name).build();
315             oper = new AppcLcmOperation(params, config);
316             assertFalse(name, oper.operationSupportsPayload());
317         }
318
319         // pick an operation that would ordinarily support payloads
320         String sup = supported.iterator().next();
321
322         // verify that it still supports payload
323         params = params.toBuilder().operation(sup).build();
324         oper = new AppcLcmOperation(params, config);
325         assertTrue(oper.operationSupportsPayload());
326
327         // try with empty payload
328         params = params.toBuilder().payload(Map.of()).build();
329         oper = new AppcLcmOperation(params, config);
330         assertFalse(oper.operationSupportsPayload());
331
332         // try with null payload
333         params = params.toBuilder().payload(null).build();
334         oper = new AppcLcmOperation(params, config);
335         assertFalse(oper.operationSupportsPayload());
336     }
337
338     @Override
339     protected void makeContext() {
340         super.makeContext();
341
342         Target target = new Target();
343         target.setResourceID(RESOURCE_ID);
344
345         params = params.toBuilder().target(target).build();
346     }
347
348     @Override
349     protected Map<String, Object> makePayload() {
350         return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
351     }
352
353     private AppcLcmDmaapWrapper makeResponse() {
354         AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
355
356         AppcLcmBody body = new AppcLcmBody();
357         response.setBody(body);
358
359         AppcLcmOutput output = new AppcLcmOutput();
360         body.setOutput(output);
361
362         AppcLcmResponseStatus status = new AppcLcmResponseStatus();
363         output.setStatus(status);
364         status.setMessage(MY_MESSAGE);
365         status.setCode(SUCCESS_CODE);
366
367         return response;
368     }
369 }