/** * Validates a list of storage file paths. This method makes sure that all storage file paths match the expected s3 key prefix value. * * @param storageFilePaths the storage file paths to be validated * @param s3KeyPrefix the S3 key prefix that storage file paths are expected to start with * @param businessObjectDataEntity the business object data entity * @param storageName the name of the storage that storage files are stored in */ public void validateStorageFilePaths(Collection<String> storageFilePaths, String s3KeyPrefix, BusinessObjectDataEntity businessObjectDataEntity, String storageName) { for (String storageFilePath : storageFilePaths) { Assert.isTrue(storageFilePath.startsWith(s3KeyPrefix), String .format("Storage file \"%s\" registered with business object data {%s} in \"%s\" storage does not match the expected S3 key prefix \"%s\".", storageFilePath, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity), storageName, s3KeyPrefix)); } }
/** * Validates a list of storage file paths. This method makes sure that all storage file paths match the expected s3 key prefix value. * * @param storageFilePaths the storage file paths to be validated * @param s3KeyPrefix the S3 key prefix that storage file paths are expected to start with * @param businessObjectDataEntity the business object data entity * @param storageName the name of the storage that storage files are stored in */ public void validateStorageFilePaths(Collection<String> storageFilePaths, String s3KeyPrefix, BusinessObjectDataEntity businessObjectDataEntity, String storageName) { for (String storageFilePath : storageFilePaths) { Assert.isTrue(storageFilePath.startsWith(s3KeyPrefix), String .format("Storage file \"%s\" registered with business object data {%s} in \"%s\" storage does not match the expected S3 key prefix \"%s\".", storageFilePath, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity), storageName, s3KeyPrefix)); } }
/** * Asserts that the status of the given data is equal to the given expected value. * * @param expectedBusinessObjectDataStatusCode - the expected status * @param businessObjectDataEntity - the data entity * * @throws IllegalArgumentException when status does not equal */ public void assertBusinessObjectDataStatusEquals(String expectedBusinessObjectDataStatusCode, BusinessObjectDataEntity businessObjectDataEntity) throws IllegalArgumentException { String businessObjectDataStatusCode = businessObjectDataEntity.getStatus().getCode(); Assert.isTrue(expectedBusinessObjectDataStatusCode.equals(businessObjectDataStatusCode), String .format("Business object data status \"%s\" does not match the expected status \"%s\" for the business object data {%s}.", businessObjectDataStatusCode, expectedBusinessObjectDataStatusCode, businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); }
/** * Asserts that the status of the given data is equal to the given expected value. * * @param expectedBusinessObjectDataStatusCode - the expected status * @param businessObjectDataEntity - the data entity * * @throws IllegalArgumentException when status does not equal */ public void assertBusinessObjectDataStatusEquals(String expectedBusinessObjectDataStatusCode, BusinessObjectDataEntity businessObjectDataEntity) throws IllegalArgumentException { String businessObjectDataStatusCode = businessObjectDataEntity.getStatus().getCode(); Assert.isTrue(expectedBusinessObjectDataStatusCode.equals(businessObjectDataStatusCode), String .format("Business object data status \"%s\" does not match the expected status \"%s\" for the business object data {%s}.", businessObjectDataStatusCode, expectedBusinessObjectDataStatusCode, businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); }
/** * Asserts that the given storage unit entity contains exactly one storage file. * * @param storageUnitEntity - storage unit to check * * @throws IllegalArgumentException when the number of storage files is not 1 */ private void assertHasOneStorageFile(StorageUnitEntity storageUnitEntity) { Assert.isTrue(storageUnitEntity.getStorageFiles().size() == 1, String .format("Found %d registered storage files when expecting one in \"%s\" storage for the business object data {%s}.", storageUnitEntity.getStorageFiles().size(), storageUnitEntity.getStorage().getName(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageUnitEntity.getBusinessObjectData()))); }
/** * Asserts that the given storage unit entity contains exactly one storage file. * * @param storageUnitEntity - storage unit to check * * @throws IllegalArgumentException when the number of storage files is not 1 */ private void assertHasOneStorageFile(StorageUnitEntity storageUnitEntity) { Assert.isTrue(storageUnitEntity.getStorageFiles().size() == 1, String .format("Found %d registered storage files when expecting one in \"%s\" storage for the business object data {%s}.", storageUnitEntity.getStorageFiles().size(), storageUnitEntity.getStorage().getName(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(storageUnitEntity.getBusinessObjectData()))); }
/** * Retrieves a storage unit for the business object data in the specified storage and validates it. * * @param storageName the storage name * @param businessObjectDataEntity the business object data entity * * @return the storage unit entity */ protected StorageUnitEntity getStorageUnit(String storageName, BusinessObjectDataEntity businessObjectDataEntity) { // Get the storage unit and make sure it exists. StorageUnitEntity storageUnitEntity = storageUnitDaoHelper.getStorageUnitEntity(storageName, businessObjectDataEntity); // Get the storage unit status. String storageUnitStatus = storageUnitEntity.getStatus().getCode(); // Validate that S3 storage unit is in RESTORED state. if (!StorageUnitStatusEntity.RESTORED.equals(storageUnitStatus)) { throw new IllegalArgumentException(String .format("S3 storage unit in \"%s\" storage must have \"%s\" status, but it actually has \"%s\" status. Business object data: {%s}", storageName, StorageUnitStatusEntity.RESTORED, storageUnitStatus, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } return storageUnitEntity; }
/** * Retrieves a storage unit for the business object data in the specified storage and validates it. * * @param storageName the storage name * @param businessObjectDataEntity the business object data entity * * @return the storage unit entity */ protected StorageUnitEntity getStorageUnit(String storageName, BusinessObjectDataEntity businessObjectDataEntity) { // Get the storage unit and make sure it exists. StorageUnitEntity storageUnitEntity = storageUnitDaoHelper.getStorageUnitEntity(storageName, businessObjectDataEntity); // Get the storage unit status. String storageUnitStatus = storageUnitEntity.getStatus().getCode(); // Validate that S3 storage unit is in RESTORED state. if (!StorageUnitStatusEntity.RESTORED.equals(storageUnitStatus)) { throw new IllegalArgumentException(String .format("S3 storage unit in \"%s\" storage must have \"%s\" status, but it actually has \"%s\" status. Business object data: {%s}", storageName, StorageUnitStatusEntity.RESTORED, storageUnitStatus, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } return storageUnitEntity; }
/** * Retrieves a storage unit entity for the business object data in the specified storage and make sure it exists. * * @param businessObjectDataEntity the business object data entity * @param storageEntity the storage entity * * @return the storage unit entity */ public StorageUnitEntity getStorageUnitEntityByBusinessObjectDataAndStorage(BusinessObjectDataEntity businessObjectDataEntity, StorageEntity storageEntity) { StorageUnitEntity storageUnitEntity = storageUnitDao.getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); if (storageUnitEntity == null) { throw new ObjectNotFoundException(String .format("Could not find storage unit in \"%s\" storage for the business object data {%s}.", storageEntity.getName(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } return storageUnitEntity; }
/** * Retrieves a storage unit entity for the business object data in the specified storage and make sure it exists. * * @param businessObjectDataEntity the business object data entity * @param storageEntity the storage entity * * @return the storage unit entity */ public StorageUnitEntity getStorageUnitEntityByBusinessObjectDataAndStorage(BusinessObjectDataEntity businessObjectDataEntity, StorageEntity storageEntity) { StorageUnitEntity storageUnitEntity = storageUnitDao.getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); if (storageUnitEntity == null) { throw new ObjectNotFoundException(String .format("Could not find storage unit in \"%s\" storage for the business object data {%s}.", storageEntity.getName(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } return storageUnitEntity; }
/** * Retrieves and validates the storage unit for the specified business object data. * * @param businessObjectDataEntity the business object data entity * @param storageEntity the storage entity * * @return the storage unit entity */ private StorageUnitEntity getStorageUnit(BusinessObjectDataEntity businessObjectDataEntity, StorageEntity storageEntity) { // Retrieve and validate storage unit for the business object data. StorageUnitEntity storageUnitEntity = storageUnitDao.getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); // Validate the storage unit. if (storageUnitEntity == null) { throw new IllegalArgumentException(String .format("Business object data has no storage unit in \"%s\" storage. Business object data: {%s}", storageEntity.getName(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } else { // Validate that storage unit is in "ARCHIVING" state. Assert.isTrue(StorageUnitStatusEntity.ARCHIVING.equals(storageUnitEntity.getStatus().getCode()), String.format( "Business object data is not currently being archived. " + "Storage unit in \"%s\" storage must have \"%s\" status, but it actually has \"%s\" status. Business object data: {%s}", storageEntity.getName(), StorageUnitStatusEntity.ARCHIVING, storageUnitEntity.getStatus().getCode(), businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } return storageUnitEntity; }
/** * Retrieves a storage unit entity for the business object data in the specified storage and make sure it exists. * * @param storageName the storage name * @param businessObjectDataEntity the business object data entity * * @return the storage unit entity */ public StorageUnitEntity getStorageUnitEntity(String storageName, BusinessObjectDataEntity businessObjectDataEntity) { StorageUnitEntity storageUnitEntity = null; StorageEntity storageEntity = storageDao.getStorageByName(storageName); // If the storage entity does exist get the storage unit entity. if (storageEntity != null) { storageUnitEntity = storageUnitDao.getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); } // If the storage unit entity was not found return object not found exception. if (storageUnitEntity == null) { throw new ObjectNotFoundException(String.format("Could not find storage unit in \"%s\" storage for the business object data {%s}.", storageName, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } return storageUnitEntity; }
/** * Retrieves a storage unit entity for the business object data in the specified storage and make sure it exists. * * @param storageName the storage name * @param businessObjectDataEntity the business object data entity * * @return the storage unit entity */ public StorageUnitEntity getStorageUnitEntity(String storageName, BusinessObjectDataEntity businessObjectDataEntity) { StorageUnitEntity storageUnitEntity = null; StorageEntity storageEntity = storageDao.getStorageByName(storageName); // If the storage entity does exist get the storage unit entity. if (storageEntity != null) { storageUnitEntity = storageUnitDao.getStorageUnitByBusinessObjectDataAndStorage(businessObjectDataEntity, storageEntity); } // If the storage unit entity was not found return object not found exception. if (storageUnitEntity == null) { throw new ObjectNotFoundException(String.format("Could not find storage unit in \"%s\" storage for the business object data {%s}.", storageName, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity))); } return storageUnitEntity; }
@Test public void testGetStorageUnitStorageUnitNoExists() { // Create a business object data entity. BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); // Mock the external calls. when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)) .thenReturn(new ArrayList<>()); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); // Try to call the method under test. 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 the external calls. verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
@Test public void testGetStorageUnitMultipleStorageUnitsExist() { // Create a business object data entity. BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); // Create a list of storage unit entities. List<StorageUnitEntity> storageUnitEntities = Arrays.asList(new StorageUnitEntity(), new StorageUnitEntity()); // Mock the external calls. when(storageUnitDao.getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity)) .thenReturn(storageUnitEntities); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); // Try to call the method under test. 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 the external calls. verify(storageUnitDao).getStorageUnitsByStoragePlatformAndBusinessObjectData(StoragePlatformEntity.S3, businessObjectDataEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
@Test public void testGetHivePartitionsMultiplePathsFound() { // Create a test business object data entity. BusinessObjectDataEntity businessObjectDataEntity = businessObjectDataDaoTestHelper .createBusinessObjectDataEntity(NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, DATA_VERSION, true, BDATA_STATUS); List<SchemaColumn> autoDiscoverableSubPartitionColumns = getPartitionColumns(Arrays.asList("Column1", "column2")); List<String> partitionPaths = Arrays.asList("/COLUMN1=111/COLUMN2=222", "/column1=111/COLUMN2=222"); List<String> storageFilePaths = getStorageFilePaths(Arrays.asList(partitionPaths.get(0) + "/file.dat", partitionPaths.get(1) + "/file.dat")); try { hive13DdlGenerator .getHivePartitions(businessObjectDataHelper.getBusinessObjectDataKey(businessObjectDataEntity), autoDiscoverableSubPartitionColumns, TEST_S3_KEY_PREFIX, storageFilePaths, businessObjectDataEntity, STORAGE_NAME); fail("Should throw an IllegalArgumentException when multiple locations exist for the same Hive partition."); } catch (IllegalArgumentException e) { assertEquals(String.format("Found two different locations for the same Hive partition. " + "Storage: {%s}, business object data: {%s}, S3 key prefix: {%s}, path[1]: {%s}, path[2]: {%s}", STORAGE_NAME, businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity), TEST_S3_KEY_PREFIX, partitionPaths.get(0), partitionPaths.get(1)), e.getMessage()); } }
@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(); }
@Test public void testGetStorageUnitStorageUnitNotRestored() { // Create a business object data entity. BusinessObjectDataEntity businessObjectDataEntity = new BusinessObjectDataEntity(); // Create a storage unit status entity. StorageUnitStatusEntity storageUnitStatusEntity = new StorageUnitStatusEntity(); storageUnitStatusEntity.setCode(STORAGE_UNIT_STATUS); // Create a storage unit entity. StorageUnitEntity storageUnitEntity = new StorageUnitEntity(); storageUnitEntity.setStatus(storageUnitStatusEntity); // Mock the external calls. when(storageUnitDaoHelper.getStorageUnitEntity(STORAGE_NAME, businessObjectDataEntity)).thenReturn(storageUnitEntity); when(businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)).thenReturn(BUSINESS_OBJECT_DATA_KEY_AS_STRING); // Try to call the method under test. try { expireRestoredBusinessObjectDataHelperServiceImpl.getStorageUnit(STORAGE_NAME, businessObjectDataEntity); } catch (IllegalArgumentException e) { assertEquals(String .format("S3 storage unit in \"%s\" storage must have \"%s\" status, but it actually has \"%s\" status. Business object data: {%s}", STORAGE_NAME, StorageUnitStatusEntity.RESTORED, STORAGE_UNIT_STATUS, BUSINESS_OBJECT_DATA_KEY_AS_STRING), e.getMessage()); } // Verify the external calls. verify(storageUnitDaoHelper).getStorageUnitEntity(STORAGE_NAME, businessObjectDataEntity); verify(businessObjectDataHelper).businessObjectDataEntityAltKeyToString(businessObjectDataEntity); verifyNoMoreInteractionsHelper(); }
businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)));
businessObjectDataHelper.businessObjectDataEntityAltKeyToString(businessObjectDataEntity)));