2 * Copyright 2019 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vtp.execution;
18 import com.google.gson.Gson;
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonParser;
22 import org.glassfish.jersey.media.multipart.ContentDisposition;
23 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.onap.vtp.execution.model.VTPTestExecution;
30 import org.open.infc.grpc.Output;
31 import org.open.infc.grpc.Result;
32 import org.onap.vtp.VTPResource;
33 import org.onap.vtp.error.VTPError;
35 import java.io.IOException;
38 import static org.junit.Assert.*;
39 @RunWith(MockitoJUnitRunner.class)
40 public class VTPExecutionResourceTest {
43 VTPExecutionResource vtpExecutionResource;
45 public void setUp() throws Exception {
46 requestId = UUID.randomUUID().toString();
48 @Test(expected = Exception.class)
49 public void testExecuteHandler() throws Exception
51 VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList();
52 List<VTPTestExecution> list= new ArrayList<>();
53 JsonParser jsonParser = new JsonParser();
54 String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":\"nine\",\"verified\":\"false\"}";
55 JsonElement rootNode = jsonParser.parse(jsonString);
57 VTPTestExecution vtp=new VTPTestExecution();
58 vtp.setEndTime("2019-03-12T11:49:52.845");
59 vtp.setProfile("abc");
60 vtp.setStatus("pass");
61 vtp.setRequestId(requestId);
62 vtp.setExecutionId("executionid");
63 vtp.setParameters(rootNode);
64 vtp.setResults(rootNode);
65 vtp.setScenario("open-cli");
66 vtp.setStartTime("2019-04-12T11:49:52.845");
67 vtp.setTestCaseName("testcase");
68 vtp.setTestSuiteName("testsuite");
70 executions.setExecutions(list);
71 //System.out.println(executions.getExecutions());
72 assertNotNull(executions.getExecutions());
73 vtpExecutionResource = new VTPExecutionResource();
74 vtpExecutionResource.executeHandler(executions,null);
75 // vtpExecutionResource.executeHandler(executions,requestId);
79 public void testExecuteHandlerForGsonCoverage() throws Exception
81 new MockUp<VTPResource>(){
83 protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPError.VTPException {
84 String dummyValue = "{\"execution-id\":\"execution-id\"}";
85 Gson gson = new Gson();
86 return gson.fromJson(dummyValue,Output.class);
91 public Map<String, String> getAddonsMap() {
92 String dummyValue = "{\"execution-id\":\"execution-id\"}";
93 Gson gson = new Gson();
94 return gson.fromJson(dummyValue,Map.class);
99 public Map<String, String> getAttrsMap() {
100 String dummyValue = "{\"results\":[{\"execution-id\":\"execution-id\"}]}";
101 Gson gson = new Gson();
102 return gson.fromJson(dummyValue,Map.class);
105 new MockUp<Output>(){
107 public boolean getSuccess() {
111 VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList();
112 List<VTPTestExecution> list= new ArrayList<>();
113 JsonParser jsonParser = new JsonParser();
114 String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":\"nine\",\"verified\":\"false\"}";
115 JsonElement rootNode = jsonParser.parse(jsonString);
117 VTPTestExecution vtp=new VTPTestExecution();
118 vtp.setEndTime("2019-03-12T11:49:52.845");
119 vtp.setProfile("abc");
120 vtp.setStatus("pass");
121 vtp.setRequestId(requestId);
122 vtp.setExecutionId("executionid");
123 vtp.setParameters(rootNode);
124 vtp.setResults(rootNode);
125 vtp.setScenario("open-cli");
126 vtp.setStartTime("2019-04-12T11:49:52.845");
127 vtp.setTestCaseName("testcase");
128 vtp.setTestSuiteName("testsuite");
130 executions.setExecutions(list);
131 //System.out.println(executions.getExecutions());
132 assertNotNull(executions.getExecutions());
133 vtpExecutionResource = new VTPExecutionResource();
134 assertNotNull(vtpExecutionResource.executeHandler(executions,null));
135 // vtpExecutionResource.executeHandler(executions,requestId);
139 public void testExecuteHandlerForGsonCoverageNegative() throws Exception
141 new MockUp<VTPResource>(){
143 protected Output makeRpc(String scenario, String requestId, String profile, String testCase, JsonElement argsJsonNode) throws VTPError.VTPException {
144 String dummyValue = "{\"execution-id\":\"execution-id\"}";
145 Gson gson = new Gson();
146 return gson.fromJson(dummyValue,Output.class);
149 new MockUp<Output>(){
151 public Map<String, String> getAddonsMap() {
152 String dummyValue = "{\"execution-id\":\"execution-id\"}";
153 Gson gson = new Gson();
154 return gson.fromJson(dummyValue,Map.class);
157 new MockUp<Output>(){
159 public Map<String, String> getAttrsMap() {
160 String dummyValue = "{\"error\":\"DummyError occurs\"}";
161 Gson gson = new Gson();
162 return gson.fromJson(dummyValue,Map.class);
165 new MockUp<Output>(){
167 public boolean getSuccess() {
171 VTPTestExecution.VTPTestExecutionList executions= new VTPTestExecution.VTPTestExecutionList();
172 List<VTPTestExecution> list= new ArrayList<>();
173 JsonParser jsonParser = new JsonParser();
174 String jsonString = "{\"name\":\"Mahesh Kumar\", \"age\":\"nine\",\"verified\":\"false\"}";
175 JsonElement rootNode = jsonParser.parse(jsonString);
177 VTPTestExecution vtp=new VTPTestExecution();
178 vtp.setEndTime("2019-03-12T11:49:52.845");
179 vtp.setProfile("abc");
180 vtp.setStatus("pass");
181 vtp.setRequestId(requestId);
182 vtp.setExecutionId("executionid");
183 vtp.setParameters(rootNode);
184 vtp.setResults(rootNode);
185 vtp.setScenario("open-cli");
186 vtp.setStartTime("2019-04-12T11:49:52.845");
187 vtp.setTestCaseName("testcase");
188 vtp.setTestSuiteName("testsuite");
190 executions.setExecutions(list);
191 //System.out.println(executions.getExecutions());
192 assertNotNull(executions.getExecutions());
193 vtpExecutionResource = new VTPExecutionResource();
194 assertNotNull(vtpExecutionResource.executeHandler(executions,null));
195 // vtpExecutionResource.executeHandler(executions,requestId);
198 public void testListTestExecutionsHandler() throws Exception
200 vtpExecutionResource = new VTPExecutionResource();
201 vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123");
204 public void testListTestExecutionsHandlerForGson() throws Exception
206 new MockUp<VTPResource>(){
208 protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException {
209 String values = "[{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " +
210 "\"request-id\":\"request-id\", \"product\":\"product\"," +
211 "\"service\":\"service\", \"command\":\"command\", " +
212 "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"}]";
213 JsonParser jsonParser = new JsonParser();
214 return jsonParser.parse(values);
217 vtpExecutionResource = new VTPExecutionResource();
218 assertNotNull(vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123"));
221 public void testListTestExecutionsHandlerTestmakeRpcAndGetJson() throws Exception
223 VTPExecutionResource vtpExecutionResource1 = new VTPExecutionResource();
224 VTPResource vtpResource = new VTPResource();
226 new MockUp<VTPResource>(){
228 protected Result makeRpc(List <String> args, int timeout) throws VTPError.VTPException {
229 Result result = Result.newBuilder().build();
233 new MockUp<Result>(){
235 public String getOutput() {
236 return "[{\"product\":\"tutorial\"}]";
239 VTPTestExecution.VTPTestExecutionList vtpTestExecutionList = vtpExecutionResource1.listTestExecutionsHandler(requestId,"tutorial","ut","list-users","abc","123","123");
240 assertTrue(vtpTestExecutionList.getExecutions().size()>0);
242 public void testListTestExecutions() throws Exception
244 vtpExecutionResource = new VTPExecutionResource();
245 vtpExecutionResource.listTestExecutions(requestId,"abc","abc","abc","abc","123","123");
247 public void testGetTestExecution() throws Exception
249 //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
250 vtpExecutionResource = new VTPExecutionResource();
251 assertNotNull(vtpExecutionResource.getTestExecution("1234"));
253 public void testGetTestExecutionHandler() throws Exception
255 //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
256 vtpExecutionResource = new VTPExecutionResource();
257 assertNotNull(vtpExecutionResource.getTestExecutionHandler("1234"));
260 public void testGetTestExecutionHandlerForGson() throws Exception
262 new MockUp<VTPResource>(){
264 protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException {
265 String values = "{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " +
266 "\"request-id\":\"request-id\", \"product\":\"product\"," +
267 "\"service\":\"service\", \"command\":\"command\", " +
268 "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"," +
269 "\"input\": \"[]\", \"output\":\"[]\"}";
270 JsonParser jsonParser = new JsonParser();
271 return jsonParser.parse(values);
274 //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
275 vtpExecutionResource = new VTPExecutionResource();
276 assertNotNull(vtpExecutionResource.getTestExecutionHandler("1234"));
279 public void testGetTestExecutionHandlerForGsonWithResultNull() throws Exception
281 new MockUp<VTPResource>(){
283 protected JsonElement makeRpcAndGetJson(List<String> args, int timeout) throws VTPError.VTPException, IOException {
284 String values = "{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " +
285 "\"request-id\":\"request-id\", \"product\":\"product\"," +
286 "\"service\":\"service\", \"command\":\"command\", " +
287 "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"," +
288 "\"input\": \"[]\", \"output\":\"null\"}";
289 JsonParser jsonParser = new JsonParser();
290 return jsonParser.parse(values);
293 VTPExecutionResource vtpExecutionResource10 = new VTPExecutionResource();
294 assertNotNull(vtpExecutionResource10.getTestExecutionHandler("1234"));
297 public void testGetTestExecutionHandlerForGsonWithResultNullForCatchException() throws Exception
299 new MockUp<VTPResource>(){
301 protected JsonElement makeRpcAndGetJson(List<String> args, int timeout)
302 throws VTPError.VTPException, IOException {
303 String values = "{\"start-time\":\"start-time\", \"end-time\":\"end-time\", " +
304 "\"request-id\":\"request-id\", \"product\":\"product\"," +
305 "\"service\":\"service\", \"command\":\"command\", " +
306 "\"profile\":\"profile\", \"status\":\"status\", \"execution-id\":\"execution-id\"," +
307 "\"input\": \"[]\", \"output\":null}";
308 JsonParser jsonParser = new JsonParser();
309 return jsonParser.parse(values);
312 VTPExecutionResource vtpExecutionResource11 = new VTPExecutionResource();
313 assertNotNull(vtpExecutionResource11.getTestExecutionHandler("1234"));
317 public void testExecuteTestcases() throws Exception
319 vtpExecutionResource = new VTPExecutionResource();
320 String execJson = "[{\"scenario\":\"tutorial\",\"testCaseName\":\"list-users\",\"testSuiteName\":\"ut\"," +
321 "\"requestId\":\"1234567890\",\"executionId\":\"123\",\"profile\":\"http\"}]";
322 assertEquals(200, vtpExecutionResource.executeTestcases(requestId,null,"exeJson").getStatus());