f9e6e8260f13ce0f4077c444aed342280bce1f4a
[policy/drools-applications.git] / controlloop / common / model-impl / vfc / src / test / java / org / onap / policy / vfc / VfcManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * vfc
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson, AT&T. All rights reserved.
6  * Modifications Copyright (C) 2018-2019 AT&T Corporation. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.vfc;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import static org.mockito.ArgumentMatchers.anyMap;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.endsWith;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.ArgumentMatchers.startsWith;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.UUID;
38
39 import org.drools.core.WorkingMemory;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.onap.policy.drools.system.PolicyEngine;
45 import org.onap.policy.rest.RestManager;
46 import org.onap.policy.rest.RestManager.Pair;
47 import org.onap.policy.vfc.util.Serialization;
48
49 public class VfcManagerTest {
50     private static WorkingMemory mockedWorkingMemory;
51
52     private RestManager   mockedRestManager;
53
54     private Pair<Integer, String> httpResponsePutOk;
55     private Pair<Integer, String> httpResponseGetOk;
56     private Pair<Integer, String> httpResponseBadResponse;
57     private Pair<Integer, String> httpResponseErr;
58
59     private VfcRequest  request;
60     private VfcResponse response;
61
62     @BeforeClass
63     public static void beforeTestVfcManager() {
64         mockedWorkingMemory = mock(WorkingMemory.class);
65     }
66
67     /**
68      * Set up the mocked REST manager.
69      */
70     @Before
71     public void setupMockedRest() {
72         mockedRestManager   = mock(RestManager.class);
73
74         httpResponsePutOk       = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
75         httpResponseGetOk       = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
76         httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
77         httpResponseErr         = mockedRestManager.new Pair<>(200, null);
78     }
79
80     /**
81      * Create the request and response before.
82      */
83     @Before
84     public void createRequestAndResponse() {
85         VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
86         actionInfo.setVmid("TheWizard");
87         actionInfo.setVmname("The Wizard of Oz");
88
89         VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
90         additionalParams.setAction("Go Home");
91         additionalParams.setActionInfo(actionInfo);
92
93         VfcHealRequest healRequest = new VfcHealRequest();
94         healRequest.setAdditionalParams(additionalParams);
95         healRequest.setCause("WestWitch");
96         healRequest.setVnfInstanceId("EmeraldCity");
97
98         final UUID requestId = UUID.randomUUID();
99         request = new VfcRequest();
100         request.setHealRequest(healRequest);
101         request.setNsInstanceId("Dorothy");
102         request.setRequestId(requestId);
103
104         List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
105
106         VfcResponseDescriptor responseDescriptor = new VfcResponseDescriptor();
107         responseDescriptor.setErrorCode("1234");
108         responseDescriptor.setProgress("Follow The Yellow Brick Road");
109         responseDescriptor.setResponseHistoryList(responseHistoryList);
110         responseDescriptor.setResponseId(UUID.randomUUID().toString());
111         responseDescriptor.setStatus("finished");
112         responseDescriptor.setStatusDescription("There's no place like home");
113
114         response = new VfcResponse();
115         response.setJobId("1234");
116         response.setRequestId(request.getRequestId().toString());
117         response.setResponseDescriptor(responseDescriptor);
118     }
119
120     /**
121      * Remove the environnment.
122      */
123     @After
124     public void tearDown() {
125         PolicyEngine.manager.getEnvironment().remove("vfc.password");
126         PolicyEngine.manager.getEnvironment().remove("vfc.username");
127         PolicyEngine.manager.getEnvironment().remove("vfc.url");
128     }
129
130     @Test
131     public void testVfcInitiation() {
132         try {
133             new VfcManager(null, null);
134             fail("test should throw an exception here");
135         }
136         catch (IllegalArgumentException e) {
137             assertEquals("the parameters \"wm\" and \"request\" on the VfcManager constructor may not be null", 
138                     e.getMessage());
139         }
140
141         try {
142             new VfcManager(mockedWorkingMemory, null);
143             fail("test should throw an exception here");
144         }
145         catch (IllegalArgumentException e) {
146             assertEquals("the parameters \"wm\" and \"request\" on the VfcManager constructor may not be null", 
147                     e.getMessage());
148         }
149
150         try {
151             new VfcManager(mockedWorkingMemory, request);
152             fail("test should throw an exception here");
153         }
154         catch (IllegalArgumentException e) {
155             assertEquals("The value of policy engine manager environment property \"vfc.url\" may not be null", 
156                     e.getMessage());
157         }
158
159         // add url; username & password are not required
160         PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
161         new VfcManager(mockedWorkingMemory, request);
162
163         // url & username, but no password
164         PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
165
166         // url, username, and password
167         PolicyEngine.manager.getEnvironment().put("vfc.password", "Toto");
168         new VfcManager(mockedWorkingMemory, request);
169     }
170
171     @Test
172     public void testVfcExecutionException() throws InterruptedException {
173         PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
174         PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
175         PolicyEngine.manager.getEnvironment().put("vfc.password", "Exception");
176
177         VfcManager manager = new VfcManager(mockedWorkingMemory, request);
178         manager.setRestManager(mockedRestManager);
179
180         Thread managerThread = new Thread(manager);
181         managerThread.start();
182
183         when(mockedRestManager.post(
184             startsWith("http://somewhere.over.the.rainbow"),
185             eq("Dorothy"),
186             eq("Exception"),
187             anyMap(),
188             anyString(),
189             anyString()))
190             .thenThrow(new RuntimeException("OzException"));
191
192         managerThread.join();
193
194         PolicyEngine.manager.getEnvironment().remove("vfc.password");
195         PolicyEngine.manager.getEnvironment().remove("vfc.username");
196         PolicyEngine.manager.getEnvironment().remove("vfc.url");
197     }
198
199     @Test
200     public void testVfcExecutionNull() throws InterruptedException {
201         PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
202         PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
203         PolicyEngine.manager.getEnvironment().put("vfc.password", "Null");
204
205         VfcManager manager = new VfcManager(mockedWorkingMemory, request);
206         manager.setRestManager(mockedRestManager);
207
208         Thread managerThread = new Thread(manager);
209         managerThread.start();
210
211         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), 
212                 eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
213                 .thenReturn(null);
214
215         managerThread.join();
216
217         PolicyEngine.manager.getEnvironment().remove("vfc.password");
218         PolicyEngine.manager.getEnvironment().remove("vfc.username");
219         PolicyEngine.manager.getEnvironment().remove("vfc.url");
220     }
221
222     @Test
223     public void testVfcExecutionError0() throws InterruptedException {
224         PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
225         PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
226         PolicyEngine.manager.getEnvironment().put("vfc.password", "Error0");
227
228         VfcManager manager = new VfcManager(mockedWorkingMemory, request);
229         manager.setRestManager(mockedRestManager);
230
231         Thread managerThread = new Thread(manager);
232         managerThread.start();
233
234         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), 
235                 eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
236                 .thenReturn(httpResponseErr);
237
238         managerThread.join();
239
240         PolicyEngine.manager.getEnvironment().remove("vfc.password");
241         PolicyEngine.manager.getEnvironment().remove("vfc.username");
242         PolicyEngine.manager.getEnvironment().remove("vfc.url");
243     }
244
245     @Test
246     public void testVfcExecutionBadResponse() throws InterruptedException {
247         PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
248         PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
249         PolicyEngine.manager.getEnvironment().put("vfc.password", "BadResponse");
250
251         VfcManager manager = new VfcManager(mockedWorkingMemory, request);
252         manager.setRestManager(mockedRestManager);
253
254         Thread managerThread = new Thread(manager);
255         managerThread.start();
256
257         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), 
258                 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
259                 .thenReturn(httpResponseBadResponse);
260
261         managerThread.join();
262
263         PolicyEngine.manager.getEnvironment().remove("vfc.password");
264         PolicyEngine.manager.getEnvironment().remove("vfc.username");
265         PolicyEngine.manager.getEnvironment().remove("vfc.url");
266     }
267
268     @Test
269     public void testVfcExecutionOk() throws InterruptedException {
270         PolicyEngine.manager.getEnvironment().put("vfc.url", "http://somewhere.over.the.rainbow");
271         PolicyEngine.manager.getEnvironment().put("vfc.username", "Dorothy");
272         PolicyEngine.manager.getEnvironment().put("vfc.password", "OK");
273
274         VfcManager manager = new VfcManager(mockedWorkingMemory, request);
275         manager.setRestManager(mockedRestManager);
276
277         Thread managerThread = new Thread(manager);
278         managerThread.start();
279
280         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"), 
281                 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
282                 .thenReturn(httpResponsePutOk);
283
284         when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
285             .thenReturn(httpResponseGetOk);
286
287         managerThread.join();
288
289         PolicyEngine.manager.getEnvironment().remove("vfc.password");
290         PolicyEngine.manager.getEnvironment().remove("vfc.username");
291         PolicyEngine.manager.getEnvironment().remove("vfc.url");
292     }
293 }