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 {
51 public void testConstructRequest() throws Exception {
52 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
53 final ControlLoopOperation operation = new ControlLoopOperation();
54 final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
56 final UUID requestId = UUID.randomUUID();
57 onset.setRequestId(requestId);
59 Policy policy = new Policy();
60 policy.setActor("Dorothy");
61 policy.setRecipe("GoToOz");
62 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
64 policy.setActor("SO");
65 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
67 policy.setRecipe("VF Module Create");
69 // empty policy payload
70 SORequest request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
71 assertNotNull(request);
73 assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
74 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
75 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
77 // non-empty policy payload
78 policy.setPayload(makePayload());
79 request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
80 assertNotNull(request);
81 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
82 assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
83 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
84 assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
87 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, null));
89 // response has no base VF module
90 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
91 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
93 // response has no non-base VF modules (other than the "dummy")
94 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
95 loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
97 policy.setRecipe("VF Module Delete");
98 SORequest deleteRequest = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
99 assertNotNull(deleteRequest);
100 assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
104 public void testSendRequest() {
106 SOActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
107 } catch (Exception e) {
108 fail("Test should not throw an exception");
113 public void testMethods() {
114 SOActorServiceProvider sp = new SOActorServiceProvider();
116 assertEquals("SO", sp.actor());
117 assertEquals(2, sp.recipes().size());
118 assertEquals("VF Module Create", sp.recipes().get(0));
119 assertEquals("VF Module Delete", sp.recipes().get(1));
120 assertEquals(0, sp.recipePayloads("VF Module Create").size());
124 * Creates a policy payload containing request & configuration parameters.
126 * @return the payload
128 private Map<String, String> makePayload() {
129 Map<String, String> payload = new TreeMap<>();
131 payload.put(SOActorServiceProvider.REQ_PARAM_NM, makeReqParams());
132 payload.put(SOActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
138 * Creates request parameters.
140 * @return request parameters, encoded as JSON
142 private String makeReqParams() {
143 SORequestParameters params = new SORequestParameters();
145 params.setUsePreload(true);
147 Map<String, String> map = new TreeMap<>();
148 map.put("akey", "avalue");
150 List<Map<String, String>> lst = new LinkedList<>();
153 params.setUserParams(lst);
155 return Serialization.gsonPretty.toJson(params);
159 * Creates configuration parameters.
161 * @return configuration parameters, encoded as JSON
163 private String makeConfigParams() {
164 Map<String, String> map = new TreeMap<>();
165 map.put("ckey", "cvalue");
167 List<Map<String, String>> lst = new LinkedList<>();
170 return Serialization.gsonPretty.toJson(lst);
174 * Reads an AAI vserver named-query response from a file.
176 * @param onset the ONSET event
177 * @param fileName name of the file containing the JSON response
178 * @return output from the AAI vserver named-query
179 * @throws IOException if the file cannot be read
181 private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
182 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
183 AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
185 return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);