diff options
author | franciscovila <javier.paradela.vila@est.tech> | 2022-02-28 12:58:36 +0000 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-03-24 09:46:57 +0000 |
commit | 13bcc9df909fe12f57661a09f31787e0efc440ba (patch) | |
tree | af90f9f1c07fe658fa31aa1a82f29dffd7972a91 /openecomp-be/api | |
parent | b286cf3d59db0ae319d62927fe97815fb6126ad8 (diff) |
Delete VSP - Support deletion of archived VSPs in onboarding BE
Allow deletion of archived VSPs
Issue-ID: SDC-3890
Signed-off-by: franciscovila <javier.paradela.vila@est.tech>
Change-Id: If1a5177a30fb0220392adcdf6bd6c4e71c37ce59
Diffstat (limited to 'openecomp-be/api')
2 files changed, 203 insertions, 39 deletions
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 76e3f674dd..824e5356e6 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,30 +15,6 @@ */ 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; @@ -109,6 +85,31 @@ 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") @@ -122,13 +123,39 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { private static final Logger LOGGER = LoggerFactory.getLogger(VendorSoftwareProductsImpl.class); private static final Object VALIDATION_VSP_CACHE_LOCK = new Object(); private static ItemCreationDto cachedValidationVsp; - private final AsdcItemManager itemManager = AsdcItemManagerFactory.getInstance().createInterface(); - private final PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); - private final VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); - private final VendorSoftwareProductManager vendorSoftwareProductManager = VspManagerFactory.getInstance().createInterface(); - private final ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); - private final NotificationPropagationManager notifier = NotificationPropagationManagerFactory.getInstance().createInterface(); - private final UniqueValueUtil uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface()); + private final AsdcItemManager itemManager; + private final PermissionsManager permissionsManager; + private final VersioningManager versioningManager; + private final VendorSoftwareProductManager vendorSoftwareProductManager; + private final ActivityLogManager activityLogManager; + private final NotificationPropagationManager notifier; + private final UniqueValueUtil uniqueValueUtil; + + public VendorSoftwareProductsImpl() { + 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()); + } + + public VendorSoftwareProductsImpl(AsdcItemManager itemManager, + PermissionsManager permissionsManager, + VersioningManager versioningManager, + VendorSoftwareProductManager vendorSoftwareProductManager, + ActivityLogManager activityLogManager, + NotificationPropagationManager notifier, + UniqueValueUtil uniqueValueUtil) { + this.itemManager = itemManager; + this.permissionsManager = permissionsManager; + this.versioningManager = versioningManager; + this.vendorSoftwareProductManager = vendorSoftwareProductManager; + this.activityLogManager = activityLogManager; + this.notifier = notifier; + this.uniqueValueUtil = uniqueValueUtil; + } @Override public Response createVsp(VspRequestDto vspRequestDto, String user) { @@ -250,15 +277,22 @@ public class VendorSoftwareProductsImpl implements VendorSoftwareProducts { } Integer certifiedVersionsCounter = vsp.getVersionStatusCounters().get(VersionStatus.Certified); if (Objects.isNull(certifiedVersionsCounter) || certifiedVersionsCounter == 0) { - versioningManager.list(vspId).forEach(version -> vendorSoftwareProductManager.deleteVsp(vspId, version)); - itemManager.delete(vsp); - permissionsManager.deleteItemPermissions(vspId); - uniqueValueUtil.deleteUniqueValue(VENDOR_SOFTWARE_PRODUCT_NAME, vsp.getName()); - notifyUsers(vspId, vsp.getName(), null, null, user, NotificationEventTypes.DELETE); - return Response.ok().build(); - } else { - return Response.status(Response.Status.FORBIDDEN).entity(new Exception(Messages.DELETE_VSP_ERROR.getErrorMessage())).build(); + return deleteVsp(vspId, user, vsp); + } + final var isVspArchived = getVspList(null, ItemStatus.ARCHIVED.name(), user).stream().anyMatch(item -> item.getId().equals(vspId)); + if (isVspArchived) { + return deleteVsp(vspId, user, vsp); } + return Response.status(Response.Status.FORBIDDEN).entity(new Exception(Messages.DELETE_VSP_ERROR.getErrorMessage())).build(); + } + + private Response deleteVsp(String vspId, String user, Item vsp) { + versioningManager.list(vspId).forEach(version -> vendorSoftwareProductManager.deleteVsp(vspId, version)); + itemManager.delete(vsp); + permissionsManager.deleteItemPermissions(vspId); + uniqueValueUtil.deleteUniqueValue(VENDOR_SOFTWARE_PRODUCT_NAME, vsp.getName()); + notifyUsers(vspId, vsp.getName(), null, null, user, NotificationEventTypes.DELETE); + return Response.ok().build(); } @Override 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 new file mode 100644 index 0000000000..1a1d0b5650 --- /dev/null +++ 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 @@ -0,0 +1,130 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.sdcrests.vsp.rest.services; + +import org.apache.http.HttpStatus; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.openecomp.core.util.UniqueValueUtil; +import org.openecomp.sdc.activitylog.ActivityLogManager; +import org.openecomp.sdc.itempermissions.PermissionsManager; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager; +import org.openecomp.sdc.versioning.AsdcItemManager; +import org.openecomp.sdc.versioning.VersioningManager; +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(); + private final String user = "cs0008"; + + @Mock + private AsdcItemManager itemManager; + @Mock + private PermissionsManager permissionsManager; + @Mock + private VersioningManager versioningManager; + @Mock + private VendorSoftwareProductManager vendorSoftwareProductManager; + @Mock + private ActivityLogManager activityLogManager; + @Mock + private NotificationPropagationManager notificationPropagationManager; + @Mock + private UniqueValueUtil uniqueValueUtil; + + private VendorSoftwareProductsImpl vendorSoftwareProducts; + + @BeforeEach + public void setUp() { + openMocks(this); + + vendorSoftwareProducts = new VendorSoftwareProductsImpl( + itemManager, + permissionsManager, + versioningManager, + vendorSoftwareProductManager, + activityLogManager, + notificationPropagationManager, + uniqueValueUtil); + + Item item = new Item(); + item.setType("vsp"); + item.setId(vspId); + when(itemManager.get( + ArgumentMatchers.eq(vspId))).thenReturn(item); + } + + @Test + void deleteVspOk() { + + Response rsp = vendorSoftwareProducts.deleteVsp(vspId, user); + assertEquals(HttpStatus.SC_OK, rsp.getStatus()); + assertNull(rsp.getEntity()); + } + + @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); + + 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()); + } + + @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.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 |