From f2e43fb0a2c36b484b686d6c22342e72da66f679 Mon Sep 17 00:00:00 2001 From: vasraz Date: Thu, 31 Mar 2022 19:05:00 +0100 Subject: Implement restore of partially deleted VSP Change-Id: I790b6cd8494e8f35c3b9891304f15272e9769a68 Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3935 --- .../sdcrests/item/rest/services/ItemsImpl.java | 15 ++- .../sdcrests/item/rest/services/ItemsImplTest.java | 145 +++++++++++++++------ 2 files changed, 120 insertions(+), 40 deletions(-) (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src') diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java index f18de66863..c184eca3c1 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java @@ -15,6 +15,8 @@ */ package org.openecomp.sdcrests.item.rest.services; +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; +import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_ID; import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_NAME; @@ -36,6 +38,10 @@ import javax.inject.Named; import javax.ws.rs.core.Response; 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.ErrorCode.ErrorCodeBuilder; +import org.openecomp.sdc.common.errors.ErrorCodeAndMessage; import org.openecomp.sdc.datatypes.model.ItemType; import org.openecomp.sdc.itempermissions.impl.types.PermissionTypes; import org.openecomp.sdc.logging.api.Logger; @@ -80,13 +86,20 @@ public class ItemsImpl implements Items { public Response actOn(ItemActionRequestDto request, String itemId, String user) { Item item = getManagersProvider().getItemManager().get(itemId); if (item == null) { - return Response.status(Response.Status.NOT_FOUND).entity(new Exception("Item does not exist.")).build(); + return Response.status(NOT_FOUND).entity(new Exception("Item does not exist.")).build(); } switch (request.getAction()) { case ARCHIVE: getManagersProvider().getItemManager().archive(item); break; case RESTORE: + final var artifactStorageManager = new StorageFactory().createArtifactStorageManager(); + if (artifactStorageManager.isEnabled() && !artifactStorageManager.exists(itemId)) { + LOGGER.error("Unable to restore partially deleted item '{}'", itemId); + final var errorCode = + new ErrorCodeBuilder().withId(INTERNAL_SERVER_ERROR.name()).withMessage("Unable to restore partially deleted VSP, re-try VSP deletion").build(); + return Response.status(INTERNAL_SERVER_ERROR).entity(new ErrorCodeAndMessage(INTERNAL_SERVER_ERROR, errorCode)).build(); + } getManagersProvider().getItemManager().restore(item); break; default: diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java index d7e9c81823..12056ad4db 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/ItemsImplTest.java @@ -19,30 +19,57 @@ */ package org.openecomp.sdcrests.item.rest.services; -import static org.junit.Assert.assertEquals; +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.be.csar.storage.StorageFactory.StorageType.MINIO; +import static org.openecomp.sdc.be.csar.storage.StorageFactory.StorageType.NONE; import static org.openecomp.sdcrests.item.types.ItemAction.ARCHIVE; import static org.openecomp.sdcrests.item.types.ItemAction.RESTORE; +import io.minio.BucketExistsArgs; +import io.minio.MinioClient; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import javax.ws.rs.core.Response; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Answers; +import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; import org.openecomp.sdc.activitylog.ActivityLogManager; +import org.openecomp.sdc.common.CommonConfigurationManager; +import org.openecomp.sdc.common.errors.ErrorCodeAndMessage; import org.openecomp.sdc.versioning.ItemManager; import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.types.Item; import org.openecomp.sdcrests.item.types.ItemActionRequestDto; -@RunWith(MockitoJUnitRunner.class) -public class ItemsImplTest { +@ExtendWith(MockitoExtension.class) +class ItemsImplTest { - private static final String ITEM_ID = "ITEM_ID"; + private static final String ITEM_ID = "item-id"; private static final String USER = "USER"; + private static final String EXTERNAL_CSAR_STORE = "externalCsarStore"; + private static final String STORAGE_TYPE = "storageType"; + private static final String ENDPOINT = "endpoint"; + private static final String CREDENTIALS = "credentials"; + private static final String TEMP_PATH = "tempPath"; + private static final String UPLOAD_PARTSIZE = "uploadPartSize"; @Mock private ManagersProvider managersProvider; @@ -56,71 +83,111 @@ public class ItemsImplTest { private VersioningManager versioningManager; @Mock private ActivityLogManager activityManager; + @Mock + private CommonConfigurationManager commonConfigurationManager; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private MinioClient.Builder builderMinio; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private BucketExistsArgs.Builder builderBucketExistsArgs; + + @InjectMocks + private ItemsImpl items; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } @Test - public void shouldInitActionSideAffectsMap() { - ItemsImpl items = new ItemsImpl(); + void shouldInitActionSideAffectsMap() { items.initActionSideAffectsMap(); - assertEquals(items.getActionSideAffectsMap().size(),2); + assertEquals(2, items.getActionSideAffectsMap().size()); } @Test - public void shouldActOnEmptyItem() { - ItemsImpl items = new ItemsImpl(); + void shouldActOnEmptyItem() { items.initActionSideAffectsMap(); items.setManagersProvider(managersProvider); - Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager); - Mockito.when(itemManager.get(Mockito.any())).thenReturn(null); + when(managersProvider.getItemManager()).thenReturn(itemManager); + when(itemManager.get(any())).thenReturn(null); Response response = items.actOn(request, ITEM_ID, USER); assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); } @Test - public void shouldActOnARCHIVE() { - ItemsImpl items = new ItemsImpl(); + void shouldActOnARCHIVE() { items.initActionSideAffectsMap(); items.setManagersProvider(managersProvider); - Mockito.when(itemManager.get(Mockito.any())).thenReturn(item); - Mockito.when(request.getAction()).thenReturn(ARCHIVE); - Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager); - Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager); - Mockito.when(versioningManager.list(Mockito.any())).thenReturn(getVersions()); - Mockito.when(managersProvider.getActivityLogManager()).thenReturn(activityManager); + when(itemManager.get(any())).thenReturn(item); + when(request.getAction()).thenReturn(ARCHIVE); + when(managersProvider.getItemManager()).thenReturn(itemManager); + when(managersProvider.getVersioningManager()).thenReturn(versioningManager); + when(versioningManager.list(any())).thenReturn(getVersions()); + when(managersProvider.getActivityLogManager()).thenReturn(activityManager); items.actOn(request, ITEM_ID, USER); - Mockito.verify(itemManager).archive(Mockito.any()); + verify(itemManager).archive(any()); } @Test - public void shouldActOnRESTORE() { - ItemsImpl items = new ItemsImpl(); + void shouldActOnRESTORE() { items.initActionSideAffectsMap(); items.setManagersProvider(managersProvider); - Mockito.when(itemManager.get(Mockito.any())).thenReturn(item); - Mockito.when(request.getAction()).thenReturn(RESTORE); - Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager); - Mockito.when(managersProvider.getVersioningManager()).thenReturn(versioningManager); - Mockito.when(versioningManager.list(Mockito.any())).thenReturn(getVersions()); - Mockito.when(managersProvider.getActivityLogManager()).thenReturn(activityManager); + when(itemManager.get(any())).thenReturn(item); + when(request.getAction()).thenReturn(RESTORE); + when(managersProvider.getItemManager()).thenReturn(itemManager); + when(managersProvider.getVersioningManager()).thenReturn(versioningManager); + when(versioningManager.list(any())).thenReturn(getVersions()); + when(managersProvider.getActivityLogManager()).thenReturn(activityManager); items.actOn(request, ITEM_ID, USER); - Mockito.verify(itemManager).restore(Mockito.any()); + verify(itemManager).restore(any()); + } + + @Test + void shouldActOnRESTORE_with_S3() throws Exception { + items.initActionSideAffectsMap(); + items.setManagersProvider(managersProvider); + when(itemManager.get(any())).thenReturn(item); + when(request.getAction()).thenReturn(RESTORE); + when(managersProvider.getItemManager()).thenReturn(itemManager); + try (MockedStatic utilities = Mockito.mockStatic(CommonConfigurationManager.class)) { + utilities.when(CommonConfigurationManager::getInstance).thenReturn(commonConfigurationManager); + try (MockedStatic minioUtilities = Mockito.mockStatic(MinioClient.class)) { + minioUtilities.when(MinioClient::builder).thenReturn(builderMinio); + when(commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, STORAGE_TYPE, NONE.name())).thenReturn(MINIO.name()); + when(commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, ENDPOINT, null)).thenReturn(new HashMap()); + when(commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, CREDENTIALS, null)).thenReturn(new HashMap()); + when(commonConfigurationManager.getConfigValue(EXTERNAL_CSAR_STORE, TEMP_PATH, null)).thenReturn(""); + when(commonConfigurationManager.getConfigValue(eq(EXTERNAL_CSAR_STORE), eq(UPLOAD_PARTSIZE), anyInt())).thenReturn(0); + when(builderBucketExistsArgs + .bucket(anyString()) + .build() + ).thenReturn(new BucketExistsArgs()); + + final var response = items.actOn(request, ITEM_ID, USER); + assertNotNull(response); + assertEquals(INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + assertNotNull(response.getEntity()); + assertTrue(response.getEntity() instanceof ErrorCodeAndMessage); + assertEquals(INTERNAL_SERVER_ERROR.getStatusCode(), ((ErrorCodeAndMessage) response.getEntity()).getStatus().getStatusCode()); + assertEquals(INTERNAL_SERVER_ERROR.name(), ((ErrorCodeAndMessage) response.getEntity()).getErrorCode()); + } + } } @Test - public void shouldGetItem() { - ItemsImpl items = new ItemsImpl(); + void shouldGetItem() { items.initActionSideAffectsMap(); items.setManagersProvider(managersProvider); - Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager); + when(managersProvider.getItemManager()).thenReturn(itemManager); Response response = items.getItem(ITEM_ID, USER); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); } @Test - public void shouldList() { - ItemsImpl items = new ItemsImpl(); + void shouldList() { items.initActionSideAffectsMap(); items.setManagersProvider(managersProvider); - Mockito.when(managersProvider.getItemManager()).thenReturn(itemManager); + when(managersProvider.getItemManager()).thenReturn(itemManager); Response response = items.list(null, null, null, null, null, USER); assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); } @@ -132,4 +199,4 @@ public class ItemsImplTest { versions.add(new Version("3")); return versions; } -} \ No newline at end of file +} -- cgit 1.2.3-korg