X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fjava%2Forg%2Fonap%2Faai%2Fbabel%2Fservice%2FTestGenerateArtifactsServiceImpl.java;h=45eadb04fa2df8ca1e522bdca81d1e39fd098090;hb=refs%2Fchanges%2F51%2F84751%2F6;hp=b1423b7dc5763417a36f297a7c3e68c4636b39e6;hpb=66b3afa06776e9944ad515206d281d67747c9770;p=aai%2Fbabel.git 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 b1423b7..45eadb0 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-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 European Software Marketing Ltd. + * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved. + * Copyright (c) 2017-2019 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. @@ -18,22 +18,22 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.onap.aai.babel.service; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; +import com.google.gson.Gson; 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 java.util.Optional; import javax.inject.Inject; import javax.security.auth.x500.X500Principal; import javax.ws.rs.core.HttpHeaders; @@ -44,14 +44,17 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; +import org.onap.aai.auth.AAIAuthException; import org.onap.aai.auth.AAIMicroServiceAuth; -import org.onap.aai.babel.xml.generator.data.GeneratorConstants; +import org.onap.aai.babel.service.data.BabelRequest; +import org.onap.aai.babel.testdata.CsarTest; +import org.onap.aai.babel.util.ArtifactTestUtils; 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 + * Direct invocation of the generate artifacts service implementation. * */ @RunWith(SpringJUnit4ClassRunner.class) @@ -59,9 +62,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; public class TestGenerateArtifactsServiceImpl { static { - if (System.getProperty("AJSC_HOME") == null) { - System.setProperty("AJSC_HOME", "."); - } + System.setProperty("CONFIG_HOME", "src/test/resources"); } @Inject @@ -69,58 +70,234 @@ public class TestGenerateArtifactsServiceImpl { @BeforeClass public static void setup() { - URL url = TestGenerateArtifactsServiceImpl.class.getClassLoader().getResource("artifact-generator.properties"); - System.setProperty(GeneratorConstants.PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE, url.getPath()); + new ArtifactTestUtils().setGeneratorSystemProperties(); + } + + /** + * Test with a valid request (and valid CSAR content) by calling the Service implementation directly using a mocked + * HTTPS request. + * + * @throws URISyntaxException + * if the URI cannot be created + * @throws IOException + * if the resource cannot be loaded + */ + @Test + public void testGenerateArtifacts() throws URISyntaxException, IOException { + Response response = processJsonRequest(CsarTest.VNF_VENDOR_CSAR, auth); + assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("response.json"))); + } + + /** + * Test with a valid request that has no Transaction ID header value. + * + * @throws URISyntaxException + * if the URI cannot be created + * @throws IOException + * if the resource cannot be loaded + */ + @Test + public void testGenerateArtifactsWithoutRequestId() throws URISyntaxException, IOException { + Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequest(), Optional.empty(), auth); + assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("response.json"))); + } + + /** + * Test with a valid request without Minor Artifact version. + * + * @throws URISyntaxException + * if the URI cannot be created + * @throws IOException + * if the resource cannot be loaded + */ + @Test + public void testGenerateArtifactsWithoutMinorArtifactVersion() throws URISyntaxException, IOException { + Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("1"), + Optional.of("transaction-id"), auth); + assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("response.json"))); + } + + /** + * Test with a valid request without Minor Artifact version. + * + * @throws URISyntaxException + * if the URI cannot be created + * @throws IOException + * if the resource cannot be loaded + */ + @Test + public void testGenerateArtifactsWithInvalidArtifactVersion() throws URISyntaxException, IOException { + Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("a"), + Optional.of("transaction-id"), auth); + assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("response.json"))); + } + + + /** + * Test with a valid request with Artifact version less than 1. + * + * @throws URISyntaxException + * if the URI cannot be created + * @throws IOException + * if the resource cannot be loaded + */ + @Test + public void testGenerateArtifactsWithArtifactVerLessThan1() throws URISyntaxException, IOException { + Response response = invokeService(CsarTest.VNF_VENDOR_CSAR.getJsonRequestWithArtifactVersion("0.1"), + Optional.of("transaction-id"), auth); + assertThat(response.toString(), response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("responseWithVersionLessThan1.json"))); + } + + + /** + * Test with a valid request, using a CSAR file that has no VNF configuration present. + * + * @throws URISyntaxException + * if the URI cannot be created + * @throws IOException + * if the resource cannot be loaded + */ + @Test + public void testGenerateArtifactsWithoutVnfConfiguration() throws IOException, URISyntaxException { + Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, auth); + assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); + assertThat(response.getEntity(), is(getResponseJson("validNoVnfConfigurationResponse.json"))); + } + + /** + * Test for a valid request with invalid CSAR file content. + * + * @throws URISyntaxException + * if the URI cannot be created + * @throws IOException + * if the resource cannot be loaded + */ + @Test + public void testGenerateArtifactsInvalidCsar() throws IOException, URISyntaxException { + Response response = processJsonRequest(CsarTest.MULTIPLE_VNF_CSAR, auth); + assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); + assertThat(response.getEntity().toString(), containsString("VNF catalog")); + } + + @Test + public void testUninitializedService() throws IOException, URISyntaxException, AAIAuthException { + AAIMicroServiceAuth uninitializedAuth = Mockito.mock(AAIMicroServiceAuth.class); + Mockito.when(uninitializedAuth.validateRequest(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenThrow(new AAIAuthException("test")); + Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, uninitializedAuth); + assertThat(response.getStatus(), is(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode())); + assertThat(response.getEntity().toString(), containsString("check the Babel service logs")); + } + + @Test + public void testUnauthorizedRequest() throws IOException, URISyntaxException, AAIAuthException { + AAIMicroServiceAuth uninitializedAuth = Mockito.mock(AAIMicroServiceAuth.class); + Mockito.when(uninitializedAuth.validateRequest(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(false); + Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR, uninitializedAuth); + assertThat(response.getStatus(), is(Response.Status.UNAUTHORIZED.getStatusCode())); + assertThat(response.getEntity().toString(), containsString("User not authorized")); } @Test public void testInvalidCsarFile() throws URISyntaxException, IOException { - String jsonRequest = readstringFromFile("jsonFiles/invalid_csar_request.json"); - Response response = processJsonRequest(jsonRequest); + BabelRequest request = new BabelRequest(); + request.setArtifactName("hello"); + request.setArtifactVersion("1.0"); + request.setCsar("xxxx"); + Response response = invokeService(new Gson().toJson(request)); 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); + Response response = invokeService("{\"csar:\"xxxx\""); 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); + BabelRequest request = new BabelRequest(); + request.setArtifactVersion("1.0"); + request.setCsar(""); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); 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"); - Response response = processJsonRequest(jsonRequest); + BabelRequest request = new BabelRequest(); + request.setArtifactName("hello"); + request.setCsar(""); + Response response = invokeService(new Gson().toJson(request)); 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"); - Response response = processJsonRequest(jsonRequest); + BabelRequest request = new BabelRequest(); + request.setArtifactName("test-name"); + request.setArtifactVersion("1.0"); + Response response = invokeService(new Gson().toJson(request)); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST.getStatusCode())); assertThat(response.getEntity(), is("No csar attribute found in the request body.")); } /** - * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API - * - * @param request for the Babel Service + * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API. + * + * @param csar + * test CSAR file + * @param auth + * the auth module * @return the Response from the HTTP API * @throws URISyntaxException + * if the URI cannot be created + * @throws IOException + * if the resource cannot be loaded */ - private Response processJsonRequest(String jsonRequest) throws URISyntaxException { + private Response processJsonRequest(CsarTest csar, AAIMicroServiceAuth auth) + throws URISyntaxException, IOException { + return invokeService(csar.getJsonRequest(), Optional.of("transaction-id"), auth); + } + + /** + * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API. + * + * @param jsonString + * the JSON request + * @return the Response from the HTTP API + * @throws URISyntaxException + * if the URI cannot be created + */ + private Response invokeService(String jsonRequest) throws URISyntaxException { + return invokeService(jsonRequest, Optional.of("transaction-id"), auth); + } + + /** + * Create a (mocked) HTTPS request and invoke the Babel generate artifacts API. + * + * @param jsonString + * the JSON request + * @param transactionId + * optional X-TransactionId value for the HTTP request + * @param auth + * the auth module + * @return the Response from the HTTP API + * @throws URISyntaxException + * if the URI cannot be created + */ + private Response invokeService(String jsonString, Optional transactionId, AAIMicroServiceAuth auth) + 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 @@ -128,13 +305,16 @@ public class TestGenerateArtifactsServiceImpl { // Create mocked request headers map MultivaluedHashMap headersMap = new MultivaluedHashMap<>(); - headersMap.put("X-TransactionId", createSingletonList("transaction-id")); + if (transactionId.isPresent()) { + headersMap.put("X-TransactionId", createSingletonList(transactionId.get())); + } 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.getHeaderString(entry.getKey())).thenReturn(entry.getValue().get(0)); } Mockito.when(headers.getRequestHeaders()).thenReturn(headersMap); @@ -153,18 +333,15 @@ public class TestGenerateArtifactsServiceImpl { servletRequest.setAttribute("javax.servlet.request.cipher_suite", ""); GenerateArtifactsServiceImpl service = new GenerateArtifactsServiceImpl(auth); - return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonRequest); + return service.generateArtifacts(mockUriInfo, headers, servletRequest, jsonString); } - private List createSingletonList(String listItem) { - return Collections.singletonList(listItem); + private String getResponseJson(String jsonResponse) throws IOException, URISyntaxException { + return new ArtifactTestUtils().getResponseJson(jsonResponse); } - private String readstringFromFile(String resourceFile) throws IOException, URISyntaxException { - return Files - .lines(Paths - .get(TestGenerateArtifactsServiceImpl.class.getClassLoader().getResource(resourceFile).toURI())) - .collect(Collectors.joining()); + private List createSingletonList(String listItem) { + return Collections.singletonList(listItem); } }