36496455a979e43455bd4b1d5204532ef26c2539
[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  * ================================================================================
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.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertSame;
31 import static org.junit.Assert.assertTrue;
32
33 import java.util.Arrays;
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.onap.policy.appclcm.AppcLcmBody;
45 import org.onap.policy.appclcm.AppcLcmCommonHeader;
46 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
47 import org.onap.policy.appclcm.AppcLcmOutput;
48 import org.onap.policy.appclcm.AppcLcmResponseStatus;
49 import org.onap.policy.common.endpoints.event.comm.TopicSink;
50 import org.onap.policy.common.endpoints.event.comm.TopicSource;
51 import org.onap.policy.common.utils.coder.Coder;
52 import org.onap.policy.common.utils.coder.CoderException;
53 import org.onap.policy.common.utils.coder.StandardCoder;
54 import org.onap.policy.controlloop.ControlLoopOperation;
55 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
56 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
57 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
58 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
59 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
60 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
61 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
62 import org.onap.policy.simulators.AppcLcmTopicServer;
63 import org.onap.policy.simulators.TopicServer;
64
65 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation<AppcLcmDmaapWrapper> {
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         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
99     }
100
101     @After
102     public void tearDown() {
103         super.tearDownBasic();
104     }
105
106     @Override
107     protected TopicServer<AppcLcmDmaapWrapper> makeServer(TopicSink sink, TopicSource source) {
108         return new AppcLcmTopicServer(sink, source);
109     }
110
111     /**
112      * Tests "success" case with simulator.
113      */
114     @Test
115     public void testSuccess() throws Exception {
116         BidirectionalTopicParams opParams =
117                         BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build();
118         config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcLcmOperation.SELECTOR_KEYS);
119
120         params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
121
122         oper = new AppcLcmOperation(params, config);
123         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
124
125         outcome = oper.start().get();
126         assertEquals(OperationResult.SUCCESS, outcome.getResult());
127         assertTrue(outcome.getResponse() instanceof AppcLcmDmaapWrapper);
128     }
129
130     @Test
131     public void testConstructor() {
132         assertEquals(DEFAULT_ACTOR, oper.getActorName());
133         assertEquals(DEFAULT_OPERATION, oper.getName());
134     }
135
136     @Test
137     public void testGetPropertyNames() {
138         assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
139     }
140
141     @Test
142     public void testMakeRequest() {
143         oper.generateSubRequestId(2);
144         String subreq = oper.getSubRequestId();
145         assertNotNull(subreq);
146
147         AppcLcmDmaapWrapper request = oper.makeRequest(2);
148         assertEquals("DefaultOperation", request.getBody().getInput().getAction());
149
150         AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
151         assertNotNull(header);
152         assertEquals(params.getRequestId(), header.getRequestId());
153
154         assertEquals(subreq, header.getSubRequestId());
155
156         assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
157
158         request = oper.makeRequest(2);
159         assertEquals(subreq, request.getBody().getInput().getCommonHeader().getSubRequestId());
160     }
161
162     /**
163      * Tests makeRequest() when a property is missing.
164      */
165     @Test
166     public void testMakeRequestMissingProperty() throws Exception {
167         oper = new AppcLcmOperation(params, config);
168         oper.generateSubRequestId(1);
169
170         assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
171                         .withMessageContaining("missing target entity");
172     }
173
174     @Test
175     public void testConvertPayload() {
176         // only builds a payload for ConfigModify
177         params = params.toBuilder().operation(AppcLcmConstants.OPERATION_CONFIG_MODIFY).build();
178         oper = new AppcLcmOperation(params, config);
179         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
180
181         oper.generateSubRequestId(2);
182         AppcLcmDmaapWrapper req = oper.makeRequest(2);
183         assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
184
185         // coder exception
186         oper = new AppcLcmOperation(params, config) {
187             @Override
188             protected Coder getCoder() {
189                 return new StandardCoder() {
190                     @Override
191                     public String encode(Object object) throws CoderException {
192                         throw new CoderException(EXPECTED_EXCEPTION);
193                     }
194                 };
195             }
196         };
197
198         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
199         oper.generateSubRequestId(2);
200
201         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
202                         .withMessageContaining("Cannot convert payload");
203     }
204
205     @Test
206     public void testGetExpectedKeyValues() {
207         oper.generateSubRequestId(2);
208         AppcLcmDmaapWrapper request = oper.makeRequest(2);
209         assertEquals(Arrays.asList(request.getBody().getInput().getCommonHeader().getSubRequestId()),
210                         oper.getExpectedKeyValues(50, request));
211     }
212
213     @Test
214     public void testDetmStatus() {
215         assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
216
217         // failure
218         response.getBody().getOutput().getStatus().setCode(405);
219         assertEquals(Status.FAILURE, oper.detmStatus(null, response));
220
221         // error
222         response.getBody().getOutput().getStatus().setCode(200);
223         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
224
225         // reject
226         response.getBody().getOutput().getStatus().setCode(305);
227         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
228
229         // accepted
230         response.getBody().getOutput().getStatus().setCode(100);
231         assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
232
233         // other
234         response.getBody().getOutput().getStatus().setCode(-1);
235         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
236
237         // null status
238         response.getBody().getOutput().setStatus(null);
239         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
240     }
241
242     @Test
243     public void testSetOutcome() {
244         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
245         assertEquals(OperationResult.SUCCESS, outcome.getResult());
246         assertEquals(MY_MESSAGE, outcome.getMessage());
247         assertSame(response, outcome.getResponse());
248
249         // failure
250         oper.setOutcome(outcome, OperationResult.FAILURE, response);
251         assertEquals(OperationResult.FAILURE, outcome.getResult());
252         assertEquals(MY_MESSAGE, outcome.getMessage());
253         assertSame(response, outcome.getResponse());
254
255         // null message
256         response.getBody().getOutput().getStatus().setMessage(null);
257         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
258         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
259         assertSame(response, outcome.getResponse());
260
261         // null status
262         response.getBody().getOutput().setStatus(null);
263         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
264         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
265         assertSame(response, outcome.getResponse());
266     }
267
268     @Test
269     public void testGetStatus() {
270         assertNotNull(oper.getStatus(response));
271
272         // null status
273         response.getBody().getOutput().setStatus(null);
274         assertNull(oper.getStatus(response));
275
276         // null outcome
277         response.getBody().setOutput(null);
278         assertNull(oper.getStatus(response));
279
280         // null body
281         response.setBody(null);
282         assertNull(oper.getStatus(response));
283
284         // null response
285         assertNull(oper.getStatus(null));
286     }
287
288     @Test
289     public void testOperationSupportsPayload() {
290         // these should support a payload
291         Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
292
293         for (String name : supported) {
294             params = params.toBuilder().operation(name).build();
295             oper = new AppcLcmOperation(params, config);
296             assertTrue(name, oper.operationSupportsPayload());
297         }
298
299         // these should NOT support a payload
300         Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
301                         .collect(Collectors.toSet());
302
303         for (String name : unsupported) {
304             params = params.toBuilder().operation(name).build();
305             oper = new AppcLcmOperation(params, config);
306             assertFalse(name, oper.operationSupportsPayload());
307         }
308
309         // pick an operation that would ordinarily support payloads
310         String sup = supported.iterator().next();
311
312         // verify that it still supports payload
313         params = params.toBuilder().operation(sup).build();
314         oper = new AppcLcmOperation(params, config);
315         assertTrue(oper.operationSupportsPayload());
316
317         // try with empty payload
318         params = params.toBuilder().payload(Map.of()).build();
319         oper = new AppcLcmOperation(params, config);
320         assertFalse(oper.operationSupportsPayload());
321
322         // try with null payload
323         params = params.toBuilder().payload(null).build();
324         oper = new AppcLcmOperation(params, config);
325         assertFalse(oper.operationSupportsPayload());
326     }
327
328     @Override
329     protected void makeContext() {
330         super.makeContext();
331
332         Map<String, String> targetEntities = new HashMap<>();
333         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
334
335         params = params.toBuilder().targetEntityIds(targetEntities).build();
336     }
337
338     @Override
339     protected Map<String, Object> makePayload() {
340         return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
341     }
342
343     private AppcLcmDmaapWrapper makeResponse() {
344         AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
345
346         AppcLcmBody body = new AppcLcmBody();
347         response.setBody(body);
348
349         AppcLcmOutput output = new AppcLcmOutput();
350         body.setOutput(output);
351
352         AppcLcmResponseStatus status = new AppcLcmResponseStatus();
353         output.setStatus(status);
354         status.setMessage(MY_MESSAGE);
355         status.setCode(SUCCESS_CODE);
356
357         return response;
358     }
359 }