bd4bb098006a5d46ecdd04c502eef038a145842f
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / test / java / org / onap / policy / controlloop / actor / vfc / VfcActorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.controlloop.actor.vfc;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28
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;
47
48 public class VfcActorTest extends BasicActor {
49
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";
53
54     /**
55      * Set up before test class.
56      * @throws Exception if the A&AI simulator cannot be started
57      */
58     @BeforeClass
59     public static void setUpSimulator() throws Exception {
60         Util.buildAaiSim();
61     }
62
63     @AfterClass
64     public static void tearDownSimulator() {
65         HttpServletServerFactoryInstance.getServerFactory().destroy();
66     }
67
68     @Test
69     public void testConstructor() {
70         VfcActor prov = new VfcActor();
71         assertEquals(0, prov.getSequenceNumber());
72
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());
76
77         assertEquals(expected.toString(), actual.toString());
78     }
79
80     @Test
81     public void testActorService() {
82         // verify that it all plugs into the ActorService
83         verifyActorService(VfcActor.NAME, "service.yaml");
84     }
85
86     @Test
87     public void testMethods() {
88         VfcActor sp = new VfcActor();
89
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());
95     }
96
97     @Test
98     public void testConstructRequestCq() throws IOException {
99         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
100         ControlLoopOperation operation = new ControlLoopOperation();
101
102         Policy policy = new Policy();
103         policy.setRecipe("GoToOz");
104
105         assertNull(VfcActor.constructRequestCq(onset, operation, policy, null));
106
107         onset.getAai().put("generic-vnf.vnf-id", DOROTHY_GALE_1939);
108         assertNull(VfcActor.constructRequestCq(onset, operation, policy, null));
109
110
111         UUID requestId = UUID.randomUUID();
112         onset.setRequestId(requestId);
113         assertNull(VfcActor.constructRequestCq(onset, operation, policy, null));
114
115         onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
116         assertNull(VfcActor.constructRequestCq(onset, operation, policy, null));
117
118
119         onset.getAai().put("service-instance.service-instance-id", "");
120         assertNull(VfcActor.constructRequestCq(onset, operation, policy, null));
121
122         assertNull(VfcActor.constructRequestCq(onset, operation, policy,
123                 loadAaiResponse(CQ_RESPONSE_JSON)));
124
125         policy.setRecipe(RESTART);
126         assertNotNull(VfcActor.constructRequestCq(onset, operation, policy,
127                 loadAaiResponse(CQ_RESPONSE_JSON)));
128
129         VfcRequest request = VfcActor.constructRequestCq(onset, operation, policy,
130                 loadAaiResponse(CQ_RESPONSE_JSON));
131
132         assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
133         assertEquals(DOROTHY_GALE_1939, request.getHealRequest().getVnfInstanceId());
134         assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
135     }
136
137     /**
138      * Reads an AAI vserver named-query response from a file.
139      *
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
143      */
144     private AaiCqResponse loadAaiResponse(String fileName) throws IOException {
145         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
146         return new AaiCqResponse(resp);
147     }
148
149 }