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