List of Input Parameters for VSP
[sdc.git] / openecomp-be / lib / openecomp-sdc-externaltesting-lib / openecomp-sdc-externaltesting-impl / src / test / java / org / openecomp / core / externaltesting / impl / ExternalTestingManagerImplTests.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.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.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;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.nio.charset.Charset;
43 import java.util.*;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class ExternalTestingManagerImplTests {
47
48   @Mock
49   private RestTemplate restTemplate;
50
51   static {
52     System.setProperty("configuration.yaml", "src/test/data/testconfiguration.yaml");
53   }
54
55   class JUnitExternalTestingManagerImpl extends ExternalTestingManagerImpl {
56     JUnitExternalTestingManagerImpl() {
57       super(Collections.singletonList(
58         new VariableResolver() {
59
60         @Override
61         public boolean resolvesVariablesForRequest(VtpTestExecutionRequest requestItem) {
62           return false;
63         }
64
65         @Override
66         public void resolve(VtpTestExecutionRequest requestItem, MultiValueMap<String, Object> body) {
67
68               // unit test resolver does nothing for this case.  See specific test for resolver.
69         }
70       }));
71     }
72   }
73
74   @InjectMocks
75   private ExternalTestingManager mgr = new JUnitExternalTestingManagerImpl();
76
77   @SuppressWarnings("unchecked")
78   private ExternalTestingManager configTestManager(boolean loadConfig) throws IOException {
79     if (loadConfig) {
80       ((ExternalTestingManagerImpl) mgr).loadConfig();
81     }
82
83     ObjectMapper mapper = new ObjectMapper();
84
85     // read mock data for API calls.
86
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);
90
91     File testSuitesFile = new File("src/test/data/testsuites.json");
92     List<VtpNameDescriptionPair> testSuites = mapper.readValue(testSuitesFile, new TypeReference<List<VtpNameDescriptionPair>>(){});
93
94     File testCasesFile = new File("src/test/data/testcases.json");
95     List<VtpTestCase> testCases = mapper.readValue(testCasesFile, new TypeReference<List<VtpTestCase>>(){});
96
97     File testCaseFile = new File("src/test/data/testcase-sriov.json");
98     VtpTestCase testCase = mapper.readValue(testCaseFile, VtpTestCase.class);
99
100     File runResultFile = new File("src/test/data/runresult.json");
101     List<VtpTestExecutionResponse> runResults = mapper.readValue(runResultFile, new TypeReference<List<VtpTestExecutionResponse>>(){});
102
103     File priorExecutionFile = new File("src/test/data/priorexecution.json");
104     VtpTestExecutionResponse priorExecution = mapper.readValue(priorExecutionFile, VtpTestExecutionResponse.class);
105
106     // create an error response as well
107     String notFound = FileUtils.readFileToString(new File("src/test/data/notfound.json"), "UTF-8");
108
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>() {};
112
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());
116
117     Mockito
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));
124
125     Mockito
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));
132
133     Mockito
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));
140
141     Mockito
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));
148
149
150     // POST for execution
151
152     Mockito
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));
159
160
161     Mockito
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));
168
169     Mockito
170         .when(restTemplate.exchange(
171             ArgumentMatchers.endsWith("/missing"),
172             ArgumentMatchers.eq(HttpMethod.GET),
173             ArgumentMatchers.any(),
174             ArgumentMatchers.eq(caseType)))
175         .thenThrow(missingException);
176
177     Mockito
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"));
184
185     return mgr;
186   }
187
188   @Test
189   public void testManager() throws IOException {
190     System.setProperty("configuration.yaml", "src/test/data/managertestconfiguration.yaml");
191     ExternalTestingManager m = configTestManager(true);
192
193     String config = m.getConfig();
194     Assert.assertNotNull(config);
195
196     List<VtpNameDescriptionPair> endpoints = m.getEndpoints();
197     Assert.assertEquals("two endpoints", 2, endpoints.size());
198
199
200     // this will exercise the internal APIs as well.
201     TestTreeNode root = m.getTestCasesAsTree();
202     Assert.assertEquals("two scenarios", 2, root.getChildren().size());
203
204
205     // handle case where remote endpoint is down.
206     try {
207       m.getTestCase("repository", "scen", "suite", "sitedown");
208       Assert.fail("not expected to retrieve sitedown test case");
209     }
210     catch (ExternalTestingException e) {
211       // expecting this exception.
212       Assert.assertNotNull(e.getDetail());
213       Assert.assertNotEquals(0, e.getCode());
214       Assert.assertNotNull(e.getTitle());
215     }
216
217     // get a particular test case
218     try {
219       m.getTestCase("repository", "scen", "suite", "missing");
220       Assert.fail("not expected to retrieve missing test case");
221     }
222     catch (ExternalTestingException e) {
223       // expecting this exception.
224       Assert.assertNotNull(e.getDetail());
225       Assert.assertNotEquals(0, e.getCode());
226       Assert.assertNotNull(e.getTitle());
227     }
228
229
230     // execute a test.
231     List<VtpTestExecutionRequest> requests = new ArrayList<>();
232     VtpTestExecutionRequest req = new VtpTestExecutionRequest();
233     req.setEndpoint("repository");
234     requests.add(req);
235
236     // send a request with the endpoint defined.
237     List<VtpTestExecutionResponse> responses = m.execute( requests, "rid");
238     Assert.assertEquals(1,responses.size());
239
240     // send a request for a prior execution.
241     VtpTestExecutionResponse execRsp = m.getExecution("repository", "execId");
242     Assert.assertEquals("COMPLETED", execRsp.getStatus());
243   }
244
245   @Test
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);
253
254       try {
255         m.getEndpoints();
256         Assert.assertTrue("should have exception here", true);
257       }
258       catch (ExternalTestingException e) {
259         // eat the exception cause this is what should happen.
260       }
261     }
262
263   @Test
264   public void testExecutionDistribution() throws IOException {
265     System.setProperty("configuration.yaml", "src/test/data/managertestconfiguration.yaml");
266     ExternalTestingManager m = configTestManager(true);
267
268     VtpTestExecutionRequest r1 = new VtpTestExecutionRequest();
269     r1.setScenario("scenario1");
270     r1.setEndpoint("vtp");
271
272     VtpTestExecutionRequest r2 = new VtpTestExecutionRequest();
273     r2.setScenario("scenario2");
274     r2.setEndpoint("vtp");
275
276     VtpTestExecutionRequest r3 = new VtpTestExecutionRequest();
277     r3.setScenario("scenario3");
278     r3.setEndpoint("repository");
279
280     List<VtpTestExecutionResponse> results = m.execute(Arrays.asList(r1,r2,r3), "rid");
281     Assert.assertEquals("three in two out merged", 2, results.size());
282   }
283 }