Custom Query Code
[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 import static org.junit.Assert.fail;
29
30 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Objects;
33 import java.util.UUID;
34 import org.apache.commons.io.IOUtils;
35 import org.eclipse.persistence.exceptions.JAXBException;
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.aai.AaiGetVnfResponse;
41 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
42 import org.onap.policy.controlloop.ControlLoopOperation;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
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 VfcActorServiceProviderTest {
49
50     /**
51      * Set up for test class.
52      */
53     @BeforeClass
54     public static void setUpSimulator() {
55         try {
56             Util.buildAaiSim();
57         } catch (Exception e) {
58             fail(e.getMessage());
59         }
60     }
61
62     @AfterClass
63     public static void tearDownSimulator() {
64         HttpServletServer.factory.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, "http://localhost:6666",
81                 "AAI", "AAI"));
82
83         UUID requestId = UUID.randomUUID();
84         onset.setRequestId(requestId);
85         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
86                 "AAI", "AAI"));
87
88         onset.getAai().put("generic-vnf.vnf-name", "Dorothy");
89         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
90                 "AAI", null));
91
92         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
93                 "AAI", "AAI"));
94
95         onset.getAai().put("service-instance.service-instance-id", "");
96         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, null, "http://localhost:6666",
97                 "AAI", "AAI"));
98
99         assertNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
100                 "http://localhost:6666", "AAI", "AAI"));
101
102         policy.setRecipe("Restart");
103         assertNotNull(VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
104                 "http://localhost:6666", "AAI", "AAI"));
105
106         VfcRequest request = VfcActorServiceProvider.constructRequest(onset, operation, policy, new AaiGetVnfResponse(),
107                 "http://localhost:6666", "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, JAXBException {
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("aai/AaiCqResponse.json")));
152
153         policy.setRecipe("Restart");
154         assertNotNull(VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
155                 loadAaiResponse("aai/AaiCqResponse.json")));
156
157         VfcRequest request = VfcActorServiceProvider.constructRequestCq(onset, operation, policy,
158                 loadAaiResponse("aai/AaiCqResponse.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      * @throws JAXBException throws JAXBException
172      */
173     private AaiCqResponse loadAaiResponse(String fileName) throws IOException, JAXBException {
174         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
175         return new AaiCqResponse(resp);
176     }
177
178 }