diff options
Diffstat (limited to 'catalog-be')
14 files changed, 390 insertions, 77 deletions
diff --git a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml index cfde6b45b8..af517c5563 100644 --- a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml +++ b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml @@ -2617,4 +2617,20 @@ errors: code: 400, message: "Invalid node_types TOSCA yaml", messageId: "SVC4161" + } + + #---------SVC4162----------------------------- + # %1 - The janusgraph status + FAILED_CREATE_ARTIFACTS_TYPES: { + code: 500, + message: "Failed to create artifact types with status '%1'.", + messageId: "SVC4162" + } + + #---------SVC4163----------------------------- + # %1 - The janusgraph status + ARTIFACT_TYPE_ALREADY_EXIST: { + code: 409, + message: "Artifact type '%1' already exist.", + messageId: "SVC4163" }
\ No newline at end of file diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactTypeImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactTypeImportManager.java new file mode 100644 index 0000000000..e6ff100fc1 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactTypeImportManager.java @@ -0,0 +1,100 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.components.impl; + +import fj.data.Either; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.ArtifactTypeDefinition; +import org.openecomp.sdc.be.model.normatives.ElementTypeEnum; +import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation; +import org.openecomp.sdc.be.utils.TypeUtils; +import org.openecomp.sdc.exception.ResponseFormat; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("artifactTypeImportManager") +public class ArtifactTypeImportManager { + + private final ArtifactTypeOperation artifactTypeOperation; + private final ComponentsUtils componentsUtils; + private final CommonImportManager commonImportManager;; + + @Autowired + public ArtifactTypeImportManager(final ArtifactTypeOperation artifactTypeOperation, final ComponentsUtils componentsUtils, + final CommonImportManager commonImportManager) { + this.artifactTypeOperation = artifactTypeOperation; + this.componentsUtils = componentsUtils; + this.commonImportManager = commonImportManager; + } + + public Either<List<ArtifactTypeDefinition>, ResponseFormat> createArtifactTypes(final String artifactTypesYml, + final String modelName, + final boolean includeToModelDefaultImports) { + if (StringUtils.isNotEmpty(modelName)) { + artifactTypeOperation.validateModel(modelName); + } + final Either<List<ArtifactTypeDefinition>, ActionStatus> artifactTypes = createArtifactTypeFromYml(artifactTypesYml, modelName); + if (artifactTypes.isRight()) { + return Either.right(componentsUtils.getResponseFormat(artifactTypes.right().value())); + } + final List<ArtifactTypeDefinition> elementTypes = createArtifactTypesByDao(artifactTypes.left().value()); + if (includeToModelDefaultImports) { + commonImportManager.addTypesToDefaultImports(ElementTypeEnum.ARTIFACT_TYPE, artifactTypesYml, modelName); + } + return Either.left(elementTypes); + } + + private Either<List<ArtifactTypeDefinition>, ActionStatus> createArtifactTypeFromYml( + final String artifactTypesYml, final String modelName) { + final Either<List<ArtifactTypeDefinition>, ActionStatus> artifactTypes = + commonImportManager.createElementTypesFromYml(artifactTypesYml, this::createArtifactTypeDefinition); + if (artifactTypes.isLeft()) { + artifactTypes.left().value().forEach(artifactType -> artifactType.setModel(modelName)); + return artifactTypes; + } + return artifactTypes; + } + + private List<ArtifactTypeDefinition> createArtifactTypesByDao( + final List<ArtifactTypeDefinition> artifactTypes) { + final List<ArtifactTypeDefinition> createdTypes = new ArrayList<>(); + artifactTypes.forEach(type -> createdTypes.add(artifactTypeOperation.createArtifactType(type))); + return createdTypes; + } + + private ArtifactTypeDefinition createArtifactTypeDefinition(final String type, + final Map<String, Object> toscaJson) { + final ArtifactTypeDefinition artifactType = new ArtifactTypeDefinition(); + artifactType.setType(type); + if (toscaJson != null) { + commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), + artifactType::setDescription); + commonImportManager.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM.getElementName(), + artifactType::setDerivedFrom); + CommonImportManager.setProperties(toscaJson, artifactType::setProperties); + } + return artifactType; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java index a566d6c73d..7b21484a7b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogic.java @@ -88,6 +88,7 @@ import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.info.ArtifactTemplateInfo; import org.openecomp.sdc.be.model.ArtifactDefinition; +import org.openecomp.sdc.be.model.ArtifactTypeDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.ComponentParametersView; @@ -113,6 +114,7 @@ import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation; import org.openecomp.sdc.be.model.operations.api.IHeatParametersOperation; import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; @@ -179,6 +181,7 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { private IElementOperation elementOperation; @javax.annotation.Resource private IHeatParametersOperation heatParametersOperation; + private final ArtifactTypeOperation artifactTypeOperation; private ArtifactCassandraDao artifactCassandraDao; private ToscaExportHandler toscaExportUtils; private CsarUtils csarUtils; @@ -193,7 +196,8 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { ArtifactsResolver artifactsResolver, IElementOperation elementDao, IGroupOperation groupOperation, IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation, InterfaceOperation interfaceOperation, InterfaceLifecycleOperation interfaceLifecycleTypeOperation, - ArtifactsOperations artifactToscaOperation) { + ArtifactsOperations artifactToscaOperation, + ArtifactTypeOperation artifactTypeOperation) { super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation); this.artifactCassandraDao = artifactCassandraDao; @@ -202,6 +206,7 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { this.lifecycleBusinessLogic = lifecycleBusinessLogic; this.userBusinessLogic = userBusinessLogic; this.artifactsResolver = artifactsResolver; + this.artifactTypeOperation = artifactTypeOperation; } public static <R> Either<Boolean, R> ifTrue(boolean predicate, Supplier<Either<Boolean, R>> ifTrue) { @@ -4263,4 +4268,11 @@ public class ArtifactsBusinessLogic extends BaseBusinessLogic { return operation == CREATE || operation == LINK; } } + + public Map<String, ArtifactTypeDefinition> getAllToscaArtifacts(final String modelName) { + if (StringUtils.isNotEmpty(modelName)) { + artifactTypeOperation.validateModel(modelName); + } + return artifactTypeOperation.getAllArtifactTypes(modelName); + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java index af05825545..5c2faccd77 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java @@ -1564,4 +1564,31 @@ public class ComponentsUtils { return allDataTypes.left().value(); } + public ActionStatus convertFromStorageResponseForArtifactType(final StorageOperationStatus storageResponse) { + ActionStatus responseEnum; + switch (storageResponse) { + case OK: + responseEnum = ActionStatus.OK; + break; + case CONNECTION_FAILURE: + case GRAPH_IS_LOCK: + responseEnum = ActionStatus.GENERAL_ERROR; + break; + case BAD_REQUEST: + responseEnum = ActionStatus.INVALID_CONTENT; + break; + case ENTITY_ALREADY_EXISTS: + responseEnum = ActionStatus.ARTIFACT_TYPE_ALREADY_EXIST; + break; + case SCHEMA_VIOLATION: + responseEnum = ActionStatus.ARTIFACT_TYPE_ALREADY_EXIST; + break; + default: + responseEnum = ActionStatus.GENERAL_ERROR; + break; + } + log.debug(CONVERT_STORAGE_RESPONSE_TO_ACTION_RESPONSE, storageResponse, responseEnum); + return responseEnum; + } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java index 097398455f..7a229c88aa 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesFetchServlet.java @@ -19,11 +19,22 @@ */ package org.openecomp.sdc.be.servlets; +import com.jcabi.aspects.Loggable; +import fj.data.Either; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.servers.Server; +import io.swagger.v3.oas.annotations.servers.Servers; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.tags.Tags; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; - import javax.inject.Inject; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; @@ -36,8 +47,8 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; - import org.apache.commons.collections4.ListUtils; +import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic; import org.openecomp.sdc.be.components.impl.CapabilitiesBusinessLogic; import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic; import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; @@ -53,6 +64,7 @@ import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum; import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.exception.BusinessException; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; import org.openecomp.sdc.be.model.CapabilityTypeDefinition; @@ -64,25 +76,12 @@ import org.openecomp.sdc.be.model.User; 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.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.common.util.ValidationUtils; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.stereotype.Controller; -import com.jcabi.aspects.Loggable; - -import fj.data.Either; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.media.ArraySchema; -import io.swagger.v3.oas.annotations.media.Content; -import io.swagger.v3.oas.annotations.media.Schema; -import io.swagger.v3.oas.annotations.responses.ApiResponse; -import io.swagger.v3.oas.annotations.servers.Server; -import io.swagger.v3.oas.annotations.servers.Servers; -import io.swagger.v3.oas.annotations.tags.Tag; -import io.swagger.v3.oas.annotations.tags.Tags; - @Loggable(prepend = true, value = Loggable.DEBUG, trim = false) @Path("/v1/catalog") @Tags({@Tag(name = "SDCE-2 APIs")}) @@ -97,18 +96,21 @@ public class TypesFetchServlet extends AbstractValidationsServlet { private final CapabilitiesBusinessLogic capabilitiesBusinessLogic; private final InterfaceOperationBusinessLogic interfaceOperationBusinessLogic; private final ResourceBusinessLogic resourceBusinessLogic; + private final ArtifactsBusinessLogic artifactsBusinessLogic; @Inject public TypesFetchServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager, PropertyBusinessLogic propertyBusinessLogic, RelationshipTypeBusinessLogic relationshipTypeBusinessLogic, CapabilitiesBusinessLogic capabilitiesBusinessLogic, - InterfaceOperationBusinessLogic interfaceOperationBusinessLogic, ResourceBusinessLogic resourceBusinessLogic) { + InterfaceOperationBusinessLogic interfaceOperationBusinessLogic, ResourceBusinessLogic resourceBusinessLogic, + ArtifactsBusinessLogic artifactsBusinessLogic) { super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager); this.propertyBusinessLogic = propertyBusinessLogic; this.relationshipTypeBusinessLogic = relationshipTypeBusinessLogic; this.capabilitiesBusinessLogic = capabilitiesBusinessLogic; this.interfaceOperationBusinessLogic = interfaceOperationBusinessLogic; this.resourceBusinessLogic = resourceBusinessLogic; + this.artifactsBusinessLogic = artifactsBusinessLogic; } @GET @@ -313,6 +315,31 @@ public class TypesFetchServlet extends AbstractValidationsServlet { } } + @GET + @Path("/artifactTypes") + @Operation(description = "Get Tosca ArtifactTypes", method = "GET", summary = "Returns tosca artifact types", responses = { + @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))), + @ApiResponse(responseCode = "200", description = "Listing successful"), + @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), + @ApiResponse(responseCode = "403", description = "Restricted operation"), + @ApiResponse(responseCode = "404", description = "Tosca Artifact Types not found")}) + @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) + public Response getAllToscaArtifactTypes(@Parameter(description = "Model name") @QueryParam("model") String model, + @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator) { + try { + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), artifactsBusinessLogic.getAllToscaArtifacts(model)); + } catch (final BusinessException e) { + throw e; + } catch (final Exception e) { + final var errorMsg = "Unexpected error while listing the Tosca Artifact types"; + BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg); + log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, this.getClass().getName(), errorMsg, e); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); + } + + } + + private Either<Map<String, Component>, Response> getComponent(ComponentBusinessLogic resourceBL, boolean isAbstract, String userId) { Either<List<Component>, ResponseFormat> actionResponse; List<Component> componentList; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java index 06738a88c5..fbd480169a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java @@ -49,6 +49,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.apache.commons.lang3.tuple.ImmutablePair; import org.glassfish.jersey.media.multipart.FormDataParam; +import org.openecomp.sdc.be.components.impl.ArtifactTypeImportManager; import org.openecomp.sdc.be.components.impl.CapabilityTypeImportManager; import org.openecomp.sdc.be.components.impl.CategoriesImportManager; import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; @@ -102,6 +103,7 @@ public class TypesUploadServlet extends AbstractValidationsServlet { private final GroupTypeImportManager groupTypeImportManager; private final PolicyTypeImportManager policyTypeImportManager; private final RelationshipTypeImportManager relationshipTypeImportManager; + private final ArtifactTypeImportManager artifactTypeImportManager; @Inject public TypesUploadServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL, @@ -110,7 +112,7 @@ public class TypesUploadServlet extends AbstractValidationsServlet { InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager, CategoriesImportManager categoriesImportManager, DataTypeImportManager dataTypeImportManager, GroupTypeImportManager groupTypeImportManager, PolicyTypeImportManager policyTypeImportManager, - RelationshipTypeImportManager relationshipTypeImportManager) { + RelationshipTypeImportManager relationshipTypeImportManager, ArtifactTypeImportManager artifactTypeImportManager) { super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager); this.capabilityTypeImportManager = capabilityTypeImportManager; this.interfaceLifecycleTypeImportManager = interfaceLifecycleTypeImportManager; @@ -119,6 +121,7 @@ public class TypesUploadServlet extends AbstractValidationsServlet { this.groupTypeImportManager = groupTypeImportManager; this.policyTypeImportManager = policyTypeImportManager; this.relationshipTypeImportManager = relationshipTypeImportManager; + this.artifactTypeImportManager = artifactTypeImportManager; } @POST @@ -178,6 +181,26 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } @POST + @Path("/artifactTypes") + @Operation(description = "Create Tosca Artifact types from yaml", method = "POST", summary = "Returns created Tosca artifact types", responses = { + @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))), + @ApiResponse(responseCode = "201", description = "Tosca Artifact types created"), + @ApiResponse(responseCode = "403", description = "Restricted operation"), + @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), + @ApiResponse(responseCode = "409", description = "Tosca Artifact Type already exist")}) + @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) + public Response uploadArtifactTypes(@Parameter(description = "Zip file containing a yaml with the TOSCA artifact types definition") + @FormDataParam("artifactsZip") File file, + @Parameter(description = "model name") @FormDataParam("model") String modelName, + @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, + @Parameter(description = "A flag to add types to the default imports") + @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { + final ConsumerTwoParam<Wrapper<Response>, String> createElementsMethod = (responseWrapper, ymlPayload) -> + createElementsType(responseWrapper, () -> artifactTypeImportManager.createArtifactTypes(ymlPayload, modelName, includeToModelDefaultImports)); + return uploadElementTypeServletLogic(createElementsMethod, file, request, creator, NodeTypeEnum.ArtifactType.getName()); + } + + @POST @Path("/categories") @Operation(description = "Create Categories from yaml", method = "POST", summary = "Returns created categories", responses = { @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))), diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactBusinessLogicTest.java index f1a1aaaea1..616d9e42f9 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactBusinessLogicTest.java @@ -20,6 +20,17 @@ package org.openecomp.sdc.be.components.impl; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.HEAT_ENV_NAME; +import static org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.HEAT_VF_ENV_NAME; + import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; @@ -28,6 +39,12 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import fj.data.Either; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Optional; import org.junit.Before; import org.junit.Test; @@ -38,10 +55,10 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnitRunner; import org.openecomp.sdc.be.components.ArtifactsResolver; -import org.openecomp.sdc.be.config.ArtifactConfigManager; import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic; import org.openecomp.sdc.be.components.utils.ArtifactBuilder; import org.openecomp.sdc.be.components.utils.ObjectGenerator; +import org.openecomp.sdc.be.config.ArtifactConfigManager; import org.openecomp.sdc.be.config.ArtifactConfiguration; import org.openecomp.sdc.be.config.ComponentType; import org.openecomp.sdc.be.dao.api.ActionStatus; @@ -65,6 +82,7 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation; import org.openecomp.sdc.be.resources.data.DAOArtifactData; import org.openecomp.sdc.be.servlets.RepresentationUtils; import org.openecomp.sdc.be.tosca.CsarUtils; @@ -74,24 +92,6 @@ import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; import org.openecomp.sdc.common.api.ArtifactTypeEnum; import org.openecomp.sdc.exception.ResponseFormat; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.HEAT_ENV_NAME; -import static org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic.HEAT_VF_ENV_NAME; - @RunWith(MockitoJUnitRunner.class) public class ArtifactBusinessLogicTest extends BaseBusinessLogicMock{ @@ -122,10 +122,6 @@ public class ArtifactBusinessLogicTest extends BaseBusinessLogicMock{ JanusGraphDao janusGraphDao; @Mock private IInterfaceLifecycleOperation interfaceLifecycleOperation; - - // public static final InformationDeployedArtifactsBusinessLogic - // informationDeployedArtifactsBusinessLogic = - // Mockito.mock(InformationDeployedArtifactsBusinessLogic.class); @Mock private ToscaExportHandler toscaExportHandler; @Mock @@ -134,13 +130,15 @@ public class ArtifactBusinessLogicTest extends BaseBusinessLogicMock{ private LifecycleBusinessLogic lifecycleBusinessLogic; @Mock private ArtifactsResolver artifactsResolver; + @Mock + private ArtifactTypeOperation artifactTypeOperation; public static final Resource resource = Mockito.mock(Resource.class); private Gson gson = new GsonBuilder().setPrettyPrinting().create(); @Before public void initMocks() { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); when(userBusinessLogic.getUser(eq("jh0003"), anyBoolean())).thenReturn(USER); when(resource.getResourceType()).thenReturn(ResourceTypeEnum.VFC); } @@ -536,16 +534,6 @@ public class ArtifactBusinessLogicTest extends BaseBusinessLogicMock{ assertThat(result.isLeft()).isTrue(); } - private ArtifactsBusinessLogic getArtifactsBusinessLogic() { - ArtifactsBusinessLogic artifactsBusinessLogic = new ArtifactsBusinessLogic(artifactCassandraDao, - toscaExportHandler, csarUtils, lifecycleBusinessLogic, - userBusinessLogic, artifactsResolver, elementDao, groupOperation, groupInstanceOperation, - groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation); - artifactsBusinessLogic.setGraphLockOperation(graphLockOperation); - artifactsBusinessLogic.setToscaOperationFacade(toscaOperationFacade); - return artifactsBusinessLogic; - } - @Test public void buildArtifactPayloadWhenShouldLockAndNotInTransaction() { ArtifactDefinition artifactDefinition = new ArtifactDefinition(); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactTypeImportManagerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactTypeImportManagerTest.java new file mode 100644 index 0000000000..28a7e9c13b --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactTypeImportManagerTest.java @@ -0,0 +1,73 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.be.components.impl; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.core.Is.is; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.model.Model; +import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation; +import org.openecomp.sdc.be.model.operations.impl.ModelOperation; + +public class ArtifactTypeImportManagerTest { + + @InjectMocks + private ArtifactTypeImportManager artifactTypeImportManager; + @Mock + private CommonImportManager commonImportManager; + @Mock + private ModelOperation modelOperation; + @Mock + private ArtifactTypeOperation artifactTypeOperation; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void createArtifactTypeFromYmlTest() throws IOException { + when(commonImportManager.createElementTypesFromYml(Mockito.anyString(), any())).thenCallRealMethod(); + when(commonImportManager.createElementTypesFromToscaJsonMap(any(), any())).thenCallRealMethod(); + when(artifactTypeOperation.createArtifactType(any())).thenCallRealMethod(); + when(modelOperation.findModelByName("test")).thenReturn(Optional.of(new Model("test"))); + final var result = artifactTypeImportManager + .createArtifactTypes(getArtifactsYml(), "test", false); + assertThat("The createElementTypesFromYml should be left", result.isLeft(), is(true)); + final var artifactTypes = result.left().value(); + assertThat("The artifact types list should have size", artifactTypes, hasSize(1)); + } + + private String getArtifactsYml() throws IOException { + return new String(Files.readAllBytes(Paths.get("src/test/resources/types/artifactTypes.yml"))); + } +} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java index 1c680e5a15..14d00efd42 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ArtifactsBusinessLogicTest.java @@ -128,6 +128,7 @@ import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.ArtifactOperation; +import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation; import org.openecomp.sdc.be.model.operations.impl.UserAdminOperation; import org.openecomp.sdc.be.resources.data.DAOArtifactData; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; @@ -197,6 +198,9 @@ public class ArtifactsBusinessLogicTest extends BaseBusinessLogicMock { private ToscaExportHandler toscaExportHandler; @Mock private LifecycleBusinessLogic lifecycleBusinessLogic; + @Mock + private ArtifactTypeOperation artifactTypeOperation; + private Gson gson = new GsonBuilder().setPrettyPrinting().create(); private static List<ArtifactType> getAllTypes() { @@ -2441,7 +2445,7 @@ public class ArtifactsBusinessLogicTest extends BaseBusinessLogicMock { toscaExportHandler, csarUtils, lifecycleBusinessLogic, userBusinessLogic, artifactsResolver, elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, - interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation); + interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation, artifactTypeOperation); artifactsBusinessLogic.setComponentsUtils(componentsUtils); return artifactsBusinessLogic; } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java index 90c1864ba7..0453dc7fef 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java @@ -129,6 +129,7 @@ import org.openecomp.sdc.be.model.operations.api.IGroupOperation; import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation; import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation; import org.openecomp.sdc.be.model.operations.impl.CsarOperation; import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; @@ -214,6 +215,7 @@ public class ResourceBusinessLogicTest { private final ToscaExportHandler toscaExportHandler = Mockito.mock(ToscaExportHandler.class); private final PolicyTypeOperation policyTypeOperation = Mockito.mock(PolicyTypeOperation.class); private final PolicyBusinessLogic policyBusinessLogic = Mockito.mock(PolicyBusinessLogic.class); + private final ArtifactTypeOperation artifactTypeOperation = Mockito.mock(ArtifactTypeOperation.class); private final DataTypeBusinessLogic dataTypeBusinessLogic = Mockito.mock(DataTypeBusinessLogic.class); private final PolicyTypeBusinessLogic policyTypeBusinessLogic = Mockito.mock(PolicyTypeBusinessLogic.class); @@ -231,7 +233,7 @@ public class ResourceBusinessLogicTest { ComponentsUtils componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class)); ArtifactsBusinessLogic artifactManager = new ArtifactsBusinessLogic(artifactCassandraDao, toscaExportHandler, csarUtils, lifecycleBl, userBusinessLogic, artifactsResolver, elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, - interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation); + interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation, artifactTypeOperation); CsarOperation csarOperation = Mockito.mock(CsarOperation.class); @InjectMocks CsarBusinessLogic csarBusinessLogic ; diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java index 2305cd3059..ef4ec18ec9 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesFetchServletTest.java @@ -20,7 +20,13 @@ package org.openecomp.sdc.be.servlets; +import static org.mockito.Mockito.mock; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Response; import org.junit.Test; +import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic; import org.openecomp.sdc.be.components.impl.CapabilitiesBusinessLogic; import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic; @@ -32,12 +38,6 @@ import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; import org.openecomp.sdc.be.user.UserBusinessLogic; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.core.Response; - -import static org.mockito.Mockito.mock; - public class TypesFetchServletTest { private TypesFetchServlet createTestSubject() { @@ -51,10 +51,11 @@ public class TypesFetchServletTest { CapabilitiesBusinessLogic capabilitiesBusinessLogic = mock(CapabilitiesBusinessLogic.class); InterfaceOperationBusinessLogic interfaceOperationBusinessLogic = mock(InterfaceOperationBusinessLogic.class); ResourceBusinessLogic resourceBusinessLogic = mock(ResourceBusinessLogic.class); + ArtifactsBusinessLogic artifactsBusinessLogic = mock(ArtifactsBusinessLogic.class); return new TypesFetchServlet(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager, propertyBusinessLogic, relationshipTypeBusinessLogic, capabilitiesBusinessLogic, - interfaceOperationBusinessLogic, resourceBusinessLogic); + interfaceOperationBusinessLogic, resourceBusinessLogic, artifactsBusinessLogic); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesUploadServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesUploadServletTest.java index 0373aa272a..ed9b9a4eff 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesUploadServletTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/TypesUploadServletTest.java @@ -23,6 +23,8 @@ package org.openecomp.sdc.be.servlets; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; import fj.data.Either; @@ -56,6 +58,7 @@ import org.junit.jupiter.api.TestInstance.Lifecycle; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.components.impl.ArtifactTypeImportManager; import org.openecomp.sdc.be.components.impl.CapabilityTypeImportManager; import org.openecomp.sdc.be.components.impl.CategoriesImportManager; import org.openecomp.sdc.be.components.impl.DataTypeImportManager; @@ -124,6 +127,8 @@ class TypesUploadServletTest extends JerseyTest { private ComponentsUtils componentUtils; @Mock private ResponseFormat responseFormat; + @Mock + private ArtifactTypeImportManager artifactTypeImportManager; @BeforeAll public void setup() { @@ -137,6 +142,7 @@ class TypesUploadServletTest extends JerseyTest { when(webApplicationContext.getBean(InterfaceLifecycleTypeImportManager.class)).thenReturn(interfaceLifecycleTypeImportManager); when(webApplicationContext.getBean(GroupTypeImportManager.class)).thenReturn(groupTypeImportManager); when(webApplicationContext.getBean(CategoriesImportManager.class)).thenReturn(categoriesImportManager); + when(webApplicationContext.getBean(ArtifactTypeImportManager.class)).thenReturn(artifactTypeImportManager); when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils); when(servletUtils.getComponentsUtils()).thenReturn(componentUtils); when(servletUtils.getUserAdmin()).thenReturn(userAdmin); @@ -164,7 +170,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingCapabilityTypeSuccessTest() { final ImmutablePair<CapabilityTypeDefinition, Boolean> immutablePair = new ImmutablePair<>(new CapabilityTypeDefinition(), true); final Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(Arrays.asList(immutablePair)); - when(capabilityTypeImportManager.createCapabilityTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); + when(capabilityTypeImportManager.createCapabilityTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("capabilityTypeZip", new File("src/test/resources/types/capabilityTypes.zip")); MultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -178,7 +184,7 @@ class TypesUploadServletTest extends JerseyTest { @Test void creatingCapabilityType_Either_isRight_FailedTest() { final Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> either = Either.right(new ResponseFormat(500)); - when(capabilityTypeImportManager.createCapabilityTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); + when(capabilityTypeImportManager.createCapabilityTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("capabilityTypeZip", new File("src/test/resources/types/capabilityTypes.zip")); MultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -193,7 +199,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingCapabilityTypeWithModelSuccessTest() { final ImmutablePair<CapabilityTypeDefinition, Boolean> immutablePair = new ImmutablePair<>(new CapabilityTypeDefinition(), true); final Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(Arrays.asList(immutablePair)); - when(capabilityTypeImportManager.createCapabilityTypes(Mockito.anyString(), Mockito.eq("testModel"), Mockito.anyBoolean())).thenReturn( + when(capabilityTypeImportManager.createCapabilityTypes(anyString(), Mockito.eq("testModel"), Mockito.anyBoolean())).thenReturn( either); final FileDataBodyPart filePart = new FileDataBodyPart("capabilityTypeZip", new File("src/test/resources/types/capabilityTypes.zip")); FormDataMultiPart multipartEntity = new FormDataMultiPart(); @@ -210,7 +216,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingDataTypeSuccessTest() { final ImmutablePair<DataTypeDefinition, Boolean> immutablePair = new ImmutablePair<>(new DataTypeDefinition(), true); final Either<List<ImmutablePair<DataTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(Arrays.asList(immutablePair)); - when(dataTypeImportManager.createDataTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); + when(dataTypeImportManager.createDataTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("dataTypesZip", new File("src/test/resources/types/datatypes.zip")); MultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -224,7 +230,7 @@ class TypesUploadServletTest extends JerseyTest { @Test void creatingDataType_Either_isRight_FailedTest() { final Either<List<ImmutablePair<DataTypeDefinition, Boolean>>, ResponseFormat> either = Either.right(new ResponseFormat(500)); - when(dataTypeImportManager.createDataTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); + when(dataTypeImportManager.createDataTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("dataTypesZip", new File("src/test/resources/types/datatypes.zip")); MultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -239,7 +245,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingDataType_AlreadyExists_FailedTest() { final ImmutablePair<DataTypeDefinition, Boolean> immutablePair = new ImmutablePair<>(new DataTypeDefinition(), false); final Either<List<ImmutablePair<DataTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(Arrays.asList(immutablePair)); - when(dataTypeImportManager.createDataTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); + when(dataTypeImportManager.createDataTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("dataTypesZip", new File("src/test/resources/types/datatypes.zip")); MultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -254,7 +260,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingDataTypeWithModelSuccessTest() { final ImmutablePair<DataTypeDefinition, Boolean> immutablePair = new ImmutablePair<>(new DataTypeDefinition(), true); final Either<List<ImmutablePair<DataTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(Arrays.asList(immutablePair)); - when(dataTypeImportManager.createDataTypes(Mockito.anyString(), Mockito.eq("testModel"), Mockito.anyBoolean())).thenReturn(either); + when(dataTypeImportManager.createDataTypes(anyString(), Mockito.eq("testModel"), Mockito.anyBoolean())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("dataTypesZip", new File("src/test/resources/types/datatypes.zip")); FormDataMultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -270,7 +276,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingRelationshipTypeSuccessTest() { final ImmutablePair<RelationshipTypeDefinition, Boolean> immutablePair = new ImmutablePair<>(new RelationshipTypeDefinition(), true); final Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(Arrays.asList(immutablePair)); - when(relationshipTypeImportManager.createRelationshipTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); + when(relationshipTypeImportManager.createRelationshipTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("relationshipTypeZip", new File("src/test/resources/types/relationship.zip")); MultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -285,7 +291,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingRelationshipType_AlreadyExists_FailedTest() { final ImmutablePair<RelationshipTypeDefinition, Boolean> immutablePair = new ImmutablePair<>(new RelationshipTypeDefinition(), false); final Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(Arrays.asList(immutablePair)); - when(relationshipTypeImportManager.createRelationshipTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); + when(relationshipTypeImportManager.createRelationshipTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("relationshipTypeZip", new File("src/test/resources/types/relationship.zip")); MultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -300,7 +306,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingRelationshipTypeWithModelSuccessTest() { final ImmutablePair<RelationshipTypeDefinition, Boolean> immutablePair = new ImmutablePair<>(new RelationshipTypeDefinition(), true); final Either<List<ImmutablePair<RelationshipTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(Arrays.asList(immutablePair)); - when(relationshipTypeImportManager.createRelationshipTypes(Mockito.anyString(), Mockito.eq("testModel"), Mockito.anyBoolean())).thenReturn( + when(relationshipTypeImportManager.createRelationshipTypes(anyString(), Mockito.eq("testModel"), Mockito.anyBoolean())).thenReturn( either); final FileDataBodyPart filePart = new FileDataBodyPart("relationshipTypeZip", new File("src/test/resources/types/relationship.zip")); FormDataMultiPart multipartEntity = new FormDataMultiPart(); @@ -366,7 +372,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingInterfaceLifecycleTypeSuccessTest() { final ImmutablePair<InterfaceDefinition, Boolean> immutablePair = new ImmutablePair<>(new InterfaceDefinition(), true); final Either<List<InterfaceDefinition>, ResponseFormat> either = Either.left(emptyList()); - when(interfaceLifecycleTypeImportManager.createLifecycleTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())) + when(interfaceLifecycleTypeImportManager.createLifecycleTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())) .thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("interfaceLifecycleTypeZip", new File("src/test/resources/types/interfaceLifecycleTypes.zip")); @@ -382,7 +388,7 @@ class TypesUploadServletTest extends JerseyTest { @Test void creatingInterfaceLifecycleType_Either_isRight_FailedTest() { final Either<List<InterfaceDefinition>, ResponseFormat> either = Either.right(new ResponseFormat(500)); - when(interfaceLifecycleTypeImportManager.createLifecycleTypes(Mockito.anyString(), Mockito.isNull(), Mockito.anyBoolean())) + when(interfaceLifecycleTypeImportManager.createLifecycleTypes(anyString(), Mockito.isNull(), Mockito.anyBoolean())) .thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("interfaceLifecycleTypeZip", new File("src/test/resources/types/interfaceLifecycleTypes.zip")); @@ -399,7 +405,7 @@ class TypesUploadServletTest extends JerseyTest { void creatingInterfaceLifecycleTypeWithModelSuccessTest() { final ImmutablePair<InterfaceDefinition, Boolean> immutablePair = new ImmutablePair<>(new InterfaceDefinition(), true); final Either<List<InterfaceDefinition>, ResponseFormat> either = Either.left(emptyList()); - when(interfaceLifecycleTypeImportManager.createLifecycleTypes(Mockito.anyString(), Mockito.eq("testModel"), Mockito.anyBoolean())) + when(interfaceLifecycleTypeImportManager.createLifecycleTypes(anyString(), Mockito.eq("testModel"), Mockito.anyBoolean())) .thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("interfaceLifecycleTypeZip", new File("src/test/resources/types/interfaceLifecycleTypes.zip")); @@ -465,7 +471,7 @@ class TypesUploadServletTest extends JerseyTest { @Test void creatingCategoriesTypeSuccessTest() { final Either<Map<String, List<CategoryDefinition>>, ResponseFormat> either = Either.left(emptyMap()); - when(categoriesImportManager.createCategories(Mockito.anyString())).thenReturn(either); + when(categoriesImportManager.createCategories(anyString())).thenReturn(either); final FileDataBodyPart filePart = new FileDataBodyPart("categoriesZip", new File("src/test/resources/types/categoryTypes.zip")); MultiPart multipartEntity = new FormDataMultiPart(); multipartEntity.bodyPart(filePart); @@ -476,6 +482,30 @@ class TypesUploadServletTest extends JerseyTest { assertEquals(HttpStatus.CREATED_201, response.getStatus()); } + @Test + void creatingArtifactTypeSuccessTest() { + when(artifactTypeImportManager.createArtifactTypes(anyString(), anyString(), anyBoolean())) + .thenReturn(Either.left(emptyList())); + final FileDataBodyPart filePart = new FileDataBodyPart("artifactsZip", new File("src/test/resources/types/artifactTypes.zip")); + final FormDataMultiPart multipartEntity = new FormDataMultiPart(); + multipartEntity.bodyPart(filePart); + multipartEntity.field("model", "testModel"); + final Response response = target().path("/v1/catalog/uploadType/artifactTypes").request(MediaType.APPLICATION_JSON) + .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA), Response.class); + assertEquals(HttpStatus.CREATED_201, response.getStatus()); + } + + @Test + void creatingArtifactTypeFailTest() { + when(artifactTypeImportManager.createArtifactTypes(anyString(), anyString(), anyBoolean())) + .thenReturn(Either.right(new ResponseFormat(500))); + final FormDataMultiPart multipartEntity = new FormDataMultiPart(); + multipartEntity.field("model", "testModel"); + final Response response = target().path("/v1/catalog/uploadType/artifactTypes").request(MediaType.APPLICATION_JSON) + .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA), Response.class); + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus()); + } + @Override protected void configureClient(ClientConfig config) { config.register(MultiPartFeature.class); @@ -489,7 +519,7 @@ class TypesUploadServletTest extends JerseyTest { final TypesUploadServlet typesUploadServlet = new TypesUploadServlet(null, null, componentUtils, servletUtils, null, capabilityTypeImportManager, interfaceLifecycleTypeImportManager, categoriesImportManager, dataTypeImportManager, - groupTypeImportManager, policyTypeImportManager, relationshipTypeImportManager); + groupTypeImportManager, policyTypeImportManager, relationshipTypeImportManager, artifactTypeImportManager); final ResourceConfig resourceConfig = new ResourceConfig().register(typesUploadServlet); resourceConfig.register(MultiPartFeature.class); diff --git a/catalog-be/src/test/resources/types/artifactTypes.yml b/catalog-be/src/test/resources/types/artifactTypes.yml new file mode 100644 index 0000000000..9d218303a5 --- /dev/null +++ b/catalog-be/src/test/resources/types/artifactTypes.yml @@ -0,0 +1,10 @@ +tosca.artifacts.Implementation.nfv.artifactTest: + derived_from: tosca.artifacts.Implementation + description: artifacts for Mistral workflows + mime_type: application/x-yaml + file_ext: [ yaml ] + properties: + prop_A: + type: string + prop_B: + type: string diff --git a/catalog-be/src/test/resources/types/artifactTypes.zip b/catalog-be/src/test/resources/types/artifactTypes.zip Binary files differnew file mode 100644 index 0000000000..2e3ed3bb43 --- /dev/null +++ b/catalog-be/src/test/resources/types/artifactTypes.zip |