From dc7259b2afe470f511a1af15418f76ccb9e421c9 Mon Sep 17 00:00:00 2001 From: "mark.j.leonard" Date: Wed, 3 Apr 2019 14:07:09 +0100 Subject: [PATCH] GenerateArtifactsServiceImpl code coverage Additional tests for coverage of the generateArtifacts() implementation. Replace use of the generic Exception by catching only the specific types of Exception thrown by the parser/generators. Modify existing tests to ensure that each branch is covered. Change-Id: Id35e8a4a51b785f0367ac04a31ed23226cdb4810 Issue-ID: AAI-2280 Signed-off-by: mark.j.leonard --- .../org/onap/aai/auth/AAIMicroServiceAuthCore.java | 2 +- .../service/GenerateArtifactsServiceImpl.java | 35 +++---- .../service/TestGenerateArtifactsServiceImpl.java | 104 +++++++++++++++++++-- 3 files changed, 115 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java b/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java index bdb3bc7..eae1a24 100644 --- a/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java +++ b/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java @@ -89,8 +89,8 @@ public class AAIMicroServiceAuthCore { if (policyAuthFileName == null) { throw new AAIAuthException("Auth policy file could not be found"); } - AAIMicroServiceAuthCore.reloadUsers(); + AAIMicroServiceAuthCore.reloadUsers(); TimerTask task = new AuthFileWatcher(new File(policyAuthFileName)); diff --git a/src/main/java/org/onap/aai/babel/service/GenerateArtifactsServiceImpl.java b/src/main/java/org/onap/aai/babel/service/GenerateArtifactsServiceImpl.java index 8d89db9..6655170 100644 --- a/src/main/java/org/onap/aai/babel/service/GenerateArtifactsServiceImpl.java +++ b/src/main/java/org/onap/aai/babel/service/GenerateArtifactsServiceImpl.java @@ -2,8 +2,8 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2019 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. @@ -36,6 +36,7 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; import org.apache.commons.lang3.time.StopWatch; +import org.onap.aai.auth.AAIAuthException; import org.onap.aai.auth.AAIMicroServiceAuth; import org.onap.aai.auth.AAIMicroServiceAuthCore; import org.onap.aai.babel.csar.CsarConverterException; @@ -53,7 +54,10 @@ import org.onap.aai.babel.util.RequestValidationException; import org.onap.aai.babel.util.RequestValidator; import org.springframework.stereotype.Service; -/** Generate SDC Artifacts by passing in a CSAR payload, Artifact Name and Artifact version */ +/** + * Generate SDC Artifacts by passing in a CSAR payload, Artifact Name and Artifact version. + * + */ @Service public class GenerateArtifactsServiceImpl implements GenerateArtifactsService { private static final LogHelper applicationLogger = LogHelper.INSTANCE; @@ -62,6 +66,7 @@ public class GenerateArtifactsServiceImpl implements GenerateArtifactsService { /** * @param authorization + * the auth module */ @Inject public GenerateArtifactsServiceImpl(final AAIMicroServiceAuth authorization) { @@ -111,11 +116,11 @@ public class GenerateArtifactsServiceImpl implements GenerateArtifactsService { response = authorized ? generateArtifacts(requestBody) : buildResponse(Status.UNAUTHORIZED, "User not authorized to perform the operation."); - } catch (Exception e) { + } catch (AAIAuthException e) { applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e); applicationLogger.logAuditError(e); return buildResponse(Status.INTERNAL_SERVER_ERROR, - "Error while processing request. Please check the babel service logs for more details.\n"); + "Error while processing request. Please check the Babel service logs for more details.\n"); } StatusCode statusDescription; @@ -134,7 +139,8 @@ public class GenerateArtifactsServiceImpl implements GenerateArtifactsService { /** * Generate XML model artifacts from request body. * - * @param requestBody the request body in JSON format + * @param requestBody + * the request body in JSON format * @return response object containing the generated XML models */ protected Response generateArtifacts(String requestBody) { @@ -168,11 +174,8 @@ public class GenerateArtifactsServiceImpl implements GenerateArtifactsService { response = processError(ApplicationMsgs.PROCESSING_VNF_CATALOG_ERROR, Status.INTERNAL_SERVER_ERROR, e, "Error converting CSAR artifact to VNF catalog."); } catch (RequestValidationException e) { - response = - processError(ApplicationMsgs.PROCESS_REQUEST_ERROR, Status.BAD_REQUEST, e, e.getLocalizedMessage()); - } catch (Exception e) { - response = processError(ApplicationMsgs.PROCESS_REQUEST_ERROR, Status.INTERNAL_SERVER_ERROR, e, - "Error while processing request. Please check the babel service logs for more details.\n"); + response = processError(ApplicationMsgs.PROCESS_REQUEST_ERROR, Status.BAD_REQUEST, // + e, e.getLocalizedMessage()); } finally { applicationLogger.logMetrics(stopwatch, LogHelper.getCallerMethodName(0)); } @@ -189,13 +192,13 @@ public class GenerateArtifactsServiceImpl implements GenerateArtifactsService { /** * Helper method to create a REST response object. * - * @param status response status code - * @param entity response payload + * @param status + * response status code + * @param entity + * response payload * @return */ private Response buildResponse(Status status, String entity) { - // @formatter:off - return Response.status(status).entity(entity).type(MediaType.TEXT_PLAIN).build(); - // @formatter:on + return Response.status(status).entity(entity).type(MediaType.TEXT_PLAIN).build(); } } 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 55311a6..d011ba6 100644 --- a/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java +++ b/src/test/java/org/onap/aai/babel/service/TestGenerateArtifactsServiceImpl.java @@ -21,6 +21,7 @@ package org.onap.aai.babel.service; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -32,6 +33,7 @@ import java.security.cert.X509Certificate; import java.util.Collections; import java.util.List; import java.util.Map.Entry; +import java.util.Optional; import javax.inject.Inject; import javax.security.auth.x500.X500Principal; import javax.ws.rs.core.HttpHeaders; @@ -42,6 +44,7 @@ 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.service.data.BabelRequest; import org.onap.aai.babel.testdata.CsarTest; @@ -70,16 +73,40 @@ public class TestGenerateArtifactsServiceImpl { 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 Exception { - Response response = processJsonRequest(CsarTest.VNF_VENDOR_CSAR); + 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"))); } /** - * No VNF Configuration exists. - * + * 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, using a CSAR file that has no VNF configuration present. + * * @throws URISyntaxException * if the URI cannot be created * @throws IOException @@ -87,11 +114,46 @@ public class TestGenerateArtifactsServiceImpl { */ @Test public void testGenerateArtifactsWithoutVnfConfiguration() throws IOException, URISyntaxException { - Response response = processJsonRequest(CsarTest.NO_VNF_CONFIG_CSAR); + 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 { BabelRequest request = new BabelRequest(); @@ -145,14 +207,17 @@ public class TestGenerateArtifactsServiceImpl { * * @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(CsarTest csar) throws IOException, URISyntaxException { - return invokeService(csar.getJsonRequest()); + private Response processJsonRequest(CsarTest csar, AAIMicroServiceAuth auth) + throws URISyntaxException, IOException { + return invokeService(csar.getJsonRequest(), Optional.of("transaction-id"), auth); } /** @@ -164,7 +229,25 @@ public class TestGenerateArtifactsServiceImpl { * @throws URISyntaxException * if the URI cannot be created */ - private Response invokeService(String jsonString) throws URISyntaxException { + 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 @@ -172,13 +255,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); -- 2.16.6