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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.policy.controlloop.actor.so;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.fail;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import java.util.LinkedList;
34 import java.util.List;
36 import java.util.TreeMap;
37 import java.util.UUID;
38 import org.apache.commons.io.IOUtils;
39 import org.junit.Test;
40 import org.onap.policy.aai.AaiNqResponse;
41 import org.onap.policy.aai.AaiNqResponseWrapper;
42 import org.onap.policy.controlloop.ControlLoopOperation;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.policy.Policy;
45 import org.onap.policy.controlloop.policy.Target;
46 import org.onap.policy.so.SoOperationType;
47 import org.onap.policy.so.SoRequest;
48 import org.onap.policy.so.SoRequestParameters;
49 import org.onap.policy.so.util.Serialization;
51 public class SoActorServiceProviderTest {
53 private static final String VF_MODULE_CREATE = "VF Module Create";
54 private static final String VF_MODULE_DELETE = "VF Module Delete";
57 public void testConstructRequest() throws Exception {
58 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
59 final ControlLoopOperation operation = new ControlLoopOperation();
60 final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
62 final UUID requestId = UUID.randomUUID();
63 onset.setRequestId(requestId);
65 Policy policy = new Policy();
66 policy.setActor("Dorothy");
67 policy.setRecipe("GoToOz");
69 instantiateTarget(policy);
71 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
73 policy.setActor("SO");
74 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
76 policy.setRecipe(VF_MODULE_CREATE);
78 // empty policy payload
79 SoRequest request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
80 assertNotNull(request);
82 assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
83 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
84 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
86 // non-empty policy payload
87 policy.setPayload(makePayload());
88 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
89 assertNotNull(request);
90 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
91 assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
92 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
93 assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
95 // payload with config, but no request params
96 policy.setPayload(makePayload());
97 policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
98 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
99 assertNotNull(request);
100 assertNull(request.getRequestDetails().getRequestParameters());
101 assertNotNull(request.getRequestDetails().getConfigurationParameters());
103 // payload with request, but no config params
104 policy.setPayload(makePayload());
105 policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
106 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
107 assertNotNull(request);
108 assertNotNull(request.getRequestDetails().getRequestParameters());
109 assertNull(request.getRequestDetails().getConfigurationParameters());
112 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, null));
114 // response has no base VF module
115 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
116 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
118 policy.setTarget(null);
120 // response has no non-base VF modules (other than the "dummy")
121 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
122 loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
124 instantiateTarget(policy);
125 policy.setRecipe(VF_MODULE_DELETE);
126 SoRequest deleteRequest = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
127 assertNotNull(deleteRequest);
128 assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
131 * NOTE: The remaining tests must be done in order
134 policy.setRecipe(VF_MODULE_CREATE);
137 aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems()
139 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
142 aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
144 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
147 aaiNqResp.setAaiNqResponse(null);
148 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
151 private void instantiateTarget(Policy policy) {
153 Target target = new Target();
154 target.setModelCustomizationId("3e2d67ad-3495-4732-82f6-b0b872791fff");
155 target.setModelInvariantId("90b793b5-b8ae-4c36-b10b-4b6372859d3a");
156 target.setModelName("SproutScalingVf..scaling_sprout..module-1");
157 target.setModelVersion("1");
158 target.setModelVersionId("2210154d-e61a-4d7f-8fb9-0face1aee3f8");
160 policy.setTarget(target);
164 public void testSendRequest() {
166 SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null, null);
167 } catch (Exception e) {
168 fail("Test should not throw an exception");
173 public void testMethods() {
174 SoActorServiceProvider sp = new SoActorServiceProvider();
176 assertEquals("SO", sp.actor());
177 assertEquals(2, sp.recipes().size());
178 assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
179 assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
180 assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
181 assertEquals(0, sp.recipeTargets("unknown recipe").size());
182 assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
186 * Creates a policy payload containing request & configuration parameters.
188 * @return the payload
190 private Map<String, String> makePayload() {
191 Map<String, String> payload = new TreeMap<>();
193 payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
194 payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
200 * Creates request parameters.
202 * @return request parameters, encoded as JSON
204 private String makeReqParams() {
205 SoRequestParameters params = new SoRequestParameters();
207 params.setUsePreload(true);
209 Map<String, String> map = new TreeMap<>();
210 map.put("akey", "avalue");
212 List<Map<String, String>> lst = new LinkedList<>();
215 params.setUserParams(lst);
217 return Serialization.gsonPretty.toJson(params);
221 * Creates configuration parameters.
223 * @return configuration parameters, encoded as JSON
225 private String makeConfigParams() {
226 Map<String, String> map = new TreeMap<>();
227 map.put("ckey", "cvalue");
229 List<Map<String, String>> lst = new LinkedList<>();
232 return Serialization.gsonPretty.toJson(lst);
236 * Reads an AAI vserver named-query response from a file.
238 * @param onset the ONSET event
239 * @param fileName name of the file containing the JSON response
240 * @return output from the AAI vserver named-query
241 * @throws IOException if the file cannot be read
243 private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
244 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
245 AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
247 return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);