Add list of errors in responce for GET request.
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / test / java / org / onap / vtp / VTPExecutionResourceTest.java
1 /**
2  * Copyright 2019 Huawei Technologies Co., Ltd.
3  * Copyright 2020 Nokia.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.onap.vtp;
18
19 import com.google.common.collect.Lists;
20 import com.google.gson.Gson;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonElement;
23 import com.google.gson.JsonParser;
24 import org.glassfish.jersey.media.multipart.ContentDisposition;
25 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.onap.vtp.error.VTPError;
34 import org.onap.vtp.execution.VTPExecutionResource;
35 import org.onap.vtp.execution.model.VTPTestExecution;
36
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.UUID;
41
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertNotNull;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class VTPExecutionResourceTest {
47
48     static class VTPExecutionResourceForTests extends VTPExecutionResource {
49         public JsonElement expectedRpcResponse = null;
50         public List<String> expectedArguments = null;
51
52         VTPExecutionResourceForTests() {
53             VTPExecutionResource.pathToExecutions = "src/test/resources/executions";
54         }
55
56         @Override
57         protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException {
58             if(expectedRpcResponse != null && expectedArguments != null) {
59                 if (args.containsAll(expectedArguments)) {
60                     return expectedRpcResponse;
61                 } else {
62                     return null;
63                 }
64             } else {
65                 return super.makeRpcAndGetJson( args, timeout);
66             }
67         }
68     }
69
70     @Mock
71     FormDataBodyPart formDataBodyPart;
72     @Mock
73     ContentDisposition contentDisposition;
74     String requestId;
75     VTPExecutionResourceForTests vtpExecutionResource;
76     @Before
77     public void setUp() {
78         vtpExecutionResource= new VTPExecutionResourceForTests();
79         requestId = UUID.randomUUID().toString();
80     }
81     @Test(expected = Exception.class)
82     public void testExecuteHandler() throws Exception
83     {
84         VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList();
85         List<VTPTestExecution> list= new ArrayList<>();
86         JsonParser jsonParser = new JsonParser();
87         String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":\"nine\",\"verified\":\"false\"}";
88         JsonElement rootNode = jsonParser.parse(jsonString);
89
90         VTPTestExecution vtp=new VTPTestExecution();
91         vtp.setEndTime("2019-03-12T11:49:52.845");
92         vtp.setProfile("open-cli-schema");
93         vtp.setStatus("pass");
94         vtp.setRequestId(requestId);
95         vtp.setExecutionId("executionid");
96         vtp.setParameters(rootNode);
97         vtp.setResults(rootNode);
98         vtp.setScenario("VTP Scenario 1");
99         vtp.setStartTime("2019-04-12T11:49:52.845");
100         vtp.setTestCaseName("s1.ts1.testcase-1");
101         vtp.setTestSuiteName("testsuite-1");
102         list.add(vtp);
103         executions.setExecutions(list);
104         System.out.println(executions.getExecutions());
105         assertNotNull(executions.getExecutions());
106        vtpExecutionResource.executeHandler(executions,null);
107     }
108
109     @Test
110     public void whenListTestExecutionsHandlerIsCalledWithProperParametersThenCorrectExecutionDataIsReturned()
111         throws IOException, VTPError.VTPException {
112         // given
113         String testStartTime = "2019-03-12T11:49:52.845";
114         String testEndTime = "2020-03-12T11:49:52.845";
115         String testProduct = "VTP Scenario 1";
116         String testCommand = "s1.ts1.testcase-1";
117         String testSuiteName = "testsuite-1";
118         String testRequestId = "test-01-request-id";
119         String testExecutionId = testRequestId + "-execution-id";
120         String testProfile = "open-cli-schema";
121         String expectedStatus = "SUCCESS";
122         JsonElement expectedResult = new Gson().fromJson("" +
123             "[{" +
124             "\"test_1\": \"error01\"," +
125             "\"test_2\": \"error02\" " +
126             "}]", JsonArray.class);
127
128         prepareMockRpcMethods(
129             testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
130             testRequestId, testExecutionId, testProfile, expectedStatus
131         );
132
133         // when
134         VTPTestExecution.VTPTestExecutionList testExecutionResponse =
135             vtpExecutionResource.listTestExecutionsHandler(
136                 testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
137             );
138
139         // then
140         assertThatListOfExecutionsContainsOneCorrectExecutionResponse(
141             testExecutionResponse,
142             testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
143             testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult
144         );
145     }
146
147     @Test
148     public void whenListTestExecutionsHandlerIsCalledWithIdThatDoesNotMatchAnyExecutionFileThenResultContainsProperInformation()
149         throws IOException, VTPError.VTPException {
150         // given
151         String testStartTime = "2020-08-09T08:49:52.845";
152         String testEndTime = "2020-08-10T08:49:55.845";
153         String testProduct = "VTP Scenario 2";
154         String testCommand = "s1.ts1.testcase-2";
155         String testSuiteName = "testsuite-2";
156         String testRequestId = "test-02-request-id";
157         String testExecutionId = testRequestId + "-execution-id";
158         String testProfile = "open-cli-schema";
159         String expectedStatus = "FAIL";
160         JsonElement expectedResult = new Gson().fromJson("" +
161             "[{ \"error\": \"unable to find execution results\"}]", JsonArray.class);
162
163         prepareMockRpcMethods(
164             testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
165             testRequestId, testExecutionId, testProfile, expectedStatus
166         );
167
168         // when
169         VTPTestExecution.VTPTestExecutionList testExecutionResponse =
170             vtpExecutionResource.listTestExecutionsHandler(
171                 testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
172             );
173
174         // then
175         assertThatListOfExecutionsContainsOneCorrectExecutionResponse(
176             testExecutionResponse,
177             testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
178             testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult
179         );
180     }
181
182     @Test
183     public void whenListTestExecutionsHandlerIsCalledWithIdThatMatchIncorrectExecutionFileThenResultContainsProperInformation()
184         throws IOException, VTPError.VTPException {
185         // given
186         String testStartTime = "2020-08-09T08:49:52.845";
187         String testEndTime = "2020-08-10T08:49:55.845";
188         String testProduct = "VTP Scenario 3";
189         String testCommand = "s1.ts1.testcase-3";
190         String testSuiteName = "testsuite-3";
191         String testRequestId = "test-incorrect-request-id";
192         String testExecutionId = testRequestId + "-execution-id";
193         String testProfile = "open-cli-schema";
194         String expectedStatus = "FAIL";
195         JsonElement expectedResult = new Gson().fromJson("" +
196                 "[{ " +
197                 "\"error\": \"fail to load execution result\"," +
198                 "\"reason\":\"Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive\"" +
199                 "}]",
200             JsonArray.class
201         );
202
203         prepareMockRpcMethods(
204             testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
205             testRequestId, testExecutionId, testProfile, expectedStatus
206         );
207
208         // when
209         VTPTestExecution.VTPTestExecutionList testExecutionResponse =
210             vtpExecutionResource.listTestExecutionsHandler(
211                 testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
212             );
213
214         // then
215         assertThatListOfExecutionsContainsOneCorrectExecutionResponse(
216             testExecutionResponse,
217             testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
218             testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult
219         );
220     }
221
222     @Rule
223     public ExpectedException exceptionRule = ExpectedException.none();
224
225     @Test
226     public void whenListTestExecutionsHandlerIsCalledAndGRpcClientIsUnReachableThenExceptionShouldBeThrown()
227         throws IOException, VTPError.VTPException {
228         // given
229         String testStartTime = "2020-08-10T08:50:20.845";
230         String testEndTime = "2020-08-11T08:51:50.845";
231         String testProduct = "VTP Scenario 3";
232         String testCommand = "s1.ts1.testcase-3";
233         String testSuiteName = "testsuite-3";
234         String testRequestId = "test-03-request-id";
235         String testProfile = "open-cli-schema";
236
237         // when
238         exceptionRule.expect(VTPError.VTPException.class);
239         exceptionRule.expectMessage("Timed out. Please use request-id to track the progress.");
240         vtpExecutionResource.listTestExecutionsHandler(
241             testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
242         );
243
244     }
245
246     private void assertThatListOfExecutionsContainsOneCorrectExecutionResponse(
247         VTPTestExecution.VTPTestExecutionList testExecutionResponse,
248         String testStartTime, String testEndTime, String testProduct, String testCommand,
249         String testSuiteName, String testRequestId, String testExecutionId, String testProfile,
250         String expectedStatus, JsonElement expectedResult ) {
251
252         assertNotNull(testExecutionResponse);
253         assertEquals(1, testExecutionResponse.getExecutions().size());
254         VTPTestExecution vtpTestExecution = testExecutionResponse.getExecutions().get(0);
255         assertFieldsInExecutionResponseAreCorrect(
256             vtpTestExecution,
257             testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
258             testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult
259         );
260     }
261
262     private void prepareMockRpcMethods(String testStartTime, String testEndTime, String testProduct, String testCommand, String testSuiteName, String testRequestId, String testExecutionId, String testProfile, String expectedStatus) {
263         vtpExecutionResource.expectedArguments =
264             buildListOfMockRpcArguments(
265                 testStartTime, testEndTime, testProduct, testCommand, testRequestId, testSuiteName
266             );
267         vtpExecutionResource.expectedRpcResponse =
268             buildExpectedRpcResponse(
269                 testStartTime, testEndTime, testProduct, testCommand, testSuiteName, testRequestId,
270                 testExecutionId, testProfile, expectedStatus
271             );
272     }
273
274     private JsonArray buildExpectedRpcResponse(
275         String testStartTime, String testEndTime, String testProduct,
276         String testCommand, String testSuiteName, String testRequestId,
277         String testExecutionId, String testProfile, String expectedStatus) {
278         return new Gson().fromJson("[{" +
279             "\"start-time\":\"" + testStartTime + "\";" +
280             "\"end-time\":\"" + testEndTime + "\";" +
281             "\"product\":\"" + testProduct + "\";" +
282             "\"command\":\"" + testCommand + "\";" +
283             "\"service\":\"" + testSuiteName + "\";" +
284             "\"request-id\":\"" + testRequestId + "\";" +
285             "\"execution-id\":\"" + testExecutionId + "\";" +
286             "\"profile\":\"" + testProfile + "\";" +
287             "\"status\":\"" + expectedStatus + "\"" +
288             "}]", JsonArray.class);
289     }
290
291     private List<String> buildListOfMockRpcArguments(
292         String testStartTime, String testEndTime, String testProduct,
293         String testCommand, String testRequestId, String testSuiteName) {
294         return Lists.newArrayList(
295             "--product", "open-cli", "execution-list", "--format", "json",
296             "--start-time", testStartTime, "--end-time", testEndTime,
297             "--service", testSuiteName, "--product", testProduct,
298             "--command", testCommand, "--request-id", testRequestId);
299     }
300
301     private void assertFieldsInExecutionResponseAreCorrect(
302         VTPTestExecution vtpTestExecution,
303         String testStartTime, String testEndTime, String testProduct,
304         String testCommand, String testSuiteName, String testRequestId,
305         String testExecutionId, String testProfile, String expectedStatus, JsonElement expectedResult) {
306         assertEquals(testStartTime, vtpTestExecution.getStartTime());
307         assertEquals(testEndTime, vtpTestExecution.getEndTime());
308         assertEquals(testProduct, vtpTestExecution.getScenario());
309         assertEquals(testCommand, vtpTestExecution.getTestCaseName());
310         assertEquals(testSuiteName, vtpTestExecution.getTestSuiteName());
311         assertEquals(testExecutionId, vtpTestExecution.getExecutionId());
312         assertEquals(testRequestId, vtpTestExecution.getRequestId());
313         assertEquals(testProfile, vtpTestExecution.getProfile());
314         assertEquals(expectedStatus, vtpTestExecution.getStatus());
315         assertEquals(expectedResult, vtpTestExecution.getResults());
316     }
317
318     @Test(expected = Exception.class)
319     public void testListTestExecutions() throws Exception
320     {
321         vtpExecutionResource.listTestExecutions(requestId, "abc", "abc", "abc", "abc", "123", "123");
322     }
323     @Test(expected = Exception.class)
324     public void testGetTestExecution() throws Exception
325     {
326         //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
327         assertNotNull(vtpExecutionResource.getTestExecution("1234"));
328     }
329     @Test(expected = Exception.class)
330     public void testGetTestExecutionHandler() throws Exception
331     {
332         //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
333         assertNotNull(vtpExecutionResource.getTestExecutionHandler("1234"));
334     }
335
336     @Test(expected = NullPointerException.class)
337     public void testExecuteTestcases() throws Exception
338     {
339
340         List<FormDataBodyPart> bodyParts= new ArrayList<>();
341         formDataBodyPart.setName("abc");
342         formDataBodyPart.setValue("123");
343         formDataBodyPart.setContentDisposition(contentDisposition);
344         formDataBodyPart.getContentDisposition().getFileName();
345         bodyParts.add(formDataBodyPart);
346       vtpExecutionResource.executeTestcases(requestId,bodyParts,"exeJson") ;
347     }
348
349     @Test(expected = Exception.class)
350     public void testGetTestExecutionLogsHandler() throws Exception {
351         assertNotNull(vtpExecutionResource.getTestExecutionLogsHandler("1234", "action"));
352     }
353 }