diff options
author | vasraz <vasyl.razinkov@est.tech> | 2022-03-24 16:38:00 +0000 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-03-28 08:37:43 +0000 |
commit | 4307dbb5f0dc5da60e55f3473259cfd059d14770 (patch) | |
tree | acb1afb19475a772f9169ed0e8f49ea722905a6c | |
parent | 0902d2829ce984730085c816b649bed957ac0d99 (diff) |
Implement VSP deletion from Storage
Change-Id: I06d86696570b2751f152dffc06fd580ef8c0c705
Signed-off-by: Vasyl Razinkov <vasyl.razinkov@est.tech>
Issue-ID: SDC-3924
7 files changed, 236 insertions, 76 deletions
diff --git a/common-app-api/src/main/java/org/openecomp/sdc/be/csar/storage/ArtifactStorageManager.java b/common-app-api/src/main/java/org/openecomp/sdc/be/csar/storage/ArtifactStorageManager.java index f7d611b717..9a4bc00162 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/be/csar/storage/ArtifactStorageManager.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/be/csar/storage/ArtifactStorageManager.java @@ -68,4 +68,11 @@ public interface ArtifactStorageManager { InputStream get(final String vspId, final String versionId); void delete(ArtifactInfo artifactInfo); + + /** + * Delete all versions and VSP itself + * + * @param vspId - VSP ID + */ + void delete(String vspId); } diff --git a/common-be/src/main/java/org/openecomp/sdc/be/csar/storage/MinIoStorageArtifactStorageManager.java b/common-be/src/main/java/org/openecomp/sdc/be/csar/storage/MinIoStorageArtifactStorageManager.java index 7b9cf087a6..bc0792b472 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/csar/storage/MinIoStorageArtifactStorageManager.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/csar/storage/MinIoStorageArtifactStorageManager.java @@ -24,11 +24,14 @@ import static org.openecomp.sdc.common.errors.Messages.EXTERNAL_CSAR_STORE_CONFI import io.minio.BucketExistsArgs; import io.minio.GetObjectArgs; +import io.minio.ListObjectsArgs; import io.minio.MakeBucketArgs; import io.minio.MinioClient; import io.minio.PutObjectArgs; +import io.minio.RemoveBucketArgs; import io.minio.RemoveObjectArgs; import io.minio.StatObjectArgs; +import io.minio.messages.Item; import java.io.InputStream; import java.util.Map; import lombok.Getter; @@ -134,12 +137,7 @@ public class MinIoStorageArtifactStorageManager implements ArtifactStorageManage @Override public InputStream get(final ArtifactInfo artifactInfo) { final MinIoArtifactInfo minioObject = (MinIoArtifactInfo) artifactInfo; - try { - return get(minioObject.getBucket(), minioObject.getObjectName()); - } catch (final Exception e) { - LOGGER.error("Failed to get - bucket: '{}', object: '{}'", minioObject.getBucket(), minioObject.getObjectName(), e); - throw new ArtifactStorageException("Failed to get Object", e); - } + return get(minioObject.getBucket(), minioObject.getObjectName()); } @Override @@ -173,6 +171,28 @@ public class MinIoStorageArtifactStorageManager implements ArtifactStorageManage } + @Override + public void delete(final String vspId) { + LOGGER.debug("DELETE VSP - bucket: '{}'", vspId); + final var listObjects = minioClient.listObjects(ListObjectsArgs.builder().bucket(vspId).build()); + listObjects.forEach(itemResult -> { + Item versionId; + try { + versionId = itemResult.get(); + } catch (final Exception e) { + LOGGER.error("Failed to get versionId for VSP - bucket: '{}'", vspId, e); + throw new ArtifactStorageException(String.format("Failed to delete VSP '%s'", vspId), e); + } + delete(new MinIoArtifactInfo(vspId, versionId.objectName())); + }); + try { + minioClient.removeBucket(RemoveBucketArgs.builder().bucket(vspId).build()); + } catch (final Exception e) { + LOGGER.error("Failed to delete VSP - bucket: '{}'", vspId, e); + throw new ArtifactStorageException(String.format("Failed to delete VSP '%s'", vspId), e); + } + } + private MinIoStorageArtifactStorageConfig readMinIoStorageArtifactStorageConfig() { final var commonConfigurationManager = CommonConfigurationManager.getInstance(); final Map<String, Object> endpoint = commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, ENDPOINT, null); diff --git a/common-be/src/main/java/org/openecomp/sdc/be/csar/storage/NoneStorageManager.java b/common-be/src/main/java/org/openecomp/sdc/be/csar/storage/NoneStorageManager.java index aec69d5bd7..562be2de14 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/csar/storage/NoneStorageManager.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/csar/storage/NoneStorageManager.java @@ -61,4 +61,9 @@ public class NoneStorageManager implements ArtifactStorageManager { throw new UnsupportedOperationException(); } + @Override + public void delete(final String vspId) { + throw new UnsupportedOperationException(); + } + } diff --git a/common-be/src/test/java/org/openecomp/sdc/be/csar/storage/MinIoStorageArtifactStorageManagerTest.java b/common-be/src/test/java/org/openecomp/sdc/be/csar/storage/MinIoStorageArtifactStorageManagerTest.java index fa577913fa..09e62841e6 100644 --- a/common-be/src/test/java/org/openecomp/sdc/be/csar/storage/MinIoStorageArtifactStorageManagerTest.java +++ b/common-be/src/test/java/org/openecomp/sdc/be/csar/storage/MinIoStorageArtifactStorageManagerTest.java @@ -20,17 +20,29 @@ package org.openecomp.sdc.be.csar.storage; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; import io.minio.BucketExistsArgs; +import io.minio.GetObjectArgs; +import io.minio.GetObjectResponse; +import io.minio.MakeBucketArgs; import io.minio.MinioClient; +import io.minio.RemoveBucketArgs; +import io.minio.RemoveObjectArgs; +import io.minio.StatObjectArgs; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Path; import javax.activation.DataHandler; +import okhttp3.Headers; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -44,6 +56,7 @@ import org.mockito.MockitoAnnotations; import org.mockito.junit.jupiter.MockitoExtension; import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.Credentials; import org.openecomp.sdc.be.csar.storage.MinIoStorageArtifactStorageConfig.EndPoint; +import org.openecomp.sdc.be.csar.storage.exception.ArtifactStorageException; @ExtendWith(MockitoExtension.class) class MinIoStorageArtifactStorageManagerTest { @@ -78,7 +91,7 @@ class MinIoStorageArtifactStorageManagerTest { } @Test - void testUpload() throws Exception { + void testUploadOK() throws Exception { when(builderBucketExistsArgs .bucket(anyString()) @@ -95,7 +108,23 @@ class MinIoStorageArtifactStorageManagerTest { } @Test - void testPersist() { + void testUploadFail() throws Exception { + + when(builderBucketExistsArgs + .bucket(anyString()) + .build() + ).thenReturn(new BucketExistsArgs()); + when(minioClient.bucketExists(any(BucketExistsArgs.class))).thenReturn(false); + + final Attachment attachment = mockAttachment(); + doThrow(new RuntimeException()).when(minioClient).makeBucket(any(MakeBucketArgs.class)); + assertThrows(ArtifactStorageException.class, () -> { + testSubject.upload(VSP_ID, VERSION_ID, attachment.getDataHandler().getInputStream()); + }); + } + + @Test + void testPersistOK() { final ArtifactInfo result = testSubject.persist(VSP_ID, VERSION_ID, new MinIoArtifactInfo(VSP_ID, VERSION_ID)); Assertions.assertNotNull(result); Assertions.assertTrue(result instanceof MinIoArtifactInfo); @@ -104,10 +133,51 @@ class MinIoStorageArtifactStorageManagerTest { } @Test + void testPersistFail() throws Exception { + doThrow(new RuntimeException()).when(minioClient).statObject(any(StatObjectArgs.class)); + assertThrows(ArtifactStorageException.class, () -> { + testSubject.persist(VSP_ID, VERSION_ID, new MinIoArtifactInfo(VSP_ID, VERSION_ID)); + }); + } + + @Test void testIsEnabled() { Assertions.assertTrue(testSubject.isEnabled()); } + @Test + void testDeleteVersionFail() throws Exception { + doThrow(new RuntimeException()).when(minioClient).removeObject(any(RemoveObjectArgs.class)); + assertThrows(ArtifactStorageException.class, () -> { + testSubject.delete(new MinIoArtifactInfo(VSP_ID, VERSION_ID)); + }); + } + + @Test + void testDeleteVspFail() throws Exception { + doThrow(new RuntimeException()).when(minioClient).removeBucket(any(RemoveBucketArgs.class)); + assertThrows(ArtifactStorageException.class, () -> { + testSubject.delete(VSP_ID); + }); + } + + @Test + void testGetOK() throws Exception { + when(minioClient.getObject(any(GetObjectArgs.class))).thenReturn( + new GetObjectResponse(Headers.of(), "", "", "", + new FileInputStream(Path.of("src/test/resources/s3StoreArtifactStorageManager/dummy.csar").toFile()))); + final InputStream inputStream = testSubject.get(new MinIoArtifactInfo(VSP_ID, VERSION_ID)); + assertNotNull(inputStream); + } + + @Test + void testGetFail() throws Exception { + doThrow(new RuntimeException()).when(minioClient).getObject(any(GetObjectArgs.class)); + assertThrows(ArtifactStorageException.class, () -> { + final InputStream inputStream = testSubject.get(new MinIoArtifactInfo(VSP_ID, VERSION_ID)); + }); + } + private Attachment mockAttachment() throws IOException { final Attachment attachment = Mockito.mock(Attachment.class); final DataHandler dataHandler = Mockito.mock(DataHandler.class); diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java index 824e5356e6..86e676020c 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/main/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImpl.java @@ -15,6 +15,30 @@ */ package org.openecomp.sdcrests.vsp.rest.services; +import static javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION; +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; +import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME; +import static org.openecomp.sdc.vendorsoftwareproduct.dao.type.OnboardingMethod.NetworkPackage; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_ID; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_NAME; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.SUBMIT_DESCRIPTION; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_ID; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_NAME; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import javax.inject.Named; +import javax.ws.rs.core.Response; import org.apache.commons.collections4.MapUtils; import org.openecomp.core.dao.UniqueValueDaoFactory; import org.openecomp.core.util.UniqueValueUtil; @@ -22,6 +46,8 @@ import org.openecomp.sdc.activitylog.ActivityLogManager; import org.openecomp.sdc.activitylog.ActivityLogManagerFactory; import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; import org.openecomp.sdc.activitylog.dao.type.ActivityType; +import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager; +import org.openecomp.sdc.be.csar.storage.StorageFactory; import org.openecomp.sdc.common.errors.CoreException; import org.openecomp.sdc.common.errors.ErrorCode; import org.openecomp.sdc.common.errors.Messages; @@ -85,31 +111,6 @@ import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; -import javax.inject.Named; -import javax.ws.rs.core.Response; -import java.io.File; -import java.io.IOException; -import java.util.Collection; -import java.util.Comparator; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.function.Predicate; -import java.util.stream.Collectors; - -import static javax.ws.rs.core.HttpHeaders.CONTENT_DISPOSITION; -import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; -import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.VENDOR_SOFTWARE_PRODUCT_NAME; -import static org.openecomp.sdc.vendorsoftwareproduct.dao.type.OnboardingMethod.NetworkPackage; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_ID; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_NAME; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.SUBMIT_DESCRIPTION; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_ID; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_NAME; - @Named @Service("vendorSoftwareProducts") @Scope(value = "prototype") @@ -130,15 +131,17 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { private final ActivityLogManager activityLogManager; private final NotificationPropagationManager notifier; private final UniqueValueUtil uniqueValueUtil; + private final ArtifactStorageManager artifactStorageManager; public VendorSoftwareProductsImpl() { - this.itemManager = AsdcItemManagerFactory.getInstance().createInterface();; + this.itemManager = AsdcItemManagerFactory.getInstance().createInterface(); this.permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); this.versioningManager = VersioningManagerFactory.getInstance().createInterface(); this.vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface(); this.activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); this.notifier = NotificationPropagationManagerFactory.getInstance().createInterface(); this.uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface()); + this.artifactStorageManager = new StorageFactory().createArtifactStorageManager(); } public VendorSoftwareProductsImpl(AsdcItemManager itemManager, @@ -147,7 +150,8 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { VendorSoftwareProductManager vendorSoftwareProductManager, ActivityLogManager activityLogManager, NotificationPropagationManager notifier, - UniqueValueUtil uniqueValueUtil) { + UniqueValueUtil uniqueValueUtil, + ArtifactStorageManager artifactStorageManager) { this.itemManager = itemManager; this.permissionsManager = permissionsManager; this.versioningManager = versioningManager; @@ -155,6 +159,7 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { this.activityLogManager = activityLogManager; this.notifier = notifier; this.uniqueValueUtil = uniqueValueUtil; + this.artifactStorageManager = artifactStorageManager; } @Override @@ -277,13 +282,32 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { } Integer certifiedVersionsCounter = vsp.getVersionStatusCounters().get(VersionStatus.Certified); if (Objects.isNull(certifiedVersionsCounter) || certifiedVersionsCounter == 0) { + if (artifactStorageManager.isEnabled() && !deleteVspFromStorage(vspId)) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new Exception(Messages.DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId))).build(); + } return deleteVsp(vspId, user, vsp); + } else { + final var isVspArchived = getVspList(null, ItemStatus.ARCHIVED.name(), user).stream().anyMatch(item -> item.getId().equals(vspId)); + if (isVspArchived) { + if (artifactStorageManager.isEnabled() && !deleteVspFromStorage(vspId)) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(new Exception(Messages.DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId))).build(); + } + return deleteVsp(vspId, user, vsp); + } + return Response.status(Response.Status.FORBIDDEN).entity(new Exception(Messages.DELETE_VSP_ERROR.getErrorMessage())).build(); } - final var isVspArchived = getVspList(null, ItemStatus.ARCHIVED.name(), user).stream().anyMatch(item -> item.getId().equals(vspId)); - if (isVspArchived) { - return deleteVsp(vspId, user, vsp); + } + + private boolean deleteVspFromStorage(final String vspId) { + try { + artifactStorageManager.delete(vspId); + } catch (final Exception e) { + LOGGER.error("Failed to delete VSP '{}'", vspId, e); + return false; } - return Response.status(Response.Status.FORBIDDEN).entity(new Exception(Messages.DELETE_VSP_ERROR.getErrorMessage())).build(); + return true; } private Response deleteVsp(String vspId, String user, Item vsp) { @@ -339,7 +363,7 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { Predicate<Item> validationVspFilter = item -> ItemType.vsp.name().equals(item.getType()) && VALIDATION_VSP_NAME .equals(item.getName()); String validationVspId = itemManager.list(validationVspFilter).stream().findFirst().orElseThrow(() -> new IllegalStateException( - "Vsp with name " + VALIDATION_VSP_NAME + " does not exist even though the name exists according to " + "unique value util")) + "Vsp with name " + VALIDATION_VSP_NAME + " does not exist even though the name exists according to " + "unique value util")) .getId(); Version validationVspVersion = versioningManager.list(validationVspId).iterator().next(); cachedValidationVsp = new ItemCreationDto(); diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImplTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImplTest.java index 1a1d0b5650..6340d0819a 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImplTest.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest/vendor-software-products-rest-services/src/test/java/org/openecomp/sdcrests/vsp/rest/services/VendorSoftwareProductsImplTest.java @@ -21,13 +21,27 @@ package org.openecomp.sdcrests.vsp.rest.services; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.openMocks; +import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_ERROR; +import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_FROM_STORAGE_ERROR; + +import java.util.List; +import java.util.UUID; +import javax.ws.rs.core.Response; import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.openecomp.core.util.UniqueValueUtil; import org.openecomp.sdc.activitylog.ActivityLogManager; +import org.openecomp.sdc.be.csar.storage.ArtifactStorageManager; import org.openecomp.sdc.itempermissions.PermissionsManager; import org.openecomp.sdc.notification.services.NotificationPropagationManager; import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager; @@ -37,17 +51,6 @@ import org.openecomp.sdc.versioning.dao.types.VersionStatus; import org.openecomp.sdc.versioning.types.Item; import org.openecomp.sdc.versioning.types.ItemStatus; -import javax.ws.rs.core.Response; -import java.util.List; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.openMocks; -import static org.openecomp.sdc.common.errors.Messages.DELETE_VSP_ERROR; - class VendorSoftwareProductsImplTest { private final String vspId = UUID.randomUUID().toString(); @@ -67,64 +70,94 @@ class VendorSoftwareProductsImplTest { private NotificationPropagationManager notificationPropagationManager; @Mock private UniqueValueUtil uniqueValueUtil; + @Mock + private ArtifactStorageManager artifactStorageManager; + @InjectMocks private VendorSoftwareProductsImpl vendorSoftwareProducts; + private Item item; + @BeforeEach public void setUp() { openMocks(this); - vendorSoftwareProducts = new VendorSoftwareProductsImpl( - itemManager, - permissionsManager, - versioningManager, - vendorSoftwareProductManager, - activityLogManager, - notificationPropagationManager, - uniqueValueUtil); - - Item item = new Item(); + item = new Item(); item.setType("vsp"); item.setId(vspId); - when(itemManager.get( - ArgumentMatchers.eq(vspId))).thenReturn(item); + when(itemManager.get(vspId)).thenReturn(item); } @Test void deleteVspOk() { + Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); + assertEquals(HttpStatus.SC_OK, rsp.getStatus()); + assertNull(rsp.getEntity()); + } + @Test + void deleteVspWithS3Ok() { + when(artifactStorageManager.isEnabled()).thenReturn(true); Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); assertEquals(HttpStatus.SC_OK, rsp.getStatus()); assertNull(rsp.getEntity()); } @Test + void deleteVspWithS3Fail() { + when(artifactStorageManager.isEnabled()).thenReturn(true); + doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString()); + Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); + assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus()); + assertEquals(rsp.getEntity().getClass(), Exception.class); + assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId)); + } + + @Test void deleteCertifiedVsp() { - Item item = new Item(); - item.setType("vsp"); - item.setId(vspId); item.addVersionStatus(VersionStatus.Certified); - when(itemManager.get( - ArgumentMatchers.eq(vspId))).thenReturn(item); + when(itemManager.get(vspId)).thenReturn(item); Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); assertEquals(HttpStatus.SC_FORBIDDEN, rsp.getStatus()); assertEquals(rsp.getEntity().getClass(), Exception.class); - assertEquals(((Exception)rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_ERROR.getErrorMessage()); + assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_ERROR.getErrorMessage()); } @Test void deleteCertifiedArchivedVsp() { - Item item = new Item(); - item.setType("vsp"); - item.setId(vspId); item.setStatus(ItemStatus.ARCHIVED); item.addVersionStatus(VersionStatus.Certified); - when(itemManager.get( - ArgumentMatchers.eq(vspId))).thenReturn(item); + when(itemManager.get(vspId)).thenReturn(item); when(itemManager.list(any())).thenReturn(List.of(item)); Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); assertEquals(HttpStatus.SC_OK, rsp.getStatus()); assertNull(rsp.getEntity()); } -}
\ No newline at end of file + + @Test + void deleteCertifiedArchivedVspWithS3OK() { + when(artifactStorageManager.isEnabled()).thenReturn(true); + item.setStatus(ItemStatus.ARCHIVED); + item.addVersionStatus(VersionStatus.Certified); + when(itemManager.get(vspId)).thenReturn(item); + when(itemManager.list(any())).thenReturn(List.of(item)); + Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); + assertEquals(HttpStatus.SC_OK, rsp.getStatus()); + assertNull(rsp.getEntity()); + } + + @Test + void deleteCertifiedArchivedVspWithS3Fail() { + when(artifactStorageManager.isEnabled()).thenReturn(true); + doThrow(new RuntimeException()).when(artifactStorageManager).delete(anyString()); + item.setStatus(ItemStatus.ARCHIVED); + item.addVersionStatus(VersionStatus.Certified); + when(itemManager.get(vspId)).thenReturn(item); + when(itemManager.list(any())).thenReturn(List.of(item)); + Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); + assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, rsp.getStatus()); + assertEquals(rsp.getEntity().getClass(), Exception.class); + assertEquals(((Exception) rsp.getEntity()).getLocalizedMessage(), DELETE_VSP_FROM_STORAGE_ERROR.formatMessage(vspId)); + } +} diff --git a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java index 93100421d2..32cc143b24 100644 --- a/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java +++ b/openecomp-be/lib/openecomp-common-lib/src/main/java/org/openecomp/sdc/common/errors/Messages.java @@ -97,6 +97,7 @@ public enum Messages { FAILED_TO_SYNC("Non existing version cannot be synced."), FAILED_TO_PUBLISH_OUT_OF_SYNC("Publish is not allowed since the version status is Out of sync"), DELETE_VSP_ERROR("VSP has been certified and cannot be deleted."), + DELETE_VSP_FROM_STORAGE_ERROR("Failed to delete VSP '%s' from Storage"), DELETE_VLM_ERROR("VLM has been certified and cannot be deleted."), ZIP_SHOULD_NOT_CONTAIN_FOLDERS("Zip file should not contain folders"), VES_ZIP_SHOULD_CONTAIN_YML_ONLY("Wrong VES EVENT Artifact was uploaded - all files contained in Artifact must be YAML files" |