2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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=========================================================
21 package org.onap.aai.babel.service;
23 import static org.hamcrest.Matchers.is;
24 import static org.junit.Assert.assertThat;
26 import java.io.IOException;
28 import java.net.URISyntaxException;
29 import java.security.cert.X509Certificate;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map.Entry;
33 import javax.inject.Inject;
34 import javax.security.auth.x500.X500Principal;
35 import javax.ws.rs.core.HttpHeaders;
36 import javax.ws.rs.core.MultivaluedHashMap;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.UriInfo;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mockito;
43 import org.onap.aai.auth.AAIMicroServiceAuth;
44 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
45 import org.onap.aai.babel.util.ArtifactTestUtils;
46 import org.springframework.mock.web.MockHttpServletRequest;
47 import org.springframework.test.context.ContextConfiguration;
48 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
51 * Direct invocation of the generate artifacts service implementation.
54 @RunWith(SpringJUnit4ClassRunner.class)
55 @ContextConfiguration(locations = {"classpath:/babel-beans.xml"})
56 public class TestGenerateArtifactsServiceImpl {
59 if (System.getProperty("APP_HOME") == null) {
60 System.setProperty("APP_HOME", ".");
62 System.setProperty("CONFIG_HOME", "src/test/resources");
65 private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties";
68 private AAIMicroServiceAuth auth;
71 public static void setup() {
72 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
73 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
77 public void testGenerateArtifacts() throws Exception {
78 Response response = processJsonRequest("success_request_vnf_catalog.json");
79 assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
80 assertThat(response.getEntity(), is(getResponseJson("response.json")));
84 public void testInvalidCsarFile() throws URISyntaxException, IOException {
85 Response response = processJsonRequest("invalid_csar_request.json");
86 assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
87 assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model."));
91 public void testInvalidJsonFile() throws URISyntaxException, IOException {
92 Response response = processJsonRequest("invalid_json_request.json");
93 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
94 assertThat(response.getEntity(), is("Malformed request."));
98 public void testMissingArtifactName() throws Exception {
99 Response response = processJsonRequest("missing_artifact_name_request.json");
100 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
101 assertThat(response.getEntity(), is("No artifact name attribute found in the request body."));
105 public void testMissingArtifactVersion() throws Exception {
106 Response response = processJsonRequest("missing_artifact_version_request.json");
107 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
108 assertThat(response.getEntity(), is("No artifact version attribute found in the request body."));
112 public void testMissingCsarFile() throws Exception {
113 Response response = processJsonRequest("missing_csar_request.json");
114 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
115 assertThat(response.getEntity(), is("No csar attribute found in the request body."));
119 * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
121 * @param resource path to the incoming JSON request
122 * @return the Response from the HTTP API
123 * @throws URISyntaxException if the URI cannot be created
124 * @throws IOException if the resource cannot be loaded
126 private Response processJsonRequest(String resource) throws URISyntaxException, IOException {
127 UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
128 Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked)
129 Mockito.when(mockUriInfo.getPath(false)).thenReturn("validate"); // URI prefix is stripped by AJSC routing
130 Mockito.when(mockUriInfo.getPathParameters()).thenReturn(new MultivaluedHashMap<String, String>());
132 // Create mocked request headers map
133 MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>();
134 headersMap.put("X-TransactionId", createSingletonList("transaction-id"));
135 headersMap.put("X-FromAppId", createSingletonList("app-id"));
136 headersMap.put("Host", createSingletonList("hostname"));
138 HttpHeaders headers = Mockito.mock(HttpHeaders.class);
139 for (Entry<String, List<String>> entry : headersMap.entrySet()) {
140 Mockito.when(headers.getRequestHeader(entry.getKey())).thenReturn(entry.getValue());
142 Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap);
144 MockHttpServletRequest servletRequest = new MockHttpServletRequest();
145 servletRequest.setSecure(true);
146 servletRequest.setScheme("https");
147 servletRequest.setServerPort(9501);
148 servletRequest.setServerName("localhost");
149 servletRequest.setRequestURI("/services/validation-service/v1/app/validate");
151 X509Certificate mockCertificate = Mockito.mock(X509Certificate.class);
152 Mockito.when(mockCertificate.getSubjectX500Principal())
153 .thenReturn(new X500Principal("CN=test, OU=qa, O=Test Ltd, L=London, ST=London, C=GB"));
155 servletRequest.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] {mockCertificate});
156 servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");
158 GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth);
159 String jsonString = getRequestJson(resource);
160 return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString);
163 private String getRequestJson(String resource) throws IOException, URISyntaxException {
164 return new ArtifactTestUtils().getRequestJson(resource);
167 private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
168 return new ArtifactTestUtils().getResponseJson(jsonResponse);
171 private List<String> createSingletonList(String listItem) {
172 return Collections.<String>singletonList(listItem);