use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class CleanupDestroyedBusinessObjectDataServiceImplTest method createDatabaseEntitiesForCleanupDestroyedBusinessObjectDataTesting.
/**
* Creates business object data and associated entities required for the cleanup destroyed business object data unit tests.
*/
public BusinessObjectDataStorageUnitKey createDatabaseEntitiesForCleanupDestroyedBusinessObjectDataTesting() {
List<SchemaColumn> columns = schemaColumnDaoTestHelper.getTestSchemaColumns();
List<SchemaColumn> partitionColumns = schemaColumnDaoTestHelper.getTestPartitionColumns();
// Create a business object format entity.
businessObjectFormatDaoTestHelper.createBusinessObjectFormatEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, FORMAT_DESCRIPTION, LATEST_VERSION_FLAG_SET, PARTITION_KEY, PARTITION_KEY_GROUP, NO_ATTRIBUTES, SCHEMA_DELIMITER_COMMA, SCHEMA_ESCAPE_CHARACTER_BACKSLASH, SCHEMA_NULL_VALUE_BACKSLASH_N, columns, partitionColumns);
// Create S3 storage entity.
StorageEntity storageEntity = storageDaoTestHelper.createStorageEntity(STORAGE_NAME, StoragePlatformEntity.S3, Arrays.asList(new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_BUCKET_NAME), S3_BUCKET_NAME), new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KEY_PREFIX_VELOCITY_TEMPLATE), S3_KEY_PREFIX_VELOCITY_TEMPLATE)));
// Create a business object data entity parent.
BusinessObjectDataEntity businessObjectDataEntityParent = businessObjectDataDaoTestHelper.createBusinessObjectDataEntity(NAMESPACE, BDEF_NAME_3, FORMAT_USAGE_CODE_3, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE_3, SUBPARTITION_VALUES, DATA_VERSION, LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.VALID);
// Create business object data entity.
BusinessObjectDataEntity businessObjectDataEntity = businessObjectDataDaoTestHelper.createBusinessObjectDataEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION, LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.DELETED, Lists.newArrayList(businessObjectDataEntityParent));
// Create a business object data entity child.
businessObjectDataDaoTestHelper.createBusinessObjectDataEntity(NAMESPACE, BDEF_NAME_2, FORMAT_USAGE_CODE_2, FORMAT_FILE_TYPE_CODE_2, FORMAT_VERSION_2, PARTITION_VALUE_2, SUBPARTITION_VALUES_2, DATA_VERSION_2, LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.VALID, Lists.newArrayList(businessObjectDataEntity));
// Create and persist a business object data attribute entity.
businessObjectDataAttributeDaoTestHelper.createBusinessObjectDataAttributeEntity(businessObjectDataEntity, ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
// Create the storage units.
StorageUnitEntity storageUnitEntity = storageUnitDaoTestHelper.createStorageUnitEntity(storageEntity, businessObjectDataEntity, StorageUnitStatusEntity.DISABLED, STORAGE_DIRECTORY_PATH);
storageFileDaoTestHelper.createStorageFileEntity(storageUnitEntity, STORAGE_DIRECTORY_PATH, FILE_SIZE, ROW_COUNT);
return storageUnitHelper.createStorageUnitKeyFromEntity(storageUnitEntity);
}
use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class CleanupDestroyedBusinessObjectDataServiceImplTest method testCleanupS3StorageUnit.
@Test
public void testCleanupS3StorageUnit() {
// Create some data for testing the cleanup
BusinessObjectDataStorageUnitKey businessObjectDataStorageUnitKey = createDatabaseEntitiesForCleanupDestroyedBusinessObjectDataTesting();
// Get a business object data key.
BusinessObjectDataKey businessObjectDataKey = businessObjectDataHelper.createBusinessObjectDataKeyFromStorageUnitKey(businessObjectDataStorageUnitKey);
// Confirm the business object data entity exists.
BusinessObjectDataEntity businessObjectDataEntity = businessObjectDataDao.getBusinessObjectDataByAltKey(businessObjectDataKey);
assertThat("The business object data entity is null.", businessObjectDataEntity, is(notNullValue()));
// Confirm the business object data attribute exists.
BusinessObjectDataAttributeKey businessObjectDataAttributeKey = businessObjectDataAttributeHelper.getBusinessObjectDataAttributeKey(businessObjectDataKey, ATTRIBUTE_NAME);
assertThat("The business object data attribute key is null.", businessObjectDataAttributeKey, is(notNullValue()));
assertThat("The business object data entity attributes size is not correct.", businessObjectDataEntity.getAttributes().size(), is(equalTo(1)));
// Confirm the business object data attribute exists.
BusinessObjectDataAttributeEntity businessObjectDataAttributeEntity = businessObjectDataAttributeDao.getBusinessObjectDataAttributeByKey(businessObjectDataAttributeKey);
assertThat("The business object data attribute entity is null.", businessObjectDataAttributeEntity, is(notNullValue()));
// Confirm the storage unity exists.
StorageUnitEntity storageUnitEntity = storageUnitDaoHelper.getStorageUnitEntityByKey(businessObjectDataStorageUnitKey);
assertThat("The storage unit entity is null.", storageUnitEntity, is(notNullValue()));
// Confirm the storage file exists.
StorageFileEntity storageFileEntity = storageFileDao.getStorageFileByStorageNameAndFilePath(STORAGE_NAME, STORAGE_DIRECTORY_PATH);
assertThat("The storage file entity is null.", storageFileEntity, is(notNullValue()));
// Confirm the business object data status history exists.
Collection<BusinessObjectDataStatusHistoryEntity> businessObjectDataStatusHistoryEntities = businessObjectDataEntity.getHistoricalStatuses();
assertThat("The business object data status history entities size is not correct.", businessObjectDataStatusHistoryEntities.size(), is(equalTo(1)));
// Confirm the business object data children
assertThat("The business object data children size is not correct.", businessObjectDataEntity.getBusinessObjectDataChildren().size(), is(equalTo(1)));
BusinessObjectDataEntity businessObjectDataEntityChild = businessObjectDataEntity.getBusinessObjectDataChildren().get(0);
assertThat("The business object data children is not correct.", businessObjectDataEntityChild.getBusinessObjectDataParents().get(0), is(businessObjectDataEntity));
// Confirm the business object data parents
assertThat("The business object data parents size is not correct.", businessObjectDataEntity.getBusinessObjectDataParents().size(), is(equalTo(1)));
assertThat(businessObjectDataEntityChild.getBusinessObjectDataParents().get(0), is(businessObjectDataEntity));
BusinessObjectDataEntity businessObjectDataEntityParent = businessObjectDataEntity.getBusinessObjectDataParents().get(0);
assertThat("The business object data parent is not correct.", businessObjectDataEntityParent.getBusinessObjectDataChildren().get(0), is(businessObjectDataEntity));
// All traces of BData are removed: BData record, Attributes, related Storage Units and Storage Files, Status history, parent-child relationships
cleanupDestroyedBusinessObjectDataService.cleanupS3StorageUnit(businessObjectDataStorageUnitKey);
// Verify the business object data has been removed.
businessObjectDataEntity = businessObjectDataDao.getBusinessObjectDataByAltKey(businessObjectDataKey);
assertThat("The business object data entity is not null.", businessObjectDataEntity, is(nullValue()));
// Verify the business object data attribute has been removed.
businessObjectDataAttributeEntity = businessObjectDataAttributeDao.getBusinessObjectDataAttributeByKey(businessObjectDataAttributeKey);
assertThat("The business object data attribute entity is not null.", businessObjectDataAttributeEntity, is(nullValue()));
// Verify the storage unit has been removed.
storageUnitEntity = storageUnitDao.getStorageUnitByKey(businessObjectDataStorageUnitKey);
assertThat("The storage unit entity is not null.", storageUnitEntity, is(nullValue()));
// Verify the storage file has been removed.
storageFileEntity = storageFileDao.getStorageFileByStorageNameAndFilePath(STORAGE_NAME, STORAGE_DIRECTORY_PATH);
assertThat("The storage file entity is not null.", storageFileEntity, is(nullValue()));
// Verify the business object data children parent relationship has been removed
businessObjectDataEntityChild = businessObjectDataDaoHelper.getBusinessObjectDataEntity(businessObjectDataHelper.createBusinessObjectDataKeyFromEntity(businessObjectDataEntityChild));
assertThat("The business object data children size is not correct.", businessObjectDataEntityChild.getBusinessObjectDataParents().size(), is(equalTo(0)));
// // Verify the business object data parent child relationship has been removed
businessObjectDataEntityParent = businessObjectDataDaoHelper.getBusinessObjectDataEntity(businessObjectDataHelper.createBusinessObjectDataKeyFromEntity(businessObjectDataEntityParent));
assertThat("The business object data parents size is not correct.", businessObjectDataEntityParent.getBusinessObjectDataChildren().size(), is(equalTo(0)));
}
use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class CleanupDestroyedBusinessObjectDataServiceImplTest method testGetS3StorageUnitsToCleanup.
@Test
public void testGetS3StorageUnitsToCleanup() {
// Create a list of business object data keys.
List<BusinessObjectDataKey> businessObjectDataKeys = new ArrayList<>();
for (int i = 0; i < 7; i++) {
businessObjectDataKeys.add(new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, Integer.toString(i), SUBPARTITION_VALUES, DATA_VERSION));
}
// Create database entities required for testing.
List<StorageUnitEntity> storageUnitEntities = Lists.newArrayList();
// Add 4 valid storage unit entries
storageUnitEntities.add(storageUnitDaoTestHelper.createStorageUnitEntity(STORAGE_NAME, StoragePlatformEntity.S3, businessObjectDataKeys.get(0), LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.DELETED, StorageUnitStatusEntity.DISABLED, NO_STORAGE_DIRECTORY_PATH));
storageUnitEntities.add(storageUnitDaoTestHelper.createStorageUnitEntity(STORAGE_NAME, StoragePlatformEntity.S3, businessObjectDataKeys.get(1), LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.DELETED, StorageUnitStatusEntity.DISABLED, NO_STORAGE_DIRECTORY_PATH));
storageUnitEntities.add(storageUnitDaoTestHelper.createStorageUnitEntity(STORAGE_NAME, StoragePlatformEntity.S3, businessObjectDataKeys.get(2), LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.DELETED, StorageUnitStatusEntity.DISABLED, NO_STORAGE_DIRECTORY_PATH));
storageUnitEntities.add(storageUnitDaoTestHelper.createStorageUnitEntity(STORAGE_NAME_2, StoragePlatformEntity.S3, businessObjectDataKeys.get(3), LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.DELETED, StorageUnitStatusEntity.DISABLED, NO_STORAGE_DIRECTORY_PATH));
// Not a valid business object data status
storageUnitEntities.add(storageUnitDaoTestHelper.createStorageUnitEntity(STORAGE_NAME, StoragePlatformEntity.S3, businessObjectDataKeys.get(4), LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.ARCHIVED, StorageUnitStatusEntity.DISABLED, NO_STORAGE_DIRECTORY_PATH));
// Not a valid storage unit status
storageUnitEntities.add(storageUnitDaoTestHelper.createStorageUnitEntity(STORAGE_NAME, StoragePlatformEntity.S3, businessObjectDataKeys.get(5), LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.DELETED, StorageUnitStatusEntity.ARCHIVED, NO_STORAGE_DIRECTORY_PATH));
// Not a valid storage platform
storageUnitEntities.add(storageUnitDaoTestHelper.createStorageUnitEntity(STORAGE_NAME_3, StoragePlatformEntity.TABLE_NAME, businessObjectDataKeys.get(6), LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.DELETED, StorageUnitStatusEntity.DISABLED, NO_STORAGE_DIRECTORY_PATH));
// Set restore expiration time values.
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
// Not a valid final destroy on date
storageUnitEntities.get(0).setFinalDestroyOn(null);
// Final destroy on date is not less than current timestamp
storageUnitEntities.get(1).setFinalDestroyOn(HerdDateUtils.addDays(currentTime, 1));
// Valid final destroy on dates
storageUnitEntities.get(2).setFinalDestroyOn(HerdDateUtils.addDays(currentTime, -1));
storageUnitEntities.get(3).setFinalDestroyOn(HerdDateUtils.addDays(currentTime, -2));
storageUnitEntities.get(4).setFinalDestroyOn(HerdDateUtils.addDays(currentTime, -2));
storageUnitEntities.get(5).setFinalDestroyOn(HerdDateUtils.addDays(currentTime, -2));
storageUnitEntities.get(6).setFinalDestroyOn(HerdDateUtils.addDays(currentTime, -2));
// Retrieve the business object data storage unit keys and validate the results. Only two entities are expected to match all select criteria.
List<BusinessObjectDataStorageUnitKey> result = cleanupDestroyedBusinessObjectDataService.getS3StorageUnitsToCleanup(MAX_RESULT);
BusinessObjectDataStorageUnitKey businessObjectDataStorageUnitKey3 = storageUnitHelper.createStorageUnitKeyFromEntity(storageUnitEntities.get(3));
BusinessObjectDataStorageUnitKey businessObjectDataStorageUnitKey2 = storageUnitHelper.createStorageUnitKeyFromEntity(storageUnitEntities.get(2));
assertEquals(Arrays.asList(businessObjectDataStorageUnitKey3, businessObjectDataStorageUnitKey2), result);
// Try to retrieve the business object data storage unit keys with max result limit set to 1. Only a single storage unit entity should get selected.
result = cleanupDestroyedBusinessObjectDataService.getS3StorageUnitsToCleanup(1);
assertEquals(Collections.singletonList(businessObjectDataStorageUnitKey3), result);
}
use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class BusinessObjectDataServiceCreateBusinessObjectDataTest method testCreateBusinessObjectDataS3ManagedBucketFileAlreadyRegistered.
@Test
public void testCreateBusinessObjectDataS3ManagedBucketFileAlreadyRegistered() {
// Create relative database entities including a storage file entity registered by a test business object data with PARTITION_VALUE_2 partition value.
BusinessObjectDataEntity businessObjectDataEntity = businessObjectDataDaoTestHelper.createBusinessObjectDataEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, INITIAL_FORMAT_VERSION, PARTITION_VALUE_2, INITIAL_DATA_VERSION, true, BDATA_STATUS);
StorageUnitEntity storageUnitEntity = storageUnitDaoTestHelper.createStorageUnitEntity(storageDao.getStorageByName(StorageEntity.MANAGED_STORAGE), businessObjectDataEntity, StorageUnitStatusEntity.ENABLED, NO_STORAGE_DIRECTORY_PATH);
storageFileDaoTestHelper.createStorageFileEntity(storageUnitEntity, String.format("%s/%s", testS3KeyPrefix, LOCAL_FILE), FILE_SIZE_1_KB, ROW_COUNT_1000);
// Build a new business object data create request containing the already registered storage file.
BusinessObjectDataCreateRequest request = businessObjectDataServiceTestHelper.createBusinessObjectDataCreateRequest(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, INITIAL_FORMAT_VERSION, PARTITION_KEY, PARTITION_VALUE, BDATA_STATUS, StorageEntity.MANAGED_STORAGE, testS3KeyPrefix, businessObjectDataServiceTestHelper.getTestStorageFiles(testS3KeyPrefix, Arrays.asList(LOCAL_FILE)));
try {
// Try to create a business object data instance.
businessObjectDataService.createBusinessObjectData(request);
fail("Should throw an AlreadyExistsException when a storage file in S3 managed storage is already registered by another business object data.");
} catch (AlreadyExistsException e) {
assertEquals(String.format("Found 1 storage file(s) matching \"%s\" S3 key prefix in \"%s\" storage that is registered with another business object data.", testS3KeyPrefix, StorageEntity.MANAGED_STORAGE), e.getMessage());
}
}
use of org.finra.herd.model.jpa.StorageUnitEntity in project herd by FINRAOS.
the class BusinessObjectDataServiceDestroyBusinessObjectDataTest method testDestroyBusinessObjectData.
@Test
public void testDestroyBusinessObjectData() throws Exception {
// Create a primary partition value that satisfies the retention threshold check.
String primaryPartitionValue = DateFormatUtils.format(DateUtils.addDays(new Date(), -1 * (RETENTION_PERIOD_DAYS + 1)), AbstractHerdDao.DEFAULT_SINGLE_DAY_DATE_MASK);
// Build the expected S3 key prefix for test business object data.
String s3KeyPrefix = getExpectedS3KeyPrefix(BDEF_NAMESPACE, DATA_PROVIDER_NAME, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_KEY, primaryPartitionValue, null, null, DATA_VERSION);
// Create S3FileTransferRequestParamsDto to access the S3 bucket location.
// Since test S3 key prefix represents a directory, we add a trailing '/' character to it.
S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = S3FileTransferRequestParamsDto.builder().withS3BucketName(S3_BUCKET_NAME).withS3KeyPrefix(s3KeyPrefix + "/").build();
// Create an S3 storage with the relative attributes.
storageDaoTestHelper.createStorageEntity(STORAGE_NAME, StoragePlatformEntity.S3, Arrays.asList(new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_BUCKET_NAME), AbstractServiceTest.S3_BUCKET_NAME), new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KEY_PREFIX_VELOCITY_TEMPLATE), AbstractServiceTest.S3_KEY_PREFIX_VELOCITY_TEMPLATE), new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_VALIDATE_PATH_PREFIX), Boolean.TRUE.toString()), new Attribute(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_VALIDATE_FILE_EXISTENCE), Boolean.TRUE.toString())));
// Create a business object data key.
BusinessObjectDataKey businessObjectDataKey = new BusinessObjectDataKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, primaryPartitionValue, NO_SUBPARTITION_VALUES, DATA_VERSION);
// Create and persist a storage unit in the storage.
StorageUnitEntity storageUnitEntity = storageUnitDaoTestHelper.createStorageUnitEntity(STORAGE_NAME, businessObjectDataKey, LATEST_VERSION_FLAG_SET, BusinessObjectDataStatusEntity.VALID, StorageUnitStatusEntity.ENABLED, NO_STORAGE_DIRECTORY_PATH);
// Add storage files to the storage unit.
for (String filePath : LOCAL_FILES) {
storageFileDaoTestHelper.createStorageFileEntity(storageUnitEntity, s3KeyPrefix + "/" + filePath, FILE_SIZE_1_KB, ROW_COUNT_1000);
}
// Get the storage files.
List<StorageFile> storageFiles = storageFileHelper.createStorageFilesFromEntities(storageUnitEntity.getStorageFiles());
// Get the business object format entity.
BusinessObjectFormatEntity businessObjectFormatEntity = storageUnitEntity.getBusinessObjectData().getBusinessObjectFormat();
// Set the retention information for the business object format, which is the latest version business object format.
businessObjectFormatEntity.setRetentionType(retentionTypeDao.getRetentionTypeByCode(RetentionTypeEntity.PARTITION_VALUE));
businessObjectFormatEntity.setRetentionPeriodInDays(RETENTION_PERIOD_DAYS);
businessObjectFormatDao.saveAndRefresh(businessObjectFormatEntity);
// Override configuration to specify some settings required for testing.
Map<String, Object> overrideMap = new HashMap<>();
overrideMap.put(ConfigurationValue.S3_OBJECT_DELETE_TAG_KEY.getKey(), S3_OBJECT_TAG_KEY);
overrideMap.put(ConfigurationValue.S3_OBJECT_DELETE_TAG_VALUE.getKey(), S3_OBJECT_TAG_VALUE);
overrideMap.put(ConfigurationValue.S3_OBJECT_DELETE_ROLE_ARN.getKey(), S3_OBJECT_TAGGER_ROLE_ARN);
overrideMap.put(ConfigurationValue.S3_OBJECT_DELETE_ROLE_SESSION_NAME.getKey(), S3_OBJECT_TAGGER_ROLE_SESSION_NAME);
modifyPropertySourceInEnvironment(overrideMap);
try {
// Put relative S3 files into the S3 bucket.
for (StorageFile storageFile : storageFiles) {
s3Operations.putObject(new PutObjectRequest(S3_BUCKET_NAME, storageFile.getFilePath(), new ByteArrayInputStream(new byte[storageFile.getFileSizeBytes().intValue()]), null), null);
}
// Request to destroy business object data.
BusinessObjectData result = businessObjectDataService.destroyBusinessObjectData(businessObjectDataKey);
// Validate the result.
assertNotNull(result);
assertEquals(storageUnitEntity.getBusinessObjectData().getId(), Integer.valueOf(result.getId()));
// Validate the status of the storage unit entity.
assertEquals(StorageUnitStatusEntity.DISABLED, storageUnitEntity.getStatus().getCode());
// Validate the status of the business object data entity.
assertEquals(BusinessObjectDataStatusEntity.DELETED, storageUnitEntity.getBusinessObjectData().getStatus().getCode());
// Validate that all S3 files are now tagged.
for (StorageFile storageFile : storageFiles) {
GetObjectTaggingResult getObjectTaggingResult = s3Operations.getObjectTagging(new GetObjectTaggingRequest(S3_BUCKET_NAME, storageFile.getFilePath()), null);
assertEquals(Arrays.asList(new Tag(S3_OBJECT_TAG_KEY, S3_OBJECT_TAG_VALUE)), getObjectTaggingResult.getTagSet());
}
} finally {
// Delete test files from S3 storage.
if (!s3Dao.listDirectory(s3FileTransferRequestParamsDto).isEmpty()) {
s3Dao.deleteDirectory(s3FileTransferRequestParamsDto);
}
s3Operations.rollback();
// Restore the property sources so we don't affect other tests.
restorePropertySourceInEnvironment();
}
}
Aggregations