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.Arrays;
32 import java.util.Objects;
33 import java.util.UUID;
34 import java.util.stream.Collectors;
35 import org.apache.commons.io.IOUtils;
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.common.endpoints.http.server.HttpServletServerFactoryInstance;
41 import org.onap.policy.controlloop.ControlLoopOperation;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.actor.test.BasicActor;
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 extends BasicActor {
50 private static final String DOROTHY_GALE_1939 = "dorothy.gale.1939";
51 private static final String CQ_RESPONSE_JSON = "aai/AaiCqResponse.json";
52 private static final String RESTART = "Restart";
55 * Set up before test class.
56 * @throws Exception if the A&AI simulator cannot be started
59 public static void setUpSimulator() throws Exception {
64 public static void tearDownSimulator() {
65 HttpServletServerFactoryInstance.getServerFactory().destroy();
69 public void testConstructor() {
70 VfcActorServiceProvider prov = new VfcActorServiceProvider();
71 assertEquals(0, prov.getSequenceNumber());
73 // verify that it has the operators we expect
74 var expected = Arrays.asList(Restart.NAME).stream().sorted().collect(Collectors.toList());
75 var actual = prov.getOperationNames().stream().sorted().collect(Collectors.toList());
77 assertEquals(expected.toString(), actual.toString());
81 public void testActorService() {
82 // verify that it all plugs into the ActorService
83 verifyActorService(VfcActorServiceProvider.NAME, "service.yaml");
87 public void testMethods() {
88 VfcActorServiceProvider sp = new VfcActorServiceProvider();
90 assertEquals("VFC", sp.actor());
91 assertEquals(1, sp.recipes().size());
92 assertEquals(RESTART, sp.recipes().get(0));
93 assertEquals("VM", sp.recipeTargets(RESTART).get(0));
94 assertEquals(0, sp.recipePayloads(RESTART).size());
98 public void testConstructRequestCq() throws IOException {
99 VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
100 ControlLoopOperation operation = new ControlLoopOperation();
102 Policy policy = new Policy();
103 policy.setRecipe("GoToOz");
105 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
107 onset.getAai().put("generic-vnf.vnf-id", DOROTHY_GALE_1939);
108 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
111 UUID requestId = UUID.randomUUID();
112 onset.setRequestId(requestId);
113 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
115 onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
116 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
119 onset.getAai().put("service-instance.service-instance-id", "");
120 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
122 assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
123 loadAaiResponse(CQ_RESPONSE_JSON)));
125 policy.setRecipe(RESTART);
126 assertNotNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
127 loadAaiResponse(CQ_RESPONSE_JSON)));
129 VfcRequest request = VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
130 loadAaiResponse(CQ_RESPONSE_JSON));
132 assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
133 assertEquals(DOROTHY_GALE_1939, request.getHealRequest().getVnfInstanceId());
134 assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
138 * Reads an AAI vserver named-query response from a file.
140 * @param fileName name of the file containing the JSON response
141 * @return output from the AAI vserver named-query
142 * @throws IOException if the file cannot be read
144 private AaiCqResponse loadAaiResponse(String fileName) throws IOException {
145 String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
146 return new AaiCqResponse(resp);