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;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.util.Objects;
32 import java.util.UUID;
33 import org.apache.commons.io.IOUtils;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.aai.AaiCqResponse;
38 import org.onap.policy.aai.AaiGetVnfResponse;
39 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
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.simulators.Util;
44 import org.onap.policy.vfc.VfcRequest;
46 public class VfcActorServiceProviderTest {
48 private static final String LOCAL_URL = "http://localhost:6666";
49 private static final String DOROTHY_GALE_1939 = "dorothy.gale.1939";
50 private static final String CQ_RESPONSE_JSON = "aai/AaiCqResponse.json";
51 private static final String RESTART = "Restart";
54 * Set up before test class.
55 * @throws Exception if the A&AI simulator cannot be started
58 public static void setUpSimulator() throws Exception {
63 public static void tearDownSimulator() {
64 HttpServletServerFactoryInstance.getServerFactory().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, LOCAL_URL,
83 UUID requestId = UUID.randomUUID();
84 onset.setRequestId(requestId);
85 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
88 onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
89 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
92 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
95 onset.getAai().put("service-instance.service-instance-id", "");
96 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
99 assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
100 LOCAL_URL, "AAI", "AAI"));
102 policy.setRecipe(RESTART);
103 assertNotNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
104 LOCAL_URL, "AAI", "AAI"));
106 VfcRequest request = VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
107 LOCAL_URL, "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 {
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(CQ_RESPONSE_JSON)));
153 policy.setRecipe(RESTART);
154 assertNotNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
155 loadAaiResponse(CQ_RESPONSE_JSON)));
157 VfcRequest request = VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
158 loadAaiResponse(CQ_RESPONSE_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
172 private AaiCqResponse loadAaiResponse(String fileName) throws IOException {
173 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
174 return new AaiCqResponse(resp);