Upgrade and clean up dependencies
[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-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.appclcm;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertSame;
32 import static org.junit.Assert.assertTrue;
33
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.stream.Collectors;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.policy.appclcm.AppcLcmBody;
48 import org.onap.policy.appclcm.AppcLcmCommonHeader;
49 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
50 import org.onap.policy.appclcm.AppcLcmOutput;
51 import org.onap.policy.appclcm.AppcLcmResponseStatus;
52 import org.onap.policy.common.endpoints.event.comm.TopicSink;
53 import org.onap.policy.common.endpoints.event.comm.TopicSource;
54 import org.onap.policy.common.utils.coder.Coder;
55 import org.onap.policy.common.utils.coder.CoderException;
56 import org.onap.policy.common.utils.coder.StandardCoder;
57 import org.onap.policy.controlloop.ControlLoopOperation;
58 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
59 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
60 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
61 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
62 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
63 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
64 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
65 import org.onap.policy.simulators.AppcLcmTopicServer;
66 import org.onap.policy.simulators.TopicServer;
67
68 @RunWith(MockitoJUnitRunner.class)
69 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation<AppcLcmDmaapWrapper> {
70
71     private static final String EXPECTED_EXCEPTION = "expected exception";
72     private static final String PAYLOAD_KEY1 = "key-A";
73     private static final String PAYLOAD_VALUE1 = "value-A";
74     private static final String MY_MESSAGE = "my-message";
75     protected static final String MY_VNF = "my-vnf";
76     protected static final String RESOURCE_ID = "my-resource";
77     private static final int SUCCESS_CODE = 400;
78
79     private AppcLcmDmaapWrapper response;
80     private AppcLcmOperation oper;
81
82     @BeforeClass
83     public static void setUpBeforeClass() throws Exception {
84         initBeforeClass(MY_SINK, MY_SOURCE);
85     }
86
87     @AfterClass
88     public static void tearDownAfterClass() {
89         destroyAfterClass();
90     }
91
92     /**
93      * Sets up.
94      */
95     @Before
96     public void setUp() {
97         super.setUpBasic();
98
99         response = makeResponse();
100
101         oper = new AppcLcmOperation(params, config);
102         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
103     }
104
105     @After
106     public void tearDown() {
107         super.tearDownBasic();
108     }
109
110     @Override
111     protected TopicServer<AppcLcmDmaapWrapper> makeServer(TopicSink sink, TopicSource source) {
112         return new AppcLcmTopicServer(sink, source);
113     }
114
115     /**
116      * Tests "success" case with simulator.
117      */
118     @Test
119     public void testSuccess() throws Exception {
120         BidirectionalTopicParams opParams =
121                         BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build();
122         config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcLcmOperation.SELECTOR_KEYS);
123
124         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
125
126         oper = new AppcLcmOperation(params, config);
127         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
128
129         outcome = oper.start().get();
130         assertEquals(OperationResult.SUCCESS, outcome.getResult());
131         assertTrue(outcome.getResponse() instanceof AppcLcmDmaapWrapper);
132     }
133
134     @Test
135     public void testConstructor() {
136         assertEquals(DEFAULT_ACTOR, oper.getActorName());
137         assertEquals(DEFAULT_OPERATION, oper.getName());
138     }
139
140     @Test
141     public void testGetPropertyNames() {
142         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
143     }
144
145     @Test
146     public void testMakeRequest() {
147         oper.generateSubRequestId(2);
148         String subreq = oper.getSubRequestId();
149         assertNotNull(subreq);
150
151         AppcLcmDmaapWrapper request = oper.makeRequest(2);
152         assertEquals("DefaultOperation", request.getBody().getInput().getAction());
153
154         AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
155         assertNotNull(header);
156         assertEquals(params.getRequestId(), header.getRequestId());
157
158         assertEquals(subreq, header.getSubRequestId());
159
160         assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
161
162         request = oper.makeRequest(2);
163         assertEquals(subreq, request.getBody().getInput().getCommonHeader().getSubRequestId());
164     }
165
166     /**
167      * Tests makeRequest() when a property is missing.
168      */
169     @Test
170     public void testMakeRequestMissingProperty() throws Exception {
171         oper = new AppcLcmOperation(params, config);
172         oper.generateSubRequestId(1);
173
174         assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
175                         .withMessageContaining("missing target entity");
176     }
177
178     @Test
179     public void testConvertPayload() {
180         // only builds a payload for ConfigModify
181         params = params.toBuilder().operation(AppcLcmConstants.OPERATION_CONFIG_MODIFY).build();
182         oper = new AppcLcmOperation(params, config);
183         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
184
185         oper.generateSubRequestId(2);
186         AppcLcmDmaapWrapper req = oper.makeRequest(2);
187         assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
188
189         // coder exception
190         oper = new AppcLcmOperation(params, config) {
191             @Override
192             protected Coder getCoder() {
193                 return new StandardCoder() {
194                     @Override
195                     public String encode(Object object) throws CoderException {
196                         throw new CoderException(EXPECTED_EXCEPTION);
197                     }
198                 };
199             }
200         };
201
202         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
203         oper.generateSubRequestId(2);
204
205         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
206                         .withMessageContaining("Cannot convert payload");
207     }
208
209     @Test
210     public void testGetExpectedKeyValues() {
211         oper.generateSubRequestId(2);
212         AppcLcmDmaapWrapper request = oper.makeRequest(2);
213         assertEquals(Arrays.asList(request.getBody().getInput().getCommonHeader().getSubRequestId()),
214                         oper.getExpectedKeyValues(50, request));
215     }
216
217     @Test
218     public void testDetmStatus() {
219         assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
220
221         // failure
222         response.getBody().getOutput().getStatus().setCode(405);
223         assertEquals(Status.FAILURE, oper.detmStatus(null, response));
224
225         // error
226         response.getBody().getOutput().getStatus().setCode(200);
227         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
228
229         // reject
230         response.getBody().getOutput().getStatus().setCode(305);
231         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
232
233         // accepted
234         response.getBody().getOutput().getStatus().setCode(100);
235         assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
236
237         // other
238         response.getBody().getOutput().getStatus().setCode(-1);
239         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
240
241         // null status
242         response.getBody().getOutput().setStatus(null);
243         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
244     }
245
246     @Test
247     public void testSetOutcome() {
248         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
249         assertEquals(OperationResult.SUCCESS, outcome.getResult());
250         assertEquals(MY_MESSAGE, outcome.getMessage());
251         assertSame(response, outcome.getResponse());
252
253         // failure
254         oper.setOutcome(outcome, OperationResult.FAILURE, response);
255         assertEquals(OperationResult.FAILURE, outcome.getResult());
256         assertEquals(MY_MESSAGE, outcome.getMessage());
257         assertSame(response, outcome.getResponse());
258
259         // null message
260         response.getBody().getOutput().getStatus().setMessage(null);
261         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
262         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
263         assertSame(response, outcome.getResponse());
264
265         // null status
266         response.getBody().getOutput().setStatus(null);
267         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
268         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
269         assertSame(response, outcome.getResponse());
270     }
271
272     @Test
273     public void testGetStatus() {
274         assertNotNull(oper.getStatus(response));
275
276         // null status
277         response.getBody().getOutput().setStatus(null);
278         assertNull(oper.getStatus(response));
279
280         // null outcome
281         response.getBody().setOutput(null);
282         assertNull(oper.getStatus(response));
283
284         // null body
285         response.setBody(null);
286         assertNull(oper.getStatus(response));
287
288         // null response
289         assertNull(oper.getStatus(null));
290     }
291
292     @Test
293     public void testOperationSupportsPayload() {
294         // these should support a payload
295         Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
296
297         for (String name : supported) {
298             params = params.toBuilder().operation(name).build();
299             oper = new AppcLcmOperation(params, config);
300             assertTrue(name, oper.operationSupportsPayload());
301         }
302
303         // these should NOT support a payload
304         Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
305                         .collect(Collectors.toSet());
306
307         for (String name : unsupported) {
308             params = params.toBuilder().operation(name).build();
309             oper = new AppcLcmOperation(params, config);
310             assertFalse(name, oper.operationSupportsPayload());
311         }
312
313         // pick an operation that would ordinarily support payloads
314         String sup = supported.iterator().next();
315
316         // verify that it still supports payload
317         params = params.toBuilder().operation(sup).build();
318         oper = new AppcLcmOperation(params, config);
319         assertTrue(oper.operationSupportsPayload());
320
321         // try with empty payload
322         params = params.toBuilder().payload(Map.of()).build();
323         oper = new AppcLcmOperation(params, config);
324         assertFalse(oper.operationSupportsPayload());
325
326         // try with null payload
327         params = params.toBuilder().payload(null).build();
328         oper = new AppcLcmOperation(params, config);
329         assertFalse(oper.operationSupportsPayload());
330     }
331
332     @Override
333     protected void makeContext() {
334         super.makeContext();
335
336         Map<String, String> targetEntities = new HashMap<>();
337         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
338
339         params = params.toBuilder().targetEntityIds(targetEntities).build();
340     }
341
342     @Override
343     protected Map<String, Object> makePayload() {
344         return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
345     }
346
347     private AppcLcmDmaapWrapper makeResponse() {
348         AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
349
350         AppcLcmBody body = new AppcLcmBody();
351         response.setBody(body);
352
353         AppcLcmOutput output = new AppcLcmOutput();
354         body.setOutput(output);
355
356         AppcLcmResponseStatus status = new AppcLcmResponseStatus();
357         output.setStatus(status);
358         status.setMessage(MY_MESSAGE);
359         status.setCode(SUCCESS_CODE);
360
361         return response;
362     }
363 }