Merge "TOSCA Compliant Guard Policies"
[policy/models.git] / models-interactions / model-actors / actor.appclcm / src / test / java / org / onap / policy / controlloop / actor / appclcm / AppcLcmOperationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.appclcm;
22
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;
32
33 import java.util.Arrays;
34 import java.util.Map;
35 import java.util.Set;
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;
56
57 public class AppcLcmOperationTest extends BasicBidirectionalTopicOperation {
58
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;
66
67     private AppcLcmDmaapWrapper response;
68     private AppcLcmOperation oper;
69
70     /**
71      * Sets up.
72      */
73     @Before
74     public void setUp() {
75         super.setUpBasic();
76
77         response = makeResponse();
78
79         oper = new AppcLcmOperation(params, config);
80     }
81
82     @Test
83     public void testConstructor() {
84         assertEquals(DEFAULT_ACTOR, oper.getActorName());
85         assertEquals(DEFAULT_OPERATION, oper.getName());
86
87         // missing target entity
88         params = params.toBuilder().targetEntity("").build();
89         assertThatIllegalArgumentException().isThrownBy(() -> new AppcLcmOperation(params, config))
90                         .withMessage("missing targetEntity");
91     }
92
93     @Test
94     public void testStartPreprocessorAsync() throws Exception {
95         context = mock(ControlLoopEventContext.class);
96         when(context.getEvent()).thenReturn(event);
97         params = params.toBuilder().context(context).build();
98
99         AtomicBoolean guardStarted = new AtomicBoolean();
100
101         oper = new AppcLcmOperation(params, config) {
102             @Override
103             protected CompletableFuture<OperationOutcome> startGuardAsync() {
104                 guardStarted.set(true);
105                 return super.startGuardAsync();
106             }
107         };
108
109         CompletableFuture<OperationOutcome> future2 = oper.startPreprocessorAsync();
110         assertNotNull(future2);
111         assertFalse(future.isDone());
112         assertTrue(guardStarted.get());
113
114         assertTrue(executor.runAll(100));
115         assertTrue(future2.isDone());
116         assertEquals(PolicyResult.SUCCESS, future2.get().getResult());
117     }
118
119     @Test
120     public void testMakeRequest() {
121         AppcLcmDmaapWrapper request = oper.makeRequest(2);
122         assertEquals("DefaultOperation", request.getBody().getInput().getAction());
123
124         AppcLcmCommonHeader header = request.getBody().getInput().getCommonHeader();
125         assertNotNull(header);
126         assertEquals(params.getRequestId(), header.getRequestId());
127
128         String subreq = header.getSubRequestId();
129         assertNotNull(subreq);
130
131         assertEquals("{vnf-id=my-target}", request.getBody().getInput().getActionIdentifiers().toString());
132
133         // a subsequent request should have a different sub-request id
134         assertNotEquals(subreq, oper.makeRequest(2).getBody().getInput().getCommonHeader().getSubRequestId());
135     }
136
137     @Test
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);
142
143         AppcLcmDmaapWrapper req = oper.makeRequest(2);
144         assertEquals("{\"key-A\":\"value-A\"}", req.getBody().getInput().getPayload());
145
146         // coder exception
147         oper = new AppcLcmOperation(params, config) {
148             @Override
149             protected Coder makeCoder() {
150                 return new StandardCoder() {
151                     @Override
152                     public String encode(Object object) throws CoderException {
153                         throw new CoderException(EXPECTED_EXCEPTION);
154                     }
155                 };
156             }
157         };
158
159         assertThatIllegalArgumentException().isThrownBy(() -> oper.makeRequest(2))
160                         .withMessage("Cannot convert payload");
161     }
162
163     @Test
164     public void testGetExpectedKeyValues() {
165         AppcLcmDmaapWrapper request = oper.makeRequest(2);
166         assertEquals(Arrays.asList(request.getBody().getInput().getCommonHeader().getSubRequestId()),
167                         oper.getExpectedKeyValues(50, request));
168     }
169
170     @Test
171     public void testDetmStatus() {
172         assertEquals(Status.SUCCESS, oper.detmStatus(null, response));
173
174         // failure
175         response.getBody().getOutput().getStatus().setCode(405);
176         assertEquals(Status.FAILURE, oper.detmStatus(null, response));
177
178         // error
179         response.getBody().getOutput().getStatus().setCode(200);
180         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
181
182         // reject
183         response.getBody().getOutput().getStatus().setCode(305);
184         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
185
186         // accepted
187         response.getBody().getOutput().getStatus().setCode(100);
188         assertEquals(Status.STILL_WAITING, oper.detmStatus(null, response));
189
190         // other
191         response.getBody().getOutput().getStatus().setCode(-1);
192         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
193
194         // null status
195         response.getBody().getOutput().setStatus(null);
196         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus(null, response));
197     }
198
199     @Test
200     public void testSetOutcome() {
201         oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
202         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
203         assertEquals(MY_MESSAGE, outcome.getMessage());
204
205         // failure
206         oper.setOutcome(outcome, PolicyResult.FAILURE, response);
207         assertEquals(PolicyResult.FAILURE, outcome.getResult());
208         assertEquals(MY_MESSAGE, outcome.getMessage());
209
210         // null message
211         response.getBody().getOutput().getStatus().setMessage(null);
212         oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
213         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
214
215         // null status
216         response.getBody().getOutput().setStatus(null);
217         oper.setOutcome(outcome, PolicyResult.SUCCESS, response);
218         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
219     }
220
221     @Test
222     public void testGetStatus() {
223         assertNotNull(oper.getStatus(response));
224
225         // null status
226         response.getBody().getOutput().setStatus(null);
227         assertNull(oper.getStatus(response));
228
229         // null outcome
230         response.getBody().setOutput(null);
231         assertNull(oper.getStatus(response));
232
233         // null body
234         response.setBody(null);
235         assertNull(oper.getStatus(response));
236
237         // null response
238         assertNull(oper.getStatus(null));
239     }
240
241     @Test
242     public void testOperationSupportsPayload() {
243         // these should support a payload
244         Set<String> supported = Set.of(AppcLcmConstants.OPERATION_CONFIG_MODIFY);
245
246         for (String name : supported) {
247             params = params.toBuilder().operation(name).build();
248             oper = new AppcLcmOperation(params, config);
249             assertTrue(name, oper.operationSupportsPayload());
250         }
251
252         // these should NOT support a payload
253         Set<String> unsupported = AppcLcmConstants.OPERATION_NAMES.stream().filter(name -> !supported.contains(name))
254                         .collect(Collectors.toSet());
255
256         for (String name : unsupported) {
257             params = params.toBuilder().operation(name).build();
258             oper = new AppcLcmOperation(params, config);
259             assertFalse(name, oper.operationSupportsPayload());
260         }
261
262         // pick an operation that would ordinarily support payloads
263         String sup = supported.iterator().next();
264
265         // verify that it still supports payload
266         params = params.toBuilder().operation(sup).build();
267         oper = new AppcLcmOperation(params, config);
268         assertTrue(oper.operationSupportsPayload());
269
270         // try with empty payload
271         params = params.toBuilder().payload(Map.of()).build();
272         oper = new AppcLcmOperation(params, config);
273         assertFalse(oper.operationSupportsPayload());
274
275         // try with null payload
276         params = params.toBuilder().payload(null).build();
277         oper = new AppcLcmOperation(params, config);
278         assertFalse(oper.operationSupportsPayload());
279     }
280
281     @Override
282     protected void makeContext() {
283         super.makeContext();
284
285         Target target = new Target();
286         target.setResourceID(RESOURCE_ID);
287
288         params = params.toBuilder().target(target).build();
289     }
290
291     @Override
292     protected Map<String, Object> makePayload() {
293         return Map.of(PAYLOAD_KEY1, PAYLOAD_VALUE1);
294     }
295
296     private AppcLcmDmaapWrapper makeResponse() {
297         AppcLcmDmaapWrapper response = new AppcLcmDmaapWrapper();
298
299         AppcLcmBody body = new AppcLcmBody();
300         response.setBody(body);
301
302         AppcLcmOutput output = new AppcLcmOutput();
303         body.setOutput(output);
304
305         AppcLcmResponseStatus status = new AppcLcmResponseStatus();
306         output.setStatus(status);
307         status.setMessage(MY_MESSAGE);
308         status.setCode(SUCCESS_CODE);
309
310         return response;
311     }
312 }