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, VfModuleDelete.NAME).stream().sorted()
78 .collect(Collectors.toList());
79 var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
81 assertEquals(expected.toString(), actual.toString());
85 public void testActorService() {
86 // verify that it all plugs into the ActorService
87 verifyActorService(SoActorServiceProvider.NAME, "service.yaml");
91 public void testSendRequest() {
92 assertThatCode(() -> SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null,
93 null)).doesNotThrowAnyException();
97 public void testMethods() {
98 SoActorServiceProvider sp = new SoActorServiceProvider();
100 assertEquals("SO", sp.actor());
101 assertEquals(2, sp.recipes().size());
102 assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
103 assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
104 assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
105 assertEquals(0, sp.recipeTargets("unknown recipe").size());
106 assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
110 public void testConstructRequestCq() throws Exception {
111 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
112 final ControlLoopOperation operation = new ControlLoopOperation();
113 final AaiCqResponse aaiCqResp = loadAaiResponseCq("aai/AaiCqResponseFull.json");
114 final AaiCqResponse aaiCqRespMissing = loadAaiResponseCq("aai/AaiCqResponseMissing.json");
115 final UUID requestId = UUID.randomUUID();
116 onset.setRequestId(requestId);
118 Policy policy = new Policy();
119 policy.setActor("Dorothy");
120 policy.setRecipe("GoToOz");
122 instantiateTargetCq(policy);
124 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp));
126 policy.setActor("SO");
128 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
130 policy.setRecipe(VF_MODULE_CREATE);
132 // empty policy payload
133 SoRequest request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
134 assertNotNull(request);
136 assertEquals("vfModuleName", request.getRequestDetails().getRequestInfo().getInstanceName());
137 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
138 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
140 // non-empty policy payload
141 policy.setPayload(makePayload());
142 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
143 assertNotNull(request);
144 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
145 assertEquals(A_VALUE, request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
146 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
147 assertEquals(C_VALUE, request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
149 // payload with config, but no request params
150 policy.setPayload(makePayload());
151 policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
152 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
153 assertNotNull(request);
154 assertNull(request.getRequestDetails().getRequestParameters());
155 assertNotNull(request.getRequestDetails().getConfigurationParameters());
157 // payload with request, but no config params
158 policy.setPayload(makePayload());
159 policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
160 request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
161 assertNotNull(request);
162 assertNotNull(request.getRequestDetails().getRequestParameters());
163 assertNull(request.getRequestDetails().getConfigurationParameters());
166 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
168 instantiateTargetCq(policy);
169 policy.setRecipe(VF_MODULE_DELETE);
170 SoRequest deleteRequest = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
171 assertNotNull(deleteRequest);
172 assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
175 * NOTE: The remaining tests must be done in order
178 policy.setRecipe(VF_MODULE_CREATE);
181 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
184 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
186 assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
190 * Reads an AAI vserver named-query response from a file.
192 * @param fileName name of the file containing the JSON response
193 * @return output from the AAI vserver named-query
194 * @throws IOException if the file cannot be read
196 private AaiCqResponse loadAaiResponseCq(String fileName) throws IOException {
197 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
198 return new AaiCqResponse(resp);
204 * Creates a policy payload containing request & configuration parameters.
206 * @return the payload
208 private Map<String, String> makePayload() {
209 Map<String, String> payload = new TreeMap<>();
211 payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
212 payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
218 * Creates request parameters.
220 * @return request parameters, encoded as JSON
222 private String makeReqParams() {
223 SoRequestParameters params = new SoRequestParameters();
225 params.setUsePreload(true);
227 Map<String, String> map = new TreeMap<>();
228 map.put("akey", A_VALUE);
230 List<Map<String, String>> lst = new LinkedList<>();
233 params.setUserParams(lst);
235 return Serialization.gsonPretty.toJson(params);
239 * Creates configuration parameters.
241 * @return configuration parameters, encoded as JSON
243 private String makeConfigParams() {
244 Map<String, String> map = new TreeMap<>();
245 map.put("ckey", C_VALUE);
247 List<Map<String, String>> lst = new LinkedList<>();
250 return Serialization.gsonPretty.toJson(lst);