2233f85ccf780073f9c1e291642d11b25eadefac
[sdc.git] /
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.core.externaltesting.impl;
18
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;
28 import org.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.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;
43
44 import java.io.File;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.nio.charset.Charset;
48 import java.util.*;
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class ExternalTestingManagerImplTests {
52
53   @Mock
54   private RestTemplate restTemplate;
55
56   @Mock
57   private VersioningManager versioningManager;
58
59   @Mock
60   private VendorSoftwareProductManager vendorSoftwareProductManager;
61
62   @Mock
63   private OrchestrationTemplateCandidateManager candidateManager;
64
65   @InjectMocks
66   private ExternalTestingManagerImpl mgr = new ExternalTestingManagerImpl();
67
68   @SuppressWarnings("unchecked")
69   private ExternalTestingManagerImpl configTestManager(boolean loadConfig) throws IOException {
70
71     MockitoAnnotations.initMocks(this);
72
73     if (loadConfig) {
74       mgr.init();
75     }
76
77     ObjectMapper mapper = new ObjectMapper();
78
79     // read mock data for API calls.
80
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);
84
85     File testSuitesFile = new File("src/test/data/testsuites.json");
86     List<VtpNameDescriptionPair> testSuites = mapper.readValue(testSuitesFile, new TypeReference<List<VtpNameDescriptionPair>>(){});
87
88     File testCasesFile = new File("src/test/data/testcases.json");
89     List<VtpTestCase> testCases = mapper.readValue(testCasesFile, new TypeReference<List<VtpTestCase>>(){});
90
91     File testCaseFile = new File("src/test/data/testcase-sriov.json");
92     VtpTestCase testCase = mapper.readValue(testCaseFile, VtpTestCase.class);
93
94     File runResultFile = new File("src/test/data/runresult.json");
95     List<VtpTestExecutionResponse> runResults = mapper.readValue(runResultFile, new TypeReference<List<VtpTestExecutionResponse>>(){});
96
97     File priorExecutionFile = new File("src/test/data/priorexecution.json");
98     VtpTestExecutionResponse priorExecution = mapper.readValue(priorExecutionFile, VtpTestExecutionResponse.class);
99
100     // create an error response as well
101     String notFound = FileUtils.readFileToString(new File("src/test/data/notfound.json"), "UTF-8");
102
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>() {};
106
107     HttpHeaders headers = new HttpHeaders();
108     headers.setContentType(MediaType.parseMediaType("application/problem+json"));
109
110     byte[] csar = IOUtils.toByteArray(new FileInputStream("src/test/data/csar.zip"));
111     byte[] heat = IOUtils.toByteArray(new FileInputStream("src/test/data/heat.zip"));
112
113     List<Version> versionList = new ArrayList<>();
114     versionList.add(new Version(UUID.randomUUID().toString()));
115
116     Mockito
117         .when(candidateManager.get(
118             ArgumentMatchers.contains("csar"),
119             ArgumentMatchers.any()))
120         .thenReturn(Optional.of(Pair.of("Processed.zip", csar)));
121
122     Mockito
123         .when(candidateManager.get(
124             ArgumentMatchers.contains("heat"),
125             ArgumentMatchers.any()))
126         .thenReturn(Optional.empty());
127
128     Mockito
129         .when(vendorSoftwareProductManager.get(
130             ArgumentMatchers.contains("heat"),
131             ArgumentMatchers.any()))
132         .thenReturn(Optional.of(Pair.of("Processed.zip", heat)));
133
134
135
136     Mockito
137         .when(vendorSoftwareProductManager.get(
138             ArgumentMatchers.contains("missing"),
139             ArgumentMatchers.any()))
140         .thenReturn(Optional.empty());
141
142     Mockito
143         .when(candidateManager.get(
144             ArgumentMatchers.contains("missing"),
145             ArgumentMatchers.any()))
146         .thenReturn(Optional.empty());
147
148
149     Mockito
150         .when(versioningManager.list(
151             ArgumentMatchers.contains("missing")))
152         .thenReturn(versionList);
153
154
155
156
157
158
159     Mockito
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));
166
167     Mockito
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));
174
175     Mockito
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));
182
183     Mockito
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));
190
191
192     // POST for execution
193
194     Mockito
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));
201
202
203     Mockito
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));
210
211
212     HttpStatusCodeException missingException = new HttpServerErrorException(HttpStatus.NOT_FOUND, "Not Found", headers, notFound.getBytes(), Charset.defaultCharset());
213     Mockito
214         .when(restTemplate.exchange(
215             ArgumentMatchers.endsWith("/missing"),
216             ArgumentMatchers.eq(HttpMethod.GET),
217             ArgumentMatchers.any(),
218             ArgumentMatchers.eq(caseType)))
219         .thenThrow(missingException);
220
221
222     Mockito
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"));
229
230     Mockito
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);
237
238
239     return mgr;
240   }
241
242   @Before
243   public void setConfigLocation() {
244     System.setProperty("config.location", "src/test/data");
245   }
246
247   @Test
248   public void testManager() throws IOException {
249     ExternalTestingManager m = configTestManager(true);
250
251     ClientConfiguration config = m.getConfig();
252     Assert.assertNotNull(config);
253
254     List<RemoteTestingEndpointDefinition> endpoints = m.getEndpoints();
255     Assert.assertEquals("two endpoints", 2, endpoints.size());
256
257
258     // this will exercise the internal APIs as well.
259     TestTreeNode root = m.getTestCasesAsTree();
260     Assert.assertEquals("two scenarios", 2, root.getChildren().size());
261
262
263     // handle case where remote endpoint is down.
264     try {
265       m.getTestCase("repository", "scen", "suite", "sitedown");
266       Assert.fail("not expected to retrieve sitedown test case");
267     }
268     catch (ExternalTestingException e) {
269       // expecting this exception.
270       Assert.assertNotNull(e.getDetail());
271       Assert.assertNotEquals(0, e.getHttpStatus());
272       Assert.assertNotNull(e.getMessageCode());
273     }
274
275     // get a particular test case
276     try {
277       m.getTestCase("repository", "scen", "suite", "missing");
278       Assert.fail("not expected to retrieve missing test case");
279     }
280     catch (ExternalTestingException e) {
281       // expecting this exception.
282       Assert.assertNotNull(e.getDetail());
283       Assert.assertNotEquals(0, e.getHttpStatus());
284       Assert.assertNotNull(e.getMessageCode());
285     }
286   }
287
288   @Test
289   public void testManagerExecution() throws IOException {
290     ExternalTestingManager m = configTestManager(true);
291
292     // execute a test.
293     List<VtpTestExecutionRequest> requests = new ArrayList<>();
294     VtpTestExecutionRequest req = new VtpTestExecutionRequest();
295     req.setEndpoint("repository");
296     requests.add(req);
297
298     // send a request with the endpoint defined.
299     List<VtpTestExecutionResponse> responses = m.execute(requests, "rid");
300     Assert.assertEquals(1, responses.size());
301
302     // send a request for a prior execution.
303     VtpTestExecutionResponse execRsp = m.getExecution("repository", "execId");
304     Assert.assertEquals("COMPLETED", execRsp.getStatus());
305   }
306
307   @Test
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());
314   }
315
316   @Test
317   public void testMissingEndpoint() throws IOException {
318     ExternalTestingManager m = configTestManager(true);
319
320     // execute a test.
321     List<VtpTestExecutionRequest> requests = new ArrayList<>();
322     VtpTestExecutionRequest req = new VtpTestExecutionRequest();
323     req.setEndpoint("repository");
324     requests.add(req);
325
326     // send a request with the endpoint defined.
327     try {
328       m.execute(requests, "throwexception");
329     }
330     catch (ExternalTestingException e) {
331       // expected.
332     }
333   }
334
335
336   @Test
337   public void testManagerConfigOverrides() throws IOException {
338     ExternalTestingManager m = configTestManager(false);
339
340     ClientConfiguration cc = new ClientConfiguration();
341     cc.setEnabled(true);
342     m.setConfig(cc);
343     Assert.assertTrue(m.getConfig().isEnabled());
344
345     List<RemoteTestingEndpointDefinition> lst = new ArrayList<>();
346     lst.add(new RemoteTestingEndpointDefinition());
347     lst.get(0).setEnabled(true);
348     m.setEndpoints(lst);
349     Assert.assertEquals(1,m.getEndpoints().size());
350   }
351
352   @Test
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());
357
358     try {
359       m.getEndpoints();
360       Assert.assertTrue("should have exception here", true);
361     }
362     catch (ExternalTestingException e) {
363       // eat the exception cause this is what should happen.
364     }
365   }
366
367   @Test
368   public void testExecutionDistribution() throws IOException {
369     ExternalTestingManager m = configTestManager(true);
370
371     VtpTestExecutionRequest r1 = new VtpTestExecutionRequest();
372     r1.setScenario("scenario1");
373     r1.setEndpoint("vtp");
374
375     VtpTestExecutionRequest r2 = new VtpTestExecutionRequest();
376     r2.setScenario("scenario2");
377     r2.setEndpoint("vtp");
378
379     VtpTestExecutionRequest r3 = new VtpTestExecutionRequest();
380     r3.setScenario("scenario3");
381     r3.setEndpoint("repository");
382
383     List<VtpTestExecutionResponse> results = m.execute(Arrays.asList(r1,r2,r3), "rid");
384     Assert.assertEquals("three in two out merged", 2, results.size());
385   }
386
387   @Test
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());
396
397     LinkedMultiValueMap<String,Object> body = new LinkedMultiValueMap<>();
398     m.attachArchiveContent(r1, body);
399
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());
403
404     LinkedMultiValueMap<String,Object> body2 = new LinkedMultiValueMap<>();
405     m.attachArchiveContent(r1, body2);
406
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());
411
412     LinkedMultiValueMap<String,Object> body3 = new LinkedMultiValueMap<>();
413     try {
414       m.attachArchiveContent(r1, body3);
415       Assert.fail("expected to receive an exception here");
416     }
417     catch (ExternalTestingException ex) {
418       Assert.assertEquals(500, ex.getHttpStatus());
419     }
420
421   }
422 }