Delete preprocessed flag from actors
[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.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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     @Test
163     public void testConvertPayload() {
164         // only builds a payload for ConfigModify
165         params = params.toBuilder().operation(AppcLcmConstants.OPERATION_CONFIG_MODIFY).build();
166         oper = new AppcLcmOperation(params, config);
167         oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
168
169         oper.generateSubRequestId(2);
170         AppcLcmDmaapWrapper req = oper.makeRequest(2);
171         assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
172
173         // coder exception
174         oper = new AppcLcmOperation(params, config) {
175             @Override
176             protected Coder getCoder() {
177                 return new StandardCoder() {
178                     @Override
179                     public String encode(Object object) throws CoderException {
180                         throw new CoderException(EXPECTED_EXCEPTION);
181                     }
182                 };
183             }
184         };
185
186         oper.generateSubRequestId(2);
187
188         assertThatThrownBy(() -> oper.makeRequest(2)).isInstanceOf(NullPointerException.class);
189     }
190
191     @Test
192     public void testGetExpectedKeyValues() {
193         oper.generateSubRequestId(2);
194         AppcLcmDmaapWrapper request = oper.makeRequest(2);
195         assertEquals(Arrays.asList(request.getBody().getInput().getCommonHeader().getSubRequestId()),
196                         oper.getExpectedKeyValues(50, request));
197     }
198
199     @Test
200     public void testDetmStatus() {
201         assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
202
203         // failure
204         response.getBody().getOutput().getStatus().setCode(405);
205         assertEquals(Status.FAILURE, oper.detmStatus(null, response));
206
207         // error
208         response.getBody().getOutput().getStatus().setCode(200);
209         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
210
211         // reject
212         response.getBody().getOutput().getStatus().setCode(305);
213         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
214
215         // accepted
216         response.getBody().getOutput().getStatus().setCode(100);
217         assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
218
219         // other
220         response.getBody().getOutput().getStatus().setCode(-1);
221         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
222
223         // null status
224         response.getBody().getOutput().setStatus(null);
225         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
226     }
227
228     @Test
229     public void testSetOutcome() {
230         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
231         assertEquals(OperationResult.SUCCESS, outcome.getResult());
232         assertEquals(MY_MESSAGE, outcome.getMessage());
233         assertSame(response, outcome.getResponse());
234
235         // failure
236         oper.setOutcome(outcome, OperationResult.FAILURE, response);
237         assertEquals(OperationResult.FAILURE, outcome.getResult());
238         assertEquals(MY_MESSAGE, outcome.getMessage());
239         assertSame(response, outcome.getResponse());
240
241         // null message
242         response.getBody().getOutput().getStatus().setMessage(null);
243         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
244         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
245         assertSame(response, outcome.getResponse());
246
247         // null status
248         response.getBody().getOutput().setStatus(null);
249         oper.setOutcome(outcome, OperationResult.SUCCESS, response);
250         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
251         assertSame(response, outcome.getResponse());
252     }
253
254     @Test
255     public void testGetStatus() {
256         assertNotNull(oper.getStatus(response));
257
258         // null status
259         response.getBody().getOutput().setStatus(null);
260         assertNull(oper.getStatus(response));
261
262         // null outcome
263         response.getBody().setOutput(null);
264         assertNull(oper.getStatus(response));
265
266         // null body
267         response.setBody(null);
268         assertNull(oper.getStatus(response));
269
270         // null response
271         assertNull(oper.getStatus(null));
272     }
273
274     @Test
275     public void testOperationSupportsPayload() {
276         // these should support a payload
277         Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
278
279         for (String name : supported) {
280             params = params.toBuilder().operation(name).build();
281             oper = new AppcLcmOperation(params, config);
282             assertTrue(name, oper.operationSupportsPayload());
283         }
284
285         // these should NOT support a payload
286         Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
287                         .collect(Collectors.toSet());
288
289         for (String name : unsupported) {
290             params = params.toBuilder().operation(name).build();
291             oper = new AppcLcmOperation(params, config);
292             assertFalse(name, oper.operationSupportsPayload());
293         }
294
295         // pick an operation that would ordinarily support payloads
296         String sup = supported.iterator().next();
297
298         // verify that it still supports payload
299         params = params.toBuilder().operation(sup).build();
300         oper = new AppcLcmOperation(params, config);
301         assertTrue(oper.operationSupportsPayload());
302
303         // try with empty payload
304         params = params.toBuilder().payload(Map.of()).build();
305         oper = new AppcLcmOperation(params, config);
306         assertFalse(oper.operationSupportsPayload());
307
308         // try with null payload
309         params = params.toBuilder().payload(null).build();
310         oper = new AppcLcmOperation(params, config);
311         assertFalse(oper.operationSupportsPayload());
312     }
313
314     @Override
315     protected void makeContext() {
316         super.makeContext();
317
318         Map<String, String> targetEntities = new HashMap<>();
319         targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
320
321         params = params.toBuilder().targetEntityIds(targetEntities).build();
322     }
323
324     @Override
325     protected Map<String, Object> makePayload() {
326         return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
327     }
328
329     private AppcLcmDmaapWrapper makeResponse() {
330         AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
331
332         AppcLcmBody body = new AppcLcmBody();
333         response.setBody(body);
334
335         AppcLcmOutput output = new AppcLcmOutput();
336         body.setOutput(output);
337
338         AppcLcmResponseStatus status = new AppcLcmResponseStatus();
339         output.setStatus(status);
340         status.setMessage(MY_MESSAGE);
341         status.setCode(SUCCESS_CODE);
342
343         return response;
344     }
345 }