2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright (c) 2017-2019 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.babel.service;
24 import static org.hamcrest.CoreMatchers.containsString;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is;
28 import com.google.gson.Gson;
29 import java.io.IOException;
31 import java.net.URISyntaxException;
32 import java.security.cert.X509Certificate;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map.Entry;
36 import java.util.Optional;
37 import javax.inject.Inject;
38 import javax.security.auth.x500.X500Principal;
39 import javax.ws.rs.core.HttpHeaders;
40 import javax.ws.rs.core.MultivaluedHashMap;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriInfo;
43 import org.junit.jupiter.api.BeforeAll;
44 import org.junit.jupiter.api.Test;
45 import org.mockito.Mockito;
46 import org.onap.aai.auth.AAIAuthException;
47 import org.onap.aai.auth.AAIMicroServiceAuth;
48 import org.onap.aai.babel.service.data.BabelRequest;
49 import org.onap.aai.babel.testdata.CsarTest;
50 import org.onap.aai.babel.util.ArtifactTestUtils;
51 import org.springframework.mock.web.MockHttpServletRequest;
52 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
55 * Direct invocation of the generate artifacts service implementation.
58 @SpringJUnitConfig(locations = {"classpath:/babel-beans.xml"})
59 public class TestGenerateArtifactsServiceImpl {
62 System.setProperty("CONFIG_HOME", "src/test/resources");
66 private AAIMicroServiceAuth auth;
69 public static void setup() {
70 new ArtifactTestUtils().setGeneratorSystemProperties();
74 * Test with a valid request (and valid CSAR content) by calling the Service implementation directly using a mocked
77 * @throws URISyntaxException
78 * if the URI cannot be created
80 * if the resource cannot be loaded
83 public void testGenerateArtifacts() throws URISyntaxException, IOException {
84 Response response = processJsonRequest(CsarTest.VNF_VENDOR_CSAR, auth);
85 assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
86 assertThat(response.getEntity(), is(getResponseJson("response.json")));
90 * Test with a valid request that has no Transaction ID header value.
92 * @throws URISyntaxException
93 * if the URI cannot be created
95 * if the resource cannot be loaded
98 public void testGenerateArtifactsWithoutRequestId() throws URISyntaxException, IOException {
99 Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequest(), Optional.empty(), auth);
100 assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
101 assertThat(response.getEntity(), is(getResponseJson("response.json")));
105 * Test with a valid request without Minor Artifact version.
107 * @throws URISyntaxException
108 * if the URI cannot be created
109 * @throws IOException
110 * if the resource cannot be loaded
113 public void testGenerateArtifactsWithoutMinorArtifactVersion() throws URISyntaxException, IOException {
114 Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("1"),
115 Optional.of("transaction-id"), auth);
116 assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
117 assertThat(response.getEntity(), is(getResponseJson("response.json")));
121 * Test with a valid request without Minor Artifact version.
123 * @throws URISyntaxException
124 * if the URI cannot be created
125 * @throws IOException
126 * if the resource cannot be loaded
129 public void testGenerateArtifactsWithInvalidArtifactVersion() throws URISyntaxException, IOException {
130 Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("a"),
131 Optional.of("transaction-id"), auth);
132 assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
133 assertThat(response.getEntity(), is(getResponseJson("response.json")));
138 * Test with a valid request with Artifact version less than 1.
140 * @throws URISyntaxException
141 * if the URI cannot be created
142 * @throws IOException
143 * if the resource cannot be loaded
146 public void testGenerateArtifactsWithArtifactVerLessThan1() throws URISyntaxException, IOException {
147 Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("0.1"),
148 Optional.of("transaction-id"), auth);
149 assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode()));
150 assertThat(response.getEntity(), is(getResponseJson("responseWithVersionLessThan1.json")));
155 * Test with a valid request, using a CSAR file that has no VNF configuration present.
157 * @throws URISyntaxException
158 * if the URI cannot be created
159 * @throws IOException
160 * if the resource cannot be loaded
163 public void testGenerateArtifactsWithoutVnfConfiguration() throws IOException, URISyntaxException {
164 Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, auth);
165 assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
166 assertThat(response.getEntity(), is(getResponseJson("validNoVnfConfigurationResponse.json")));
170 * Test for a valid request with invalid CSAR file content.
172 * @throws URISyntaxException
173 * if the URI cannot be created
174 * @throws IOException
175 * if the resource cannot be loaded
178 public void testGenerateArtifactsInvalidCsar() throws IOException, URISyntaxException {
179 Response response = processJsonRequest(CsarTest.MULTIPLE_VNF_CSAR, auth);
180 assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
181 assertThat(response.getEntity().toString(), containsString("VNF catalog"));
185 public void testUninitializedService() throws IOException, URISyntaxException, AAIAuthException {
186 AAIMicroServiceAuth uninitializedAuth = Mockito.mock(AAIMicroServiceAuth.class);
187 Mockito.when(uninitializedAuth.validateRequest(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
188 .thenThrow(new AAIAuthException("test"));
189 Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, uninitializedAuth);
190 assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
191 assertThat(response.getEntity().toString(), containsString("check the Babel service logs"));
195 public void testUnauthorizedRequest() throws IOException, URISyntaxException, AAIAuthException {
196 AAIMicroServiceAuth uninitializedAuth = Mockito.mock(AAIMicroServiceAuth.class);
197 Mockito.when(uninitializedAuth.validateRequest(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
199 Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, uninitializedAuth);
200 assertThat(response.getStatus(), is(Response.Status.UNAUTHORIZED.getStatusCode()));
201 assertThat(response.getEntity().toString(), containsString("User not authorized"));
205 public void testInvalidCsarFile() throws URISyntaxException, IOException {
206 BabelRequest request = new BabelRequest();
207 request.setArtifactName("hello");
208 request.setArtifactVersion("1.0");
209 request.setCsar("xxxx");
210 Response response = invokeService(new Gson().toJson(request));
211 assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
212 assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model."));
216 public void testInvalidJsonFile() throws URISyntaxException, IOException {
217 Response response = invokeService("{\"csar:\"xxxx\"");
218 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
219 assertThat(response.getEntity(), is("Malformed request."));
223 public void testMissingArtifactName() throws Exception {
224 BabelRequest request = new BabelRequest();
225 request.setArtifactVersion("1.0");
227 Response response = invokeService(new Gson().toJson(request));
228 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
229 assertThat(response.getEntity(), is("No artifact name attribute found in the request body."));
233 public void testMissingArtifactVersion() throws Exception {
234 BabelRequest request = new BabelRequest();
235 request.setArtifactName("hello");
237 Response response = invokeService(new Gson().toJson(request));
238 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
239 assertThat(response.getEntity(), is("No artifact version attribute found in the request body."));
243 public void testMissingCsarFile() throws Exception {
244 BabelRequest request = new BabelRequest();
245 request.setArtifactName("test-name");
246 request.setArtifactVersion("1.0");
247 Response response = invokeService(new Gson().toJson(request));
248 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
249 assertThat(response.getEntity(), is("No csar attribute found in the request body."));
253 * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
259 * @return the Response from the HTTP API
260 * @throws URISyntaxException
261 * if the URI cannot be created
262 * @throws IOException
263 * if the resource cannot be loaded
265 private Response processJsonRequest(CsarTest csar, AAIMicroServiceAuth auth)
266 throws URISyntaxException, IOException {
267 return invokeService(csar.getJsonRequest(), Optional.of("transaction-id"), auth);
271 * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
275 * @return the Response from the HTTP API
276 * @throws URISyntaxException
277 * if the URI cannot be created
279 private Response invokeService(String jsonRequest) throws URISyntaxException {
280 return invokeService(jsonRequest, Optional.of("transaction-id"), auth);
284 * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
288 * @param transactionId
289 * optional X-TransactionId value for the HTTP request
292 * @return the Response from the HTTP API
293 * @throws URISyntaxException
294 * if the URI cannot be created
296 private Response invokeService(String jsonString, Optional<String> transactionId, AAIMicroServiceAuth auth)
297 throws URISyntaxException {
298 UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
299 Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked)
300 Mockito.when(mockUriInfo.getPath(false)).thenReturn("validate"); // URI prefix is stripped by AJSC routing
301 Mockito.when(mockUriInfo.getPathParameters()).thenReturn(new MultivaluedHashMap<String, String>());
303 // Create mocked request headers map
304 MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>();
305 if (transactionId.isPresent()) {
306 headersMap.put("X-TransactionId", createSingletonList(transactionId.get()));
308 headersMap.put("X-FromAppId", createSingletonList("app-id"));
309 headersMap.put("Host", createSingletonList("hostname"));
311 HttpHeaders headers = Mockito.mock(HttpHeaders.class);
312 for (Entry<String, List<String>> entry : headersMap.entrySet()) {
313 Mockito.when(headers.getRequestHeader(entry.getKey())).thenReturn(entry.getValue());
314 Mockito.when(headers.getHeaderString(entry.getKey())).thenReturn(entry.getValue().get(0));
316 Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap);
318 MockHttpServletRequest servletRequest = new MockHttpServletRequest();
319 servletRequest.setSecure(true);
320 servletRequest.setScheme("https");
321 servletRequest.setServerPort(9501);
322 servletRequest.setServerName("localhost");
323 servletRequest.setRequestURI("/services/validation-service/v1/app/validate");
325 X509Certificate mockCertificate = Mockito.mock(X509Certificate.class);
326 Mockito.when(mockCertificate.getSubjectX500Principal())
327 .thenReturn(new X500Principal("CN=test, OU=qa, O=Test Ltd, L=London, ST=London, C=GB"));
329 servletRequest.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] {mockCertificate});
330 servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");
332 GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth);
333 return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString);
336 private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
337 return new ArtifactTestUtils().getResponseJson(jsonResponse);
340 private List<String> createSingletonList(String listItem) {
341 return Collections.<String>singletonList(listItem);