Code Improvements-Vnfsdk-refrepo sonar issue fixes
[vnfsdk/refrepo.git] / vnfmarket-be / vnf-sdk-marketplace / src / test / java / org / onap / vtp / execution / VTPExecutionResourceTest.java
1 /**
2  * Copyright 2019 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vtp.execution;
17
18 import com.google.gson.Gson;
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonParser;
21 import mockit.MockUp;
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;
34
35 import java.io.IOException;
36 import java.util.*;
37
38 import static org.junit.Assert.*;
39 @RunWith(MockitoJUnitRunner.class)
40 public class VTPExecutionResourceTest {
41
42     String requestId;
43     VTPExecutionResource vtpExecutionResource;
44     @Before
45     public void setUp() throws Exception {
46         requestId = UUID.randomUUID().toString();
47     }
48     @Test(expected = Exception.class)
49     public void testExecuteHandler() throws Exception
50     {
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);
56
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");
69         list.add(vtp);
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);
76
77     }
78     @Test
79     public void testExecuteHandlerForGsonCoverage() throws Exception
80     {
81         new MockUp<VTPResource>(){
82             @mockit.Mock
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);
87             }
88         };
89         new MockUp<Output>(){
90             @mockit.Mock
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);
95             }
96         };
97         new MockUp<Output>(){
98             @mockit.Mock
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);
103             }
104         };
105         new MockUp<Output>(){
106             @mockit.Mock
107             public boolean getSuccess() {
108                 return true;
109             }
110         };
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);
116
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");
129         list.add(vtp);
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);
136
137     }
138     @Test
139     public void testExecuteHandlerForGsonCoverageNegative() throws Exception
140     {
141         new MockUp<VTPResource>(){
142             @mockit.Mock
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);
147             }
148         };
149         new MockUp<Output>(){
150             @mockit.Mock
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);
155             }
156         };
157         new MockUp<Output>(){
158             @mockit.Mock
159             public Map<String, String> getAttrsMap() {
160                 String dummyValue = "{\"error\":\"DummyError occurs\"}";
161                 Gson gson = new Gson();
162                 return gson.fromJson(dummyValue,Map.class);
163             }
164         };
165         new MockUp<Output>(){
166             @mockit.Mock
167             public boolean getSuccess() {
168                 return false;
169             }
170         };
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);
176
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");
189         list.add(vtp);
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);
196
197     }
198     public void testListTestExecutionsHandler() throws Exception
199     {
200         vtpExecutionResource = new VTPExecutionResource();
201         vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123");
202     }
203     @Test
204     public void testListTestExecutionsHandlerForGson() throws Exception
205     {
206         new MockUp<VTPResource>(){
207             @mockit.Mock
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);
215             }
216         };
217         vtpExecutionResource = new VTPExecutionResource();
218         assertNotNull(vtpExecutionResource.listTestExecutionsHandler(requestId,"abc","abc","abc","abc","123","123"));
219     }
220     @Test
221     public void testListTestExecutionsHandlerTestmakeRpcAndGetJson() throws Exception
222     {
223         VTPExecutionResource vtpExecutionResource1 = new VTPExecutionResource();
224         VTPResource vtpResource = new VTPResource();
225
226         new MockUp<VTPResource>(){
227             @mockit.Mock
228             protected Result makeRpc(List <String> args, int timeout) throws VTPError.VTPException {
229                 Result result = Result.newBuilder().build();
230                 return result;
231             }
232         };
233         new MockUp<Result>(){
234             @mockit.Mock
235             public String getOutput() {
236                 return "[{\"product\":\"tutorial\"}]";
237             }
238         };
239         VTPTestExecution.VTPTestExecutionList vtpTestExecutionList = vtpExecutionResource1.listTestExecutionsHandler(requestId,"tutorial","ut","list-users","abc","123","123");
240         assertTrue(vtpTestExecutionList.getExecutions().size()>0);
241     }
242     public void testListTestExecutions() throws Exception
243     {
244         vtpExecutionResource = new VTPExecutionResource();
245         vtpExecutionResource.listTestExecutions(requestId,"abc","abc","abc","abc","123","123");
246     }
247     public void testGetTestExecution() throws Exception
248     {
249         //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
250         vtpExecutionResource = new VTPExecutionResource();
251         assertNotNull(vtpExecutionResource.getTestExecution("1234"));
252     }
253     public void testGetTestExecutionHandler() throws Exception
254     {
255         //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
256         vtpExecutionResource = new VTPExecutionResource();
257         assertNotNull(vtpExecutionResource.getTestExecutionHandler("1234"));
258     }
259     @Test
260     public void testGetTestExecutionHandlerForGson() throws Exception
261     {
262         new MockUp<VTPResource>(){
263             @mockit.Mock
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);
272             }
273         };
274         //assertNotNull(vtpExecutionResource.getTestExecution("abc"));
275         vtpExecutionResource = new VTPExecutionResource();
276         assertNotNull(vtpExecutionResource.getTestExecutionHandler("1234"));
277     }
278     @Test
279     public void testGetTestExecutionHandlerForGsonWithResultNull() throws Exception
280     {
281         new MockUp<VTPResource>(){
282             @mockit.Mock
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);
291             }
292         };
293         VTPExecutionResource vtpExecutionResource10 = new VTPExecutionResource();
294         assertNotNull(vtpExecutionResource10.getTestExecutionHandler("1234"));
295     }
296     @Test
297     public void testGetTestExecutionHandlerForGsonWithResultNullForCatchException() throws Exception
298     {
299         new MockUp<VTPResource>(){
300             @mockit.Mock
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);
310             }
311         };
312         VTPExecutionResource vtpExecutionResource11 = new VTPExecutionResource();
313         assertNotNull(vtpExecutionResource11.getTestExecutionHandler("1234"));
314     }
315
316     @Test
317     public void testExecuteTestcases() throws Exception
318     {
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());
323     }
324 }