From 4307dbb5f0dc5da60e55f3473259cfd059d14770 Mon Sep 17 00:00:00 2001 From: vasraz Date: Thu, 24 Mar 2022 16:38:00 +0000 Subject: Implement VSP deletion from Storage Change-Id: I06d86696570b2751f152dffc06fd580ef8c0c705 Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3924 --- .../rest/services/VendorSoftwareProductsImpl.java | 88 ++++++++++------- .../services/VendorSoftwareProductsImplTest.java | 105 ++++++++++++++------- 2 files changed, 125 insertions(+), 68 deletions(-) (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/vendor-software-products-rest') 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 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)); + } +} -- cgit 1.2.3-korg