2 * Copyright © 2019 iconectiv
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.
17 package org.openecomp.core.externaltesting.impl;
19 import com.fasterxml.jackson.core.type.TypeReference;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import org.apache.commons.io.FileUtils;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.ArgumentMatchers;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.core.externaltesting.api.*;
31 import org.openecomp.core.externaltesting.errors.ExternalTestingException;
32 import org.springframework.core.ParameterizedTypeReference;
33 import org.springframework.http.*;
34 import org.springframework.util.MultiValueMap;
35 import org.springframework.web.client.HttpServerErrorException;
36 import org.springframework.web.client.HttpStatusCodeException;
37 import org.springframework.web.client.ResourceAccessException;
38 import org.springframework.web.client.RestTemplate;
41 import java.io.IOException;
42 import java.nio.charset.Charset;
45 @RunWith(MockitoJUnitRunner.class)
46 public class ExternalTestingManagerImplTests {
49 private RestTemplate restTemplate;
52 System.setProperty("configuration.yaml", "src/test/data/testconfiguration.yaml");
55 class JUnitExternalTestingManagerImpl extends ExternalTestingManagerImpl {
56 JUnitExternalTestingManagerImpl() {
57 super(Collections.singletonList(
58 new VariableResolver() {
61 public boolean resolvesVariablesForRequest(VtpTestExecutionRequest requestItem) {
66 public void resolve(VtpTestExecutionRequest requestItem, MultiValueMap<String, Object> body) {
68 // unit test resolver does nothing for this case. See specific test for resolver.
75 private ExternalTestingManager mgr = new JUnitExternalTestingManagerImpl();
77 @SuppressWarnings("unchecked")
78 private ExternalTestingManager configTestManager(boolean loadConfig) throws IOException {
80 ((ExternalTestingManagerImpl) mgr).loadConfig();
83 ObjectMapper mapper = new ObjectMapper();
85 // read mock data for API calls.
87 File scenarioFile = new File("src/test/data/scenarios.json");
88 TypeReference<List<VtpNameDescriptionPair>> typ = new TypeReference<List<VtpNameDescriptionPair>>(){};
89 List<VtpNameDescriptionPair> scenarios = mapper.readValue(scenarioFile, typ);
91 File testSuitesFile = new File("src/test/data/testsuites.json");
92 List<VtpNameDescriptionPair> testSuites = mapper.readValue(testSuitesFile, new TypeReference<List<VtpNameDescriptionPair>>(){});
94 File testCasesFile = new File("src/test/data/testcases.json");
95 List<VtpTestCase> testCases = mapper.readValue(testCasesFile, new TypeReference<List<VtpTestCase>>(){});
97 File testCaseFile = new File("src/test/data/testcase-sriov.json");
98 VtpTestCase testCase = mapper.readValue(testCaseFile, VtpTestCase.class);
100 File runResultFile = new File("src/test/data/runresult.json");
101 List<VtpTestExecutionResponse> runResults = mapper.readValue(runResultFile, new TypeReference<List<VtpTestExecutionResponse>>(){});
103 File priorExecutionFile = new File("src/test/data/priorexecution.json");
104 VtpTestExecutionResponse priorExecution = mapper.readValue(priorExecutionFile, VtpTestExecutionResponse.class);
106 // create an error response as well
107 String notFound = FileUtils.readFileToString(new File("src/test/data/notfound.json"), "UTF-8");
109 ParameterizedTypeReference<List<VtpNameDescriptionPair>> listOfPairType = new ParameterizedTypeReference<List<VtpNameDescriptionPair>>() {};
110 ParameterizedTypeReference<List<VtpTestCase>> listOfCasesType = new ParameterizedTypeReference<List<VtpTestCase>>() {};
111 ParameterizedTypeReference<VtpTestCase> caseType = new ParameterizedTypeReference<VtpTestCase>() {};
113 HttpHeaders headers = new HttpHeaders();
114 headers.setContentType(MediaType.parseMediaType("application/problem+json"));
115 HttpStatusCodeException missingException = new HttpServerErrorException(HttpStatus.NOT_FOUND, "Not Found", headers, notFound.getBytes(), Charset.defaultCharset());
118 .when(restTemplate.exchange(
119 ArgumentMatchers.endsWith("/scenarios"),
120 ArgumentMatchers.eq(HttpMethod.GET),
121 ArgumentMatchers.any(),
122 ArgumentMatchers.eq(listOfPairType)))
123 .thenReturn(new ResponseEntity(scenarios, HttpStatus.OK));
126 .when(restTemplate.exchange(
127 ArgumentMatchers.endsWith("/testsuites"),
128 ArgumentMatchers.eq(HttpMethod.GET),
129 ArgumentMatchers.any(),
130 ArgumentMatchers.eq(listOfPairType)))
131 .thenReturn(new ResponseEntity(testSuites, HttpStatus.OK));
134 .when(restTemplate.exchange(
135 ArgumentMatchers.endsWith("/testcases"),
136 ArgumentMatchers.eq(HttpMethod.GET),
137 ArgumentMatchers.any(),
138 ArgumentMatchers.eq(listOfCasesType)))
139 .thenReturn(new ResponseEntity(testCases, HttpStatus.OK));
142 .when(restTemplate.exchange(
143 ArgumentMatchers.endsWith("/sriov"),
144 ArgumentMatchers.eq(HttpMethod.GET),
145 ArgumentMatchers.any(),
146 ArgumentMatchers.eq(caseType)))
147 .thenReturn(new ResponseEntity(testCase, HttpStatus.OK));
150 // POST for execution
153 .when(restTemplate.exchange(
154 ArgumentMatchers.contains("executions"),
155 ArgumentMatchers.eq(HttpMethod.POST),
156 ArgumentMatchers.any(),
157 ArgumentMatchers.eq(new ParameterizedTypeReference<List<VtpTestExecutionResponse>>() {})))
158 .thenReturn(new ResponseEntity(runResults, HttpStatus.OK));
162 .when(restTemplate.exchange(
163 ArgumentMatchers.contains("/executions/"),
164 ArgumentMatchers.eq(HttpMethod.GET),
165 ArgumentMatchers.any(),
166 ArgumentMatchers.eq(new ParameterizedTypeReference<VtpTestExecutionResponse>() {})))
167 .thenReturn(new ResponseEntity(priorExecution, HttpStatus.OK));
170 .when(restTemplate.exchange(
171 ArgumentMatchers.endsWith("/missing"),
172 ArgumentMatchers.eq(HttpMethod.GET),
173 ArgumentMatchers.any(),
174 ArgumentMatchers.eq(caseType)))
175 .thenThrow(missingException);
178 .when(restTemplate.exchange(
179 ArgumentMatchers.endsWith("/sitedown"),
180 ArgumentMatchers.eq(HttpMethod.GET),
181 ArgumentMatchers.any(),
182 ArgumentMatchers.eq(caseType)))
183 .thenThrow(new ResourceAccessException("Remote site is down"));
189 public void testManager() throws IOException {
190 System.setProperty("configuration.yaml", "src/test/data/managertestconfiguration.yaml");
191 ExternalTestingManager m = configTestManager(true);
193 String config = m.getConfig();
194 Assert.assertNotNull(config);
196 List<VtpNameDescriptionPair> endpoints = m.getEndpoints();
197 Assert.assertEquals("two endpoints", 2, endpoints.size());
200 // this will exercise the internal APIs as well.
201 TestTreeNode root = m.getTestCasesAsTree();
202 Assert.assertEquals("two scenarios", 2, root.getChildren().size());
205 // handle case where remote endpoint is down.
207 m.getTestCase("repository", "scen", "suite", "sitedown");
208 Assert.fail("not expected to retrieve sitedown test case");
210 catch (ExternalTestingException e) {
211 // expecting this exception.
212 Assert.assertNotNull(e.getDetail());
213 Assert.assertNotEquals(0, e.getCode());
214 Assert.assertNotNull(e.getTitle());
217 // get a particular test case
219 m.getTestCase("repository", "scen", "suite", "missing");
220 Assert.fail("not expected to retrieve missing test case");
222 catch (ExternalTestingException e) {
223 // expecting this exception.
224 Assert.assertNotNull(e.getDetail());
225 Assert.assertNotEquals(0, e.getCode());
226 Assert.assertNotNull(e.getTitle());
231 List<VtpTestExecutionRequest> requests = new ArrayList<>();
232 VtpTestExecutionRequest req = new VtpTestExecutionRequest();
233 req.setEndpoint("repository");
236 // send a request with the endpoint defined.
237 List<VtpTestExecutionResponse> responses = m.execute( requests, "rid");
238 Assert.assertEquals(1,responses.size());
240 // send a request for a prior execution.
241 VtpTestExecutionResponse execRsp = m.getExecution("repository", "execId");
242 Assert.assertEquals("COMPLETED", execRsp.getStatus());
246 public void testManagerErrorCases() throws IOException {
247 ExternalTestingManager m = configTestManager(false);
248 Map<String,Object> expectedEmptyConfig = new HashMap<>();
249 expectedEmptyConfig.put("enabled", false);
250 String expected = new ObjectMapper().writeValueAsString(expectedEmptyConfig);
251 String emptyConfig = m.getConfig();
252 Assert.assertEquals(expected, emptyConfig);
256 Assert.assertTrue("should have exception here", true);
258 catch (ExternalTestingException e) {
259 // eat the exception cause this is what should happen.
264 public void testExecutionDistribution() throws IOException {
265 System.setProperty("configuration.yaml", "src/test/data/managertestconfiguration.yaml");
266 ExternalTestingManager m = configTestManager(true);
268 VtpTestExecutionRequest r1 = new VtpTestExecutionRequest();
269 r1.setScenario("scenario1");
270 r1.setEndpoint("vtp");
272 VtpTestExecutionRequest r2 = new VtpTestExecutionRequest();
273 r2.setScenario("scenario2");
274 r2.setEndpoint("vtp");
276 VtpTestExecutionRequest r3 = new VtpTestExecutionRequest();
277 r3.setScenario("scenario3");
278 r3.setEndpoint("repository");
280 List<VtpTestExecutionResponse> results = m.execute(Arrays.asList(r1,r2,r3), "rid");
281 Assert.assertEquals("three in two out merged", 2, results.size());