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