Fix models due to sonar changes in common
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / test / java / org / onap / policy / controlloop / actor / vfc / VfcActorServiceProviderTest.java
1 /*-
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
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.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;
45
46 public class VfcActorServiceProviderTest {
47
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";
52
53     /**
54      * Set up before test class.
55      * @throws Exception if the A&AI simulator cannot be started
56      */
57     @BeforeClass
58     public static void setUpSimulator() throws Exception {
59         Util.buildAaiSim();
60     }
61
62     @AfterClass
63     public static void tearDownSimulator() {
64         HttpServletServerFactoryInstance.getServerFactory().destroy();
65     }
66
67     @Test
68     public void testConstructRequest() {
69         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
70         ControlLoopOperation operation = new ControlLoopOperation();
71
72         Policy policy = new Policy();
73         policy.setRecipe("GoToOz");
74
75         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, null, null, null));
76
77         onset.getAai().put("generic-vnf.vnf-id", DOROTHY_GALE_1939);
78         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, null, null, null));
79
80         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
81                 "AAI", "AAI"));
82
83         UUID requestId = UUID.randomUUID();
84         onset.setRequestId(requestId);
85         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
86                 "AAI", "AAI"));
87
88         onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
89         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
90                 "AAI", null));
91
92         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
93                 "AAI", "AAI"));
94
95         onset.getAai().put("service-instance.service-instance-id", "");
96         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, LOCAL_URL,
97                 "AAI", "AAI"));
98
99         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
100                 LOCAL_URL, "AAI", "AAI"));
101
102         policy.setRecipe(RESTART);
103         assertNotNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
104                 LOCAL_URL, "AAI", "AAI"));
105
106         VfcRequest request = VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
107                 LOCAL_URL, "AAI", "AAI");
108
109         assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
110         assertEquals(DOROTHY_GALE_1939, request.getHealRequest().getVnfInstanceId());
111         assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
112     }
113
114     @Test
115     public void testMethods() {
116         VfcActorServiceProvider sp = new VfcActorServiceProvider();
117
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());
123     }
124
125     @Test
126     public void testConstructRequestCq() throws IOException {
127         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
128         ControlLoopOperation operation = new ControlLoopOperation();
129
130         Policy policy = new Policy();
131         policy.setRecipe("GoToOz");
132
133         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
134
135         onset.getAai().put("generic-vnf.vnf-id", DOROTHY_GALE_1939);
136         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
137
138
139         UUID requestId = UUID.randomUUID();
140         onset.setRequestId(requestId);
141         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
142
143         onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
144         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
145
146
147         onset.getAai().put("service-instance.service-instance-id", "");
148         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy, null));
149
150         assertNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
151                 loadAaiResponse(CQ_RESPONSE_JSON)));
152
153         policy.setRecipe(RESTART);
154         assertNotNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
155                 loadAaiResponse(CQ_RESPONSE_JSON)));
156
157         VfcRequest request = VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
158                 loadAaiResponse(CQ_RESPONSE_JSON));
159
160         assertEquals(requestId, Objects.requireNonNull(request).getRequestId());
161         assertEquals(DOROTHY_GALE_1939, request.getHealRequest().getVnfInstanceId());
162         assertEquals("restartvm", request.getHealRequest().getAdditionalParams().getAction());
163     }
164
165     /**
166      * Reads an AAI vserver named-query response from a file.
167      *
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      */
172     private AaiCqResponse loadAaiResponse(String fileName) throws IOException {
173         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
174         return new AaiCqResponse(resp);
175     }
176
177 }