Remove drools PDP dependency
[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 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.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.rest.RestManager;
42 import org.onap.policy.rest.RestManager.Pair;
43 import org.onap.policy.vfc.VfcManager.VfcCallback;
44 import org.onap.policy.vfc.util.Serialization;
45
46 public class VfcManagerTest implements VfcCallback {
47
48     private RestManager   mockedRestManager;
49
50     private Pair<Integer, String> httpResponsePutOk;
51     private Pair<Integer, String> httpResponseGetOk;
52     private Pair<Integer, String> httpResponseBadResponse;
53     private Pair<Integer, String> httpResponseErr;
54
55     private VfcRequest  request;
56     private VfcResponse response;
57
58     /**
59      * Set up the mocked REST manager.
60      */
61     @Before
62     public void setupMockedRest() {
63         mockedRestManager   = mock(RestManager.class);
64
65         httpResponsePutOk       = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
66         httpResponseGetOk       = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
67         httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
68         httpResponseErr         = mockedRestManager.new Pair<>(200, null);
69     }
70
71     /**
72      * Create the request and response before.
73      */
74     @Before
75     public void createRequestAndResponse() {
76         VfcHealActionVmInfo actionInfo = new VfcHealActionVmInfo();
77         actionInfo.setVmid("TheWizard");
78         actionInfo.setVmname("The Wizard of Oz");
79
80         VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
81         additionalParams.setAction("Go Home");
82         additionalParams.setActionInfo(actionInfo);
83
84         VfcHealRequest healRequest = new VfcHealRequest();
85         healRequest.setAdditionalParams(additionalParams);
86         healRequest.setCause("WestWitch");
87         healRequest.setVnfInstanceId("EmeraldCity");
88
89         final UUID requestId = UUID.randomUUID();
90         request = new VfcRequest();
91         request.setHealRequest(healRequest);
92         request.setNsInstanceId("Dorothy");
93         request.setRequestId(requestId);
94
95         List<VfcResponseDescriptor> responseHistoryList = new ArrayList<>();;
96
97         VfcResponseDescriptor responseDescriptor = new VfcResponseDescriptor();
98         responseDescriptor.setErrorCode("1234");
99         responseDescriptor.setProgress("Follow The Yellow Brick Road");
100         responseDescriptor.setResponseHistoryList(responseHistoryList);
101         responseDescriptor.setResponseId(UUID.randomUUID().toString());
102         responseDescriptor.setStatus("finished");
103         responseDescriptor.setStatusDescription("There's no place like home");
104
105         response = new VfcResponse();
106         response.setJobId("1234");
107         response.setRequestId(request.getRequestId().toString());
108         response.setResponseDescriptor(responseDescriptor);
109     }
110
111     @Test
112     public void testVfcInitiation() {
113         try {
114             new VfcManager(null, null, null, null, null);
115             fail("test should throw an exception here");
116         }
117         catch (IllegalArgumentException e) {
118             assertEquals("the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null",
119                     e.getMessage());
120         }
121
122         try {
123             new VfcManager(this, null, null, null, null);
124             fail("test should throw an exception here");
125         }
126         catch (IllegalArgumentException e) {
127             assertEquals("the parameters \"cb\" and \"request\" on the VfcManager constructor may not be null",
128                     e.getMessage());
129         }
130
131         try {
132             new VfcManager(this, request, null, null, null);
133             fail("test should throw an exception here");
134         }
135         catch (IllegalArgumentException e) {
136             assertEquals("the \"url\" parameter on the VfcManager constructor may not be null",
137                     e.getMessage());
138         }
139
140         new VfcManager(this, request, "http://somewhere.over.the.rainbow", null, null);
141
142         new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Toto");
143     }
144
145     @Test
146     public void testVfcExecutionException() throws InterruptedException {
147         VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Exception");
148         manager.setRestManager(mockedRestManager);
149
150         Thread managerThread = new Thread(manager);
151         managerThread.start();
152
153         when(mockedRestManager.post(
154             startsWith("http://somewhere.over.the.rainbow"),
155             eq("Dorothy"),
156             eq("Exception"),
157             anyMap(),
158             anyString(),
159             anyString()))
160             .thenThrow(new RuntimeException("OzException"));
161
162         managerThread.join();
163     }
164
165     @Test
166     public void testVfcExecutionNull() throws InterruptedException {
167         VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Null");
168         manager.setRestManager(mockedRestManager);
169
170         Thread managerThread = new Thread(manager);
171         managerThread.start();
172
173         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
174                 eq("Dorothy"), eq("Null"), anyMap(), anyString(), anyString()))
175                 .thenReturn(null);
176
177         managerThread.join();
178     }
179
180     @Test
181     public void testVfcExecutionError0() throws InterruptedException {
182         VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Error0");
183         manager.setRestManager(mockedRestManager);
184
185         Thread managerThread = new Thread(manager);
186         managerThread.start();
187
188         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
189                 eq("Dorothy"), eq("Error0"), anyMap(), anyString(), anyString()))
190                 .thenReturn(httpResponseErr);
191
192         managerThread.join();
193     }
194
195     @Test
196     public void testVfcExecutionBadResponse() throws InterruptedException {
197         VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "BadResponse");
198         manager.setRestManager(mockedRestManager);
199
200         Thread managerThread = new Thread(manager);
201         managerThread.start();
202
203         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
204                 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
205                 .thenReturn(httpResponseBadResponse);
206
207         managerThread.join();
208     }
209
210     @Test
211     public void testVfcExecutionOk() throws InterruptedException {
212         VfcManager manager = new VfcManager(this, request, "http://somewhere.over.the.rainbow", "Dorothy", "Ok");
213         manager.setRestManager(mockedRestManager);
214
215         Thread managerThread = new Thread(manager);
216         managerThread.start();
217
218         when(mockedRestManager.post(startsWith("http://somewhere.over.the.rainbow"),
219                 eq("Dorothy"), eq("OK"), anyMap(), anyString(), anyString()))
220                 .thenReturn(httpResponsePutOk);
221
222         when(mockedRestManager.get(endsWith("1234"), eq("Dorothy"), eq("OK"), anyMap()))
223             .thenReturn(httpResponseGetOk);
224
225         managerThread.join();
226     }
227
228     @Override
229     public void onResponse(VfcResponse responseError) {
230         //
231         // Nothing needs to be done
232         //
233     }
234 }