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.assertj.core.api.Assertions.assertThatCode;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import java.util.Arrays;
34 import java.util.LinkedList;
35 import java.util.List;
37 import java.util.TreeMap;
38 import java.util.UUID;
39 import java.util.stream.Collectors;
40 import org.apache.commons.io.IOUtils;
41 import org.junit.Test;
42 import org.onap.policy.aai.AaiCqResponse;
43 import org.onap.policy.controlloop.ControlLoopOperation;
44 import org.onap.policy.controlloop.VirtualControlLoopEvent;
45 import org.onap.policy.controlloop.actor.test.BasicActor;
46 import org.onap.policy.controlloop.policy.Policy;
47 import org.onap.policy.controlloop.policy.Target;
48 import org.onap.policy.so.SoOperationType;
49 import org.onap.policy.so.SoRequest;
50 import org.onap.policy.so.SoRequestParameters;
51 import org.onap.policy.so.util.Serialization;
53 public class SoActorServiceProviderTest extends BasicActor {
55 private static final String C_VALUE = "cvalue";
56 private static final String A_VALUE = "avalue";
57 private static final String VF_MODULE_CREATE = "VF Module Create";
58 private static final String VF_MODULE_DELETE = "VF Module Delete";
60 private void instantiateTargetCq(Policy policy) {
62 Target target = new Target();
63 target.setModelCustomizationId("47958575-138f-452a-8c8d-d89b595f8164");
64 target.setModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
65 target.setModelName("VfwclVfwsnkBbefb8ce2bde..base_vfw..module-0");
66 target.setModelVersion("1");
67 target.setModelVersionId("94b18b1d-cc91-4f43-911a-e6348665f292");
69 policy.setTarget(target);
73 public void testConstructor() {
74 SoActorServiceProvider prov = new SoActorServiceProvider();
76 // verify that it has the operators we expect
77 var expected = Arrays.asList(VfModuleCreate.NAME).stream().sorted().collect(Collectors.toList());
78 var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
80 assertEquals(expected.toString(), actual.toString());
84 public void testActorService() {
85 // verify that it all plugs into the ActorService
86 verifyActorService(SoActorServiceProvider.NAME, "service.yaml");
90 public void testSendRequest() {
91 assertThatCode(() -> SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null,
92 null)).doesNotThrowAnyException();
96 public void testMethods() {
97 SoActorServiceProvider sp = new SoActorServiceProvider();
99 assertEquals("SO", sp.actor());
100 assertEquals(2, sp.recipes().size());
101 assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
102 assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
103 assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
104 assertEquals(0, sp.recipeTargets("unknown recipe").size());
105 assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
109 public void testConstructRequestCq() throws Exception {
110 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
111 final ControlLoopOperation operation = new ControlLoopOperation();
112 final AaiCqResponse aaiCqResp = loadAaiResponseCq("aai/AaiCqResponseFull.json");
113 final AaiCqResponse aaiCqRespMissing = loadAaiResponseCq("aai/AaiCqResponseMissing.json");
114 final UUID requestId = UUID.randomUUID();
115 onset.setRequestId(requestId);
117 Policy policy = new Policy();
118 policy.setActor("Dorothy");
119 policy.setRecipe("GoToOz");
121 instantiateTargetCq(policy);
123 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp));
125 policy.setActor("SO");
127 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
129 policy.setRecipe(VF_MODULE_CREATE);
131 // empty policy payload
132 SoRequest request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
133 assertNotNull(request);
135 assertEquals("vfModuleName", request.getRequestDetails().getRequestInfo().getInstanceName());
136 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
137 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
139 // non-empty policy payload
140 policy.setPayload(makePayload());
141 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
142 assertNotNull(request);
143 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
144 assertEquals(A_VALUE, request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
145 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
146 assertEquals(C_VALUE, request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
148 // payload with config, but no request params
149 policy.setPayload(makePayload());
150 policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
151 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
152 assertNotNull(request);
153 assertNull(request.getRequestDetails().getRequestParameters());
154 assertNotNull(request.getRequestDetails().getConfigurationParameters());
156 // payload with request, but no config params
157 policy.setPayload(makePayload());
158 policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
159 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
160 assertNotNull(request);
161 assertNotNull(request.getRequestDetails().getRequestParameters());
162 assertNull(request.getRequestDetails().getConfigurationParameters());
165 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
167 instantiateTargetCq(policy);
168 policy.setRecipe(VF_MODULE_DELETE);
169 SoRequest deleteRequest = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
170 assertNotNull(deleteRequest);
171 assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
174 * NOTE: The remaining tests must be done in order
177 policy.setRecipe(VF_MODULE_CREATE);
180 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
183 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
185 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
189 * Reads an AAI vserver named-query response from a file.
191 * @param fileName name of the file containing the JSON response
192 * @return output from the AAI vserver named-query
193 * @throws IOException if the file cannot be read
195 private AaiCqResponse loadAaiResponseCq(String fileName) throws IOException {
196 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
197 return new AaiCqResponse(resp);
203 * Creates a policy payload containing request & configuration parameters.
205 * @return the payload
207 private Map<String, String> makePayload() {
208 Map<String, String> payload = new TreeMap<>();
210 payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
211 payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
217 * Creates request parameters.
219 * @return request parameters, encoded as JSON
221 private String makeReqParams() {
222 SoRequestParameters params = new SoRequestParameters();
224 params.setUsePreload(true);
226 Map<String, String> map = new TreeMap<>();
227 map.put("akey", A_VALUE);
229 List<Map<String, String>> lst = new LinkedList<>();
232 params.setUserParams(lst);
234 return Serialization.gsonPretty.toJson(params);
238 * Creates configuration parameters.
240 * @return configuration parameters, encoded as JSON
242 private String makeConfigParams() {
243 Map<String, String> map = new TreeMap<>();
244 map.put("ckey", C_VALUE);
246 List<Map<String, String>> lst = new LinkedList<>();
249 return Serialization.gsonPretty.toJson(lst);