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.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
33 import java.util.Arrays;
36 import java.util.concurrent.CompletableFuture;
37 import java.util.concurrent.atomic.AtomicBoolean;
38 import java.util.stream.Collectors;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.appclcm.AppcLcmBody;
42 import org.onap.policy.appclcm.AppcLcmCommonHeader;
43 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
44 import org.onap.policy.appclcm.AppcLcmOutput;
45 import org.onap.policy.appclcm.AppcLcmResponseStatus;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.controlloop.ControlLoopOperation;
50 import org.onap.policy.controlloop.actor.test.BasicBidirectionalTopicOperation;
51 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
52 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
53 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
54 import org.onap.policy.controlloop.policy.PolicyResult;
55 import org.onap.policy.controlloop.policy.Target;
57 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
59 private static final String EXPECTED_EXCEPTION = "expected exception";
60 private static final String PAYLOAD_KEY1 = "key-A";
61 private static final String PAYLOAD_VALUE1 = "value-A";
62 private static final String MY_MESSAGE = "my-message";
63 protected static final String MY_VNF = "my-vnf";
64 protected static final String RESOURCE_ID = "my-resource";
65 private static final int SUCCESS_CODE = 400;
67 private AppcLcmDmaapWrapper response;
68 private AppcLcmOperation oper;
77 response = makeResponse();
79 oper = new AppcLcmOperation(params, config);
83 public void testConstructor() {
84 assertEquals(DEFAULT_ACTOR, oper.getActorName());
85 assertEquals(DEFAULT_OPERATION, oper.getName());
87 // missing target entity
88 params = params.toBuilder().targetEntity("").build();
89 assertThatIllegalArgumentException().isThrownBy(() -> new AppcLcmOperation(params, config))
90 .withMessage("missing targetEntity");
94 public void testStartPreprocessorAsync() throws Exception {
95 context = mock(ControlLoopEventContext.class);
96 when(context.getEvent()).thenReturn(event);
97 params = params.toBuilder().context(context).build();
99 AtomicBoolean guardStarted = new AtomicBoolean();
101 oper = new AppcLcmOperation(params, config) {
103 protected CompletableFuture<OperationOutcome> startGuardAsync() {
104 guardStarted.set(true);
105 return super.startGuardAsync();
109 CompletableFuture<OperationOutcome> future2 = oper.startPreprocessorAsync();
110 assertNotNull(future2);
111 assertFalse(future.isDone());
112 assertTrue(guardStarted.get());
114 assertTrue(executor.runAll(100));
115 assertTrue(future2.isDone());
116 assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
120 public void testMakeRequest() {
121 AppcLcmDmaapWrapper request = oper.makeRequest(2);
122 assertEquals("DefaultOperation", request.getBody().getInput().getAction());
124 AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
125 assertNotNull(header);
126 assertEquals(params.getRequestId(), header.getRequestId());
128 String subreq = header.getSubRequestId();
129 assertNotNull(subreq);
131 assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
133 // a subsequent request should have a different sub-request id
134 assertNotEquals(subreq, oper.makeRequest(2).getBody().getInput().getCommonHeader().getSubRequestId());
138 public void testConvertPayload() {
139 // only builds a payload for ConfigModify
140 params = params.toBuilder().operation(AppcLcmConstants.OPERATION_CONFIG_MODIFY).build();
141 oper = new AppcLcmOperation(params, config);
143 AppcLcmDmaapWrapper req = oper.makeRequest(2);
144 assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
147 oper = new AppcLcmOperation(params, config) {
149 protected Coder makeCoder() {
150 return new StandardCoder() {
152 public String encode(Object object) throws CoderException {
153 throw new CoderException(EXPECTED_EXCEPTION);
159 assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
160 .withMessage("Cannot convert payload");
164 public void testGetExpectedKeyValues() {
165 AppcLcmDmaapWrapper request = oper.makeRequest(2);
166 assertEquals(Arrays.asList(request.getBody().getInput().getCommonHeader().getSubRequestId()),
167 oper.getExpectedKeyValues(50, request));
171 public void testDetmStatus() {
172 assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
175 response.getBody().getOutput().getStatus().setCode(405);
176 assertEquals(Status.FAILURE, oper.detmStatus(null, response));
179 response.getBody().getOutput().getStatus().setCode(200);
180 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
183 response.getBody().getOutput().getStatus().setCode(305);
184 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
187 response.getBody().getOutput().getStatus().setCode(100);
188 assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
191 response.getBody().getOutput().getStatus().setCode(-1);
192 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
195 response.getBody().getOutput().setStatus(null);
196 assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
200 public void testSetOutcome() {
201 oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
202 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
203 assertEquals(MY_MESSAGE, outcome.getMessage());
206 oper.setOutcome(outcome, PolicyResult.FAILURE, response);
207 assertEquals(PolicyResult.FAILURE, outcome.getResult());
208 assertEquals(MY_MESSAGE, outcome.getMessage());
211 response.getBody().getOutput().getStatus().setMessage(null);
212 oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
213 assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
216 response.getBody().getOutput().setStatus(null);
217 oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
218 assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
222 public void testGetStatus() {
223 assertNotNull(oper.getStatus(response));
226 response.getBody().getOutput().setStatus(null);
227 assertNull(oper.getStatus(response));
230 response.getBody().setOutput(null);
231 assertNull(oper.getStatus(response));
234 response.setBody(null);
235 assertNull(oper.getStatus(response));
238 assertNull(oper.getStatus(null));
242 public void testOperationSupportsPayload() {
243 // these should support a payload
244 Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
246 for (String name : supported) {
247 params = params.toBuilder().operation(name).build();
248 oper = new AppcLcmOperation(params, config);
249 assertTrue(name, oper.operationSupportsPayload());
252 // these should NOT support a payload
253 Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
254 .collect(Collectors.toSet());
256 for (String name : unsupported) {
257 params = params.toBuilder().operation(name).build();
258 oper = new AppcLcmOperation(params, config);
259 assertFalse(name, oper.operationSupportsPayload());
262 // pick an operation that would ordinarily support payloads
263 String sup = supported.iterator().next();
265 // verify that it still supports payload
266 params = params.toBuilder().operation(sup).build();
267 oper = new AppcLcmOperation(params, config);
268 assertTrue(oper.operationSupportsPayload());
270 // try with empty payload
271 params = params.toBuilder().payload(Map.of()).build();
272 oper = new AppcLcmOperation(params, config);
273 assertFalse(oper.operationSupportsPayload());
275 // try with null payload
276 params = params.toBuilder().payload(null).build();
277 oper = new AppcLcmOperation(params, config);
278 assertFalse(oper.operationSupportsPayload());
282 protected void makeContext() {
285 Target target = new Target();
286 target.setResourceID(RESOURCE_ID);
288 params = params.toBuilder().target(target).build();
292 protected Map<String, Object> makePayload() {
293 return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
296 private AppcLcmDmaapWrapper makeResponse() {
297 AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
299 AppcLcmBody body = new AppcLcmBody();
300 response.setBody(body);
302 AppcLcmOutput output = new AppcLcmOutput();
303 body.setOutput(output);
305 AppcLcmResponseStatus status = new AppcLcmResponseStatus();
306 output.setStatus(status);
307 status.setMessage(MY_MESSAGE);
308 status.setCode(SUCCESS_CODE);