From 30ff812c65389f5ca4c91d9774d425e81ef62049 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Wed, 13 Apr 2022 11:21:15 +0100 Subject: 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 --- .../errors/VendorLicenseErrorCodes.java | 6 +++ .../VendorLicenseModelDaoZusammenImpl.java | 6 ++- .../VendorSoftwareProductInfoDaoZusammenImpl.java | 5 ++- .../dao/impl/zusammen/ItemZusammenDaoImpl.java | 52 ++++++++++++++-------- .../dao/impl/zusammen/ItemZusammenDaoImplTest.java | 30 ++++++------- 5 files changed, 62 insertions(+), 37 deletions(-) (limited to 'openecomp-be/lib') 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 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 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 statusCounter : ((Map) 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 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 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 items = itemDao.list(); - assertEquals(items.size(), 3); + assertEquals(3, items.size()); Iterator 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 zusammenStatusesMap = - zusammenItem.getInfo().getProperty(ITEM_VERSIONS_STATUSES); + zusammenItem.getInfo().getProperty(ITEM_VERSIONS_STATUSES.getName()); Map statusesMap = item.getVersionStatusCounters(); zusammenStatusesMap.entrySet() -- cgit 1.2.3-korg