use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.
the class BusinessObjectDefinitionTagServiceTest method testCreateBusinessObjectDefinitionTagBusinessObjectDefinitionTagAlreadyExists.
@Test
public void testCreateBusinessObjectDefinitionTagBusinessObjectDefinitionTagAlreadyExists() {
// Create a business object definition tag key.
BusinessObjectDefinitionTagKey businessObjectDefinitionTagKey = new BusinessObjectDefinitionTagKey(new BusinessObjectDefinitionKey(BDEF_NAMESPACE, BDEF_NAME), new TagKey(TAG_TYPE, TAG_CODE));
// Create and persist a business object definition tag entity.
businessObjectDefinitionTagDaoTestHelper.createBusinessObjectDefinitionTagEntity(businessObjectDefinitionTagKey);
// Try to add a duplicate business object definition tag.
try {
businessObjectDefinitionTagService.createBusinessObjectDefinitionTag(new BusinessObjectDefinitionTagCreateRequest(businessObjectDefinitionTagKey));
fail();
} catch (AlreadyExistsException e) {
assertEquals(String.format("Tag with tag type \"%s\" and code \"%s\" already exists for business object definition {%s}.", TAG_TYPE, TAG_CODE, businessObjectDefinitionServiceTestHelper.getExpectedBusinessObjectDefinitionKeyAsString(BDEF_NAMESPACE, BDEF_NAME)), e.getMessage());
}
}
use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.
the class DataProviderServiceTest method testCreateDataProviderAlreadyExists.
@Test
public void testCreateDataProviderAlreadyExists() throws Exception {
// Create and persist a data provider.
dataProviderDaoTestHelper.createDataProviderEntity(DATA_PROVIDER_NAME);
// Try to create a data provider when it already exists.
try {
dataProviderService.createDataProvider(new DataProviderCreateRequest(DATA_PROVIDER_NAME));
fail("Should throw an AlreadyExistsException when data provider already exists.");
} catch (AlreadyExistsException e) {
assertEquals(String.format("Unable to create data provider \"%s\" because it already exists.", DATA_PROVIDER_NAME), e.getMessage());
}
}
use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.
the class RelationalTableRegistrationHelperServiceTest method testRegisterRelationalTableBusinessObjectDefinitionAlreadyExists.
@Test
public void testRegisterRelationalTableBusinessObjectDefinitionAlreadyExists() {
// Create a namespace.
namespaceDaoTestHelper.createNamespaceEntity(BDEF_NAMESPACE);
// Create a business object definition.
businessObjectDefinitionDaoTestHelper.createBusinessObjectDefinitionEntity(new BusinessObjectDefinitionKey(BDEF_NAMESPACE, BDEF_NAME), DATA_PROVIDER_NAME, BDEF_DESCRIPTION);
// Try to register a relational table when specified business object definition already exists.
try {
relationalTableRegistrationHelperService.registerRelationalTable(new RelationalTableRegistrationCreateRequest(BDEF_NAMESPACE, BDEF_NAME, BDEF_DISPLAY_NAME, FORMAT_USAGE_CODE, DATA_PROVIDER_NAME, RELATIONAL_SCHEMA_NAME, RELATIONAL_TABLE_NAME, STORAGE_NAME), relationalTableRegistrationServiceTestHelper.getExpectedSchemaColumns(), APPEND_TO_EXISTING_BUSINESS_OBJECT_DEFINTION_FALSE);
fail();
} catch (AlreadyExistsException ex) {
Assert.assertEquals(String.format("Unable to create business object definition with name \"%s\" because it already exists for namespace \"%s\".", BDEF_NAME, BDEF_NAMESPACE), ex.getMessage());
}
}
use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.
the class BusinessObjectDataDaoHelper method createBusinessObjectData.
/**
* Creates a new business object data from the request information.
*
* @param request the request
* @param fileSizeRequired specifies if fileSizeBytes value is required or not
*
* @return the newly created and persisted business object data
*/
public BusinessObjectData createBusinessObjectData(BusinessObjectDataCreateRequest request, boolean fileSizeRequired) {
if (StringUtils.isBlank(request.getStatus())) {
request.setStatus(BusinessObjectDataStatusEntity.VALID);
} else {
request.setStatus(request.getStatus().trim());
}
// Get the status entity if status is specified else set it to VALID
BusinessObjectDataStatusEntity businessObjectDataStatusEntity = businessObjectDataStatusDaoHelper.getBusinessObjectDataStatusEntity(request.getStatus());
// Perform the validation.
validateBusinessObjectDataCreateRequest(request, fileSizeRequired, businessObjectDataStatusEntity);
// Get the business object format for the specified parameters and make sure it exists.
BusinessObjectFormatEntity businessObjectFormatEntity = businessObjectFormatDaoHelper.getBusinessObjectFormatEntity(new BusinessObjectFormatKey(request.getNamespace(), request.getBusinessObjectDefinitionName(), request.getBusinessObjectFormatUsage(), request.getBusinessObjectFormatFileType(), request.getBusinessObjectFormatVersion()));
attributeDaoHelper.validateAttributesAgainstBusinessObjectDataAttributeDefinitions(request.getAttributes(), businessObjectFormatEntity.getAttributeDefinitions());
// Ensure the specified partition key matches what's configured within the business object format.
Assert.isTrue(businessObjectFormatEntity.getPartitionKey().equalsIgnoreCase(request.getPartitionKey()), String.format("Partition key \"%s\" doesn't match configured business object format partition key \"%s\".", request.getPartitionKey(), businessObjectFormatEntity.getPartitionKey()));
// Get the latest format version for this business object data, if it exists.
BusinessObjectDataEntity existingBusinessObjectDataEntity = businessObjectDataDao.getBusinessObjectDataByAltKey(new BusinessObjectDataKey(request.getNamespace(), request.getBusinessObjectDefinitionName(), request.getBusinessObjectFormatUsage(), request.getBusinessObjectFormatFileType(), request.getBusinessObjectFormatVersion(), request.getPartitionValue(), request.getSubPartitionValues(), null));
// Throw an error if this business object data already exists and createNewVersion flag is not set.
if (existingBusinessObjectDataEntity != null && (!Boolean.TRUE.equals(request.isCreateNewVersion()) || Boolean.TRUE.equals(existingBusinessObjectDataEntity.getStatus().getPreRegistrationStatus()))) {
throw new AlreadyExistsException("Unable to create business object data because it already exists.");
}
// Create a business object data entity from the request information.
// Please note that simply adding 1 to the latest version without "DB locking" is sufficient here,
// even for multi-threading, since we are relying on the DB having version as part of the alternate key.
Integer businessObjectDataVersion = existingBusinessObjectDataEntity == null ? BusinessObjectDataEntity.BUSINESS_OBJECT_DATA_INITIAL_VERSION : existingBusinessObjectDataEntity.getVersion() + 1;
BusinessObjectDataEntity newVersionBusinessObjectDataEntity = createBusinessObjectDataEntity(request, businessObjectFormatEntity, businessObjectDataVersion, businessObjectDataStatusEntity);
// Update the existing latest business object data version entity, so it would not be flagged as the latest version anymore.
if (existingBusinessObjectDataEntity != null) {
existingBusinessObjectDataEntity.setLatestVersion(Boolean.FALSE);
businessObjectDataDao.saveAndRefresh(existingBusinessObjectDataEntity);
}
// Add an entry to the business object data status history table.
BusinessObjectDataStatusHistoryEntity businessObjectDataStatusHistoryEntity = new BusinessObjectDataStatusHistoryEntity();
businessObjectDataStatusHistoryEntity.setBusinessObjectData(newVersionBusinessObjectDataEntity);
businessObjectDataStatusHistoryEntity.setStatus(businessObjectDataStatusEntity);
List<BusinessObjectDataStatusHistoryEntity> businessObjectDataStatusHistoryEntities = new ArrayList<>();
businessObjectDataStatusHistoryEntities.add(businessObjectDataStatusHistoryEntity);
newVersionBusinessObjectDataEntity.setHistoricalStatuses(businessObjectDataStatusHistoryEntities);
// Persist the new entity.
newVersionBusinessObjectDataEntity = businessObjectDataDao.saveAndRefresh(newVersionBusinessObjectDataEntity);
// Create a status change notification to be sent on create business object data event.
messageNotificationEventService.processBusinessObjectDataStatusChangeNotificationEvent(businessObjectDataHelper.getBusinessObjectDataKey(newVersionBusinessObjectDataEntity), businessObjectDataStatusEntity.getCode(), null);
// Create and return the business object data object from the persisted entity.
return businessObjectDataHelper.createBusinessObjectDataFromEntity(newVersionBusinessObjectDataEntity);
}
use of org.finra.herd.model.AlreadyExistsException in project herd by FINRAOS.
the class BusinessObjectDataDaoHelper method createStorageFileEntitiesFromStorageFiles.
/**
* Creates a list of storage file entities from a list of storage files.
*
* @param storageFiles the list of storage files
* @param storageEntity the storage entity
* @param storageFilesDiscovered specifies whether the storage files were actually discovered in the storage
* @param expectedS3KeyPrefix the expected S3 key prefix
* @param storageUnitEntity the storage unit entity that storage file entities will belong to
* @param directoryPath the storage directory path
* @param validatePathPrefix specifies whether the storage has S3 key prefix validation enabled
* @param validateFileExistence specifies whether the storage has file existence enabled
* @param validateFileSize specifies whether the storage has file validation enabled
* @param isS3StoragePlatform specifies whether the storage platform type is S3
*
* @return the list of storage file entities
*/
private List<StorageFileEntity> createStorageFileEntitiesFromStorageFiles(List<StorageFile> storageFiles, StorageEntity storageEntity, boolean storageFilesDiscovered, String expectedS3KeyPrefix, StorageUnitEntity storageUnitEntity, String directoryPath, boolean validatePathPrefix, boolean validateFileExistence, boolean validateFileSize, boolean isS3StoragePlatform) {
List<StorageFileEntity> storageFileEntities = null;
// Process storage files if they are specified.
if (CollectionUtils.isNotEmpty(storageFiles)) {
storageFileEntities = new ArrayList<>();
storageUnitEntity.setStorageFiles(storageFileEntities);
// If the validate file existence flag is configured for this storage and storage files were not discovered, prepare for S3 file validation.
S3FileTransferRequestParamsDto params = null;
Map<String, StorageFile> actualS3Keys = null;
if (validateFileExistence && isS3StoragePlatform && !storageFilesDiscovered) {
// Get the validate file parameters.
params = getFileValidationParams(storageEntity, expectedS3KeyPrefix, storageUnitEntity, validatePathPrefix);
// When listing S3 files, we ignore 0 byte objects that represent S3 directories.
actualS3Keys = storageFileHelper.getStorageFilesMapFromS3ObjectSummaries(s3Service.listDirectory(params, true));
}
// storage by some other business object data that start with the expected S3 key prefix.
if (validatePathPrefix && isS3StoragePlatform) {
// Since the S3 key prefix represents a directory, we add a trailing '/' character to it.
String expectedS3KeyPrefixWithTrailingSlash = expectedS3KeyPrefix + "/";
Long registeredStorageFileCount = storageFileDao.getStorageFileCount(storageEntity.getName(), expectedS3KeyPrefixWithTrailingSlash);
if (registeredStorageFileCount > 0) {
throw new AlreadyExistsException(String.format("Found %d storage file(s) matching \"%s\" S3 key prefix in \"%s\" " + "storage that is registered with another business object data.", registeredStorageFileCount, expectedS3KeyPrefix, storageEntity.getName()));
}
}
for (StorageFile storageFile : storageFiles) {
StorageFileEntity storageFileEntity = new StorageFileEntity();
storageFileEntities.add(storageFileEntity);
storageFileEntity.setStorageUnit(storageUnitEntity);
storageFileEntity.setPath(storageFile.getFilePath());
storageFileEntity.setFileSizeBytes(storageFile.getFileSizeBytes());
storageFileEntity.setRowCount(storageFile.getRowCount());
// Skip storage file validation if storage files were discovered.
if (!storageFilesDiscovered) {
// Otherwise, if a directory path is specified, ensure it is consistent with the file path.
if (validatePathPrefix && isS3StoragePlatform) {
// Ensure the S3 file key prefix adheres to the S3 naming convention.
Assert.isTrue(storageFileEntity.getPath().startsWith(expectedS3KeyPrefix), String.format("Specified storage file path \"%s\" does not match the expected S3 key prefix \"%s\".", storageFileEntity.getPath(), expectedS3KeyPrefix));
} else if (directoryPath != null) {
// When storage directory path is specified, ensure that storage file path starts with it.
Assert.isTrue(storageFileEntity.getPath().startsWith(directoryPath), String.format("Storage file path \"%s\" does not match the storage directory path \"%s\".", storageFileEntity.getPath(), directoryPath));
}
// Ensure the file exists in S3 if the validate file existence flag is configured for this storage.
if (validateFileExistence && isS3StoragePlatform) {
// Validate storage file.
storageFileHelper.validateStorageFile(storageFile, params.getS3BucketName(), actualS3Keys, validateFileSize);
}
}
}
}
return storageFileEntities;
}
Aggregations