2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.appclcm;
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;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.List;
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;
65 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation<AppcLcmDmaapWrapper> {
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;
75 private AppcLcmDmaapWrapper response;
76 private AppcLcmOperation oper;
79 public static void setUpBeforeClass() throws Exception {
80 initBeforeClass(MY_SINK, MY_SOURCE);
84 public static void tearDownAfterClass() {
95 response = makeResponse();
97 oper = new AppcLcmOperation(params, config);
98 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
102 public void tearDown() {
103 super.tearDownBasic();
107 protected TopicServer<AppcLcmDmaapWrapper> makeServer(TopicSink sink, TopicSource source) {
108 return new AppcLcmTopicServer(sink, source);
112 * Tests "success" case with simulator.
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);
120 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
122 oper = new AppcLcmOperation(params, config);
123 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
125 outcome = oper.start().get();
126 assertEquals(OperationResult.SUCCESS, outcome.getResult());
127 assertTrue(outcome.getResponse() instanceof AppcLcmDmaapWrapper);
131 public void testConstructor() {
132 assertEquals(DEFAULT_ACTOR, oper.getActorName());
133 assertEquals(DEFAULT_OPERATION, oper.getName());
137 public void testGetPropertyNames() {
138 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
142 public void testMakeRequest() {
143 oper.generateSubRequestId(2);
144 String subreq = oper.getSubRequestId();
145 assertNotNull(subreq);
147 AppcLcmDmaapWrapper request = oper.makeRequest(2);
148 assertEquals("DefaultOperation", request.getBody().getInput().getAction());
150 AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
151 assertNotNull(header);
152 assertEquals(params.getRequestId(), header.getRequestId());
154 assertEquals(subreq, header.getSubRequestId());
156 assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
158 request = oper.makeRequest(2);
159 assertEquals(subreq, request.getBody().getInput().getCommonHeader().getSubRequestId());
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);
169 oper.generateSubRequestId(2);
170 AppcLcmDmaapWrapper req = oper.makeRequest(2);
171 assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
174 oper = new AppcLcmOperation(params, config) {
176 protected Coder getCoder() {
177 return new StandardCoder() {
179 public String encode(Object object) throws CoderException {
180 throw new CoderException(EXPECTED_EXCEPTION);
186 oper.generateSubRequestId(2);
188 assertThatThrownBy(() -> oper.makeRequest(2)).isInstanceOf(NullPointerException.class);
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));
200 public void testDetmStatus() {
201 assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
204 response.getBody().getOutput().getStatus().setCode(405);
205 assertEquals(Status.FAILURE, oper.detmStatus(null, response));
208 response.getBody().getOutput().getStatus().setCode(200);
209 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
212 response.getBody().getOutput().getStatus().setCode(305);
213 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
216 response.getBody().getOutput().getStatus().setCode(100);
217 assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
220 response.getBody().getOutput().getStatus().setCode(-1);
221 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
224 response.getBody().getOutput().setStatus(null);
225 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
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());
236 oper.setOutcome(outcome, OperationResult.FAILURE, response);
237 assertEquals(OperationResult.FAILURE, outcome.getResult());
238 assertEquals(MY_MESSAGE, outcome.getMessage());
239 assertSame(response, outcome.getResponse());
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());
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());
255 public void testGetStatus() {
256 assertNotNull(oper.getStatus(response));
259 response.getBody().getOutput().setStatus(null);
260 assertNull(oper.getStatus(response));
263 response.getBody().setOutput(null);
264 assertNull(oper.getStatus(response));
267 response.setBody(null);
268 assertNull(oper.getStatus(response));
271 assertNull(oper.getStatus(null));
275 public void testOperationSupportsPayload() {
276 // these should support a payload
277 Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
279 for (String name : supported) {
280 params = params.toBuilder().operation(name).build();
281 oper = new AppcLcmOperation(params, config);
282 assertTrue(name, oper.operationSupportsPayload());
285 // these should NOT support a payload
286 Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
287 .collect(Collectors.toSet());
289 for (String name : unsupported) {
290 params = params.toBuilder().operation(name).build();
291 oper = new AppcLcmOperation(params, config);
292 assertFalse(name, oper.operationSupportsPayload());
295 // pick an operation that would ordinarily support payloads
296 String sup = supported.iterator().next();
298 // verify that it still supports payload
299 params = params.toBuilder().operation(sup).build();
300 oper = new AppcLcmOperation(params, config);
301 assertTrue(oper.operationSupportsPayload());
303 // try with empty payload
304 params = params.toBuilder().payload(Map.of()).build();
305 oper = new AppcLcmOperation(params, config);
306 assertFalse(oper.operationSupportsPayload());
308 // try with null payload
309 params = params.toBuilder().payload(null).build();
310 oper = new AppcLcmOperation(params, config);
311 assertFalse(oper.operationSupportsPayload());
315 protected void makeContext() {
318 Map<String, String> targetEntities = new HashMap<>();
319 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
321 params = params.toBuilder().targetEntityIds(targetEntities).build();
325 protected Map<String, Object> makePayload() {
326 return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
329 private AppcLcmDmaapWrapper makeResponse() {
330 AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
332 AppcLcmBody body = new AppcLcmBody();
333 response.setBody(body);
335 AppcLcmOutput output = new AppcLcmOutput();
336 body.setOutput(output);
338 AppcLcmResponseStatus status = new AppcLcmResponseStatus();
339 output.setStatus(status);
340 status.setMessage(MY_MESSAGE);
341 status.setCode(SUCCESS_CODE);