2 * ============LICENSE_START=======================================================
3 * TestSOActorServiceProvider
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson. All rights reserved.
6 * Modifications Copyright (C) 2018 AT&T. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.so;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.fail;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.util.LinkedList;
32 import java.util.List;
34 import java.util.TreeMap;
35 import java.util.UUID;
36 import org.apache.commons.io.IOUtils;
37 import org.junit.Test;
38 import org.onap.policy.aai.AaiNqResponse;
39 import org.onap.policy.aai.AaiNqResponseWrapper;
40 import org.onap.policy.controlloop.ControlLoopOperation;
41 import org.onap.policy.controlloop.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.policy.Policy;
43 import org.onap.policy.so.SORequest;
44 import org.onap.policy.so.SORequestParameters;
45 import org.onap.policy.so.SoOperationType;
46 import org.onap.policy.so.util.Serialization;
48 public class SoActorServiceProviderTest {
50 private static final String VF_MODULE_CREATE = "VF Module Create";
51 private static final String VF_MODULE_DELETE = "VF Module Delete";
54 public void testConstructRequest() throws Exception {
55 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
56 final ControlLoopOperation operation = new ControlLoopOperation();
57 final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
59 final UUID requestId = UUID.randomUUID();
60 onset.setRequestId(requestId);
62 Policy policy = new Policy();
63 policy.setActor("Dorothy");
64 policy.setRecipe("GoToOz");
66 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
68 policy.setActor("SO");
69 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
71 policy.setRecipe(VF_MODULE_CREATE);
73 // empty policy payload
74 SORequest request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
75 assertNotNull(request);
77 assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
78 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
79 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
81 // non-empty policy payload
82 policy.setPayload(makePayload());
83 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
84 assertNotNull(request);
85 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
86 assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
87 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
88 assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
90 // payload with config, but no request params
91 policy.setPayload(makePayload());
92 policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
93 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
94 assertNotNull(request);
95 assertNull(request.getRequestDetails().getRequestParameters());
96 assertNotNull(request.getRequestDetails().getConfigurationParameters());
98 // payload with request, but no config params
99 policy.setPayload(makePayload());
100 policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
101 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
102 assertNotNull(request);
103 assertNotNull(request.getRequestDetails().getRequestParameters());
104 assertNull(request.getRequestDetails().getConfigurationParameters());
107 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, null));
109 // response has no base VF module
110 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
111 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
113 // response has no non-base VF modules (other than the "dummy")
114 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
115 loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
117 policy.setRecipe(VF_MODULE_DELETE);
118 SORequest deleteRequest = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
119 assertNotNull(deleteRequest);
120 assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
123 * NOTE: The remaining tests must be done in order
126 policy.setRecipe(VF_MODULE_CREATE);
129 aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems()
131 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
134 aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
136 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
139 aaiNqResp.setAaiNqResponse(null);
140 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
144 public void testSendRequest() {
146 SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
147 } catch (Exception e) {
148 fail("Test should not throw an exception");
153 public void testMethods() {
154 SoActorServiceProvider sp = new SoActorServiceProvider();
156 assertEquals("SO", sp.actor());
157 assertEquals(2, sp.recipes().size());
158 assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
159 assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
160 assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
161 assertEquals(0, sp.recipeTargets("unknown recipe").size());
162 assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
166 * Creates a policy payload containing request & configuration parameters.
168 * @return the payload
170 private Map<String, String> makePayload() {
171 Map<String, String> payload = new TreeMap<>();
173 payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
174 payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
180 * Creates request parameters.
182 * @return request parameters, encoded as JSON
184 private String makeReqParams() {
185 SORequestParameters params = new SORequestParameters();
187 params.setUsePreload(true);
189 Map<String, String> map = new TreeMap<>();
190 map.put("akey", "avalue");
192 List<Map<String, String>> lst = new LinkedList<>();
195 params.setUserParams(lst);
197 return Serialization.gsonPretty.toJson(params);
201 * Creates configuration parameters.
203 * @return configuration parameters, encoded as JSON
205 private String makeConfigParams() {
206 Map<String, String> map = new TreeMap<>();
207 map.put("ckey", "cvalue");
209 List<Map<String, String>> lst = new LinkedList<>();
212 return Serialization.gsonPretty.toJson(lst);
216 * Reads an AAI vserver named-query response from a file.
218 * @param onset the ONSET event
219 * @param fileName name of the file containing the JSON response
220 * @return output from the AAI vserver named-query
221 * @throws IOException if the file cannot be read
223 private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
224 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
225 AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
227 return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);