2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.appclcm;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26 import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertFalse;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertNull;
31 import static org.junit.Assert.assertSame;
32 import static org.junit.Assert.assertTrue;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
39 import java.util.stream.Collectors;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.policy.appclcm.AppcLcmBody;
48 import org.onap.policy.appclcm.AppcLcmCommonHeader;
49 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
50 import org.onap.policy.appclcm.AppcLcmOutput;
51 import org.onap.policy.appclcm.AppcLcmResponseStatus;
52 import org.onap.policy.common.endpoints.event.comm.TopicSink;
53 import org.onap.policy.common.endpoints.event.comm.TopicSource;
54 import org.onap.policy.common.utils.coder.Coder;
55 import org.onap.policy.common.utils.coder.CoderException;
56 import org.onap.policy.common.utils.coder.StandardCoder;
57 import org.onap.policy.controlloop.ControlLoopOperation;
58 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
59 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
60 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
61 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
62 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
63 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicParams;
64 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
65 import org.onap.policy.simulators.AppcLcmTopicServer;
66 import org.onap.policy.simulators.TopicServer;
68 @RunWith(MockitoJUnitRunner.class)
69 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation<AppcLcmDmaapWrapper> {
71 private static final String EXPECTED_EXCEPTION = "expected exception";
72 private static final String PAYLOAD_KEY1 = "key-A";
73 private static final String PAYLOAD_VALUE1 = "value-A";
74 private static final String MY_MESSAGE = "my-message";
75 protected static final String MY_VNF = "my-vnf";
76 protected static final String RESOURCE_ID = "my-resource";
77 private static final int SUCCESS_CODE = 400;
79 private AppcLcmDmaapWrapper response;
80 private AppcLcmOperation oper;
83 public static void setUpBeforeClass() throws Exception {
84 initBeforeClass(MY_SINK, MY_SOURCE);
88 public static void tearDownAfterClass() {
99 response = makeResponse();
101 oper = new AppcLcmOperation(params, config);
102 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
106 public void tearDown() {
107 super.tearDownBasic();
111 protected TopicServer<AppcLcmDmaapWrapper> makeServer(TopicSink sink, TopicSource source) {
112 return new AppcLcmTopicServer(sink, source);
116 * Tests "success" case with simulator.
119 public void testSuccess() throws Exception {
120 BidirectionalTopicParams opParams =
121 BidirectionalTopicParams.builder().sinkTopic(MY_SINK).sourceTopic(MY_SOURCE).build();
122 config = new BidirectionalTopicConfig(blockingExecutor, opParams, topicMgr, AppcLcmOperation.SELECTOR_KEYS);
124 params = params.toBuilder().retry(0).timeoutSec(5).executor(blockingExecutor).build();
126 oper = new AppcLcmOperation(params, config);
127 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
129 outcome = oper.start().get();
130 assertEquals(OperationResult.SUCCESS, outcome.getResult());
131 assertTrue(outcome.getResponse() instanceof AppcLcmDmaapWrapper);
135 public void testConstructor() {
136 assertEquals(DEFAULT_ACTOR, oper.getActorName());
137 assertEquals(DEFAULT_OPERATION, oper.getName());
141 public void testGetPropertyNames() {
142 assertThat(oper.getPropertyNames()).isEqualTo(List.of(OperationProperties.AAI_TARGET_ENTITY));
146 public void testMakeRequest() {
147 oper.generateSubRequestId(2);
148 String subreq = oper.getSubRequestId();
149 assertNotNull(subreq);
151 AppcLcmDmaapWrapper request = oper.makeRequest(2);
152 assertEquals("DefaultOperation", request.getBody().getInput().getAction());
154 AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
155 assertNotNull(header);
156 assertEquals(params.getRequestId(), header.getRequestId());
158 assertEquals(subreq, header.getSubRequestId());
160 assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
162 request = oper.makeRequest(2);
163 assertEquals(subreq, request.getBody().getInput().getCommonHeader().getSubRequestId());
167 * Tests makeRequest() when a property is missing.
170 public void testMakeRequestMissingProperty() throws Exception {
171 oper = new AppcLcmOperation(params, config);
172 oper.generateSubRequestId(1);
174 assertThatIllegalStateException().isThrownBy(() -> oper.makeRequest(1))
175 .withMessageContaining("missing target entity");
179 public void testConvertPayload() {
180 // only builds a payload for ConfigModify
181 params = params.toBuilder().operation(AppcLcmConstants.OPERATION_CONFIG_MODIFY).build();
182 oper = new AppcLcmOperation(params, config);
183 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
185 oper.generateSubRequestId(2);
186 AppcLcmDmaapWrapper req = oper.makeRequest(2);
187 assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
190 oper = new AppcLcmOperation(params, config) {
192 protected Coder getCoder() {
193 return new StandardCoder() {
195 public String encode(Object object) throws CoderException {
196 throw new CoderException(EXPECTED_EXCEPTION);
202 oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, TARGET_ENTITY);
203 oper.generateSubRequestId(2);
205 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
206 .withMessageContaining("Cannot convert payload");
210 public void testGetExpectedKeyValues() {
211 oper.generateSubRequestId(2);
212 AppcLcmDmaapWrapper request = oper.makeRequest(2);
213 assertEquals(Arrays.asList(request.getBody().getInput().getCommonHeader().getSubRequestId()),
214 oper.getExpectedKeyValues(50, request));
218 public void testDetmStatus() {
219 assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
222 response.getBody().getOutput().getStatus().setCode(405);
223 assertEquals(Status.FAILURE, oper.detmStatus(null, response));
226 response.getBody().getOutput().getStatus().setCode(200);
227 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
230 response.getBody().getOutput().getStatus().setCode(305);
231 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
234 response.getBody().getOutput().getStatus().setCode(100);
235 assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
238 response.getBody().getOutput().getStatus().setCode(-1);
239 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
242 response.getBody().getOutput().setStatus(null);
243 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
247 public void testSetOutcome() {
248 oper.setOutcome(outcome, OperationResult.SUCCESS, response);
249 assertEquals(OperationResult.SUCCESS, outcome.getResult());
250 assertEquals(MY_MESSAGE, outcome.getMessage());
251 assertSame(response, outcome.getResponse());
254 oper.setOutcome(outcome, OperationResult.FAILURE, response);
255 assertEquals(OperationResult.FAILURE, outcome.getResult());
256 assertEquals(MY_MESSAGE, outcome.getMessage());
257 assertSame(response, outcome.getResponse());
260 response.getBody().getOutput().getStatus().setMessage(null);
261 oper.setOutcome(outcome, OperationResult.SUCCESS, response);
262 assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
263 assertSame(response, outcome.getResponse());
266 response.getBody().getOutput().setStatus(null);
267 oper.setOutcome(outcome, OperationResult.SUCCESS, response);
268 assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
269 assertSame(response, outcome.getResponse());
273 public void testGetStatus() {
274 assertNotNull(oper.getStatus(response));
277 response.getBody().getOutput().setStatus(null);
278 assertNull(oper.getStatus(response));
281 response.getBody().setOutput(null);
282 assertNull(oper.getStatus(response));
285 response.setBody(null);
286 assertNull(oper.getStatus(response));
289 assertNull(oper.getStatus(null));
293 public void testOperationSupportsPayload() {
294 // these should support a payload
295 Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
297 for (String name : supported) {
298 params = params.toBuilder().operation(name).build();
299 oper = new AppcLcmOperation(params, config);
300 assertTrue(name, oper.operationSupportsPayload());
303 // these should NOT support a payload
304 Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
305 .collect(Collectors.toSet());
307 for (String name : unsupported) {
308 params = params.toBuilder().operation(name).build();
309 oper = new AppcLcmOperation(params, config);
310 assertFalse(name, oper.operationSupportsPayload());
313 // pick an operation that would ordinarily support payloads
314 String sup = supported.iterator().next();
316 // verify that it still supports payload
317 params = params.toBuilder().operation(sup).build();
318 oper = new AppcLcmOperation(params, config);
319 assertTrue(oper.operationSupportsPayload());
321 // try with empty payload
322 params = params.toBuilder().payload(Map.of()).build();
323 oper = new AppcLcmOperation(params, config);
324 assertFalse(oper.operationSupportsPayload());
326 // try with null payload
327 params = params.toBuilder().payload(null).build();
328 oper = new AppcLcmOperation(params, config);
329 assertFalse(oper.operationSupportsPayload());
333 protected void makeContext() {
336 Map<String, String> targetEntities = new HashMap<>();
337 targetEntities.put(ControlLoopOperationParams.PARAMS_ENTITY_RESOURCEID, RESOURCE_ID);
339 params = params.toBuilder().targetEntityIds(targetEntities).build();
343 protected Map<String, Object> makePayload() {
344 return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
347 private AppcLcmDmaapWrapper makeResponse() {
348 AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
350 AppcLcmBody body = new AppcLcmBody();
351 response.setBody(body);
353 AppcLcmOutput output = new AppcLcmOutput();
354 body.setOutput(output);
356 AppcLcmResponseStatus status = new AppcLcmResponseStatus();
357 output.setStatus(status);
358 status.setMessage(MY_MESSAGE);
359 status.setCode(SUCCESS_CODE);