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";
67 private AAIMicroServiceAuth auth;
70 public static void setup() {
71 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
72 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
76 public void testInvalidCsarFile() throws URISyntaxException, IOException {
77 Response response = processJsonRequest("invalid_csar_request.json");
78 assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
79 assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model."));
83 public void testInvalidJsonFile() throws URISyntaxException, IOException {
84 Response response = processJsonRequest("invalid_json_request.json");
85 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
86 assertThat(response.getEntity(), is("Malformed request."));
90 public void testMissingArtifactName() throws Exception {
91 Response response = processJsonRequest("missing_artifact_name_request.json");
92 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
93 assertThat(response.getEntity(), is("No artifact name attribute found in the request body."));
97 public void testMissingArtifactVersion() throws Exception {
98 Response response = processJsonRequest("missing_artifact_version_request.json");
99 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
100 assertThat(response.getEntity(), is("No artifact version attribute found in the request body."));
104 public void testMissingCsarFile() throws Exception {
105 Response response = processJsonRequest("missing_csar_request.json");
106 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
107 assertThat(response.getEntity(), is("No csar attribute found in the request body."));
111 * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API
113 * @param resource path to the incoming JSON request
114 * @return the Response from the HTTP API
115 * @throws URISyntaxException
116 * @throws IOException
118 private Response processJsonRequest(String resource) throws URISyntaxException, IOException {
119 UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
120 Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked)
121 Mockito.when(mockUriInfo.getPath(false)).thenReturn("validate"); // URI prefix is stripped by AJSC routing
122 Mockito.when(mockUriInfo.getPathParameters()).thenReturn(new MultivaluedHashMap<String, String>());
124 // Create mocked request headers map
125 MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>();
126 headersMap.put("X-TransactionId", createSingletonList("transaction-id"));
127 headersMap.put("X-FromAppId", createSingletonList("app-id"));
128 headersMap.put("Host", createSingletonList("hostname"));
130 HttpHeaders headers = Mockito.mock(HttpHeaders.class);
131 for (Entry<String, List<String>> entry : headersMap.entrySet()) {
132 Mockito.when(headers.getRequestHeader(entry.getKey())).thenReturn(entry.getValue());
134 Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap);
136 MockHttpServletRequest servletRequest = new MockHttpServletRequest();
137 servletRequest.setSecure(true);
138 servletRequest.setScheme("https");
139 servletRequest.setServerPort(9501);
140 servletRequest.setServerName("localhost");
141 servletRequest.setRequestURI("/services/validation-service/v1/app/validate");
143 X509Certificate mockCertificate = Mockito.mock(X509Certificate.class);
144 Mockito.when(mockCertificate.getSubjectX500Principal())
145 .thenReturn(new X500Principal("CN=test, OU=qa, O=Test Ltd, L=London, ST=London, C=GB"));
147 servletRequest.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] {mockCertificate});
148 servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");
150 GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth);
151 String jsonString = getRequestJson(resource);
152 return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString);
155 private String getRequestJson(String resource) throws IOException, URISyntaxException {
156 return new ArtifactTestUtils().getRequestJson(resource);
159 private List<String> createSingletonList(String listItem) {
160 return Collections.<String>singletonList(listItem);