2 * ============LICENSE_START=======================================================
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
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.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;
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 * Tests makeRequest() when a property is missing.
166 public void testMakeRequestMissingProperty() throws Exception {
167 oper = new AppcLcmOperation(params, config);
168 oper.generateSubRequestId(1);
170 assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
171 .withMessageContaining("missing target entity");
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);
181 oper.generateSubRequestId(2);
182 AppcLcmDmaapWrapper req = oper.makeRequest(2);
183 assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
186 oper = new AppcLcmOperation(params, config) {
188 protected Coder getCoder() {
189 return new StandardCoder() {
191 public String encode(Object object) throws CoderException {
192 throw new CoderException(EXPECTED_EXCEPTION);
198 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
199 oper.generateSubRequestId(2);
201 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
202 .withMessageContaining("Cannot convert payload");
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));
214 public void testDetmStatus() {
215 assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
218 response.getBody().getOutput().getStatus().setCode(405);
219 assertEquals(Status.FAILURE, oper.detmStatus(null, response));
222 response.getBody().getOutput().getStatus().setCode(200);
223 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
226 response.getBody().getOutput().getStatus().setCode(305);
227 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
230 response.getBody().getOutput().getStatus().setCode(100);
231 assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
234 response.getBody().getOutput().getStatus().setCode(-1);
235 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
238 response.getBody().getOutput().setStatus(null);
239 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
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());
250 oper.setOutcome(outcome, OperationResult.FAILURE, response);
251 assertEquals(OperationResult.FAILURE, outcome.getResult());
252 assertEquals(MY_MESSAGE, outcome.getMessage());
253 assertSame(response, outcome.getResponse());
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());
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());
269 public void testGetStatus() {
270 assertNotNull(oper.getStatus(response));
273 response.getBody().getOutput().setStatus(null);
274 assertNull(oper.getStatus(response));
277 response.getBody().setOutput(null);
278 assertNull(oper.getStatus(response));
281 response.setBody(null);
282 assertNull(oper.getStatus(response));
285 assertNull(oper.getStatus(null));
289 public void testOperationSupportsPayload() {
290 // these should support a payload
291 Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
293 for (String name : supported) {
294 params = params.toBuilder().operation(name).build();
295 oper = new AppcLcmOperation(params, config);
296 assertTrue(name, oper.operationSupportsPayload());
299 // these should NOT support a payload
300 Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
301 .collect(Collectors.toSet());
303 for (String name : unsupported) {
304 params = params.toBuilder().operation(name).build();
305 oper = new AppcLcmOperation(params, config);
306 assertFalse(name, oper.operationSupportsPayload());
309 // pick an operation that would ordinarily support payloads
310 String sup = supported.iterator().next();
312 // verify that it still supports payload
313 params = params.toBuilder().operation(sup).build();
314 oper = new AppcLcmOperation(params, config);
315 assertTrue(oper.operationSupportsPayload());
317 // try with empty payload
318 params = params.toBuilder().payload(Map.of()).build();
319 oper = new AppcLcmOperation(params, config);
320 assertFalse(oper.operationSupportsPayload());
322 // try with null payload
323 params = params.toBuilder().payload(null).build();
324 oper = new AppcLcmOperation(params, config);
325 assertFalse(oper.operationSupportsPayload());
329 protected void makeContext() {
332 Map<String, String> targetEntities = new HashMap<>();
333 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
335 params = params.toBuilder().targetEntityIds(targetEntities).build();
339 protected Map<String, Object> makePayload() {
340 return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
343 private AppcLcmDmaapWrapper makeResponse() {
344 AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
346 AppcLcmBody body = new AppcLcmBody();
347 response.setBody(body);
349 AppcLcmOutput output = new AppcLcmOutput();
350 body.setOutput(output);
352 AppcLcmResponseStatus status = new AppcLcmResponseStatus();
353 output.setStatus(status);
354 status.setMessage(MY_MESSAGE);
355 status.setCode(SUCCESS_CODE);