X-Git-Url: https://gerrit.onap.org/r/gitweb?p=aai%2Fbabel.git;a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Fservice%2FTestGenerateArtifactsServiceImpl.java;h=b1423b7dc5763417a36f297a7c3e68c4636b39e6;hp=5d2309f8a8f27ab11370e6225aca1764dee02274;hb=66b3afa06776e9944ad515206d281d67747c9770;hpb=161f5a7d9b900ae34a4886d7f7fb01ea496f71eb diff --git a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java index 5d2309f..b1423b7 100644 --- a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java +++ b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017 European Software Marketing Ltd. + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 European Software Marketing Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,6 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. */ package org.onap.aai.babel.service; @@ -26,60 +24,79 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import java.io.IOException; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.cert.X509Certificate; +import java.util.Collections; +import java.util.List; +import java.util.Map.Entry; import java.util.stream.Collectors; +import javax.inject.Inject; +import javax.security.auth.x500.X500Principal; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.aai.auth.AAIMicroServiceAuth; +import org.onap.aai.babel.xml.generator.data.GeneratorConstants; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * Direct invocation of the generate artifacts service implementation * */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = {"classpath:/babel-beans.xml"}) public class TestGenerateArtifactsServiceImpl { - + + static { + if (System.getProperty("AJSC_HOME") == null) { + System.setProperty("AJSC_HOME", "."); + } + } + + @Inject + private AAIMicroServiceAuth auth; + @BeforeClass public static void setup() { URL url = TestGenerateArtifactsServiceImpl.class.getClassLoader().getResource("artifact-generator.properties"); - System.setProperty("artifactgenerator.config", url.getPath()); + System.setProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE, url.getPath()); } - + @Test - public void testGenerateArtifacts() throws Exception { - String jsonRequest = readstringFromFile("jsonFiles/success_request.json"); + public void testInvalidCsarFile() throws URISyntaxException, IOException { + String jsonRequest = readstringFromFile("jsonFiles/invalid_csar_request.json"); Response response = processJsonRequest(jsonRequest); - assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); - assertThat(response.getEntity(), is(readstringFromFile("response/response.json"))); + assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); + assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model.")); } - @Test - public void testInvalidCsarFile() throws URISyntaxException, IOException{ - String jsonRequest = readstringFromFile("jsonFiles/invalid_csar_request.json"); - Response response = processJsonRequest(jsonRequest); - assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); - assertThat(response.getEntity(), is("Error converting CSAR artifact to XML model.")); - } - - @Test - public void testInvalidJsonFile() throws URISyntaxException, IOException{ - String jsonRequest = readstringFromFile("jsonFiles/invalid_json_request.json"); - Response response = processJsonRequest(jsonRequest); - assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); - assertThat(response.getEntity(), is("Malformed request.")); + public void testInvalidJsonFile() throws URISyntaxException, IOException { + String jsonRequest = readstringFromFile("jsonFiles/invalid_json_request.json"); + Response response = processJsonRequest(jsonRequest); + assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); + assertThat(response.getEntity(), is("Malformed request.")); } - + @Test public void testMissingArtifactName() throws Exception { String jsonRequest = readstringFromFile("jsonFiles/missing_artifact_name_request.json"); Response response = processJsonRequest(jsonRequest); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); - assertThat(response.getEntity(), is("No artifact name attribute found in the request body." )); + assertThat(response.getEntity(), is("No artifact name attribute found in the request body.")); } - + @Test public void testMissingArtifactVersion() throws Exception { String jsonRequest = readstringFromFile("jsonFiles/missing_artifact_version_request.json"); @@ -87,7 +104,7 @@ public class TestGenerateArtifactsServiceImpl { assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No artifact version attribute found in the request body.")); } - + @Test public void testMissingCsarFile() throws Exception { String jsonRequest = readstringFromFile("jsonFiles/missing_csar_request.json"); @@ -95,17 +112,59 @@ public class TestGenerateArtifactsServiceImpl { assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No csar attribute found in the request body.")); } - - private Response processJsonRequest(String jsonRequest) { - GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(/* No authentiction required */ null); - return service.generateArtifacts(jsonRequest); + /** + * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API + * + * @param request for the Babel Service + * @return the Response from the HTTP API + * @throws URISyntaxException + */ + private Response processJsonRequest(String jsonRequest) throws URISyntaxException { + UriInfo mockUriInfo = Mockito.mock(UriInfo.class); + Mockito.when(mockUriInfo.getRequestUri()).thenReturn(new URI("/validate")); // NOSONAR (mocked) + Mockito.when(mockUriInfo.getPath(false)).thenReturn("validate"); // URI prefix is stripped by AJSC routing + Mockito.when(mockUriInfo.getPathParameters()).thenReturn(new MultivaluedHashMap()); + + // Create mocked request headers map + MultivaluedHashMap headersMap = new MultivaluedHashMap<>(); + headersMap.put("X-TransactionId", createSingletonList("transaction-id")); + headersMap.put("X-FromAppId", createSingletonList("app-id")); + headersMap.put("Host", createSingletonList("hostname")); + + HttpHeaders headers = Mockito.mock(HttpHeaders.class); + for (Entry> entry : headersMap.entrySet()) { + Mockito.when(headers.getRequestHeader(entry.getKey())).thenReturn(entry.getValue()); + } + Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap); + + MockHttpServletRequest servletRequest = new MockHttpServletRequest(); + servletRequest.setSecure(true); + servletRequest.setScheme("https"); + servletRequest.setServerPort(9501); + servletRequest.setServerName("localhost"); + servletRequest.setRequestURI("/services/validation-service/v1/app/validate"); + + X509Certificate mockCertificate = Mockito.mock(X509Certificate.class); + Mockito.when(mockCertificate.getSubjectX500Principal()) + .thenReturn(new X500Principal("CN=test, OU=qa, O=Test Ltd, L=London, ST=London, C=GB")); + + servletRequest.setAttribute("javax.servlet.request.X509Certificate", new X509Certificate[] {mockCertificate}); + servletRequest.setAttribute("javax.servlet.request.cipher_suite", ""); + + GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth); + return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonRequest); + } + + private List createSingletonList(String listItem) { + return Collections.singletonList(listItem); } private String readstringFromFile(String resourceFile) throws IOException, URISyntaxException { - return Files.lines(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI())) + return Files + .lines(Paths + .get(TestGenerateArtifactsServiceImpl.class.getClassLoader().getResource(resourceFile).toURI())) .collect(Collectors.joining()); } - - + }