2 * ============LICENSE_START=======================================================
3 * TestSOActorServiceProvider
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018 AT&T. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.controlloop.actor.so;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 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.util.Serialization;
47 public class TestSOActorServiceProvider {
50 public void testConstructRequest() throws Exception {
51 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
52 final ControlLoopOperation operation = new ControlLoopOperation();
53 final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
55 final UUID requestId = UUID.randomUUID();
56 onset.setRequestId(requestId);
58 Policy policy = new Policy();
59 policy.setActor("Dorothy");
60 policy.setRecipe("GoToOz");
61 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
63 policy.setActor("SO");
64 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
66 policy.setRecipe("VF Module Create");
68 // empty policy payload
69 SORequest request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
70 assertNotNull(request);
72 assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
73 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
74 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
76 // non-empty policy payload
77 policy.setPayload(makePayload());
78 request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
79 assertNotNull(request);
80 assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
81 assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
82 assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
83 assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
86 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, null));
88 // response has no base VF module
89 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
90 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
92 // response has no non-base VF modules (other than the "dummy")
93 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
94 loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
98 public void testSendRequest() {
100 SOActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
101 } catch (Exception e) {
102 fail("Test should not throw an exception");
107 public void testMethods() {
108 SOActorServiceProvider sp = new SOActorServiceProvider();
110 assertEquals("SO", sp.actor());
111 assertEquals(1, sp.recipes().size());
112 assertEquals("VF Module Create", sp.recipes().get(0));
113 assertEquals(0, sp.recipePayloads("VF Module Create").size());
117 * Creates a policy payload containing request & configuration parameters.
119 * @return the payload
121 private Map<String, String> makePayload() {
122 Map<String, String> payload = new TreeMap<>();
124 payload.put(SOActorServiceProvider.REQ_PARAM_NM, makeReqParams());
125 payload.put(SOActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
131 * Creates request parameters.
133 * @return request parameters, encoded as JSON
135 private String makeReqParams() {
136 SORequestParameters params = new SORequestParameters();
138 params.setUsePreload(true);
140 Map<String, String> map = new TreeMap<>();
141 map.put("akey", "avalue");
143 List<Map<String, String>> lst = new LinkedList<>();
146 params.setUserParams(lst);
148 return Serialization.gsonPretty.toJson(params);
152 * Creates configuration parameters.
154 * @return configuration parameters, encoded as JSON
156 private String makeConfigParams() {
157 Map<String, String> map = new TreeMap<>();
158 map.put("ckey", "cvalue");
160 List<Map<String, String>> lst = new LinkedList<>();
163 return Serialization.gsonPretty.toJson(lst);
167 * Reads an AAI vserver named-query response from a file.
169 * @param onset the ONSET event
170 * @param fileName name of the file containing the JSON response
171 * @return output from the AAI vserver named-query
172 * @throws IOException if the file cannot be read
174 private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
175 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
176 AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
178 return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);