2 * ============LICENSE_START=======================================================
3 * ONAP - Policy Drools Applications
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson. All rights reserved.
6 * Modifications Copyright (C) 2018-2019 AT&T Corp. All rights reserved.
7 * Modifications Copyright (C) 2019 Nordix Foundation.
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.vfc;
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;
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Objects;
33 import java.util.UUID;
34 import org.apache.commons.io.IOUtils;
35 import org.eclipse.persistence.exceptions.JAXBException;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.aai.AaiCqResponse;
40 import org.onap.policy.aai.AaiGetVnfResponse;
41 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
42 import org.onap.policy.controlloop.ControlLoopOperation;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.policy.Policy;
45 import org.onap.policy.simulators.Util;
46 import org.onap.policy.vfc.VfcRequest;
48 public class VfcActorServiceProviderTest {
51 * Set up for test class.
54 public static void setUpSimulator() {
57 } catch (Exception e) {
63 public static void tearDownSimulator() {
64 HttpServletServer.factory.destroy();
68 public void testConstructRequest() {
69 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
70 ControlLoopOperation operation = new ControlLoopOperation();
72 Policy policy = new Policy();
73 policy.setRecipe("GoToOz");
75 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, null, null, null));
77 onset.getAai().put("generic-vnf.vnf-id", "dorothy.gale.1939");
78 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, null, null, null));
80 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
83 UUID requestId = UUID.randomUUID();
84 onset.setRequestId(requestId);
85 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
88 onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
89 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
92 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
95 onset.getAai().put("service-instance.service-instance-id", "");
96 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
99 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
100 "http://localhost:6666", "AAI", "AAI"));
102 policy.setRecipe("Restart");
103 assertNotNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
104 "http://localhost:6666", "AAI", "AAI"));
106 VfcRequest request = VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
107 "http://localhost:6666", "AAI", "AAI");
109 assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
110 assertEquals("dorothy.gale.1939", request.getHealRequest().getVnfInstanceId());
111 assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
115 public void testMethods() {
116 VfcActorServiceProvider sp = new VfcActorServiceProvider();
118 assertEquals("VFC", sp.actor());
119 assertEquals(1, sp.recipes().size());
120 assertEquals("Restart", sp.recipes().get(0));
121 assertEquals("VM", sp.recipeTargets("Restart").get(0));
122 assertEquals(0, sp.recipePayloads("Restart").size());
126 public void testConstructRequestCq() throws IOException, JAXBException {
127 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
128 ControlLoopOperation operation = new ControlLoopOperation();
130 Policy policy = new Policy();
131 policy.setRecipe("GoToOz");
133 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
135 onset.getAai().put("generic-vnf.vnf-id", "dorothy.gale.1939");
136 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
139 UUID requestId = UUID.randomUUID();
140 onset.setRequestId(requestId);
141 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
143 onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
144 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
147 onset.getAai().put("service-instance.service-instance-id", "");
148 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
150 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
151 loadAaiResponse("aai/AaiCqResponse.json")));
153 policy.setRecipe("Restart");
154 assertNotNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
155 loadAaiResponse("aai/AaiCqResponse.json")));
157 VfcRequest request = VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
158 loadAaiResponse("aai/AaiCqResponse.json"));
160 assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
161 assertEquals("dorothy.gale.1939", request.getHealRequest().getVnfInstanceId());
162 assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
166 * Reads an AAI vserver named-query response from a file.
168 * @param fileName name of the file containing the JSON response
169 * @return output from the AAI vserver named-query
170 * @throws IOException if the file cannot be read
171 * @throws JAXBException throws JAXBException
173 private AaiCqResponse loadAaiResponse(String fileName) throws IOException, JAXBException {
174 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
175 return new AaiCqResponse(resp);