Unique identifier for test execution
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / externaltesting-rest / externaltesting-rest-services / src / test / java / org / openecomp / sdcrests / externaltesting / rest / services / ApiTest.java
1 /*
2  * Copyright © 2019 iconectiv
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
17 package org.openecomp.sdcrests.externaltesting.rest.services;
18
19 import static org.mockito.Mockito.when;
20 import static org.mockito.MockitoAnnotations.initMocks;
21 import static org.powermock.api.mockito.PowerMockito.mockStatic;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Map;
27 import javax.ws.rs.core.Response;
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.openecomp.core.externaltesting.api.ClientConfiguration;
34 import org.openecomp.core.externaltesting.api.ExternalTestingManager;
35 import org.openecomp.core.externaltesting.api.RemoteTestingEndpointDefinition;
36 import org.openecomp.core.externaltesting.api.TestTreeNode;
37 import org.openecomp.core.externaltesting.api.VtpNameDescriptionPair;
38 import org.openecomp.core.externaltesting.api.VtpTestCase;
39 import org.openecomp.core.externaltesting.api.VtpTestExecutionOutput;
40 import org.openecomp.core.externaltesting.api.VtpTestExecutionRequest;
41 import org.openecomp.core.externaltesting.api.VtpTestExecutionResponse;
42 import org.openecomp.core.externaltesting.errors.ExternalTestingException;
43 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
44 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({VspManagerFactory.class})
50 public class ApiTest {
51
52     private static final String EP = "ep";
53     private static final String EXEC = "exec";
54     private static final String SC = "sc";
55     private static final String TS = "ts";
56     private static final String TC = "tc";
57     private static final String EXPECTED = "Expected";
58
59     @Mock
60     private ExternalTestingManager testingManager;
61     @Mock
62     private VspManagerFactory vspManagerFactory;
63     @Mock
64     VendorSoftwareProductManager vendorSoftwareProductManager;
65
66     @Before
67     public void setUp() {
68         try {
69             initMocks(this);
70             mockStatic(VspManagerFactory.class);
71             when(VspManagerFactory.getInstance()).thenReturn(vspManagerFactory);
72             when(vspManagerFactory.createInterface()).thenReturn(vendorSoftwareProductManager);
73             when(vspManagerFactory.getInstance().createInterface()).thenReturn(vendorSoftwareProductManager);
74         } catch (Exception e) {
75             e.printStackTrace();
76         }
77     }
78
79
80     /**
81      * At the API level, test that the code does not throw
82      * exceptions but there's not much to test.
83      */
84     @Test
85     public void testApi() {
86
87
88         ExternalTestingImpl testing = new ExternalTestingImpl(testingManager);
89         Assert.assertNotNull(testing.getConfig());
90         Assert.assertNotNull(testing.getEndpoints());
91         Assert.assertNotNull(testing.getExecution(EP, EXEC));
92         Assert.assertNotNull(testing.getScenarios(EP));
93         Assert.assertNotNull(testing.getTestcase(EP, SC, TS, TC));
94         Assert.assertNotNull(testing.getTestcases(EP, SC));
95         Assert.assertNotNull(testing.getTestsuites(EP, SC));
96         Assert.assertNotNull(testing.getTestCasesAsTree());
97
98         List<VtpTestExecutionRequest> requests =
99                 Arrays.asList(new VtpTestExecutionRequest(), new VtpTestExecutionRequest());
100         Assert.assertNotNull(testing.execute("vspId", "vspVersionId", "abc", null, "[]"));
101
102
103         ClientConfiguration cc = new ClientConfiguration();
104         Assert.assertNotNull(testing.setConfig(cc));
105
106         ArrayList<RemoteTestingEndpointDefinition> lst = new ArrayList<>();
107         Assert.assertNotNull(testing.setEndpoints(lst));
108     }
109
110     class ApiTestExternalTestingManager implements ExternalTestingManager {
111
112         @Override
113         public ClientConfiguration getConfig() {
114             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
115         }
116
117         @Override
118         public ClientConfiguration setConfig(ClientConfiguration config) {
119             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
120         }
121
122         @Override
123         public List<RemoteTestingEndpointDefinition> setEndpoints(List<RemoteTestingEndpointDefinition> endpoints) {
124             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
125         }
126
127         @Override
128         public TestTreeNode getTestCasesAsTree() {
129             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
130         }
131
132         @Override
133         public List<RemoteTestingEndpointDefinition> getEndpoints() {
134             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
135         }
136
137         @Override
138         public List<VtpNameDescriptionPair> getScenarios(String endpoint) {
139             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
140         }
141
142         @Override
143         public List<VtpNameDescriptionPair> getTestSuites(String endpoint, String scenario) {
144             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
145         }
146
147         @Override
148         public List<VtpTestCase> getTestCases(String endpoint, String scenario) {
149             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
150         }
151
152         @Override
153         public VtpTestCase getTestCase(String endpoint, String scenario, String testSuite, String testCaseName) {
154             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
155         }
156
157         @Override
158         public List<VtpTestExecutionResponse> execute(List<VtpTestExecutionRequest> requests, String vspId,
159                 String vspVersionId, String requestId, Map<String, byte[]> fileMap) {
160
161             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
162         }
163
164         @Override
165         public VtpTestExecutionResponse getExecution(String endpoint, String executionId) {
166             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
167         }
168
169         @Override
170         public List<VtpTestExecutionOutput> getExecutionIds(String endpoint, String requestId) {
171             throw new ExternalTestingException(EXPECTED, 500, EXPECTED);
172         }
173
174     }
175
176     /**
177      * Test the exception handler logic for configuration get/set.
178      */
179     @Test()
180     public void testConfigExceptions() {
181         initMocks(this);
182
183         ExternalTestingManager m = new ApiTestExternalTestingManager();
184         ExternalTestingImpl testingF = new ExternalTestingImpl(m);
185
186         Response getResponse = testingF.getConfig();
187         Assert.assertEquals(500, getResponse.getStatus());
188
189         Response setResponse = testingF.setConfig(new ClientConfiguration());
190         Assert.assertEquals(500, setResponse.getStatus());
191     }
192
193     /**
194      * Test the exception handler logic for endpoint get/set.
195      */
196     @Test()
197     public void testEndpointExceptions() {
198         initMocks(this);
199
200         ExternalTestingManager m = new ApiTestExternalTestingManager();
201         ExternalTestingImpl testingF = new ExternalTestingImpl(m);
202
203         Response getResponse = testingF.getEndpoints();
204         Assert.assertEquals(500, getResponse.getStatus());
205
206         Response setResponse = testingF.setEndpoints(new ArrayList<>());
207         Assert.assertEquals(500, setResponse.getStatus());
208     }
209
210     /**
211      * Test the exception handler logic for executions (invocation and query).
212      */
213     @Test()
214     public void testExecutionExceptions() {
215         initMocks(this);
216
217         ExternalTestingManager m = new ApiTestExternalTestingManager();
218         ExternalTestingImpl testingF = new ExternalTestingImpl(m);
219
220         Response invokeResponse = testingF.execute("vspId", "vspVersionId", "abc", null, "[]");
221         Assert.assertEquals(500, invokeResponse.getStatus());
222
223         Response getResponse = testingF.getExecution(EP, EXEC);
224         Assert.assertEquals(500, getResponse.getStatus());
225     }
226
227
228     /**
229      * Test the exception handler logic for the cases when the
230      * testing manager throws an accessing the scenarios.
231      */
232     @Test()
233     public void testScenarioExceptions() {
234         initMocks(this);
235
236         ExternalTestingManager m = new ApiTestExternalTestingManager();
237         ExternalTestingImpl testingF = new ExternalTestingImpl(m);
238
239         Response response = testingF.getScenarios(EP);
240         Assert.assertEquals(500, response.getStatus());
241     }
242
243     /**
244      * Test the exception handler logic for the cases when the
245      * testing manager throws an accessing a test case.
246      */
247     @Test()
248     public void testTestCaseExceptions() {
249         initMocks(this);
250
251         ExternalTestingManager m = new ApiTestExternalTestingManager();
252         ExternalTestingImpl testingF = new ExternalTestingImpl(m);
253
254         Response response = testingF.getTestcase(EP, SC, TS, TC);
255         Assert.assertEquals(500, response.getStatus());
256     }
257
258     /**
259      * Test the exception handler logic for the cases when the
260      * testing manager throws an accessing the test cases.
261      */
262     @Test()
263     public void testTestCasesExceptions() {
264         initMocks(this);
265
266         ExternalTestingManager m = new ApiTestExternalTestingManager();
267         ExternalTestingImpl testingF = new ExternalTestingImpl(m);
268
269         Response response = testingF.getTestcases(EP, SC);
270         Assert.assertEquals(500, response.getStatus());
271     }
272
273     /**
274      * Test the exception handler logic for the cases when the
275      * testing manager throws an accessing the test suites.
276      */
277     @Test()
278     public void testTestSuitesExceptions() {
279         initMocks(this);
280
281         ExternalTestingManager m = new ApiTestExternalTestingManager();
282         ExternalTestingImpl testingF = new ExternalTestingImpl(m);
283
284         Response response = testingF.getTestsuites(EP, SC);
285         Assert.assertEquals(500, response.getStatus());
286     }
287
288     /**
289      * Test the exception handler logic for the cases when the
290      * testing manager throws an accessing the test tree.
291      */
292     @Test()
293     public void testTreeExceptions() {
294         initMocks(this);
295
296         ExternalTestingManager m = new ApiTestExternalTestingManager();
297         ExternalTestingImpl testingF = new ExternalTestingImpl(m);
298
299         Response response = testingF.getTestCasesAsTree();
300         Assert.assertEquals(500, response.getStatus());
301     }
302 }