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