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 com.google.gson.Gson;
27 import java.io.IOException;
29 import java.net.URISyntaxException;
30 import java.security.cert.X509Certificate;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map.Entry;
34 import javax.inject.Inject;
35 import javax.security.auth.x500.X500Principal;
36 import javax.ws.rs.core.HttpHeaders;
37 import javax.ws.rs.core.MultivaluedHashMap;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriInfo;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.onap.aai.auth.AAIMicroServiceAuth;
45 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
46 import org.onap.aai.babel.service.data.BabelRequest;
47 import org.onap.aai.babel.util.ArtifactTestUtils;
48 import org.onap.aai.babel.xml.generator.data.GeneratorUtil;
49 import org.springframework.mock.web.MockHttpServletRequest;
50 import org.springframework.test.context.ContextConfiguration;
51 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
54 * Direct invocation of the generate artifacts service implementation.
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ContextConfiguration(locations = {"classpath:/babel-beans.xml"})
59 public class TestGenerateArtifactsServiceImpl {
62 if (System.getProperty("APP_HOME") == null) {
63 System.setProperty("APP_HOME", ".");
65 System.setProperty("CONFIG_HOME", "src/test/resources");
68 private static final String ARTIFACT_GENERATOR_CONFIG = "artifact-generator.properties";
71 private AAIMicroServiceAuth auth;
74 public static void setup() {
75 System.setProperty(ArtifactGeneratorToscaParser.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE,
76 new ArtifactTestUtils().getResourcePath(ARTIFACT_GENERATOR_CONFIG));
80 public void testGenerateArtifacts() throws Exception {
81 Response response = processJsonRequest(getRequestJson("success_request_vnf_catalog.json"));
82 assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
83 assertThat(response.getEntity(), is(getResponseJson("response.json")));
87 * No VNF Configuration exists.
92 public void testGenerateArtifactsWithoutVnfConfiguration() throws Exception {
93 final byte[] csarContent = new ArtifactTestUtils().getCompressedArtifact("noVnfConfiguration.csar");
95 BabelRequest babelRequest = new BabelRequest();
96 babelRequest.setCsar(new String(GeneratorUtil.encode(csarContent)));
97 babelRequest.setArtifactVersion("3.0");
98 babelRequest.setArtifactName("service-Vscpass-Test");
100 Response response = processJsonRequest(new Gson().toJson(babelRequest));
101 assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode()));
102 assertThat(response.getEntity(), is(getResponseJson("validNoVnfConfigurationResponse.json")));
106 public void testInvalidCsarFile() throws URISyntaxException, IOException {
107 Response response = processJsonRequest(getRequestJson("invalid_csar_request.json"));
108 assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
109 assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model."));
113 public void testInvalidJsonFile() throws URISyntaxException, IOException {
114 Response response = processJsonRequest(getRequestJson("invalid_json_request.json"));
115 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
116 assertThat(response.getEntity(), is("Malformed request."));
120 public void testMissingArtifactName() throws Exception {
121 Response response = processJsonRequest(getRequestJson("missing_artifact_name_request.json"));
122 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
123 assertThat(response.getEntity(), is("No artifact name attribute found in the request body."));
127 public void testMissingArtifactVersion() throws Exception {
128 Response response = processJsonRequest(getRequestJson("missing_artifact_version_request.json"));
129 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
130 assertThat(response.getEntity(), is("No artifact version attribute found in the request body."));
134 public void testMissingCsarFile() throws Exception {
135 Response response = processJsonRequest(getRequestJson("missing_csar_request.json"));
136 assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode()));
137 assertThat(response.getEntity(), is("No csar attribute found in the request body."));
141 * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
143 * @param resource path to the incoming JSON request
144 * @return the Response from the HTTP API
145 * @throws URISyntaxException if the URI cannot be created
146 * @throws IOException if the resource cannot be loaded
148 private Response processJsonRequest(String jsonString) throws URISyntaxException, IOException {
149 UriInfo mockUriInfo = Mockito.mock(UriInfo.class);
150 Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked)
151 Mockito.when(mockUriInfo.getPath(false)).thenReturn("validate"); // URI prefix is stripped by AJSC routing
152 Mockito.when(mockUriInfo.getPathParameters()).thenReturn(new MultivaluedHashMap<String, String>());
154 // Create mocked request headers map
155 MultivaluedHashMap<String, String> headersMap = new MultivaluedHashMap<>();
156 headersMap.put("X-TransactionId", createSingletonList("transaction-id"));
157 headersMap.put("X-FromAppId", createSingletonList("app-id"));
158 headersMap.put("Host", createSingletonList("hostname"));
160 HttpHeaders headers = Mockito.mock(HttpHeaders.class);
161 for (Entry<String, List<String>> entry : headersMap.entrySet()) {
162 Mockito.when(headers.getRequestHeader(entry.getKey())).thenReturn(entry.getValue());
164 Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap);
166 MockHttpServletRequest servletRequest = new MockHttpServletRequest();
167 servletRequest.setSecure(true);
168 servletRequest.setScheme("https");
169 servletRequest.setServerPort(9501);
170 servletRequest.setServerName("localhost");
171 servletRequest.setRequestURI("/services/validation-service/v1/app/validate");
173 X509Certificate mockCertificate = Mockito.mock(X509Certificate.class);
174 Mockito.when(mockCertificate.getSubjectX500Principal())
175 .thenReturn(new X500Principal("CN=test, OU=qa, O=Test Ltd, L=London, ST=London, C=GB"));
177 servletRequest.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] {mockCertificate});
178 servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");
180 GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth);
181 return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString);
184 private String getRequestJson(String resource) throws IOException, URISyntaxException {
185 return new ArtifactTestUtils().getRequestJson(resource);
188 private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
189 return new ArtifactTestUtils().getResponseJson(jsonResponse);
192 private List<String> createSingletonList(String listItem) {
193 return Collections.<String>singletonList(listItem);