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