diff options
author | andre.schmid <andre.schmid@est.tech> | 2022-04-13 11:21:15 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-04-21 09:48:05 +0000 |
commit | 30ff812c65389f5ca4c91d9774d425e81ef62049 (patch) | |
tree | b7d293c23621df6c1084b9ff7dbde9c7d7d888ac /openecomp-be | |
parent | 225dabdf0812d1a3d9eb3a96740507744bb2b807 (diff) |
Check if VLM is used before deleting
Checks if the VLM is in use by any VSP before allowing to delete it.
Add unit tests for the delete method, considering the restriction.
Applies minor refactors with related code.
Change-Id: I4ff6ddf3959a4ca92ab68b29c8913fc6f0ebdb3c
Issue-ID: SDC-3966
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'openecomp-be')
9 files changed, 434 insertions, 60 deletions
diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json index 5b2e808781..3d2a48107f 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/openecomp-sdc-common-rest/src/main/resources/errorCodesToResponseStatusMapping.json @@ -19,6 +19,8 @@ "PACKAGE_NOT_FOUND": "NOT_FOUND", "PACKAGE_INVALID": "BAD_REQUEST", "VENDOR_LICENSE_MODEL_NOT_FOUND": "NOT_FOUND", + "VLM_IS_IN_USE_DELETE_ERROR": "FORBIDDEN", + "VLM_IS_CERTIFIED_DELETE_ERROR": "FORBIDDEN", "VENDOR_LICENSE_ENTITY_NOT_FOUND": "NOT_FOUND", "VERSIONABLE_SUB_ENTITY_NOT_FOUND": "NOT_FOUND", "FEATURE_GROUP_NOT_EXIST_FOR_VSP": "NOT_FOUND", diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/exception/VendorLicenseModelExceptionSupplier.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/exception/VendorLicenseModelExceptionSupplier.java new file mode 100644 index 0000000000..330a2dd4d1 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/exception/VendorLicenseModelExceptionSupplier.java @@ -0,0 +1,87 @@ +/* + * - + * ============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.vendorlicense.rest.exception; + +import static org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes.VLM_IS_CERTIFIED_DELETE_ERROR; +import static org.openecomp.sdc.vendorlicense.errors.VendorLicenseErrorCodes.VLM_IS_IN_USE_DELETE_ERROR; + +import java.util.List; +import java.util.function.Supplier; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCode; +import org.openecomp.sdc.common.errors.ErrorCode.ErrorCodeBuilder; +import org.openecomp.sdc.vendorlicense.errors.VendorLicenseModelNotFoundErrorBuilder; + +/** + * Supplies exceptions happened for a Vendor License Model operation . + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class VendorLicenseModelExceptionSupplier { + + /** + * Provides a could not find Vendor License Model exception. + * + * @param vlmId the Vendor License Model id + * @return a Supplier for the exception + */ + public static Supplier<CoreException> couldNotFindVlm(final String vlmId) { + final ErrorCode errorCode = new VendorLicenseModelNotFoundErrorBuilder(vlmId).build(); + return () -> new CoreException((errorCode)); + } + + /** + * Provides a cannot delete used Vendor License Model exception. + * + * @param vmlId the Vendor License Model id + * @param vspNameList the list of VSP names that uses the VLM + * @return a Supplier for the exception + */ + public static Supplier<CoreException> cantDeleteUsedVlm(final String vmlId, final List<String> vspNameList) { + final String errorMsg = String.format( + "Vendor License Model '%s' is in use by %s Vendor Software Product(s) and cannot be deleted.", + vmlId, String.join(", ", vspNameList) + ); + final ErrorCode errorCode = new ErrorCodeBuilder() + .withId(VLM_IS_IN_USE_DELETE_ERROR) + .withMessage(errorMsg) + .build(); + return () -> new CoreException((errorCode)); + } + + /** + * Provides a cannot delete certified Vendor License Model exception. + * + * @param vmlId the Vendor License Model id + * @return a Supplier for the exception + */ + public static Supplier<CoreException> cantDeleteCertifiedVlm(final String vmlId) { + final String errorMsg = String.format("Vendor License Model '%s' has been certified and cannot be deleted.", vmlId); + final ErrorCode errorCode = new ErrorCodeBuilder() + .withId(VLM_IS_CERTIFIED_DELETE_ERROR) + .withMessage(errorMsg) + .build(); + return () -> new CoreException((errorCode)); + } + +} diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java index 0255b14753..f4e638c07b 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java @@ -23,10 +23,12 @@ import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERS import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_NAME; import static org.openecomp.sdcrests.vendorlicense.types.VendorLicenseModelActionRequestDto.VendorLicenseModelAction.Submit; +import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.function.Predicate; +import java.util.stream.Collectors; import javax.inject.Named; import javax.ws.rs.core.Response; import org.openecomp.core.dao.UniqueValueDaoFactory; @@ -35,8 +37,6 @@ 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.common.errors.CoreException; -import org.openecomp.sdc.common.errors.ErrorCode; import org.openecomp.sdc.common.errors.Messages; import org.openecomp.sdc.datatypes.model.ItemType; import org.openecomp.sdc.healing.factory.HealingManagerFactory; @@ -52,6 +52,9 @@ import org.openecomp.sdc.vendorlicense.VendorLicenseConstants; import org.openecomp.sdc.vendorlicense.VendorLicenseManager; import org.openecomp.sdc.vendorlicense.VendorLicenseManagerFactory; import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDaoFactory; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; import org.openecomp.sdc.versioning.AsdcItemManager; import org.openecomp.sdc.versioning.AsdcItemManagerFactory; import org.openecomp.sdc.versioning.VersioningManager; @@ -67,6 +70,7 @@ import org.openecomp.sdcrests.item.types.ItemCreationDto; import org.openecomp.sdcrests.item.types.ItemDto; import org.openecomp.sdcrests.item.types.VersionDto; import org.openecomp.sdcrests.vendorlicense.rest.VendorLicenseModels; +import org.openecomp.sdcrests.vendorlicense.rest.exception.VendorLicenseModelExceptionSupplier; import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapVendorLicenseModelEntityToDto; import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapVendorLicenseModelRequestDtoToVendorLicenseModelEntity; import org.openecomp.sdcrests.vendorlicense.types.VendorLicenseModelActionRequestDto; @@ -86,13 +90,55 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { private static final String SUBMIT_ITEM_ACTION = "Submit_Item"; private static final String SUBMIT_HEALED_VERSION_ERROR = "VLM Id %s: Error while submitting version %s created based on Certified version %s for healing purpose."; private static final Logger LOGGER = LoggerFactory.getLogger(VendorLicenseModelsImpl.class); - private PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); - private NotificationPropagationManager notifier = NotificationPropagationManagerFactory.getInstance().createInterface(); - private AsdcItemManager asdcItemManager = AsdcItemManagerFactory.getInstance().createInterface(); - private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); - private VendorLicenseManager vendorLicenseManager = VendorLicenseManagerFactory.getInstance().createInterface(); - private ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); - private UniqueValueUtil uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface()); + + private final PermissionsManager permissionsManager; + private final NotificationPropagationManager notifier; + private final AsdcItemManager asdcItemManager; + private final VersioningManager versioningManager; + private final VendorLicenseManager vendorLicenseManager; + private final ActivityLogManager activityLogManager; + private final UniqueValueUtil uniqueValueUtil; + private final VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao; + + public VendorLicenseModelsImpl() { + this.permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); + this.notifier = NotificationPropagationManagerFactory.getInstance().createInterface(); + this.asdcItemManager = AsdcItemManagerFactory.getInstance().createInterface(); + this.versioningManager = VersioningManagerFactory.getInstance().createInterface(); + this.vendorLicenseManager = VendorLicenseManagerFactory.getInstance().createInterface(); + this.activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); + this.uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface()); + this.vendorSoftwareProductInfoDao = VendorSoftwareProductInfoDaoFactory.getInstance().createInterface(); + } + + /** + * Test purpose constructor. + * @param permissionsManager the {@link PermissionsManager} instance + * @param notifier the {@link NotificationPropagationManager} instance + * @param asdcItemManager the {@link AsdcItemManager} instance + * @param versioningManager the {@link VersioningManager} instance + * @param vendorLicenseManager the {@link VendorLicenseManager} instance + * @param activityLogManager the {@link ActivityLogManager} instance + * @param uniqueValueUtil the {@link UniqueValueUtil} instance + * @param vendorSoftwareProductInfoDao the {@link VendorSoftwareProductInfoDao} instance + */ + VendorLicenseModelsImpl(final PermissionsManager permissionsManager, + final NotificationPropagationManager notifier, + final AsdcItemManager asdcItemManager, + final VersioningManager versioningManager, + final VendorLicenseManager vendorLicenseManager, + final ActivityLogManager activityLogManager, + final UniqueValueUtil uniqueValueUtil, + final VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao) { + this.permissionsManager = permissionsManager; + this.notifier = notifier; + this.asdcItemManager = asdcItemManager; + this.versioningManager = versioningManager; + this.vendorLicenseManager = vendorLicenseManager; + this.activityLogManager = activityLogManager; + this.uniqueValueUtil = uniqueValueUtil; + this.vendorSoftwareProductInfoDao = vendorSoftwareProductInfoDao; + } @Override public Response listLicenseModels(String versionStatus, String itemStatus, String user) { @@ -158,21 +204,28 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { } @Override - public Response deleteLicenseModel(String vlmId, String user) { - Item vlm = asdcItemManager.get(vlmId); - if (!vlm.getType().equals(ItemType.vlm.name())) { - throw new CoreException((new ErrorCode.ErrorCodeBuilder().withMessage(String.format("Vlm with id %s does not exist.", vlmId)).build())); + public Response deleteLicenseModel(final String vlmId, final String user) { + final Item vlm = asdcItemManager.get(vlmId); + if (vlm == null || !ItemType.vlm.getName().equals(vlm.getType())) { + throw VendorLicenseModelExceptionSupplier.couldNotFindVlm(vlmId).get(); } - Integer certifiedVersionsCounter = vlm.getVersionStatusCounters().get(VersionStatus.Certified); - if (Objects.isNull(certifiedVersionsCounter) || certifiedVersionsCounter == 0) { - asdcItemManager.delete(vlm); - permissionsManager.deleteItemPermissions(vlmId); - uniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlm.getName()); - notifyUsers(vlmId, vlm.getName(), null, null, user, NotificationEventTypes.DELETE); - return Response.ok().build(); - } else { - return Response.status(Response.Status.FORBIDDEN).entity(new Exception(Messages.DELETE_VLM_ERROR.getErrorMessage())).build(); + + final List<String> vlmUsedByAnyVsp = findVspsUsingVlm(vlm.getId()); + if (!vlmUsedByAnyVsp.isEmpty()) { + throw VendorLicenseModelExceptionSupplier.cantDeleteUsedVlm(vlmId, vlmUsedByAnyVsp).get(); } + + final Integer certifiedVersionsCounter = vlm.getVersionStatusCounters().get(VersionStatus.Certified); + final boolean wasVlmAtLeastOnceCertified = certifiedVersionsCounter != null && certifiedVersionsCounter > 0; + if (wasVlmAtLeastOnceCertified) { + throw VendorLicenseModelExceptionSupplier.cantDeleteCertifiedVlm(vlmId).get(); + } + + asdcItemManager.delete(vlm); + permissionsManager.deleteItemPermissions(vlmId); + uniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlm.getName()); + notifyUsers(vlmId, vlm.getName(), null, null, user, NotificationEventTypes.DELETE); + return Response.ok().build(); } @Override @@ -189,6 +242,14 @@ public class VendorLicenseModelsImpl implements VendorLicenseModels { return Response.ok().build(); } + private List<String> findVspsUsingVlm(final String vlmId) { + final Collection<VspDetails> vspDetailsList = vendorSoftwareProductInfoDao.list(null); + return vspDetailsList.stream() + .filter(vspDetails -> vlmId.equals(vspDetails.getVendorId())) + .map(VspDetails::getName) + .collect(Collectors.toList()); + } + private void submit(String vlmId, Version version, String message, String user) { vendorLicenseManager.validate(vlmId, version); versioningManager.submit(vlmId, version, message); diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/test/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImplTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/test/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImplTest.java new file mode 100644 index 0000000000..48cb42d160 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/test/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImplTest.java @@ -0,0 +1,199 @@ +/* + * - + * ============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.vendorlicense.rest.services; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.core.util.UniqueValueUtil; +import org.openecomp.sdc.activitylog.ActivityLogManager; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.datatypes.model.ItemType; +import org.openecomp.sdc.itempermissions.PermissionsManager; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.vendorlicense.VendorLicenseConstants; +import org.openecomp.sdc.vendorlicense.VendorLicenseManager; +import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao; +import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails; +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.sdcrests.vendorlicense.rest.exception.VendorLicenseModelExceptionSupplier; + +class VendorLicenseModelsImplTest { + + @Mock + private PermissionsManager permissionsManager; + @Mock + private NotificationPropagationManager notifier; + @Mock + private AsdcItemManager asdcItemManager; + @Mock + private VersioningManager versioningManager; + @Mock + private VendorLicenseManager vendorLicenseManager; + @Mock + private ActivityLogManager activityLogManager; + @Mock + private UniqueValueUtil uniqueValueUtil; + @Mock + private VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao; + + @InjectMocks + private VendorLicenseModelsImpl vendorLicenseModels; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void deleteLicenseModelSuccessTest() { + //given + final String vlmId = "vlmId"; + final String vlmName = "vlmName"; + final String userId = "userId"; + + final Item vlmItem = new Item(); + vlmItem.setId(vlmId); + vlmItem.setType(ItemType.vlm.getName()); + vlmItem.setName(vlmName); + when(asdcItemManager.get(vlmId)).thenReturn(vlmItem); + + final VspDetails vspDetailsThatDontUseVlm1 = new VspDetails(); + vspDetailsThatDontUseVlm1.setVendorId("otherVendorId"); + final VspDetails vspDetailsThatDontUseVlm2 = new VspDetails(); + vspDetailsThatDontUseVlm2.setVendorId("otherVendorId"); + final List<VspDetails> vspDetailsList = List.of(vspDetailsThatDontUseVlm1, vspDetailsThatDontUseVlm2); + when(vendorSoftwareProductInfoDao.list(null)).thenReturn(vspDetailsList); + + //when + final Response response = vendorLicenseModels.deleteLicenseModel(vlmId, userId); + //then + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + verify(asdcItemManager).delete(vlmItem); + verify(permissionsManager).deleteItemPermissions(vlmItem.getId()); + verify(uniqueValueUtil).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName()); + verify(notifier).notifySubscribers(any(Event.class), eq(userId)); + } + + @Test + void deleteLicenseModel_cantDeleteVlmInUseTest() { + //given + final String vlmId = "vlmId"; + final String vlmName = "vlmName"; + final String userId = "userId"; + + final Item vlmItem = new Item(); + vlmItem.setId(vlmId); + vlmItem.setType(ItemType.vlm.getName()); + vlmItem.setName(vlmName); + when(asdcItemManager.get(vlmId)).thenReturn(vlmItem); + + final VspDetails vspDetailsThatUsesVlm = new VspDetails(); + vspDetailsThatUsesVlm.setName("VspThatUsesVlm"); + vspDetailsThatUsesVlm.setVendorId(vlmId); + final VspDetails vspDetailsThatDontUseVlm = new VspDetails(); + vspDetailsThatDontUseVlm.setName("VspThatDontUseVlm"); + vspDetailsThatDontUseVlm.setVendorId("otherVendorId"); + final List<VspDetails> vspDetailsList = List.of(vspDetailsThatUsesVlm, vspDetailsThatDontUseVlm); + when(vendorSoftwareProductInfoDao.list(null)).thenReturn(vspDetailsList); + + //when + final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, userId)); + //then + final CoreException expectedException = + VendorLicenseModelExceptionSupplier.cantDeleteUsedVlm(vlmId, List.of(vspDetailsThatUsesVlm.getName())).get(); + assertEquals(expectedException.code().id(), actualException.code().id()); + assertEquals(expectedException.code().message(), actualException.code().message()); + assertEquals(expectedException.code().category(), actualException.code().category()); + verify(asdcItemManager, never()).delete(vlmItem); + verify(permissionsManager, never()).deleteItemPermissions(vlmItem.getId()); + verify(uniqueValueUtil, never()).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName()); + verify(notifier, never()).notifySubscribers(any(Event.class), eq(userId)); + } + + @Test + void deleteLicenseModel_cantDeleteCertifiedTest() { + //given + final String vlmId = "vlmId"; + final String vlmName = "vlmName"; + final String userId = "userId"; + + final Item vlmItem = new Item(); + vlmItem.setId(vlmId); + vlmItem.setType(ItemType.vlm.getName()); + vlmItem.setName(vlmName); + vlmItem.setVersionStatusCounters(Map.of(VersionStatus.Certified, 1)); + when(asdcItemManager.get(vlmId)).thenReturn(vlmItem); + when(vendorSoftwareProductInfoDao.list(null)).thenReturn(Collections.emptyList()); + + //when + final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, userId)); + //then + final CoreException expectedException = VendorLicenseModelExceptionSupplier.cantDeleteCertifiedVlm(vlmId).get(); + assertEquals(expectedException.code().id(), actualException.code().id()); + assertEquals(expectedException.code().message(), actualException.code().message()); + assertEquals(expectedException.code().category(), actualException.code().category()); + verify(asdcItemManager, never()).delete(vlmItem); + verify(permissionsManager, never()).deleteItemPermissions(vlmItem.getId()); + verify(uniqueValueUtil, never()).deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlmItem.getName()); + verify(notifier, never()).notifySubscribers(any(Event.class), eq(userId)); + } + + @Test + void deleteLicenseModel_incorrectItemTypeTest() { + //given + final String vlmId = "vlmId"; + + final Item vlmItem = new Item(); + vlmItem.setId(vlmId); + vlmItem.setType("incorrectType"); + when(asdcItemManager.get(vlmId)).thenReturn(vlmItem); + + //when/then + final CoreException actualException = assertThrows(CoreException.class, () -> vendorLicenseModels.deleteLicenseModel(vlmId, "userId")); + + final CoreException expectedException = VendorLicenseModelExceptionSupplier.couldNotFindVlm(vlmId).get(); + assertEquals(expectedException.code().id(), actualException.code().id()); + assertEquals(expectedException.code().message(), actualException.code().message()); + } + +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/errors/VendorLicenseErrorCodes.java b/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/errors/VendorLicenseErrorCodes.java index 62ea84b48b..eecb597ab2 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/errors/VendorLicenseErrorCodes.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-api/src/main/java/org/openecomp/sdc/vendorlicense/errors/VendorLicenseErrorCodes.java @@ -19,6 +19,10 @@ */ package org.openecomp.sdc.vendorlicense.errors; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) public class VendorLicenseErrorCodes { public static final String VENDOR_LICENSE_MODEL_NOT_FOUND = "VENDOR_LICENSE_MODEL_NOT_FOUND"; @@ -30,4 +34,6 @@ public class VendorLicenseErrorCodes { public static final String LIMIT_INVALID_TIME = "LIMIT_INVALID_TIME"; public static final String DUPLICATE_LIMIT_NAME_NOT_ALLOWED = "DUPLICATE_LIMIT_NAME_NOT_ALLOWED"; public static final String DATE_RANGE_INVALID = "DATE_RANGE_INVALID"; + public static final String VLM_IS_IN_USE_DELETE_ERROR = "VLM_IS_IN_USE_DELETE_ERROR"; + public static final String VLM_IS_CERTIFIED_DELETE_ERROR = "VLM_IS_CERTIFIED_DELETE_ERROR"; } diff --git a/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-core/src/main/java/org/openecomp/sdc/vendorlicense/dao/impl/zusammen/VendorLicenseModelDaoZusammenImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-core/src/main/java/org/openecomp/sdc/vendorlicense/dao/impl/zusammen/VendorLicenseModelDaoZusammenImpl.java index 2c4702aec1..8f974c4df8 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-core/src/main/java/org/openecomp/sdc/vendorlicense/dao/impl/zusammen/VendorLicenseModelDaoZusammenImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-core/src/main/java/org/openecomp/sdc/vendorlicense/dao/impl/zusammen/VendorLicenseModelDaoZusammenImpl.java @@ -15,6 +15,8 @@ */ package org.openecomp.sdc.vendorlicense.dao.impl.zusammen; +import static org.openecomp.sdc.versioning.dao.impl.zusammen.ItemZusammenDaoImpl.ItemInfoProperty.ITEM_TYPE; + import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; import com.amdocs.zusammen.datatypes.SessionContext; import com.amdocs.zusammen.datatypes.item.Action; @@ -34,7 +36,7 @@ import org.openecomp.sdc.versioning.types.VersionableEntityStoreType; public class VendorLicenseModelDaoZusammenImpl implements VendorLicenseModelDao { - private ZusammenAdaptor zusammenAdaptor; + private final ZusammenAdaptor zusammenAdaptor; public VendorLicenseModelDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) { this.zusammenAdaptor = zusammenAdaptor; @@ -50,7 +52,7 @@ public class VendorLicenseModelDaoZusammenImpl implements VendorLicenseModelDao public Collection<VendorLicenseModelEntity> list(VendorLicenseModelEntity vendorLicenseModelEntity) { ElementToVLMGeneralConvertor convertor = new ElementToVLMGeneralConvertor(); return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext()).stream() - .filter(item -> "VendorLicenseModel".equals(item.getInfo().getProperty("item_type"))).map(item -> { + .filter(item -> "VendorLicenseModel".equals(item.getInfo().getProperty(ITEM_TYPE.getName()))).map(item -> { VendorLicenseModelEntity entity = convertor.convert(item); entity.setId(item.getId().getValue()); entity.setVersion(null); diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/dao/impl/zusammen/VendorSoftwareProductInfoDaoZusammenImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/dao/impl/zusammen/VendorSoftwareProductInfoDaoZusammenImpl.java index 78ab818927..6e17679e4c 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/dao/impl/zusammen/VendorSoftwareProductInfoDaoZusammenImpl.java +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/dao/impl/zusammen/VendorSoftwareProductInfoDaoZusammenImpl.java @@ -18,6 +18,7 @@ package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen; import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement; import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext; +import static org.openecomp.sdc.versioning.dao.impl.zusammen.ItemZusammenDaoImpl.ItemInfoProperty.ITEM_TYPE; import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; import com.amdocs.zusammen.datatypes.SessionContext; @@ -31,6 +32,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import org.openecomp.core.zusammen.api.ZusammenAdaptor; import org.openecomp.sdc.datatypes.model.ElementType; +import org.openecomp.sdc.datatypes.model.ItemType; import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao; import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToVSPGeneralConvertor; import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToVSPQuestionnaireConvertor; @@ -60,7 +62,8 @@ public class VendorSoftwareProductInfoDaoZusammenImpl implements VendorSoftwareP @Override public Collection<VspDetails> list(VspDetails entity) { return zusammenAdaptor.listItems(createSessionContext()).stream() - .filter(item -> "VendorSoftwareProduct".equals(item.getInfo().getProperty("item_type"))).map(new ElementToVSPGeneralConvertor()::convert) + .filter(item -> ItemType.vsp.getName().equals(item.getInfo().getProperty(ITEM_TYPE.getName()))) + .map(new ElementToVSPGeneralConvertor()::convert) .collect(Collectors.toList()); } diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImpl.java index 6ac18d9ff9..e2e7b1cc1e 100644 --- a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImpl.java +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImpl.java @@ -21,9 +21,13 @@ package org.openecomp.sdc.versioning.dao.impl.zusammen; import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.item.Info; +import java.util.Arrays; import java.util.Collection; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; +import lombok.AllArgsConstructor; +import lombok.Getter; import org.openecomp.core.zusammen.api.ZusammenAdaptor; import org.openecomp.core.zusammen.api.ZusammenUtil; import org.openecomp.sdc.versioning.dao.ItemDao; @@ -33,7 +37,7 @@ import org.openecomp.sdc.versioning.types.ItemStatus; public class ItemZusammenDaoImpl implements ItemDao { - private ZusammenAdaptor zusammenAdaptor; + private final ZusammenAdaptor zusammenAdaptor; public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) { this.zusammenAdaptor = zusammenAdaptor; @@ -74,7 +78,7 @@ public class ItemZusammenDaoImpl implements ItemDao { item.setId(zusammenItem.getId().getValue()); item.setName(zusammenItem.getInfo().getName()); item.setDescription(zusammenItem.getInfo().getDescription()); - zusammenItem.getInfo().getProperties().entrySet().forEach(property -> addPropertyToItem(property.getKey(), property.getValue(), item)); + zusammenItem.getInfo().getProperties().forEach((key, value) -> addPropertyToItem(key, value, item)); item.setCreationTime(zusammenItem.getCreationTime()); item.setModificationTime(zusammenItem.getModificationTime()); if (item.getStatus() == null) { @@ -85,17 +89,23 @@ public class ItemZusammenDaoImpl implements ItemDao { } private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) { - switch (propertyKey) { - case InfoPropertyName.ITEM_TYPE: + final ItemInfoProperty itemInfoProperty = ItemInfoProperty.findByName(propertyKey).orElse(null); + if (itemInfoProperty == null) { + item.addProperty(propertyKey, propertyValue); + return; + } + + switch (itemInfoProperty) { + case ITEM_TYPE: item.setType((String) propertyValue); break; - case InfoPropertyName.ITEM_OWNER: + case ITEM_OWNER: item.setOwner((String) propertyValue); break; - case InfoPropertyName.ITEM_STATUS: + case ITEM_STATUS: item.setStatus(ItemStatus.valueOf((String) propertyValue)); break; - case InfoPropertyName.ITEM_VERSIONS_STATUSES: + case ITEM_VERSIONS_STATUSES: for (Map.Entry<String, Number> statusCounter : ((Map<String, Number>) propertyValue).entrySet()) { item.getVersionStatusCounters().put(VersionStatus.valueOf(statusCounter.getKey()), statusCounter.getValue().intValue()); } @@ -109,25 +119,29 @@ public class ItemZusammenDaoImpl implements ItemDao { Info info = new Info(); info.setName(item.getName()); info.setDescription(item.getDescription()); - info.addProperty(InfoPropertyName.ITEM_TYPE, item.getType()); - info.addProperty(InfoPropertyName.ITEM_OWNER, item.getOwner()); + info.addProperty(ItemInfoProperty.ITEM_TYPE.getName(), item.getType()); + info.addProperty(ItemInfoProperty.ITEM_OWNER.getName(), item.getOwner()); if (item.getStatus() != null) { - info.addProperty(InfoPropertyName.ITEM_STATUS, item.getStatus()); + info.addProperty(ItemInfoProperty.ITEM_STATUS.getName(), item.getStatus()); } - info.addProperty(InfoPropertyName.ITEM_VERSIONS_STATUSES, item.getVersionStatusCounters()); - item.getProperties().entrySet().forEach(property -> info.addProperty(property.getKey(), property.getValue())); + info.addProperty(ItemInfoProperty.ITEM_VERSIONS_STATUSES.getName(), item.getVersionStatusCounters()); + item.getProperties().forEach(info::addProperty); return info; } - private static final class InfoPropertyName { + @AllArgsConstructor + @Getter + public enum ItemInfoProperty { + ITEM_TYPE("item_type"), + ITEM_VERSIONS_STATUSES("item_versions_statuses"), + ITEM_OWNER("Owner"), + ITEM_STATUS("status"); - private static final String ITEM_TYPE = "item_type"; - private static final String ITEM_VERSIONS_STATUSES = "item_versions_statuses"; - private static final String ITEM_OWNER = "Owner"; - private static final String ITEM_STATUS = "status"; + private final String name; - private InfoPropertyName() { - throw new IllegalStateException("Constants class"); + public static Optional<ItemInfoProperty> findByName(final String name) { + return Arrays.stream(values()).filter(itemInfoProperty -> itemInfoProperty.getName().equals(name)).findFirst(); } + } } diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/test/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImplTest.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/test/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImplTest.java index a4584d6041..2749185213 100644 --- a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/test/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImplTest.java +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/test/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImplTest.java @@ -20,6 +20,8 @@ import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.verify; +import static org.openecomp.sdc.versioning.dao.impl.zusammen.ItemZusammenDaoImpl.ItemInfoProperty.ITEM_TYPE; +import static org.openecomp.sdc.versioning.dao.impl.zusammen.ItemZusammenDaoImpl.ItemInfoProperty.ITEM_VERSIONS_STATUSES; import static org.openecomp.sdc.versioning.dao.impl.zusammen.TestUtil.createZusammenContext; import com.amdocs.zusammen.datatypes.Id; @@ -49,8 +51,6 @@ import org.openecomp.sdc.versioning.types.ItemStatus; public class ItemZusammenDaoImplTest { private static final String USER = "user1"; - private static final String ITEM_TYPE = "item_type"; - private static final String ITEM_VERSIONS_STATUSES = "item_versions_statuses"; private static final String APP_PROP_1 = "app_prop1"; private static final String APP_PROP_2 = "app_prop2"; private static final String tenant = "dox"; @@ -69,7 +69,7 @@ public class ItemZusammenDaoImplTest { @Test public void testListWhenNone() throws Exception { doReturn(new ArrayList<>()).when(zusammenAdaptorMock) - .listItems(eq(createZusammenContext(USER))); + .listItems(createZusammenContext(USER)); Collection<Item> items = itemDao.list(); @@ -91,10 +91,10 @@ public class ItemZusammenDaoImplTest { createItem("2", "vlm1", "vlm 1", "vlm", new Date(), new Date(), vlm1versionStatuses), createItem("3", "vsp2", "vsp 2", "vsp", new Date(), new Date(), vsp2versionStatuses)) .collect(Collectors.toList()); - doReturn(returnedItems).when(zusammenAdaptorMock).listItems(eq(createZusammenContext(USER))); + doReturn(returnedItems).when(zusammenAdaptorMock).listItems(createZusammenContext(USER)); Collection<Item> items = itemDao.list(); - assertEquals(items.size(), 3); + assertEquals(3, items.size()); Iterator<Item> itemIterator = items.iterator(); assertItemEquals(itemIterator.next(), returnedItems.get(0)); @@ -125,13 +125,13 @@ public class ItemZusammenDaoImplTest { createItem("1", "vsp1", "vsp 1", "vsp", new Date(System.currentTimeMillis() - 100), new Date(), versionStatuses); doReturn(toBeReturned).when(zusammenAdaptorMock) - .getItem(eq(createZusammenContext(USER)), eq(new Id(inputItem.getId()))); + .getItem(createZusammenContext(USER), new Id(inputItem.getId())); Item item = itemDao.get(inputItem); Assert.assertNotNull(item); assertItemEquals(item, toBeReturned); - assertEquals(item.getStatus(), ItemStatus.ACTIVE); + assertEquals(ItemStatus.ACTIVE, item.getStatus()); } @@ -153,8 +153,8 @@ public class ItemZusammenDaoImplTest { Info capturedInfo = capturedZusammenInfo.getValue(); assertEquals(capturedInfo.getName(), inputItem.getName()); assertEquals(capturedInfo.getDescription(), inputItem.getDescription()); - assertEquals(capturedInfo.getProperty(ITEM_TYPE), inputItem.getType()); - assertEquals(capturedInfo.getProperty(ITEM_VERSIONS_STATUSES), + assertEquals(capturedInfo.getProperty(ITEM_TYPE.getName()), inputItem.getType()); + assertEquals(capturedInfo.getProperty(ITEM_VERSIONS_STATUSES.getName()), inputItem.getVersionStatusCounters()); assertEquals(item.getId(), itemId); @@ -186,8 +186,8 @@ public class ItemZusammenDaoImplTest { Info capturedInfo = capturedZusammenInfo.getValue(); assertEquals(capturedInfo.getName(), item.getName()); assertEquals(capturedInfo.getDescription(), item.getDescription()); - assertEquals(capturedInfo.getProperty(ITEM_TYPE), item.getType()); - assertEquals(capturedInfo.getProperty(ITEM_VERSIONS_STATUSES), + assertEquals(capturedInfo.getProperty(ITEM_TYPE.getName()), item.getType()); + assertEquals(capturedInfo.getProperty(ITEM_VERSIONS_STATUSES.getName()), item.getVersionStatusCounters()); } @@ -201,8 +201,8 @@ public class ItemZusammenDaoImplTest { Info info = new Info(); info.setName(name); info.setDescription(description); - info.addProperty(ITEM_TYPE, type); - info.addProperty(ITEM_VERSIONS_STATUSES, versionStatusCounters); + info.addProperty(ITEM_TYPE.getName(), type); + info.addProperty(ITEM_VERSIONS_STATUSES.getName(), versionStatusCounters); info.addProperty(APP_PROP_1, "app_prop1_value"); info.addProperty(APP_PROP_2, 8); item.setInfo(info); @@ -215,14 +215,14 @@ public class ItemZusammenDaoImplTest { assertEquals(item.getId(), zusammenItem.getId().getValue()); assertEquals(item.getName(), zusammenItem.getInfo().getName()); assertEquals(item.getDescription(), zusammenItem.getInfo().getDescription()); - assertEquals(item.getType(), zusammenItem.getInfo().getProperty(ITEM_TYPE)); + assertEquals(item.getType(), zusammenItem.getInfo().getProperty(ITEM_TYPE.getName())); assertEquals(item.getProperties().get(APP_PROP_1), zusammenItem.getInfo().getProperty(APP_PROP_1)); assertEquals(item.getProperties().get(APP_PROP_2), zusammenItem.getInfo().getProperty(APP_PROP_2)); Map<String, Number> zusammenStatusesMap = - zusammenItem.getInfo().getProperty(ITEM_VERSIONS_STATUSES); + zusammenItem.getInfo().getProperty(ITEM_VERSIONS_STATUSES.getName()); Map<VersionStatus, Integer> statusesMap = item.getVersionStatusCounters(); zusammenStatusesMap.entrySet() |