5ceca17e039ae47ac37953ce4357b9351afa60d7
[aai/babel.git] / src / test / java / org / onap / aai / babel / service / TestGenerateArtifactsServiceImpl.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.aai.babel.service;
23
24 import static org.hamcrest.CoreMatchers.containsString;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.is;
27
28 import com.google.gson.Gson;
29 import java.io.IOException;
30 import java.net.URI;
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;
53
54 /**
55  * Direct invocation of the generate artifacts service implementation.
56  *
57  */
58 @SpringJUnitConfig(locations = {"classpath:/babel-beans.xml"})
59 public class TestGenerateArtifactsServiceImpl {
60
61     static {
62         System.setProperty("CONFIG_HOME", "src/test/resources");
63     }
64
65     @Inject
66     private AAIMicroServiceAuth auth;
67
68     @BeforeAll
69     public static void setup() {
70         new ArtifactTestUtils().setGeneratorSystemProperties();
71     }
72
73     /**
74      * Test with a valid request (and valid CSAR content) by calling the Service implementation directly using a mocked
75      * HTTPS request.
76      *
77      * @throws URISyntaxException
78      *             if the URI cannot be created
79      * @throws IOException
80      *             if the resource cannot be loaded
81      */
82     @Test
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")));
87     }
88
89     /**
90      * Test with a valid request that has no Transaction ID header value.
91      *
92      * @throws URISyntaxException
93      *             if the URI cannot be created
94      * @throws IOException
95      *             if the resource cannot be loaded
96      */
97     @Test
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")));
102     }
103     
104     /**
105      * Test with a valid request without Minor Artifact version.
106      *
107      * @throws URISyntaxException
108      *             if the URI cannot be created
109      * @throws IOException
110      *             if the resource cannot be loaded
111      */
112     @Test
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")));
118     }
119     
120     /**
121      * Test with a valid request without Minor Artifact version.
122      *
123      * @throws URISyntaxException
124      *             if the URI cannot be created
125      * @throws IOException
126      *             if the resource cannot be loaded
127      */
128     @Test
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")));
134     }
135     
136     
137     /**
138      * Test with a valid request with Artifact version less than 1.
139      *
140      * @throws URISyntaxException
141      *             if the URI cannot be created
142      * @throws IOException
143      *             if the resource cannot be loaded
144      */
145     @Test
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")));
151     }
152
153
154     /**
155      * Test with a valid request, using a CSAR file that has no VNF configuration present.
156      *
157      * @throws URISyntaxException
158      *             if the URI cannot be created
159      * @throws IOException
160      *             if the resource cannot be loaded
161      */
162     @Test
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")));
167     }
168
169     /**
170      * Test for a valid request with invalid CSAR file content.
171      *
172      * @throws URISyntaxException
173      *             if the URI cannot be created
174      * @throws IOException
175      *             if the resource cannot be loaded
176      */
177     @Test
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"));
182     }
183
184     @Test
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"));
192     }
193
194     @Test
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()))
198                 .thenReturn(false);
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"));
202     }
203
204     @Test
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."));
213     }
214
215     @Test
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."));
220     }
221
222     @Test
223     public void testMissingArtifactName() throws Exception {
224         BabelRequest request = new BabelRequest();
225         request.setArtifactVersion("1.0");
226         request.setCsar("");
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."));
230     }
231
232     @Test
233     public void testMissingArtifactVersion() throws Exception {
234         BabelRequest request = new BabelRequest();
235         request.setArtifactName("hello");
236         request.setCsar("");
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."));
240     }
241
242     @Test
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."));
250     }
251
252     /**
253      * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
254      *
255      * @param csar
256      *            test CSAR file
257      * @param auth
258      *            the auth module
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
264      */
265     private Response processJsonRequest(CsarTest csar, AAIMicroServiceAuth auth)
266             throws URISyntaxException, IOException {
267         return invokeService(csar.getJsonRequest(), Optional.of("transaction-id"), auth);
268     }
269
270     /**
271      * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
272      *
273      * @param jsonString
274      *            the JSON request
275      * @return the Response from the HTTP API
276      * @throws URISyntaxException
277      *             if the URI cannot be created
278      */
279     private Response invokeService(String jsonRequest) throws URISyntaxException {
280         return invokeService(jsonRequest, Optional.of("transaction-id"), auth);
281     }
282
283     /**
284      * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API.
285      *
286      * @param jsonString
287      *            the JSON request
288      * @param transactionId
289      *            optional X-TransactionId value for the HTTP request
290      * @param auth
291      *            the auth module
292      * @return the Response from the HTTP API
293      * @throws URISyntaxException
294      *             if the URI cannot be created
295      */
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>());
302
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()));
307         }
308         headersMap.put("X-FromAppId", createSingletonList("app-id"));
309         headersMap.put("Host", createSingletonList("hostname"));
310
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));
315         }
316         Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap);
317
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");
324
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"));
328
329         servletRequest.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] {mockCertificate});
330         servletRequest.setAttribute("javax.servlet.request.cipher_suite", "");
331
332         GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth);
333         return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString);
334     }
335
336     private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException {
337         return new ArtifactTestUtils().getResponseJson(jsonResponse);
338     }
339
340     private List<String> createSingletonList(String listItem) {
341         return Collections.<String>singletonList(listItem);
342     }
343
344 }