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.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.controlloop.ControlLoopOperation;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.policy.Policy;
42 import org.onap.policy.simulators.Util;
43 import org.onap.policy.vfc.VfcRequest;
45 public class VfcActorServiceProviderTest {
47 private static final String DOROTHY_GALE_1939 = "dorothy.gale.1939";
48 private static final String CQ_RESPONSE_JSON = "aai/AaiCqResponse.json";
49 private static final String RESTART = "Restart";
52 * Set up before test class.
53 * @throws Exception if the A&AI simulator cannot be started
56 public static void setUpSimulator() throws Exception {
61 public static void tearDownSimulator() {
62 HttpServletServerFactoryInstance.getServerFactory().destroy();
66 public void testMethods() {
67 VfcActorServiceProvider sp = new VfcActorServiceProvider();
69 assertEquals("VFC", sp.actor());
70 assertEquals(1, sp.recipes().size());
71 assertEquals(RESTART, sp.recipes().get(0));
72 assertEquals("VM", sp.recipeTargets(RESTART).get(0));
73 assertEquals(0, sp.recipePayloads(RESTART).size());
77 public void testConstructRequestCq() throws IOException {
78 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
79 ControlLoopOperation operation = new ControlLoopOperation();
81 Policy policy = new Policy();
82 policy.setRecipe("GoToOz");
84 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
86 onset.getAai().put("generic-vnf.vnf-id", DOROTHY_GALE_1939);
87 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
90 UUID requestId = UUID.randomUUID();
91 onset.setRequestId(requestId);
92 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
94 onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
95 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
98 onset.getAai().put("service-instance.service-instance-id", "");
99 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
101 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
102 loadAaiResponse(CQ_RESPONSE_JSON)));
104 policy.setRecipe(RESTART);
105 assertNotNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
106 loadAaiResponse(CQ_RESPONSE_JSON)));
108 VfcRequest request = VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
109 loadAaiResponse(CQ_RESPONSE_JSON));
111 assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
112 assertEquals(DOROTHY_GALE_1939, request.getHealRequest().getVnfInstanceId());
113 assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
117 * Reads an AAI vserver named-query response from a file.
119 * @param fileName name of the file containing the JSON response
120 * @return output from the AAI vserver named-query
121 * @throws IOException if the file cannot be read
123 private AaiCqResponse loadAaiResponse(String fileName) throws IOException {
124 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
125 return new AaiCqResponse(resp);