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.so.SoOperationType;
46 import org.onap.policy.so.SoRequest;
47 import org.onap.policy.so.SoRequestParameters;
48 import org.onap.policy.so.util.Serialization;
50 public class SoActorServiceProviderTest {
52 private static final String VF_MODULE_CREATE = "VF Module Create";
53 private static final String VF_MODULE_DELETE = "VF Module Delete";
56 public void testConstructRequest() throws Exception {
57 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
58 final ControlLoopOperation operation = new ControlLoopOperation();
59 final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
61 final UUID requestId = UUID.randomUUID();
62 onset.setRequestId(requestId);
64 Policy policy = new Policy();
65 policy.setActor("Dorothy");
66 policy.setRecipe("GoToOz");
68 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
70 policy.setActor("SO");
71 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
73 policy.setRecipe(VF_MODULE_CREATE);
75 // empty policy payload
76 SoRequest request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
77 assertNotNull(request);
79 assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
80 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
81 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
83 // non-empty policy payload
84 policy.setPayload(makePayload());
85 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
86 assertNotNull(request);
87 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
88 assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
89 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
90 assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
92 // payload with config, but no request params
93 policy.setPayload(makePayload());
94 policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
95 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
96 assertNotNull(request);
97 assertNull(request.getRequestDetails().getRequestParameters());
98 assertNotNull(request.getRequestDetails().getConfigurationParameters());
100 // payload with request, but no config params
101 policy.setPayload(makePayload());
102 policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
103 request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
104 assertNotNull(request);
105 assertNotNull(request.getRequestDetails().getRequestParameters());
106 assertNull(request.getRequestDetails().getConfigurationParameters());
109 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, null));
111 // response has no base VF module
112 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
113 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
115 // response has no non-base VF modules (other than the "dummy")
116 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
117 loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
119 policy.setRecipe(VF_MODULE_DELETE);
120 SoRequest deleteRequest = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
121 assertNotNull(deleteRequest);
122 assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
125 * NOTE: The remaining tests must be done in order
128 policy.setRecipe(VF_MODULE_CREATE);
131 aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems()
133 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
136 aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
138 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
141 aaiNqResp.setAaiNqResponse(null);
142 assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
146 public void testSendRequest() {
148 SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
149 } catch (Exception e) {
150 fail("Test should not throw an exception");
155 public void testMethods() {
156 SoActorServiceProvider sp = new SoActorServiceProvider();
158 assertEquals("SO", sp.actor());
159 assertEquals(2, sp.recipes().size());
160 assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
161 assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
162 assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
163 assertEquals(0, sp.recipeTargets("unknown recipe").size());
164 assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
168 * Creates a policy payload containing request & configuration parameters.
170 * @return the payload
172 private Map<String, String> makePayload() {
173 Map<String, String> payload = new TreeMap<>();
175 payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
176 payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
182 * Creates request parameters.
184 * @return request parameters, encoded as JSON
186 private String makeReqParams() {
187 SoRequestParameters params = new SoRequestParameters();
189 params.setUsePreload(true);
191 Map<String, String> map = new TreeMap<>();
192 map.put("akey", "avalue");
194 List<Map<String, String>> lst = new LinkedList<>();
197 params.setUserParams(lst);
199 return Serialization.gsonPretty.toJson(params);
203 * Creates configuration parameters.
205 * @return configuration parameters, encoded as JSON
207 private String makeConfigParams() {
208 Map<String, String> map = new TreeMap<>();
209 map.put("ckey", "cvalue");
211 List<Map<String, String>> lst = new LinkedList<>();
214 return Serialization.gsonPretty.toJson(lst);
218 * Reads an AAI vserver named-query response from a file.
220 * @param onset the ONSET event
221 * @param fileName name of the file containing the JSON response
222 * @return output from the AAI vserver named-query
223 * @throws IOException if the file cannot be read
225 private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
226 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
227 AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
229 return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);