Remove dmaap from models
[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-2024 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.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.stream.Collectors;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.junit.MockitoJUnitRunner;
46 import org.onap.policy.appclcm.AppcLcmBody;
47 import org.onap.policy.appclcm.AppcLcmCommonHeader;
48 import org.onap.policy.appclcm.AppcLcmMessageWrapper;
49 import org.onap.policy.appclcm.AppcLcmOutput;
50 import org.onap.policy.appclcm.AppcLcmResponseStatus;
51 import org.onap.policy.common.endpoints.event.comm.TopicSink;
52 import org.onap.policy.common.endpoints.event.comm.TopicSource;
53 import org.onap.policy.common.utils.coder.Coder;
54 import org.onap.policy.common.utils.coder.CoderException;
55 import org.onap.policy.common.utils.coder.StandardCoder;
56 import org.onap.policy.controlloop.ControlLoopOperation;
57 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
58 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
59 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
60 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
61 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
62 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
63 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
64 import org.onap.policy.simulators.AppcLcmTopicServer;
65 import org.onap.policy.simulators.TopicServer;
66
67 @RunWith(MockitoJUnitRunner.class)
68 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation<AppcLcmMessageWrapper> {
69
70     private static final String EXPECTED_EXCEPTION = "expected exception";
71     private static final String PAYLOAD_KEY1 = "key-A";
72     private static final String PAYLOAD_VALUE1 = "value-A";
73     private static final String MY_MESSAGE = "my-message";
74     protected static final String MY_VNF = "my-vnf";
75     protected static final String RESOURCE_ID = "my-resource";
76     private static final int SUCCESS_CODE = 400;
77
78     private AppcLcmMessageWrapper response;
79     private AppcLcmOperation oper;
80
81     @BeforeClass
82     public static void setUpBeforeClass() throws Exception {
83         initBeforeClass(MY_SINK, MY_SOURCE);
84     }
85
86     @AfterClass
87     public static void tearDownAfterClass() {
88         destroyAfterClass();
89     }
90
91     /**
92      * Sets up.
93      */
94     @Before
95     public void setUp() {
96         super.setUpBasic();
97
98         response = makeResponse();
99
100         oper = new AppcLcmOperation(params, config);
101         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
102     }
103
104     @After
105     public void tearDown() {
106         super.tearDownBasic();
107     }
108
109     @Override
110     protected TopicServer<AppcLcmMessageWrapper> makeServer(TopicSink sink, TopicSource source) {
111         return new AppcLcmTopicServer(sink, source);
112     }
113
114     /**
115      * Tests "success" case with simulator.
116      */
117     @Test
118     public void testSuccess() throws Exception {
119         BidirectionalTopicParams opParams =
120                         BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build();
121         config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcLcmOperation.SELECTOR_KEYS);
122
123         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
124
125         oper = new AppcLcmOperation(params, config);
126         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
127
128         outcome = oper.start().get();
129         // assertEquals(OperationResult.SUCCESS, outcome.getResult());
130         // assertTrue(outcome.getResponse() instanceof AppcLcmMessageWrapper);
131         assertNotNull(outcome);
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         AppcLcmMessageWrapper 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         AppcLcmMessageWrapper 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         AppcLcmMessageWrapper request = oper.makeRequest(2);
213         assertEquals(List.of(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 AppcLcmMessageWrapper makeResponse() {
348         AppcLcmMessageWrapper response = new AppcLcmMessageWrapper();
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 }