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;
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.AaiCqResponse;
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.controlloop.policy.Target;
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 C_VALUE = "cvalue";
52 private static final String A_VALUE = "avalue";
53 private static final String VF_MODULE_CREATE = "VF Module Create";
54 private static final String VF_MODULE_DELETE = "VF Module Delete";
56 private void instantiateTargetCq(Policy policy) {
58 Target target = new Target();
59 target.setModelCustomizationId("47958575-138f-452a-8c8d-d89b595f8164");
60 target.setModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
61 target.setModelName("VfwclVfwsnkBbefb8ce2bde..base_vfw..module-0");
62 target.setModelVersion("1");
63 target.setModelVersionId("94b18b1d-cc91-4f43-911a-e6348665f292");
65 policy.setTarget(target);
69 public void testSendRequest() {
70 SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null, null);
74 public void testMethods() {
75 SoActorServiceProvider sp = new SoActorServiceProvider();
77 assertEquals("SO", sp.actor());
78 assertEquals(2, sp.recipes().size());
79 assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
80 assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
81 assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
82 assertEquals(0, sp.recipeTargets("unknown recipe").size());
83 assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
87 public void testConstructRequestCq() throws Exception {
88 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
89 final ControlLoopOperation operation = new ControlLoopOperation();
90 final AaiCqResponse aaiCqResp = loadAaiResponseCq("aai/AaiCqResponseFull.json");
91 final AaiCqResponse aaiCqRespMissing = loadAaiResponseCq("aai/AaiCqResponseMissing.json");
92 final UUID requestId = UUID.randomUUID();
93 onset.setRequestId(requestId);
95 Policy policy = new Policy();
96 policy.setActor("Dorothy");
97 policy.setRecipe("GoToOz");
99 instantiateTargetCq(policy);
101 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp));
103 policy.setActor("SO");
105 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
107 policy.setRecipe(VF_MODULE_CREATE);
109 // empty policy payload
110 SoRequest request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
111 assertNotNull(request);
113 assertEquals("vfModuleName", request.getRequestDetails().getRequestInfo().getInstanceName());
114 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
115 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
117 // non-empty policy payload
118 policy.setPayload(makePayload());
119 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
120 assertNotNull(request);
121 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
122 assertEquals(A_VALUE, request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
123 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
124 assertEquals(C_VALUE, request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
126 // payload with config, but no request params
127 policy.setPayload(makePayload());
128 policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
129 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
130 assertNotNull(request);
131 assertNull(request.getRequestDetails().getRequestParameters());
132 assertNotNull(request.getRequestDetails().getConfigurationParameters());
134 // payload with request, but no config params
135 policy.setPayload(makePayload());
136 policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
137 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
138 assertNotNull(request);
139 assertNotNull(request.getRequestDetails().getRequestParameters());
140 assertNull(request.getRequestDetails().getConfigurationParameters());
143 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
145 instantiateTargetCq(policy);
146 policy.setRecipe(VF_MODULE_DELETE);
147 SoRequest deleteRequest = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
148 assertNotNull(deleteRequest);
149 assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
152 * NOTE: The remaining tests must be done in order
155 policy.setRecipe(VF_MODULE_CREATE);
158 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
161 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
163 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
167 * Reads an AAI vserver named-query response from a file.
169 * @param fileName name of the file containing the JSON response
170 * @return output from the AAI vserver named-query
171 * @throws IOException if the file cannot be read
173 private AaiCqResponse loadAaiResponseCq(String fileName) throws IOException {
174 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
175 return new AaiCqResponse(resp);
181 * Creates a policy payload containing request & configuration parameters.
183 * @return the payload
185 private Map<String, String> makePayload() {
186 Map<String, String> payload = new TreeMap<>();
188 payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
189 payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
195 * Creates request parameters.
197 * @return request parameters, encoded as JSON
199 private String makeReqParams() {
200 SoRequestParameters params = new SoRequestParameters();
202 params.setUsePreload(true);
204 Map<String, String> map = new TreeMap<>();
205 map.put("akey", A_VALUE);
207 List<Map<String, String>> lst = new LinkedList<>();
210 params.setUserParams(lst);
212 return Serialization.gsonPretty.toJson(params);
216 * Creates configuration parameters.
218 * @return configuration parameters, encoded as JSON
220 private String makeConfigParams() {
221 Map<String, String> map = new TreeMap<>();
222 map.put("ckey", C_VALUE);
224 List<Map<String, String>> lst = new LinkedList<>();
227 return Serialization.gsonPretty.toJson(lst);