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.UUID;
32 import org.apache.commons.io.IOUtils;
33 import org.junit.Test;
34 import org.onap.policy.aai.AaiNqResponse;
35 import org.onap.policy.aai.AaiNqResponseWrapper;
36 import org.onap.policy.controlloop.ControlLoopOperation;
37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.policy.Policy;
39 import org.onap.policy.so.SORequest;
40 import org.onap.policy.so.util.Serialization;
42 public class TestSOActorServiceProvider {
45 public void testConstructRequest() throws Exception {
46 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
47 final ControlLoopOperation operation = new ControlLoopOperation();
48 final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
50 final UUID requestId = UUID.randomUUID();
51 onset.setRequestId(requestId);
53 Policy policy = new Policy();
54 policy.setActor("Dorothy");
55 policy.setRecipe("GoToOz");
56 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
58 policy.setActor("SO");
59 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
61 policy.setRecipe("VF Module Create");
62 SORequest request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
63 assertNotNull(request);
65 assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
66 assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
67 assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
70 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, null));
72 // response has no base VF module
73 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
74 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
76 // response has no non-base VF modules (other than the "dummy")
77 assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy,
78 loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
82 public void testSendRequest() {
84 SOActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null);
85 } catch (Exception e) {
86 fail("Test should not throw an exception");
91 public void testMethods() {
92 SOActorServiceProvider sp = new SOActorServiceProvider();
94 assertEquals("SO", sp.actor());
95 assertEquals(1, sp.recipes().size());
96 assertEquals("VF Module Create", sp.recipes().get(0));
97 assertEquals(0, sp.recipePayloads("VF Module Create").size());
101 * Reads an AAI vserver named-query response from a file.
103 * @param onset the ONSET event
104 * @param fileName name of the file containing the JSON response
105 * @return output from the AAI vserver named-query
106 * @throws IOException if the file cannot be read
108 private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName)
110 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
111 AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
113 return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);