Fix broken models build due to json upgrade
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / SoActorServiceProviderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * TestSOActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018-2019 AT&T. All rights reserved.
8  * Modifications Copyright (C) 2019 Nordix Foundation.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.controlloop.actor.so;
25
26 import static org.assertj.core.api.Assertions.assertThatCode;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import java.util.Arrays;
34 import java.util.LinkedList;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.TreeMap;
38 import java.util.UUID;
39 import java.util.stream.Collectors;
40 import org.apache.commons.io.IOUtils;
41 import org.junit.Test;
42 import org.onap.policy.aai.AaiCqResponse;
43 import org.onap.policy.controlloop.ControlLoopOperation;
44 import org.onap.policy.controlloop.VirtualControlLoopEvent;
45 import org.onap.policy.controlloop.actor.test.BasicActor;
46 import org.onap.policy.controlloop.policy.Policy;
47 import org.onap.policy.controlloop.policy.Target;
48 import org.onap.policy.so.SoOperationType;
49 import org.onap.policy.so.SoRequest;
50 import org.onap.policy.so.SoRequestParameters;
51 import org.onap.policy.so.util.Serialization;
52
53 public class SoActorServiceProviderTest extends BasicActor {
54
55     private static final String C_VALUE = "cvalue";
56     private static final String A_VALUE = "avalue";
57     private static final String VF_MODULE_CREATE = "VF Module Create";
58     private static final String VF_MODULE_DELETE = "VF Module Delete";
59
60     private void instantiateTargetCq(Policy policy) {
61
62         Target target = new Target();
63         target.setModelCustomizationId("47958575-138f-452a-8c8d-d89b595f8164");
64         target.setModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
65         target.setModelName("VfwclVfwsnkBbefb8ce2bde..base_vfw..module-0");
66         target.setModelVersion("1");
67         target.setModelVersionId("94b18b1d-cc91-4f43-911a-e6348665f292");
68
69         policy.setTarget(target);
70     }
71
72     @Test
73     public void testConstructor() {
74         SoActorServiceProvider prov = new SoActorServiceProvider();
75
76         // verify that it has the operators we expect
77         var expected = Arrays.asList(VfModuleCreate.NAME, VfModuleDelete.NAME).stream().sorted()
78                         .collect(Collectors.toList());
79         var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
80
81         assertEquals(expected.toString(), actual.toString());
82     }
83
84     @Test
85     public void testActorService() {
86         // verify that it all plugs into the ActorService
87         verifyActorService(SoActorServiceProvider.NAME, "service.yaml");
88     }
89
90     @Test
91     public void testSendRequest() {
92         assertThatCode(() -> SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null,
93                         null)).doesNotThrowAnyException();
94     }
95
96     @Test
97     public void testMethods() {
98         SoActorServiceProvider sp = new SoActorServiceProvider();
99
100         assertEquals("SO", sp.actor());
101         assertEquals(2, sp.recipes().size());
102         assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
103         assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
104         assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
105         assertEquals(0, sp.recipeTargets("unknown recipe").size());
106         assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
107     }
108
109     @Test
110     public void testConstructRequestCq() throws Exception {
111         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
112         final ControlLoopOperation operation = new ControlLoopOperation();
113         final AaiCqResponse aaiCqResp = loadAaiResponseCq("aai/AaiCqResponseFull.json");
114         final AaiCqResponse aaiCqRespMissing = loadAaiResponseCq("aai/AaiCqResponseMissing.json");
115         final UUID requestId = UUID.randomUUID();
116         onset.setRequestId(requestId);
117
118         Policy policy = new Policy();
119         policy.setActor("Dorothy");
120         policy.setRecipe("GoToOz");
121
122         instantiateTargetCq(policy);
123
124         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp));
125
126         policy.setActor("SO");
127
128         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
129
130         policy.setRecipe(VF_MODULE_CREATE);
131
132         // empty policy payload
133         SoRequest request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
134         assertNotNull(request);
135
136         assertEquals("vfModuleName", request.getRequestDetails().getRequestInfo().getInstanceName());
137         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
138         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
139
140         // non-empty policy payload
141         policy.setPayload(makePayload());
142         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
143         assertNotNull(request);
144         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
145         assertEquals(A_VALUE, request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
146         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
147         assertEquals(C_VALUE, request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
148
149         // payload with config, but no request params
150         policy.setPayload(makePayload());
151         policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
152         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
153         assertNotNull(request);
154         assertNull(request.getRequestDetails().getRequestParameters());
155         assertNotNull(request.getRequestDetails().getConfigurationParameters());
156
157         // payload with request, but no config params
158         policy.setPayload(makePayload());
159         policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
160         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
161         assertNotNull(request);
162         assertNotNull(request.getRequestDetails().getRequestParameters());
163         assertNull(request.getRequestDetails().getConfigurationParameters());
164
165         // null response
166         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
167
168         instantiateTargetCq(policy);
169         policy.setRecipe(VF_MODULE_DELETE);
170         SoRequest deleteRequest = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
171         assertNotNull(deleteRequest);
172         assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
173
174         /*
175          * NOTE: The remaining tests must be done in order
176          */
177
178         policy.setRecipe(VF_MODULE_CREATE);
179
180         // null tenant
181         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
182
183         // null service item
184         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
185
186         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
187     }
188
189     /**
190      * Reads an AAI vserver named-query response from a file.
191      *
192      * @param fileName name of the file containing the JSON response
193      * @return output from the AAI vserver named-query
194      * @throws IOException if the file cannot be read
195      */
196     private AaiCqResponse loadAaiResponseCq(String fileName) throws IOException {
197         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
198         return new AaiCqResponse(resp);
199     }
200
201
202
203     /**
204      * Creates a policy payload containing request & configuration parameters.
205      *
206      * @return the payload
207      */
208     private Map<String, String> makePayload() {
209         Map<String, String> payload = new TreeMap<>();
210
211         payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
212         payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
213
214         return payload;
215     }
216
217     /**
218      * Creates request parameters.
219      *
220      * @return request parameters, encoded as JSON
221      */
222     private String makeReqParams() {
223         SoRequestParameters params = new SoRequestParameters();
224
225         params.setUsePreload(true);
226
227         Map<String, String> map = new TreeMap<>();
228         map.put("akey", A_VALUE);
229
230         List<Map<String, String>> lst = new LinkedList<>();
231         lst.add(map);
232
233         params.setUserParams(lst);
234
235         return Serialization.gsonPretty.toJson(params);
236     }
237
238     /**
239      * Creates configuration parameters.
240      *
241      * @return configuration parameters, encoded as JSON
242      */
243     private String makeConfigParams() {
244         Map<String, String> map = new TreeMap<>();
245         map.put("ckey", C_VALUE);
246
247         List<Map<String, String>> lst = new LinkedList<>();
248         lst.add(map);
249
250         return Serialization.gsonPretty.toJson(lst);
251     }
252
253 }