From a0886f054b8e1a880c133ac0fd0cca673aae9b93 Mon Sep 17 00:00:00 2001 From: Piotr Darosz Date: Wed, 4 Sep 2019 14:19:27 +0200 Subject: catalog-be code coverage increase Add tests for DistributionCatalogServlet class Change-Id: I3da0460f09c803a0ec5a26bd2c7f70f36c6998a1 Issue-ID: SDC-2326 Signed-off-by: Piotr Darosz --- .../servlet/DistributionCatalogServlet.java | 62 +-- .../src/test/java/org/openecomp/sdc/TestUtils.java | 43 ++ .../servlet/DistributionCatalogServletTest.java | 451 +++++++++++++++++++++ 3 files changed, 527 insertions(+), 29 deletions(-) create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/TestUtils.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/distribution/servlet/DistributionCatalogServletTest.java diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/distribution/servlet/DistributionCatalogServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/distribution/servlet/DistributionCatalogServlet.java index bd644e3edb..31c3d67aa6 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/distribution/servlet/DistributionCatalogServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/distribution/servlet/DistributionCatalogServlet.java @@ -16,6 +16,8 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * Modifications copyright (c) 2019 Nokia + * ================================================================================ */ package org.openecomp.sdc.be.distribution.servlet; @@ -24,6 +26,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Map; +import javax.inject.Inject; import javax.inject.Singleton; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; @@ -43,9 +46,9 @@ import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData; import org.openecomp.sdc.be.servlets.BeGenericServlet; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.datastructure.Wrapper; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; -import org.springframework.beans.factory.annotation.Autowired; import com.jcabi.aspects.Loggable; import fj.data.Either; import io.swagger.v3.oas.annotations.OpenAPIDefinition; @@ -76,7 +79,7 @@ public class DistributionCatalogServlet extends BeGenericServlet { private static final Logger log = Logger.getLogger(DistributionCatalogServlet.class); private final ArtifactsBusinessLogic artifactsBusinessLogic; - @Autowired + @Inject public DistributionCatalogServlet(UserBusinessLogic userBusinessLogic, ComponentsUtils componentsUtils, ArtifactsBusinessLogic artifactsBusinessLogic) { @@ -128,13 +131,10 @@ public class DistributionCatalogServlet extends BeGenericServlet { @PathParam("serviceVersion") final String serviceVersion, @PathParam("artifactName") final String artifactName) { - Response response = null; String requestURI = request.getRequestURI(); - if (instanceIdHeader == null || instanceIdHeader.isEmpty()) { - log.debug(MISSING_X_ECOMP_INSTANCE_ID_HEADER); - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID); - getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - return buildErrorResponse(responseFormat); + Wrapper responseWrapper = validateInstanceIdHeader(new Wrapper<>(), instanceIdHeader, requestURI); + if(!responseWrapper.isEmpty()) { + return responseWrapper.getInnerElement(); } try { @@ -143,7 +143,7 @@ public class DistributionCatalogServlet extends BeGenericServlet { if (downloadRsrcArtifactEither.isRight()) { ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value(); getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - response = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(buildErrorResponse(responseFormat)); } else { byte[] value = downloadRsrcArtifactEither.left().value(); InputStream is = new ByteArrayInputStream(value); @@ -152,9 +152,9 @@ public class DistributionCatalogServlet extends BeGenericServlet { headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName)); ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK); getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - response = buildOkResponse(responseFormat, is, headers); + responseWrapper.setInnerElement(buildOkResponse(responseFormat, is, headers)); } - return response; + return responseWrapper.getInnerElement(); } catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download Murano package artifact for service - external API"); @@ -163,6 +163,16 @@ public class DistributionCatalogServlet extends BeGenericServlet { } } + private Wrapper validateInstanceIdHeader(Wrapper responseWrapper, String instanceIdHeader, String requestURI) { + if (instanceIdHeader == null || instanceIdHeader.isEmpty()) { + log.debug(MISSING_X_ECOMP_INSTANCE_ID_HEADER); + ResponseFormat errorResponseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID); + getComponentsUtils().auditDistributionDownload(errorResponseFormat, new DistributionData(instanceIdHeader, requestURI)); + responseWrapper.setInnerElement(buildErrorResponse(errorResponseFormat)); + } + return responseWrapper; + } + /** * * @param requestId @@ -205,14 +215,11 @@ public class DistributionCatalogServlet extends BeGenericServlet { @PathParam("resourceVersion") final String resourceVersion, @PathParam("artifactName") final String artifactName) { - Response response = null; String requestURI = request.getRequestURI(); + Wrapper responseWrapper = validateInstanceIdHeader(new Wrapper<>(), instanceIdHeader, requestURI); - if (instanceIdHeader == null || instanceIdHeader.isEmpty()) { - log.debug(MISSING_X_ECOMP_INSTANCE_ID_HEADER); - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID); - getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - return buildErrorResponse(responseFormat); + if(!responseWrapper.isEmpty()) { + return responseWrapper.getInnerElement(); } try { @@ -221,7 +228,7 @@ public class DistributionCatalogServlet extends BeGenericServlet { if (downloadRsrcArtifactEither.isRight()) { ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value(); getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - response = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(buildErrorResponse(responseFormat)); } else { byte[] value = downloadRsrcArtifactEither.left().value(); // Returning 64-encoded as it was received during upload @@ -230,9 +237,9 @@ public class DistributionCatalogServlet extends BeGenericServlet { headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName)); ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK); getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - response = buildOkResponse(responseFormat, is, headers); + responseWrapper.setInnerElement(buildOkResponse(responseFormat, is, headers)); } - return response; + return responseWrapper.getInnerElement(); } catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API"); @@ -281,14 +288,11 @@ public class DistributionCatalogServlet extends BeGenericServlet { @PathParam("resourceInstanceName") final String resourceInstanceName, @PathParam("artifactName") final String artifactName) { - Response response = null; String requestURI = request.getRequestURI(); + Wrapper responseWrapper = validateInstanceIdHeader(new Wrapper<>(), instanceIdHeader, requestURI); - if (instanceIdHeader == null || instanceIdHeader.isEmpty()) { - log.debug(MISSING_X_ECOMP_INSTANCE_ID_HEADER); - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID); - getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - return buildErrorResponse(responseFormat); + if(!responseWrapper.isEmpty()) { + return responseWrapper.getInnerElement(); } try { @@ -297,7 +301,7 @@ public class DistributionCatalogServlet extends BeGenericServlet { if (downloadRsrcArtifactEither.isRight()) { ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value(); getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - response = buildErrorResponse(responseFormat); + responseWrapper.setInnerElement(buildErrorResponse(responseFormat)); } else { byte[] value = downloadRsrcArtifactEither.left().value(); // Returning 64-encoded as it was received during upload @@ -306,9 +310,9 @@ public class DistributionCatalogServlet extends BeGenericServlet { headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName)); ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK); getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI)); - response = buildOkResponse(responseFormat, is, headers); + responseWrapper.setInnerElement(buildOkResponse(responseFormat, is, headers)); } - return response; + return responseWrapper.getInnerElement(); } catch (Exception e) { BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API"); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/TestUtils.java b/catalog-be/src/test/java/org/openecomp/sdc/TestUtils.java new file mode 100644 index 0000000000..2c06f22632 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/TestUtils.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc; + +import org.apache.commons.io.IOUtils; + +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.Assert.assertArrayEquals; + +public class TestUtils { + public static boolean downloadedPayloadMatchesExpected(Response response, byte[] expected) { + boolean result = false; + try (InputStream is = response.readEntity(InputStream.class)) { + byte[] body = IOUtils.toByteArray(is); + assertArrayEquals(expected, body); + result = true; + } catch(Exception ex) { + result = false; + } + return result; + } +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/distribution/servlet/DistributionCatalogServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/distribution/servlet/DistributionCatalogServletTest.java new file mode 100644 index 0000000000..326fcdc61f --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/distribution/servlet/DistributionCatalogServletTest.java @@ -0,0 +1,451 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.distribution.servlet; + +import fj.data.Either; +import org.apache.commons.text.StrSubstitutor; +import org.apache.http.HttpStatus; +import org.glassfish.hk2.utilities.binding.AbstractBinder; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic; +import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; +import org.openecomp.sdc.be.config.ConfigurationManager; +import org.openecomp.sdc.be.config.SpringConfig; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.impl.WebAppContextWrapper; +import org.openecomp.sdc.be.user.UserBusinessLogic; +import org.openecomp.sdc.common.api.ConfigurationSource; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.impl.ExternalConfiguration; +import org.openecomp.sdc.common.impl.FSConfigurationSource; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.web.context.WebApplicationContext; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.TestUtils.downloadedPayloadMatchesExpected; + +public class DistributionCatalogServletTest extends JerseyTest { + + private static final HttpServletRequest HTTP_SERVLET_REQUEST = Mockito.mock(HttpServletRequest.class); + private static final UserBusinessLogic USER_BUSINESS_LOGIC = Mockito.mock(UserBusinessLogic.class); + private static final ArtifactsBusinessLogic ARTIFACTS_BUSINESS_LOGIC = Mockito.mock(ArtifactsBusinessLogic.class); + private static final ServletContext SERVLET_CONTEXT = Mockito.mock(ServletContext.class); + private static final WebAppContextWrapper WEB_APP_CONTEXT_WRAPPER = Mockito.mock(WebAppContextWrapper.class); + private static final WebApplicationContext WEB_APPLICATION_CONTEXT = Mockito.mock(WebApplicationContext.class); + private static final ComponentsUtils COMPONENT_UTILS = Mockito.mock(ComponentsUtils.class); + private static final ResponseFormat OK_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_OK); + private static final ResponseFormat GENERAL_ERROR_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_INTERNAL_SERVER_ERROR); + private static final ResponseFormat CREATED_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_CREATED); + private static final ResponseFormat NO_CONTENT_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_NO_CONTENT); + private static final ResponseFormat UNAUTHORIZED_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_UNAUTHORIZED); + private static final ResponseFormat NOT_FOUND_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_NOT_FOUND); + private static final ResponseFormat BAD_REQUEST_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_BAD_REQUEST); + private static final String SERVICE_VERSION = "serviceVersion"; + private static final String ARTIFACT_NAME = "artifactName"; + private static final String SERVICE_NAME = "serviceName"; + private static final String RESOURCE_NAME = "resourceName"; + private static final String RESOURCE_VERSION = "resourceVersion"; + private static final String RESOURCE_INSTANCE_NAME = "resourceInstanceName"; + private static final byte[] BYTE_ARRAY = new byte[]{0xA, 0xB, 0xC, 0xD}; + + @BeforeClass + public static void setup() { + when(SERVLET_CONTEXT.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(WEB_APP_CONTEXT_WRAPPER); + when(WEB_APP_CONTEXT_WRAPPER.getWebAppContext(SERVLET_CONTEXT)).thenReturn(WEB_APPLICATION_CONTEXT); + + setUpResponseFormatsForMocks(); + setUpMockTestConfiguration(); + } + + private static void setUpMockTestConfiguration() { + String appConfigDir = "src/test/resources/config"; + ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir); + ConfigurationManager configurationManager = new ConfigurationManager(configurationSource); + + org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration(); + configuration.setJanusGraphInMemoryGraph(true); + + configurationManager.setConfiguration(configuration); + ExternalConfiguration.setAppName("catalog-be"); + } + + private static void setUpResponseFormatsForMocks() { + when(COMPONENT_UTILS.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(UNAUTHORIZED_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(ActionStatus.OK)).thenReturn(OK_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(ActionStatus.CREATED)).thenReturn(CREATED_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(ActionStatus.NO_CONTENT)).thenReturn(NO_CONTENT_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(BAD_REQUEST_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(GENERAL_ERROR_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(any(ComponentException.class))) + .thenReturn(GENERAL_ERROR_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), any())).thenReturn(NOT_FOUND_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), any())).thenReturn(NOT_FOUND_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), any())).thenReturn(NOT_FOUND_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), any())).thenReturn(NOT_FOUND_RESPONSE_FORMAT); + when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), any())).thenReturn(BAD_REQUEST_RESPONSE_FORMAT); + ByResponseFormatComponentException ce = Mockito.mock(ByResponseFormatComponentException.class); + when(ce.getResponseFormat()).thenReturn(UNAUTHORIZED_RESPONSE_FORMAT); + } + + @Before + public void resetSomeMocks() { + reset(ARTIFACTS_BUSINESS_LOGIC); + } + + @Test + public void downloadServiceArtifactMissingInstanceIdHeaderTest() { + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, SERVICE_NAME); + parametersMap.put(SERVICE_VERSION, SERVICE_VERSION); + parametersMap.put(ARTIFACT_NAME, ARTIFACT_NAME); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + Response response = target() + .path(path) + .request() + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_BAD_REQUEST); + } + + @Test + public void downloadServiceArtifactNoArtifactFoundTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + Either downloadServiceArtifactEither = Either.right(NOT_FOUND_RESPONSE_FORMAT); + + when(ARTIFACTS_BUSINESS_LOGIC.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName)) + .thenReturn(downloadServiceArtifactEither); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND); + } + + @Test + public void downloadServiceArtifactExceptionDuringProcessingTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + when(ARTIFACTS_BUSINESS_LOGIC.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName)) + .thenThrow(new RuntimeException("Test exception: downloadServiceArtifact")); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + + @Test + public void downloadServiceArtifactTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + Either downloadServiceArtifactEither = Either.left(BYTE_ARRAY); + when(ARTIFACTS_BUSINESS_LOGIC.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName)) + .thenReturn(downloadServiceArtifactEither); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK); + assertTrue(response.getHeaders().containsKey(Constants.CONTENT_DISPOSITION_HEADER)); + assertTrue(downloadedPayloadMatchesExpected(response, BYTE_ARRAY)); + } + + @Test + public void downloadResouceArtifactNoArtifactFoundTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String resourceName = RESOURCE_NAME; + String resourceVersion = RESOURCE_VERSION; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(RESOURCE_NAME, resourceName); + parametersMap.put(RESOURCE_VERSION, resourceVersion); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + Either downloadResourceArtifactEither = Either.right(NOT_FOUND_RESPONSE_FORMAT); + when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName, + resourceVersion, artifactName)) + .thenReturn(downloadResourceArtifactEither); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND); + } + + @Test + public void downloadResouceArtifactExceptionDuringProcessingTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String resourceName = RESOURCE_NAME; + String resourceVersion = RESOURCE_VERSION; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(RESOURCE_NAME, resourceName); + parametersMap.put(RESOURCE_VERSION, resourceVersion); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName, + resourceVersion, artifactName)) + .thenThrow(new RuntimeException("Test exception: downloadResouceArtifact")); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + + @Test + public void downloadResouceArtifactTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String resourceName = RESOURCE_NAME; + String resourceVersion = RESOURCE_VERSION; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(RESOURCE_NAME, resourceName); + parametersMap.put(RESOURCE_VERSION, resourceVersion); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + Either downloadResourceArtifactEither = Either.left(BYTE_ARRAY); + when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName, + resourceVersion, artifactName)) + .thenReturn(downloadResourceArtifactEither); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK); + assertTrue(response.getHeaders().containsKey(Constants.CONTENT_DISPOSITION_HEADER)); + assertTrue(downloadedPayloadMatchesExpected(response, BYTE_ARRAY)); + } + + @Test + public void downloadResourceInstanceArtifactNoArtifactFoundTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String resourceInstanceName = RESOURCE_INSTANCE_NAME; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(RESOURCE_INSTANCE_NAME, resourceInstanceName); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + Either downloadResourceArtifactEither = Either.right(NOT_FOUND_RESPONSE_FORMAT); + when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName, + artifactName)) + .thenReturn(downloadResourceArtifactEither); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND); + } + + @Test + public void downloadResourceInstanceArtifactExceptionDuringProcessingTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String resourceInstanceName = RESOURCE_INSTANCE_NAME; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(RESOURCE_INSTANCE_NAME, resourceInstanceName); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName, + artifactName)) + .thenThrow(new RuntimeException("Test exception: ownloadResourceInstanceArtifact")); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + + @Test + public void downloadResourceInstanceArtifactTest() { + String serviceName = SERVICE_NAME; + String serviceVersion = SERVICE_VERSION; + String resourceInstanceName = RESOURCE_INSTANCE_NAME; + String artifactName = ARTIFACT_NAME; + + Map parametersMap = new HashMap<>(); + parametersMap.put(SERVICE_NAME, serviceName); + parametersMap.put(SERVICE_VERSION, serviceVersion); + parametersMap.put(RESOURCE_INSTANCE_NAME, resourceInstanceName); + parametersMap.put(ARTIFACT_NAME, artifactName); + + String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}"; + String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}"); + + Either downloadResourceArtifactEither = Either.left(BYTE_ARRAY); + when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName, + artifactName)) + .thenReturn(downloadResourceArtifactEither); + + Response response = target() + .path(path) + .request() + .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString()) + .accept(MediaType.APPLICATION_OCTET_STREAM) + .get(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK); + assertTrue(response.getHeaders().containsKey(Constants.CONTENT_DISPOSITION_HEADER)); + assertTrue(downloadedPayloadMatchesExpected(response, BYTE_ARRAY)); + } + + @Override + protected Application configure() { + ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); + return new ResourceConfig(DistributionCatalogServlet.class) + .register(new AbstractBinder() { + + @Override + protected void configure() { + bind(HTTP_SERVLET_REQUEST).to(HttpServletRequest.class); + bind(USER_BUSINESS_LOGIC).to(UserBusinessLogic.class); + bind(COMPONENT_UTILS).to(ComponentsUtils.class); + bind(ARTIFACTS_BUSINESS_LOGIC).to(ArtifactsBusinessLogic.class); + } + }) + .property("contextConfig", context); + } +} \ No newline at end of file -- cgit 1.2.3-korg