2 * Copyright 2019 Huawei Technologies Co., Ltd.
3 * Copyright 2020 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.UUID;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertNotNull;
45 @RunWith(MockitoJUnitRunner.class)
46 public class VTPExecutionResourceTest {
48 static class VTPExecutionResourceForTests extends VTPExecutionResource {
49 public JsonElement expectedRpcResponse = null;
50 public List<String> expectedArguments = null;
52 VTPExecutionResourceForTests() {
53 VTPExecutionResource.pathToExecutions = "src/test/resources/executions";
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;
65 return super.makeRpcAndGetJson( args, timeout);
71 FormDataBodyPart formDataBodyPart;
73 ContentDisposition contentDisposition;
75 VTPExecutionResourceForTests vtpExecutionResource;
78 vtpExecutionResource= new VTPExecutionResourceForTests();
79 requestId = UUID.randomUUID().toString();
81 @Test(expected = Exception.class)
82 public void testExecuteHandler() throws Exception
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);
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");
103 executions.setExecutions(list);
104 System.out.println(executions.getExecutions());
105 assertNotNull(executions.getExecutions());
106 vtpExecutionResource.executeHandler(executions,null);
110 public void whenListTestExecutionsHandlerIsCalledWithProperParametersThenCorrectExecutionDataIsReturned()
111 throws IOException, VTPError.VTPException {
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("" +
124 "\"test_1\": \"error01\"," +
125 "\"test_2\": \"error02\" " +
126 "}]", JsonArray.class);
128 prepareMockRpcMethods(
129 testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
130 testRequestId, testExecutionId, testProfile, expectedStatus
134 VTPTestExecution.VTPTestExecutionList testExecutionResponse =
135 vtpExecutionResource.listTestExecutionsHandler(
136 testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
140 assertThatListOfExecutionsContainsOneCorrectExecutionResponse(
141 testExecutionResponse,
142 testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
143 testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult
148 public void whenListTestExecutionsHandlerIsCalledWithIdThatDoesNotMatchAnyExecutionFileThenResultContainsProperInformation()
149 throws IOException, VTPError.VTPException {
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);
163 prepareMockRpcMethods(
164 testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
165 testRequestId, testExecutionId, testProfile, expectedStatus
169 VTPTestExecution.VTPTestExecutionList testExecutionResponse =
170 vtpExecutionResource.listTestExecutionsHandler(
171 testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
175 assertThatListOfExecutionsContainsOneCorrectExecutionResponse(
176 testExecutionResponse,
177 testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
178 testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult
183 public void whenListTestExecutionsHandlerIsCalledWithIdThatMatchIncorrectExecutionFileThenResultContainsProperInformation()
184 throws IOException, VTPError.VTPException {
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("" +
197 "\"error\": \"fail to load execution result\"," +
198 "\"reason\":\"Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive\"" +
203 prepareMockRpcMethods(
204 testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
205 testRequestId, testExecutionId, testProfile, expectedStatus
209 VTPTestExecution.VTPTestExecutionList testExecutionResponse =
210 vtpExecutionResource.listTestExecutionsHandler(
211 testRequestId, testProduct, testSuiteName, testCommand, testProfile, testStartTime, testEndTime
215 assertThatListOfExecutionsContainsOneCorrectExecutionResponse(
216 testExecutionResponse,
217 testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
218 testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult
223 public ExpectedException exceptionRule = ExpectedException.none();
226 public void whenListTestExecutionsHandlerIsCalledAndGRpcClientIsUnReachableThenExceptionShouldBeThrown()
227 throws IOException, VTPError.VTPException {
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";
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
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 ) {
252 assertNotNull(testExecutionResponse);
253 assertEquals(1, testExecutionResponse.getExecutions().size());
254 VTPTestExecution vtpTestExecution = testExecutionResponse.getExecutions().get(0);
255 assertFieldsInExecutionResponseAreCorrect(
257 testStartTime, testEndTime, testProduct, testCommand, testSuiteName,
258 testRequestId, testExecutionId, testProfile, expectedStatus, expectedResult
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
267 vtpExecutionResource.expectedRpcResponse =
268 buildExpectedRpcResponse(
269 testStartTime, testEndTime, testProduct, testCommand, testSuiteName, testRequestId,
270 testExecutionId, testProfile, expectedStatus
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);
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);
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());
318 @Test(expected = Exception.class)
319 public void testListTestExecutions() throws Exception
321 vtpExecutionResource.listTestExecutions(requestId, "abc", "abc", "abc", "abc", "123", "123");
323 @Test(expected = Exception.class)
324 public void testGetTestExecution() throws Exception
326 //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
327 assertNotNull(vtpExecutionResource.getTestExecution("1234"));
329 @Test(expected = Exception.class)
330 public void testGetTestExecutionHandler() throws Exception
332 //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
333 assertNotNull(vtpExecutionResource.getTestExecutionHandler("1234"));
336 @Test(expected = NullPointerException.class)
337 public void testExecuteTestcases() throws Exception
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") ;
349 @Test(expected = Exception.class)
350 public void testGetTestExecutionLogsHandler() throws Exception {
351 assertNotNull(vtpExecutionResource.getTestExecutionLogsHandler("1234", "action"));