Test new actors against simulators
[policy/models.git] / models-interactions / model-actors / actor.appc / src / test / java / org / onap / policy / controlloop / actor / appc / AppcOperationTest.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.appc;
22
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertSame;
28
29 import java.util.Arrays;
30 import java.util.Map;
31 import java.util.TreeMap;
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.appc.CommonHeader;
39 import org.onap.policy.appc.Request;
40 import org.onap.policy.appc.ResponseCode;
41 import org.onap.policy.appc.ResponseStatus;
42 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation.Status;
43 import org.onap.policy.controlloop.policy.PolicyResult;
44
45 public class AppcOperationTest extends BasicAppcOperation {
46     private AppcOperation oper;
47
48     @BeforeClass
49     public static void setUpBeforeClass() throws Exception {
50         // use same topic name for both sides
51         initBeforeClass(MY_SINK, MY_SINK);
52     }
53
54     @AfterClass
55     public static void tearDownAfterClass() {
56         destroyAfterClass();
57     }
58
59     /**
60      * Sets up.
61      */
62     @Before
63     @Override
64     public void setUp() throws Exception {
65         super.setUp();
66
67         oper = new AppcOperation(params, config) {
68             @Override
69             protected Pair<String, Request> makeRequest(int attempt) {
70                 return oper.makeRequest(attempt, MY_VNF);
71             }
72         };
73     }
74
75     @After
76     @Override
77     public void tearDown() {
78         super.tearDown();
79     }
80
81     @Test
82     public void testConstructor() {
83         assertEquals(DEFAULT_ACTOR, oper.getActorName());
84         assertEquals(DEFAULT_OPERATION, oper.getName());
85     }
86
87     @Test
88     public void testStartPreprocessorAsync() {
89         assertNotNull(oper.startPreprocessorAsync());
90     }
91
92     @Test
93     public void testMakeRequest() {
94         Pair<String, Request> result = oper.makeRequest(2, MY_VNF);
95         String subreq = result.getLeft();
96         assertNotNull(subreq);
97
98         Request request = result.getRight();
99         assertEquals(DEFAULT_OPERATION, request.getAction());
100
101         assertNotNull(request.getPayload());
102
103         CommonHeader header = request.getCommonHeader();
104         assertNotNull(header);
105         assertEquals(params.getRequestId(), header.getRequestId());
106
107         assertEquals(subreq, header.getSubRequestId());
108
109         // a subsequent request should have a different sub-request id
110         result = oper.makeRequest(2, MY_VNF);
111         assertNotEquals(subreq, result.getLeft());
112
113         assertNotNull(result.getLeft());
114         assertEquals(result.getLeft(), result.getRight().getCommonHeader().getSubRequestId());
115
116         // repeat using a null payload
117         params = params.toBuilder().payload(null).build();
118         oper = new AppcOperation(params, config) {
119             @Override
120             protected Pair<String, Request> makeRequest(int attempt) {
121                 return oper.makeRequest(attempt, MY_VNF);
122             }
123         };
124         assertEquals(Map.of(AppcOperation.VNF_ID_KEY, MY_VNF), oper.makeRequest(2, MY_VNF).getRight().getPayload());
125     }
126
127     @Test
128     public void testConvertPayload() {
129         Request request = oper.makeRequest(2, MY_VNF).getRight();
130
131         // @formatter:off
132         assertEquals(
133             Map.of(AppcOperation.VNF_ID_KEY, MY_VNF,
134                     KEY1, Map.of("input", "hello"),
135                     KEY2, Map.of("output", "world")),
136             request.getPayload());
137         // @formatter:on
138
139
140         /*
141          * insert invalid json text into the payload.
142          */
143         Map<String, Object> payload = new TreeMap<>(params.getPayload());
144         payload.put("invalid-key", "{invalid json");
145
146         params = params.toBuilder().payload(payload).build();
147
148         oper = new AppcOperation(params, config) {
149             @Override
150             protected Pair<String, Request> makeRequest(int attempt) {
151                 return oper.makeRequest(attempt, MY_VNF);
152             }
153         };
154         request = oper.makeRequest(2, MY_VNF).getRight();
155
156         // @formatter:off
157         assertEquals(
158             Map.of(AppcOperation.VNF_ID_KEY, MY_VNF,
159                     KEY1, Map.of("input", "hello"),
160                     KEY2, Map.of("output", "world")),
161             request.getPayload());
162         // @formatter:on
163
164
165         /*
166          * insert null item into the payload.
167          */
168         payload = new TreeMap<>();
169         payload.put(KEY1, "abc");
170         payload.put(KEY2, null);
171         payload.put(KEY3, "def");
172         params = params.toBuilder().payload(payload).build();
173
174         oper = new AppcOperation(params, config) {
175             @Override
176             protected Pair<String, Request> makeRequest(int attempt) {
177                 return oper.makeRequest(attempt, MY_VNF);
178             }
179         };
180         request = oper.makeRequest(2, MY_VNF).getRight();
181
182         payload.put(AppcOperation.VNF_ID_KEY, MY_VNF);
183         payload.put(KEY1, "abc");
184         payload.put(KEY2, null);
185         payload.put(KEY3, "def");
186
187         assertEquals(payload, request.getPayload());
188     }
189
190     @Test
191     public void testGetExpectedKeyValues() {
192         Request request = oper.makeRequest(2, MY_VNF).getRight();
193         assertEquals(Arrays.asList(request.getCommonHeader().getSubRequestId()),
194                         oper.getExpectedKeyValues(50, request));
195     }
196
197     @Test
198     public void testDetmStatusStringResponse() {
199         final ResponseStatus status = response.getStatus();
200
201         // null status (i.e., it's a Request, not a Response)
202         response.setStatus(null);
203         assertEquals(Status.STILL_WAITING, oper.detmStatus("", response));
204         response.setStatus(status);
205
206         // invalid code
207         status.setCode(-45);
208         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus("", response))
209                         .withMessage("unknown APPC-C response status code: -45");
210
211         status.setCode(ResponseCode.SUCCESS.getValue());
212         assertEquals(Status.SUCCESS, oper.detmStatus("", response));
213
214         status.setCode(ResponseCode.FAILURE.getValue());
215         assertEquals(Status.FAILURE, oper.detmStatus("", response));
216
217         status.setCode(ResponseCode.ERROR.getValue());
218         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus("", response))
219                         .withMessage("APP-C request was not accepted, code=" + ResponseCode.ERROR.getValue());
220
221         status.setCode(ResponseCode.REJECT.getValue());
222         assertThatIllegalArgumentException().isThrownBy(() -> oper.detmStatus("", response))
223                         .withMessage("APP-C request was not accepted, code=" + ResponseCode.REJECT.getValue());
224
225         status.setCode(ResponseCode.ACCEPT.getValue());
226         assertEquals(Status.STILL_WAITING, oper.detmStatus("", response));
227     }
228
229     @Test
230     public void testSetOutcome() {
231         final ResponseStatus status = response.getStatus();
232
233         // null status
234         response.setStatus(null);
235         assertSame(outcome, oper.setOutcome(outcome, PolicyResult.SUCCESS, response));
236         assertEquals(PolicyResult.SUCCESS, outcome.getResult());
237         assertNotNull(outcome.getMessage());
238         response.setStatus(status);
239
240         // null description
241         status.setDescription(null);
242         assertSame(outcome, oper.setOutcome(outcome, PolicyResult.FAILURE, response));
243         assertEquals(PolicyResult.FAILURE, outcome.getResult());
244         assertNotNull(outcome.getMessage());
245         status.setDescription(MY_DESCRIPTION);
246
247         for (PolicyResult result : PolicyResult.values()) {
248             assertSame(outcome, oper.setOutcome(outcome, result, response));
249             assertEquals(result, outcome.getResult());
250             assertEquals(MY_DESCRIPTION, outcome.getMessage());
251         }
252     }
253 }