BusinessObjectDataInitiateRestoreHelperServiceImpl implements BusinessObjectDataInitiateRestoreHelperService { protected StorageUnitEntity getStorageUnit(BusinessObjectDataEntity businessObjectDataEntity) { List<StorageUnitEntity> s3StorageUnitEntities = storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); if (CollectionUtils.isEmpty(s3StorageUnitEntities)) { throw new IllegalArgumentException(String.format("Business object data has no S3 storage unit. Business object data: {%s}", businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } if (CollectionUtils.size(s3StorageUnitEntities) > 1) { throw new IllegalArgumentException(String .format("Business object data has multiple (%s) %s storage units. Business object data: {%s}", s3StorageUnitEntities.size(), StoragePlatformEntity.S3, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } StorageUnitEntity storageUnitEntity = s3StorageUnitEntities.get(0); String storageUnitStatus = storageUnitEntity.getStatus().getCode(); if (!StorageUnitStatusEntity.ARCHIVED.equals(storageUnitStatus)) { String storageName = storageUnitEntity.getStorage().getName(); if (StorageUnitStatusEntity.ENABLED.equals(storageUnitStatus)) { throw new IllegalArgumentException(String .format("Business object data is already available in \"%s\" S3 storage. Business object data: {%s}", storageName, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageUnitEntity.getBusinessObjectData()))); } else if (StorageUnitStatusEntity.RESTORING.equals(storageUnitStatus)) { throw new IllegalArgumentException(String .format("Business object data is already being restored in \"%s\" S3 storage. Business object data: {%s}", storageName, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageUnitEntity.getBusinessObjectData()))); } else { throw new IllegalArgumentException(String.format("Business object data is not archived. " + "S3 storage unit in \"%s\" storage must have \"%s\" status, but it actually has \"%s\" status. Business object data: {%s}", storageName, StorageUnitStatusEntity.ARCHIVED, storageUnitStatus, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageUnitEntity.getBusinessObjectData()))); } } return storageUnitEntity; }  @PublishNotificationMessages @Override @Transactional(propagation = Propagation.REQUIRES_NEW) BusinessObjectData executeInitiateRestoreAfterStep(BusinessObjectDataRestoreDto businessObjectDataRestoreDto); @Override @Transactional(propagation = Propagation.NOT_SUPPORTED) void executeS3SpecificSteps(BusinessObjectDataRestoreDto businessObjectDataRestoreDto); @PublishNotificationMessages @Override @Transactional(propagation = Propagation.REQUIRES_NEW) BusinessObjectDataRestoreDto prepareToInitiateRestore(BusinessObjectDataKey businessObjectDataKey, Integer expirationInDays,
        String archiveRetrievalOption);  }
@Test public void testGetStorageUnit() { BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); StorageUnitStatusEntity storageUnitStatusEntity = new StorageUnitStatusEntity(); storageUnitStatusEntity.setCode(StorageUnitStatusEntity.ARCHIVED); StorageUnitEntity storageUnitEntity = new StorageUnitEntity(); storageUnitEntity.setStatus(storageUnitStatusEntity); when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)) .thenReturn(Collections.singletonList(storageUnitEntity)); StorageUnitEntity result = businessObjectDataInitiateRestoreHelperServiceImpl.getStorageUnit(businessObjectDataEntity); verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); verifyNoMoreInteractionsHelper(); assertEquals(result, storageUnitEntity); }
@Test public void testGetStorageUnitMultipleStorageUnitsExist() { BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); List<StorageUnitEntity> storageUnitEntities = Arrays.asList(new StorageUnitEntity(), new StorageUnitEntity()); when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)) .thenReturn(storageUnitEntities); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); try { businessObjectDataInitiateRestoreHelperServiceImpl.getStorageUnit(businessObjectDataEntity); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("Business object data has multiple (%d) S3 storage units. Business object data: {%s}", storageUnitEntities.size(), BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
@Test public void testGetStorageUnitStorageUnitAlreadyEnabled() { BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); StorageUnitStatusEntity storageUnitStatusEntity = new StorageUnitStatusEntity(); storageUnitStatusEntity.setCode(StorageUnitStatusEntity.ENABLED); StorageEntity storageEntity = new StorageEntity(); storageEntity.setName(STORAGE_NAME); StorageUnitEntity storageUnitEntity = new StorageUnitEntity(); storageUnitEntity.setStorage(storageEntity); storageUnitEntity.setBusinessObjectData(businessObjectDataEntity); storageUnitEntity.setStatus(storageUnitStatusEntity); when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)) .thenReturn(Collections.singletonList(storageUnitEntity)); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); try { businessObjectDataInitiateRestoreHelperServiceImpl.getStorageUnit(businessObjectDataEntity); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("Business object data is already available in \"%s\" S3 storage. Business object data: {%s}", STORAGE_NAME, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
@Test public void testGetStorageUnitStorageUnitAlreadyRestoring() { BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); StorageUnitStatusEntity storageUnitStatusEntity = new StorageUnitStatusEntity(); storageUnitStatusEntity.setCode(StorageUnitStatusEntity.RESTORING); StorageEntity storageEntity = new StorageEntity(); storageEntity.setName(STORAGE_NAME); StorageUnitEntity storageUnitEntity = new StorageUnitEntity(); storageUnitEntity.setStorage(storageEntity); storageUnitEntity.setBusinessObjectData(businessObjectDataEntity); storageUnitEntity.setStatus(storageUnitStatusEntity); when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)) .thenReturn(Collections.singletonList(storageUnitEntity)); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); try { businessObjectDataInitiateRestoreHelperServiceImpl.getStorageUnit(businessObjectDataEntity); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("Business object data is already being restored in \"%s\" S3 storage. Business object data: {%s}", STORAGE_NAME, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
@Test public void testGetStorageUnitStorageUnitNoExists() { BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)) .thenReturn(new ArrayList<>()); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); try { businessObjectDataInitiateRestoreHelperServiceImpl.getStorageUnit(businessObjectDataEntity); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format("Business object data has no S3 storage unit. Business object data: {%s}", BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
@Test public void testGetStorageUnitStorageUnitNotArchived() { BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); StorageUnitStatusEntity storageUnitStatusEntity = new StorageUnitStatusEntity(); storageUnitStatusEntity.setCode(STORAGE_UNIT_STATUS); StorageEntity storageEntity = new StorageEntity(); storageEntity.setName(STORAGE_NAME); StorageUnitEntity storageUnitEntity = new StorageUnitEntity(); storageUnitEntity.setStorage(storageEntity); storageUnitEntity.setBusinessObjectData(businessObjectDataEntity); storageUnitEntity.setStatus(storageUnitStatusEntity); when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)) .thenReturn(Collections.singletonList(storageUnitEntity)); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); try { businessObjectDataInitiateRestoreHelperServiceImpl.getStorageUnit(businessObjectDataEntity); fail(); } catch (IllegalArgumentException e) { assertEquals(String.format( "Business object data is not archived. S3 storage unit in \"%s\" storage must have \"%s\" status, but it actually has \"%s\" status. " + "Business object data: {%s}", STORAGE_NAME, StorageUnitStatusEntity.ARCHIVED, STORAGE_UNIT_STATUS, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }