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