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 ++++++++++++---------- 1 file changed, 33 insertions(+), 29 deletions(-) (limited to 'catalog-be/src/main/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"); -- cgit 1.2.3-korg