@Override public StorageUnitEntity getStorageUnitByBusinessObjectDataAndStorage(BusinessObjectDataEntity businessObjectDataEntity, StorageEntity storageEntity) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class); // The criteria root is the storage unit. Root<StorageUnitEntity> storageUnitEntityRoot = criteria.from(StorageUnitEntity.class); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(storageUnitEntityRoot.get(StorageUnitEntity_.businessObjectData), businessObjectDataEntity)); predicates.add(builder.equal(storageUnitEntityRoot.get(StorageUnitEntity_.storageName), storageEntity.getName())); // Add the clauses for the query. criteria.select(storageUnitEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))); // Execute the query. List<StorageUnitEntity> resultList = entityManager.createQuery(criteria).getResultList(); // Return single result or null. return resultList.size() >= 1 ? resultList.get(0) : null; }
storageEntity = new StorageEntity(); storageEntity.setName(storageCreateRequest.getName()); storageEntity.setStoragePlatform(storagePlatformEntity); storageEntity.setAttributes(attributeEntities); for (Attribute attribute : storageCreateRequest.getAttributes())
/** * Creates a storage from it's entity object. * * @param storageEntity the storage entity * * @return the storage */ private Storage createStorageFromEntity(StorageEntity storageEntity) { // Create a list of attributes. List<Attribute> attributes = new ArrayList<>(); for (StorageAttributeEntity attributeEntity : storageEntity.getAttributes()) { attributes.add(new Attribute(attributeEntity.getName(), attributeEntity.getValue())); } return new Storage(storageEntity.getName(), storageEntity.getStoragePlatform().getName(), attributes); }
/** * Returns the S3 object key prefix based on the given storage, business object format and business object data key. * * @param storageEntity the storage entity * @param businessObjectFormatEntity the business object format entity * @param businessObjectDataKey the business object data key * * @return the S3 key prefix */ public String buildS3KeyPrefix(StorageEntity storageEntity, BusinessObjectFormatEntity businessObjectFormatEntity, BusinessObjectDataKey businessObjectDataKey) { // Validate that storage platform is of "S3" storage platform type. Assert.isTrue(StoragePlatformEntity.S3.equals(storageEntity.getStoragePlatform().getName()), String.format("The specified storage \"%s\" is not an S3 storage platform.", storageEntity.getName())); // Retrieve S3 key prefix velocity template storage attribute value and store it in memory. // Please note that it is not required, so we pass in a "false" flag. String s3KeyPrefixVelocityTemplate = storageHelper .getStorageAttributeValueByName(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KEY_PREFIX_VELOCITY_TEMPLATE), storageEntity, false); // Validate that S3 key prefix velocity template is configured. Assert.isTrue(StringUtils.isNotBlank(s3KeyPrefixVelocityTemplate), String.format("Storage \"%s\" has no S3 key prefix velocity template configured.", storageEntity.getName())); return buildS3KeyPrefix(s3KeyPrefixVelocityTemplate, businessObjectFormatEntity, businessObjectDataKey, storageEntity.getName()); }
String attributeValue = null; for (StorageAttributeEntity attributeEntity : storageEntity.getAttributes()) throw new IllegalStateException(String.format("Attribute \"%s\" for \"%s\" storage must be configured.", attributeName, storageEntity.getName())); String.format("Attribute \"%s\" for \"%s\" storage must have a value that is not blank.", attributeName, storageEntity.getName()));
@Test public void testValidateStorageValidatePathPrefixNotEnabled() { // Create a storage entity without any attributes. StorageEntity storageEntity = new StorageEntity(); storageEntity.setName(STORAGE_NAME); // Mock the external calls. when(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_VALIDATE_PATH_PREFIX)).thenReturn(S3_ATTRIBUTE_NAME_VALIDATE_PATH_PREFIX); when(storageHelper.getBooleanStorageAttributeValueByName(S3_ATTRIBUTE_NAME_VALIDATE_PATH_PREFIX, storageEntity, false, true)).thenReturn(false); // Try to call the method under test. try { businessObjectDataInitiateDestroyHelperServiceImpl.validateStorage(storageEntity); fail(); } catch (IllegalStateException e) { assertEquals(String.format("Path prefix validation must be enabled on \"%s\" storage.", STORAGE_NAME), e.getMessage()); } // Verify the external calls. verify(configurationHelper).getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_VALIDATE_PATH_PREFIX); verify(storageHelper).getBooleanStorageAttributeValueByName(S3_ATTRIBUTE_NAME_VALIDATE_PATH_PREFIX, storageEntity, false, true); verifyNoMoreInteractionsHelper(); }
StorageEntity storageEntity = new StorageEntity(); storageEntity.setStoragePlatform(storagePlatformEntity); storageEntity.setName(STORAGE_NAME);
StorageEntity storageEntity = new StorageEntity(); storageEntity.setName(STORAGE_NAME); StorageUnitEntity storageUnitEntity = new StorageUnitEntity(); storageUnitEntity.setStorage(storageEntity); .format("Storage file \"%s\" doesn't exist in \"%s\" storage. Business object data: {%s}", filePath, storageUnitEntity.getStorage().getName(), businessObjectDataHelper.businessObjectDataKeyToString(businessObjectDataKey))));
for (StorageAttributeEntity attributeEntity : storageEntity.getAttributes()) storageEntity.getAttributes().add(attributeEntity); attributeEntity.setStorage(storageEntity); attributeEntity.setName(attribute.getName()); storageEntity.getAttributes().retainAll(retainedAttributeEntities); storageEntity.getAttributes().addAll(createdAttributeEntities);
StorageEntity storageEntity = new StorageEntity(); storageEntity.setStoragePlatform(storagePlatformEntity); StorageUnitEntity storageUnitEntity1 = new StorageUnitEntity(); storageUnitEntity1.setStorage(storageEntity);
@Test public void testGetStorageUnitEntityByBusinessObjectDataAndStorageName() { // Create a business object data entity. BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); // Create a storage unit entity. StorageUnitEntity storageUnitEntity = new StorageUnitEntity(); // Create a storage entity StorageEntity storageEntity = new StorageEntity(); // Mock the external calls. when(storageDao.getStorageByName(STORAGE_NAME)).thenReturn(storageEntity); when(storageUnitDao.getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity)).thenReturn(storageUnitEntity); // Call the method under test. StorageUnitEntity result = storageUnitDaoHelper.getStorageUnitEntity(STORAGE_NAME, businessObjectDataEntity); // Validate the results. assertEquals(storageUnitEntity, result); // Verify the external calls. verify(storageDao).getStorageByName(STORAGE_NAME); verify(storageUnitDao).getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); verifyNoMoreInteractionsHelper(); }
/** * Discovers storage files in the specified S3 storage that match the S3 key prefix. * * @param storageEntity the storage entity * @param s3KeyPrefix the S3 key prefix * * @return the list of discovered storage files */ private List<StorageFile> discoverStorageFiles(StorageEntity storageEntity, String s3KeyPrefix) { // Only S3 storage platform is currently supported for storage file discovery. Assert.isTrue(storageEntity.getStoragePlatform().getName().equals(StoragePlatformEntity.S3), String.format("Cannot discover storage files at \"%s\" storage platform.", storageEntity.getStoragePlatform().getName())); // Get S3 bucket access parameters. S3FileTransferRequestParamsDto params = storageHelper.getS3BucketAccessParams(storageEntity); // Retrieve a list of all keys/objects from the S3 bucket matching the specified S3 key prefix. // Since S3 key prefix represents the directory, we add a trailing '/' character to it, unless it is already present. params.setS3KeyPrefix(StringUtils.appendIfMissing(s3KeyPrefix, "/")); // When listing S3 files, we ignore 0 byte objects that represent S3 directories. List<S3ObjectSummary> s3ObjectSummaries = s3Service.listDirectory(params, true); // Fail registration if no storage files were discovered. if (CollectionUtils.isEmpty(s3ObjectSummaries)) { throw new ObjectNotFoundException(String.format("Found no files at \"s3://%s/%s\" location.", params.getS3BucketName(), params.getS3KeyPrefix())); } return storageFileHelper.createStorageFilesFromS3ObjectSummaries(s3ObjectSummaries); }
@Test public void testGetStorageUnitEntityByBusinessObjectDataAndStorageStorageUnitNoExists() { // Create a business object data entity. BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); // Create a storage entity. StorageEntity storageEntity = new StorageEntity(); storageEntity.setName(STORAGE_NAME); // Mock the external calls. when(storageUnitDao.getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity)).thenReturn(null); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); // Try to call the method under test. try { storageUnitDaoHelper.getStorageUnitEntityByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); fail(); } catch (ObjectNotFoundException e) { assertEquals(String .format("Could not find storage unit in \"%s\" storage for the business object data {%s}.", STORAGE_NAME, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } // Verify the external calls. verify(storageUnitDao).getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
/** * Returns the S3 object key prefix based on the given storage, business object format and business object data key. * * @param storageEntity the storage entity * @param businessObjectFormatEntity the business object format entity * @param businessObjectDataKey the business object data key * * @return the S3 key prefix */ public String buildS3KeyPrefix(StorageEntity storageEntity, BusinessObjectFormatEntity businessObjectFormatEntity, BusinessObjectDataKey businessObjectDataKey) { // Validate that storage platform is of "S3" storage platform type. Assert.isTrue(StoragePlatformEntity.S3.equals(storageEntity.getStoragePlatform().getName()), String.format("The specified storage \"%s\" is not an S3 storage platform.", storageEntity.getName())); // Retrieve S3 key prefix velocity template storage attribute value and store it in memory. // Please note that it is not required, so we pass in a "false" flag. String s3KeyPrefixVelocityTemplate = storageHelper .getStorageAttributeValueByName(configurationHelper.getProperty(ConfigurationValue.S3_ATTRIBUTE_NAME_KEY_PREFIX_VELOCITY_TEMPLATE), storageEntity, false); // Validate that S3 key prefix velocity template is configured. Assert.isTrue(StringUtils.isNotBlank(s3KeyPrefixVelocityTemplate), String.format("Storage \"%s\" has no S3 key prefix velocity template configured.", storageEntity.getName())); return buildS3KeyPrefix(s3KeyPrefixVelocityTemplate, businessObjectFormatEntity, businessObjectDataKey, storageEntity.getName()); }
StorageEntity storageEntity = new StorageEntity(); storageEntity.setStoragePlatform(storagePlatformEntity); storageEntity.setName(STORAGE_NAME);
String attributeValue = null; for (StorageAttributeEntity attributeEntity : storageEntity.getAttributes()) throw new IllegalStateException(String.format("Attribute \"%s\" for \"%s\" storage must be configured.", attributeName, storageEntity.getName())); String.format("Attribute \"%s\" for \"%s\" storage must have a value that is not blank.", attributeName, storageEntity.getName()));
for (StorageAttributeEntity attributeEntity : storageEntity.getAttributes()) storageEntity.getAttributes().add(attributeEntity); attributeEntity.setStorage(storageEntity); attributeEntity.setName(attribute.getName()); storageEntity.getAttributes().retainAll(retainedAttributeEntities); storageEntity.getAttributes().addAll(createdAttributeEntities);
@Test public void testGetStorageUnitEntityByBusinessObjectDataAndStorageNameStorageUnitNoExists() { // Create a business object data entity. BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); // Create a storage entity StorageEntity storageEntity = new StorageEntity(); // Mock the external calls. when(storageDao.getStorageByName(STORAGE_NAME)).thenReturn(storageEntity); when(storageUnitDao.getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity)).thenReturn(null); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); // Try to call the method under test. try { storageUnitDaoHelper.getStorageUnitEntity(STORAGE_NAME, businessObjectDataEntity); fail(); } catch (ObjectNotFoundException e) { assertEquals(String .format("Could not find storage unit in \"%s\" storage for the business object data {%s}.", STORAGE_NAME, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } // Verify the external calls. verify(storageDao).getStorageByName(STORAGE_NAME); verify(storageUnitDao).getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
/** * Discovers storage files in the specified S3 storage that match the S3 key prefix. * * @param storageEntity the storage entity * @param s3KeyPrefix the S3 key prefix * * @return the list of discovered storage files */ private List<StorageFile> discoverStorageFiles(StorageEntity storageEntity, String s3KeyPrefix) { // Only S3 storage platform is currently supported for storage file discovery. Assert.isTrue(storageEntity.getStoragePlatform().getName().equals(StoragePlatformEntity.S3), String.format("Cannot discover storage files at \"%s\" storage platform.", storageEntity.getStoragePlatform().getName())); // Get S3 bucket access parameters. S3FileTransferRequestParamsDto params = storageHelper.getS3BucketAccessParams(storageEntity); // Retrieve a list of all keys/objects from the S3 bucket matching the specified S3 key prefix. // Since S3 key prefix represents the directory, we add a trailing '/' character to it, unless it is already present. params.setS3KeyPrefix(StringUtils.appendIfMissing(s3KeyPrefix, "/")); // When listing S3 files, we ignore 0 byte objects that represent S3 directories. List<S3ObjectSummary> s3ObjectSummaries = s3Service.listDirectory(params, true); // Fail registration if no storage files were discovered. if (CollectionUtils.isEmpty(s3ObjectSummaries)) { throw new ObjectNotFoundException(String.format("Found no files at \"s3://%s/%s\" location.", params.getS3BucketName(), params.getS3KeyPrefix())); } return storageFileHelper.createStorageFilesFromS3ObjectSummaries(s3ObjectSummaries); }
@Override public StorageUnitEntity getStorageUnitByBusinessObjectDataAndStorage(BusinessObjectDataEntity businessObjectDataEntity, StorageEntity storageEntity) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<StorageUnitEntity> criteria = builder.createQuery(StorageUnitEntity.class); // The criteria root is the storage unit. Root<StorageUnitEntity> storageUnitEntityRoot = criteria.from(StorageUnitEntity.class); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(storageUnitEntityRoot.get(StorageUnitEntity_.businessObjectData), businessObjectDataEntity)); predicates.add(builder.equal(storageUnitEntityRoot.get(StorageUnitEntity_.storageName), storageEntity.getName())); // Add the clauses for the query. criteria.select(storageUnitEntityRoot).where(builder.and(predicates.toArray(new Predicate[predicates.size()]))); // Execute the query. List<StorageUnitEntity> resultList = entityManager.createQuery(criteria).getResultList(); // Return single result or null. return resultList.size() >= 1 ? resultList.get(0) : null; }