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.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;
32 import java.util.Arrays;
33 import java.util.HashMap;
34 import java.util.List;
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;
64 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation<AppcLcmDmaapWrapper> {
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;
74 private AppcLcmDmaapWrapper response;
75 private AppcLcmOperation oper;
78 public static void setUpBeforeClass() throws Exception {
79 initBeforeClass(MY_SINK, MY_SOURCE);
83 public static void tearDownAfterClass() {
94 response = makeResponse();
96 oper = new AppcLcmOperation(params, config);
100 public void tearDown() {
101 super.tearDownBasic();
105 protected TopicServer<AppcLcmDmaapWrapper> makeServer(TopicSink sink, TopicSource source) {
106 return new AppcLcmTopicServer(sink, source);
110 * Tests "success" case with simulator.
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);
118 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
120 oper = new AppcLcmOperation(params, config);
122 outcome = oper.start().get();
123 assertEquals(OperationResult.SUCCESS, outcome.getResult());
124 assertTrue(outcome.getResponse() instanceof AppcLcmDmaapWrapper);
128 public void testConstructor() {
129 assertEquals(DEFAULT_ACTOR, oper.getActorName());
130 assertEquals(DEFAULT_OPERATION, oper.getName());
134 public void testGetPropertyNames() {
135 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
139 public void testMakeRequest() {
140 oper.generateSubRequestId(2);
141 String subreq = oper.getSubRequestId();
142 assertNotNull(subreq);
144 AppcLcmDmaapWrapper request = oper.makeRequest(2);
145 assertEquals("DefaultOperation", request.getBody().getInput().getAction());
147 AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
148 assertNotNull(header);
149 assertEquals(params.getRequestId(), header.getRequestId());
151 assertEquals(subreq, header.getSubRequestId());
153 assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
155 request = oper.makeRequest(2);
156 assertEquals(subreq, request.getBody().getInput().getCommonHeader().getSubRequestId());
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);
165 oper.generateSubRequestId(2);
166 AppcLcmDmaapWrapper req = oper.makeRequest(2);
167 assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
170 oper = new AppcLcmOperation(params, config) {
172 protected Coder getCoder() {
173 return new StandardCoder() {
175 public String encode(Object object) throws CoderException {
176 throw new CoderException(EXPECTED_EXCEPTION);
182 oper.generateSubRequestId(2);
184 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
185 .withMessage("Cannot convert payload");
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));
197 public void testDetmStatus() {
198 assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
201 response.getBody().getOutput().getStatus().setCode(405);
202 assertEquals(Status.FAILURE, oper.detmStatus(null, response));
205 response.getBody().getOutput().getStatus().setCode(200);
206 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
209 response.getBody().getOutput().getStatus().setCode(305);
210 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
213 response.getBody().getOutput().getStatus().setCode(100);
214 assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
217 response.getBody().getOutput().getStatus().setCode(-1);
218 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
221 response.getBody().getOutput().setStatus(null);
222 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
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());
233 oper.setOutcome(outcome, OperationResult.FAILURE, response);
234 assertEquals(OperationResult.FAILURE, outcome.getResult());
235 assertEquals(MY_MESSAGE, outcome.getMessage());
236 assertSame(response, outcome.getResponse());
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());
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());
252 public void testGetStatus() {
253 assertNotNull(oper.getStatus(response));
256 response.getBody().getOutput().setStatus(null);
257 assertNull(oper.getStatus(response));
260 response.getBody().setOutput(null);
261 assertNull(oper.getStatus(response));
264 response.setBody(null);
265 assertNull(oper.getStatus(response));
268 assertNull(oper.getStatus(null));
272 public void testOperationSupportsPayload() {
273 // these should support a payload
274 Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
276 for (String name : supported) {
277 params = params.toBuilder().operation(name).build();
278 oper = new AppcLcmOperation(params, config);
279 assertTrue(name, oper.operationSupportsPayload());
282 // these should NOT support a payload
283 Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
284 .collect(Collectors.toSet());
286 for (String name : unsupported) {
287 params = params.toBuilder().operation(name).build();
288 oper = new AppcLcmOperation(params, config);
289 assertFalse(name, oper.operationSupportsPayload());
292 // pick an operation that would ordinarily support payloads
293 String sup = supported.iterator().next();
295 // verify that it still supports payload
296 params = params.toBuilder().operation(sup).build();
297 oper = new AppcLcmOperation(params, config);
298 assertTrue(oper.operationSupportsPayload());
300 // try with empty payload
301 params = params.toBuilder().payload(Map.of()).build();
302 oper = new AppcLcmOperation(params, config);
303 assertFalse(oper.operationSupportsPayload());
305 // try with null payload
306 params = params.toBuilder().payload(null).build();
307 oper = new AppcLcmOperation(params, config);
308 assertFalse(oper.operationSupportsPayload());
312 protected void makeContext() {
315 Map<String, String> targetEntities = new HashMap<>();
316 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
318 params = params.toBuilder().targetEntityIds(targetEntities).build();
322 protected Map<String, Object> makePayload() {
323 return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
326 private AppcLcmDmaapWrapper makeResponse() {
327 AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
329 AppcLcmBody body = new AppcLcmBody();
330 response.setBody(body);
332 AppcLcmOutput output = new AppcLcmOutput();
333 body.setOutput(output);
335 AppcLcmResponseStatus status = new AppcLcmResponseStatus();
336 output.setStatus(status);
337 status.setMessage(MY_MESSAGE);
338 status.setCode(SUCCESS_CODE);