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.apache.commons.io.IOUtils;
23 import org.apache.commons.lang3.tuple.Pair;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.core.externaltesting.api.*;
31 import org.openecomp.core.externaltesting.errors.ExternalTestingException;
32 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
34 import org.openecomp.sdc.versioning.VersioningManager;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36 import org.springframework.core.ParameterizedTypeReference;
37 import org.springframework.http.*;
38 import org.springframework.util.LinkedMultiValueMap;
39 import org.springframework.web.client.HttpServerErrorException;
40 import org.springframework.web.client.HttpStatusCodeException;
41 import org.springframework.web.client.ResourceAccessException;
42 import org.springframework.web.client.RestTemplate;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.nio.charset.Charset;
50 @RunWith(MockitoJUnitRunner.class)
51 public class ExternalTestingManagerImplTests {
54 private RestTemplate restTemplate;
57 private VersioningManager versioningManager;
60 private VendorSoftwareProductManager vendorSoftwareProductManager;
63 private OrchestrationTemplateCandidateManager candidateManager;
66 private ExternalTestingManagerImpl mgr = new ExternalTestingManagerImpl();
68 @SuppressWarnings("unchecked")
69 private ExternalTestingManagerImpl configTestManager(boolean loadConfig) throws IOException {
71 MockitoAnnotations.initMocks(this);
77 ObjectMapper mapper = new ObjectMapper();
79 // read mock data for API calls.
81 File scenarioFile = new File("src/test/data/scenarios.json");
82 TypeReference<List<VtpNameDescriptionPair>> typ = new TypeReference<List<VtpNameDescriptionPair>>(){};
83 List<VtpNameDescriptionPair> scenarios = mapper.readValue(scenarioFile, typ);
85 File testSuitesFile = new File("src/test/data/testsuites.json");
86 List<VtpNameDescriptionPair> testSuites = mapper.readValue(testSuitesFile, new TypeReference<List<VtpNameDescriptionPair>>(){});
88 File testCasesFile = new File("src/test/data/testcases.json");
89 List<VtpTestCase> testCases = mapper.readValue(testCasesFile, new TypeReference<List<VtpTestCase>>(){});
91 File testCaseFile = new File("src/test/data/testcase-sriov.json");
92 VtpTestCase testCase = mapper.readValue(testCaseFile, VtpTestCase.class);
94 File runResultFile = new File("src/test/data/runresult.json");
95 List<VtpTestExecutionResponse> runResults = mapper.readValue(runResultFile, new TypeReference<List<VtpTestExecutionResponse>>(){});
97 File priorExecutionFile = new File("src/test/data/priorexecution.json");
98 VtpTestExecutionResponse priorExecution = mapper.readValue(priorExecutionFile, VtpTestExecutionResponse.class);
100 // create an error response as well
101 String notFound = FileUtils.readFileToString(new File("src/test/data/notfound.json"), "UTF-8");
103 ParameterizedTypeReference<List<VtpNameDescriptionPair>> listOfPairType = new ParameterizedTypeReference<List<VtpNameDescriptionPair>>() {};
104 ParameterizedTypeReference<List<VtpTestCase>> listOfCasesType = new ParameterizedTypeReference<List<VtpTestCase>>() {};
105 ParameterizedTypeReference<VtpTestCase> caseType = new ParameterizedTypeReference<VtpTestCase>() {};
107 HttpHeaders headers = new HttpHeaders();
108 headers.setContentType(MediaType.parseMediaType("application/problem+json"));
110 byte[] csar = IOUtils.toByteArray(new FileInputStream("src/test/data/csar.zip"));
111 byte[] heat = IOUtils.toByteArray(new FileInputStream("src/test/data/heat.zip"));
113 List<Version> versionList = new ArrayList<>();
114 versionList.add(new Version(UUID.randomUUID().toString()));
117 .when(candidateManager.get(
118 ArgumentMatchers.contains("csar"),
119 ArgumentMatchers.any()))
120 .thenReturn(Optional.of(Pair.of("Processed.zip", csar)));
123 .when(candidateManager.get(
124 ArgumentMatchers.contains("heat"),
125 ArgumentMatchers.any()))
126 .thenReturn(Optional.empty());
129 .when(vendorSoftwareProductManager.get(
130 ArgumentMatchers.contains("heat"),
131 ArgumentMatchers.any()))
132 .thenReturn(Optional.of(Pair.of("Processed.zip", heat)));
137 .when(vendorSoftwareProductManager.get(
138 ArgumentMatchers.contains("missing"),
139 ArgumentMatchers.any()))
140 .thenReturn(Optional.empty());
143 .when(candidateManager.get(
144 ArgumentMatchers.contains("missing"),
145 ArgumentMatchers.any()))
146 .thenReturn(Optional.empty());
150 .when(versioningManager.list(
151 ArgumentMatchers.contains("missing")))
152 .thenReturn(versionList);
160 .when(restTemplate.exchange(
161 ArgumentMatchers.endsWith("/scenarios"),
162 ArgumentMatchers.eq(HttpMethod.GET),
163 ArgumentMatchers.any(),
164 ArgumentMatchers.eq(listOfPairType)))
165 .thenReturn(new ResponseEntity(scenarios, HttpStatus.OK));
168 .when(restTemplate.exchange(
169 ArgumentMatchers.endsWith("/testsuites"),
170 ArgumentMatchers.eq(HttpMethod.GET),
171 ArgumentMatchers.any(),
172 ArgumentMatchers.eq(listOfPairType)))
173 .thenReturn(new ResponseEntity(testSuites, HttpStatus.OK));
176 .when(restTemplate.exchange(
177 ArgumentMatchers.endsWith("/testcases"),
178 ArgumentMatchers.eq(HttpMethod.GET),
179 ArgumentMatchers.any(),
180 ArgumentMatchers.eq(listOfCasesType)))
181 .thenReturn(new ResponseEntity(testCases, HttpStatus.OK));
184 .when(restTemplate.exchange(
185 ArgumentMatchers.endsWith("/sriov"),
186 ArgumentMatchers.eq(HttpMethod.GET),
187 ArgumentMatchers.any(),
188 ArgumentMatchers.eq(caseType)))
189 .thenReturn(new ResponseEntity(testCase, HttpStatus.OK));
192 // POST for execution
195 .when(restTemplate.exchange(
196 ArgumentMatchers.contains("executions"),
197 ArgumentMatchers.eq(HttpMethod.POST),
198 ArgumentMatchers.any(),
199 ArgumentMatchers.eq(new ParameterizedTypeReference<List<VtpTestExecutionResponse>>() {})))
200 .thenReturn(new ResponseEntity(runResults, HttpStatus.OK));
204 .when(restTemplate.exchange(
205 ArgumentMatchers.contains("/executions/"),
206 ArgumentMatchers.eq(HttpMethod.GET),
207 ArgumentMatchers.any(),
208 ArgumentMatchers.eq(new ParameterizedTypeReference<VtpTestExecutionResponse>() {})))
209 .thenReturn(new ResponseEntity(priorExecution, HttpStatus.OK));
212 HttpStatusCodeException missingException = new HttpServerErrorException(HttpStatus.NOT_FOUND, "Not Found", headers, notFound.getBytes(), Charset.defaultCharset());
214 .when(restTemplate.exchange(
215 ArgumentMatchers.endsWith("/missing"),
216 ArgumentMatchers.eq(HttpMethod.GET),
217 ArgumentMatchers.any(),
218 ArgumentMatchers.eq(caseType)))
219 .thenThrow(missingException);
223 .when(restTemplate.exchange(
224 ArgumentMatchers.endsWith("/sitedown"),
225 ArgumentMatchers.eq(HttpMethod.GET),
226 ArgumentMatchers.any(),
227 ArgumentMatchers.eq(caseType)))
228 .thenThrow(new ResourceAccessException("Remote site is down"));
231 .when(restTemplate.exchange(
232 ArgumentMatchers.endsWith("throwexception"),
233 ArgumentMatchers.eq(HttpMethod.POST),
234 ArgumentMatchers.any(),
235 ArgumentMatchers.eq(new ParameterizedTypeReference<List<VtpTestExecutionResponse>>() {})))
236 .thenThrow(missingException);
243 public void setConfigLocation() {
244 System.setProperty("config.location", "src/test/data");
248 public void testManager() throws IOException {
249 ExternalTestingManager m = configTestManager(true);
251 ClientConfiguration config = m.getConfig();
252 Assert.assertNotNull(config);
254 List<RemoteTestingEndpointDefinition> endpoints = m.getEndpoints();
255 Assert.assertEquals("two endpoints", 2, endpoints.size());
258 // this will exercise the internal APIs as well.
259 TestTreeNode root = m.getTestCasesAsTree();
260 Assert.assertEquals("two scenarios", 2, root.getChildren().size());
263 // handle case where remote endpoint is down.
265 m.getTestCase("repository", "scen", "suite", "sitedown");
266 Assert.fail("not expected to retrieve sitedown test case");
268 catch (ExternalTestingException e) {
269 // expecting this exception.
270 Assert.assertNotNull(e.getDetail());
271 Assert.assertNotEquals(0, e.getHttpStatus());
272 Assert.assertNotNull(e.getMessageCode());
275 // get a particular test case
277 m.getTestCase("repository", "scen", "suite", "missing");
278 Assert.fail("not expected to retrieve missing test case");
280 catch (ExternalTestingException e) {
281 // expecting this exception.
282 Assert.assertNotNull(e.getDetail());
283 Assert.assertNotEquals(0, e.getHttpStatus());
284 Assert.assertNotNull(e.getMessageCode());
289 public void testManagerExecution() throws IOException {
290 ExternalTestingManager m = configTestManager(true);
293 List<VtpTestExecutionRequest> requests = new ArrayList<>();
294 VtpTestExecutionRequest req = new VtpTestExecutionRequest();
295 req.setEndpoint("repository");
298 // send a request with the endpoint defined.
299 List<VtpTestExecutionResponse> responses = m.execute(requests, "rid");
300 Assert.assertEquals(1, responses.size());
302 // send a request for a prior execution.
303 VtpTestExecutionResponse execRsp = m.getExecution("repository", "execId");
304 Assert.assertEquals("COMPLETED", execRsp.getStatus());
308 public void testMissingConfig() throws IOException {
309 // directory exists but no config file should be found here.
310 System.setProperty("config.location", "src/test");
311 ExternalTestingManager m = configTestManager(true);
312 Assert.assertFalse("missing config client enabled false", m.getConfig().isEnabled());
313 Assert.assertEquals("missing config no endpoints", 0, m.getEndpoints().size());
317 public void testMissingEndpoint() throws IOException {
318 ExternalTestingManager m = configTestManager(true);
321 List<VtpTestExecutionRequest> requests = new ArrayList<>();
322 VtpTestExecutionRequest req = new VtpTestExecutionRequest();
323 req.setEndpoint("repository");
326 // send a request with the endpoint defined.
328 m.execute(requests, "throwexception");
330 catch (ExternalTestingException e) {
337 public void testManagerConfigOverrides() throws IOException {
338 ExternalTestingManager m = configTestManager(false);
340 ClientConfiguration cc = new ClientConfiguration();
343 Assert.assertTrue(m.getConfig().isEnabled());
345 List<RemoteTestingEndpointDefinition> lst = new ArrayList<>();
346 lst.add(new RemoteTestingEndpointDefinition());
347 lst.get(0).setEnabled(true);
349 Assert.assertEquals(1,m.getEndpoints().size());
353 public void testManagerErrorCases() throws IOException {
354 ExternalTestingManager m = configTestManager(false);
355 ClientConfiguration emptyConfig = m.getConfig();
356 Assert.assertFalse("empty configuration should have client enabled of false", emptyConfig.isEnabled());
360 Assert.assertTrue("should have exception here", true);
362 catch (ExternalTestingException e) {
363 // eat the exception cause this is what should happen.
368 public void testExecutionDistribution() throws IOException {
369 ExternalTestingManager m = configTestManager(true);
371 VtpTestExecutionRequest r1 = new VtpTestExecutionRequest();
372 r1.setScenario("scenario1");
373 r1.setEndpoint("vtp");
375 VtpTestExecutionRequest r2 = new VtpTestExecutionRequest();
376 r2.setScenario("scenario2");
377 r2.setEndpoint("vtp");
379 VtpTestExecutionRequest r3 = new VtpTestExecutionRequest();
380 r3.setScenario("scenario3");
381 r3.setEndpoint("repository");
383 List<VtpTestExecutionResponse> results = m.execute(Arrays.asList(r1,r2,r3), "rid");
384 Assert.assertEquals("three in two out merged", 2, results.size());
388 public void testArchiveProcessing() throws IOException {
389 ExternalTestingManagerImpl m = configTestManager(true);
390 VtpTestExecutionRequest r1 = new VtpTestExecutionRequest();
391 r1.setScenario("scenario1");
392 r1.setEndpoint("vtp");
393 r1.setParameters(new HashMap<>());
394 r1.getParameters().put(ExternalTestingManagerImpl.VSP_ID, "something.with.csar.content");
395 r1.getParameters().put(ExternalTestingManagerImpl.VSP_VERSION, UUID.randomUUID().toString());
397 LinkedMultiValueMap<String,Object> body = new LinkedMultiValueMap<>();
398 m.attachArchiveContent(r1, body);
400 r1.setParameters(new HashMap<>());
401 r1.getParameters().put(ExternalTestingManagerImpl.VSP_ID, "something.with.heat.content");
402 r1.getParameters().put(ExternalTestingManagerImpl.VSP_VERSION, UUID.randomUUID().toString());
404 LinkedMultiValueMap<String,Object> body2 = new LinkedMultiValueMap<>();
405 m.attachArchiveContent(r1, body2);
407 // now, let's handle a missing archive.
408 r1.setParameters(new HashMap<>());
409 r1.getParameters().put(ExternalTestingManagerImpl.VSP_ID, "something.with.missing.content");
410 r1.getParameters().put(ExternalTestingManagerImpl.VSP_VERSION, UUID.randomUUID().toString());
412 LinkedMultiValueMap<String,Object> body3 = new LinkedMultiValueMap<>();
414 m.attachArchiveContent(r1, body3);
415 Assert.fail("expected to receive an exception here");
417 catch (ExternalTestingException ex) {
418 Assert.assertEquals(500, ex.getHttpStatus());