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 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.controlloop.actor.so;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.fail;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.LinkedList;
33 import java.util.List;
35 import java.util.TreeMap;
36 import java.util.UUID;
37 import org.apache.commons.io.IOUtils;
38 import org.junit.Test;
39 import org.onap.policy.aai.AaiNqResponse;
40 import org.onap.policy.aai.AaiNqResponseWrapper;
41 import org.onap.policy.controlloop.ControlLoopOperation;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.policy.Policy;
44 import org.onap.policy.so.SoOperationType;
45 import org.onap.policy.so.SoRequest;
46 import org.onap.policy.so.SoRequestParameters;
47 import org.onap.policy.so.util.Serialization;
49 public class SoActorServiceProviderTest {
51 private static final String VF_MODULE_CREATE = "VF Module Create";
52 private static final String VF_MODULE_DELETE = "VF Module Delete";
55 public void testConstructRequest() throws Exception {
56 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
57 final ControlLoopOperation operation = new ControlLoopOperation();
58 final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
60 final UUID requestId = UUID.randomUUID();
61 onset.setRequestId(requestId);
63 Policy policy = new Policy();
64 policy.setActor("Dorothy");
65 policy.setRecipe("GoToOz");
67 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
69 policy.setActor("SO");
70 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
72 policy.setRecipe(VF_MODULE_CREATE);
74 // empty policy payload
75 SoRequest request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
76 assertNotNull(request);
78 assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
79 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
80 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
82 // non-empty policy payload
83 policy.setPayload(makePayload());
84 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
85 assertNotNull(request);
86 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
87 assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
88 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
89 assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
91 // payload with config, but no request params
92 policy.setPayload(makePayload());
93 policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
94 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
95 assertNotNull(request);
96 assertNull(request.getRequestDetails().getRequestParameters());
97 assertNotNull(request.getRequestDetails().getConfigurationParameters());
99 // payload with request, but no config params
100 policy.setPayload(makePayload());
101 policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
102 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
103 assertNotNull(request);
104 assertNotNull(request.getRequestDetails().getRequestParameters());
105 assertNull(request.getRequestDetails().getConfigurationParameters());
108 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, null));
110 // response has no base VF module
111 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
112 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
114 // response has no non-base VF modules (other than the "dummy")
115 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
116 loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
118 policy.setRecipe(VF_MODULE_DELETE);
119 SoRequest deleteRequest = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
120 assertNotNull(deleteRequest);
121 assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
124 * NOTE: The remaining tests must be done in order
127 policy.setRecipe(VF_MODULE_CREATE);
130 aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems()
132 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
135 aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
137 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
140 aaiNqResp.setAaiNqResponse(null);
141 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
145 public void testSendRequest() {
147 SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
148 } catch (Exception e) {
149 fail("Test should not throw an exception");
154 public void testMethods() {
155 SoActorServiceProvider sp = new SoActorServiceProvider();
157 assertEquals("SO", sp.actor());
158 assertEquals(2, sp.recipes().size());
159 assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
160 assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
161 assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
162 assertEquals(0, sp.recipeTargets("unknown recipe").size());
163 assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
167 * Creates a policy payload containing request & configuration parameters.
169 * @return the payload
171 private Map<String, String> makePayload() {
172 Map<String, String> payload = new TreeMap<>();
174 payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
175 payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
181 * Creates request parameters.
183 * @return request parameters, encoded as JSON
185 private String makeReqParams() {
186 SoRequestParameters params = new SoRequestParameters();
188 params.setUsePreload(true);
190 Map<String, String> map = new TreeMap<>();
191 map.put("akey", "avalue");
193 List<Map<String, String>> lst = new LinkedList<>();
196 params.setUserParams(lst);
198 return Serialization.gsonPretty.toJson(params);
202 * Creates configuration parameters.
204 * @return configuration parameters, encoded as JSON
206 private String makeConfigParams() {
207 Map<String, String> map = new TreeMap<>();
208 map.put("ckey", "cvalue");
210 List<Map<String, String>> lst = new LinkedList<>();
213 return Serialization.gsonPretty.toJson(lst);
217 * Reads an AAI vserver named-query response from a file.
219 * @param onset the ONSET event
220 * @param fileName name of the file containing the JSON response
221 * @return output from the AAI vserver named-query
222 * @throws IOException if the file cannot be read
224 private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
225 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
226 AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
228 return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);