Search in sources :

Example 31 with Storage

use of org.finra.herd.model.api.xml.Storage in project herd by FINRAOS.

the class DownloaderControllerTest method testLogLocalDirectoryContents.

@Test
public void testLogLocalDirectoryContents() throws Exception {
    String appenderName = "TestWriterAppender";
    StringWriter stringWriter = addLoggingWriterAppender(appenderName);
    LogLevel originalLevel = getLogLevel(DownloaderController.class);
    setLogLevel(DownloaderController.class, LogLevel.INFO);
    /*
         * Create and inject mock objects
         */
    DownloaderWebClient mockDownloaderWebClient = mock(DownloaderWebClient.class);
    DownloaderWebClient originalDownloaderWebClient = (DownloaderWebClient) ReflectionTestUtils.getField(downloaderController, "downloaderWebClient");
    ReflectionTestUtils.setField(downloaderController, "downloaderWebClient", mockDownloaderWebClient);
    DownloaderManifestReader mockDownloaderManifestReader = mock(DownloaderManifestReader.class);
    DownloaderManifestReader originalDownloaderManifestReader = (DownloaderManifestReader) ReflectionTestUtils.getField(downloaderController, "manifestReader");
    ReflectionTestUtils.setField(downloaderController, "manifestReader", mockDownloaderManifestReader);
    BusinessObjectDataHelper mockBusinessObjectDataHelper = mock(BusinessObjectDataHelper.class);
    BusinessObjectDataHelper originalBusinessObjectDataHelper = (BusinessObjectDataHelper) ReflectionTestUtils.getField(downloaderController, "businessObjectDataHelper");
    ReflectionTestUtils.setField(downloaderController, "businessObjectDataHelper", mockBusinessObjectDataHelper);
    S3Service mockS3Service = mock(S3Service.class);
    S3Service originalS3Service = (S3Service) ReflectionTestUtils.getField(downloaderController, "s3Service");
    ReflectionTestUtils.setField(downloaderController, "s3Service", mockS3Service);
    StorageFileHelper mockStorageFileHelper = mock(StorageFileHelper.class);
    StorageFileHelper originalStorageFileHelper = (StorageFileHelper) ReflectionTestUtils.getField(downloaderController, "storageFileHelper");
    ReflectionTestUtils.setField(downloaderController, "storageFileHelper", mockStorageFileHelper);
    StorageHelper mockStorageHelper = mock(StorageHelper.class);
    StorageHelper originalStorageHelper = (StorageHelper) ReflectionTestUtils.getField(downloaderController, "storageHelper");
    ReflectionTestUtils.setField(downloaderController, "storageHelper", mockStorageHelper);
    /*
         * Start test
         */
    Path localPath = Files.createTempDirectory(null);
    try {
        String s3KeyPrefix = "s3KeyPrefix";
        String storageName = "storageName";
        Path targetDirectoryPath = localPath.resolve(s3KeyPrefix);
        Path targetFilePath = targetDirectoryPath.resolve("file");
        DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
        BusinessObjectData businessObjectData = new BusinessObjectData();
        StorageUnit storageUnit = new StorageUnit(new Storage(storageName, null, null), null, null, StorageUnitStatusEntity.ENABLED, null, null, null);
        S3KeyPrefixInformation s3KeyPrefixInformation = new S3KeyPrefixInformation();
        s3KeyPrefixInformation.setS3KeyPrefix(s3KeyPrefix);
        /*
             * Mock operations on mocked dependencies
             */
        when(mockDownloaderManifestReader.readJsonManifest(any())).thenReturn(downloaderInputManifestDto);
        when(mockDownloaderWebClient.getBusinessObjectData(any())).thenReturn(businessObjectData);
        when(mockBusinessObjectDataHelper.getStorageUnitByStorageName(any(), any())).thenReturn(storageUnit);
        when(mockDownloaderWebClient.getS3KeyPrefix(any())).thenReturn(s3KeyPrefixInformation);
        when(mockS3Service.downloadDirectory(any())).then(new Answer<S3FileTransferResultsDto>() {

            @Override
            public S3FileTransferResultsDto answer(InvocationOnMock invocation) throws Throwable {
                Files.createFile(targetFilePath);
                return null;
            }
        });
        /*
             * Make the call to the method under test
             */
        RegServerAccessParamsDto regServerAccessParamsDto = null;
        File manifestPath = null;
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
        s3FileTransferRequestParamsDto.setLocalPath(localPath.toString());
        s3FileTransferRequestParamsDto.setMaxThreads(1);
        downloaderController.performDownload(regServerAccessParamsDto, manifestPath, s3FileTransferRequestParamsDto);
        assertEquals(String.format("Found 1 files in \"%s\" target local directory:%n    %s%n", targetDirectoryPath, targetFilePath), stringWriter.toString());
    } finally {
        setLogLevel(DownloaderController.class, originalLevel);
        removeLoggingAppender(appenderName);
        /*
             * Restore mocked dependencies to their original implementation
             */
        ReflectionTestUtils.setField(downloaderController, "downloaderWebClient", originalDownloaderWebClient);
        ReflectionTestUtils.setField(downloaderController, "manifestReader", originalDownloaderManifestReader);
        ReflectionTestUtils.setField(downloaderController, "businessObjectDataHelper", originalBusinessObjectDataHelper);
        ReflectionTestUtils.setField(downloaderController, "s3Service", originalS3Service);
        ReflectionTestUtils.setField(downloaderController, "storageFileHelper", originalStorageFileHelper);
        ReflectionTestUtils.setField(downloaderController, "storageHelper", originalStorageHelper);
        // Clean up any temporary files
        FileUtils.deleteDirectory(localPath.toFile());
    }
}
Also used : StorageFileHelper(org.finra.herd.service.helper.StorageFileHelper) Path(java.nio.file.Path) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) RegServerAccessParamsDto(org.finra.herd.model.dto.RegServerAccessParamsDto) LogLevel(org.finra.herd.core.helper.LogLevel) Storage(org.finra.herd.model.api.xml.Storage) StringWriter(java.io.StringWriter) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StorageHelper(org.finra.herd.service.helper.StorageHelper) BusinessObjectDataHelper(org.finra.herd.service.helper.BusinessObjectDataHelper) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) S3Service(org.finra.herd.service.S3Service) S3FileTransferResultsDto(org.finra.herd.model.dto.S3FileTransferResultsDto) File(java.io.File) Test(org.junit.Test)

Example 32 with Storage

use of org.finra.herd.model.api.xml.Storage in project herd by FINRAOS.

the class DownloaderControllerTest method testCreateDownloaderOutputManifestDtoAssertOutputAttributesSetWhenBdataAttributesSet.

@Test
public void testCreateDownloaderOutputManifestDtoAssertOutputAttributesSetWhenBdataAttributesSet() {
    BusinessObjectData businessObjectData = new BusinessObjectData();
    businessObjectData.setAttributes(new ArrayList<>());
    businessObjectData.getAttributes().add(new Attribute("name", "value"));
    StorageUnit storageUnit = new StorageUnit(new Storage("storageName", null, null), null, null, StorageUnitStatusEntity.ENABLED, null, null, null);
    String s3KeyPrefix = "s3KeyPrefix";
    DownloaderOutputManifestDto actual = downloaderController.createDownloaderOutputManifestDto(businessObjectData, storageUnit, s3KeyPrefix);
    assertEquals(1, actual.getAttributes().size());
    assertEquals("value", actual.getAttributes().get("name"));
}
Also used : Storage(org.finra.herd.model.api.xml.Storage) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) Attribute(org.finra.herd.model.api.xml.Attribute) DownloaderOutputManifestDto(org.finra.herd.model.dto.DownloaderOutputManifestDto) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Test(org.junit.Test)

Example 33 with Storage

use of org.finra.herd.model.api.xml.Storage in project herd by FINRAOS.

the class DownloaderControllerTest method testPerformDownloadAssertCredentialsRetrieved.

/**
 * Asserts that the controller is sending the proper implementation of credentials provider when calling S3.
 */
@Test
public void testPerformDownloadAssertCredentialsRetrieved() throws Exception {
    /*
         * Create and inject mock objects
         */
    DownloaderWebClient mockDownloaderWebClient = mock(DownloaderWebClient.class);
    DownloaderWebClient originalDownloaderWebClient = (DownloaderWebClient) ReflectionTestUtils.getField(downloaderController, "downloaderWebClient");
    ReflectionTestUtils.setField(downloaderController, "downloaderWebClient", mockDownloaderWebClient);
    DownloaderManifestReader mockDownloaderManifestReader = mock(DownloaderManifestReader.class);
    DownloaderManifestReader originalDownloaderManifestReader = (DownloaderManifestReader) ReflectionTestUtils.getField(downloaderController, "manifestReader");
    ReflectionTestUtils.setField(downloaderController, "manifestReader", mockDownloaderManifestReader);
    BusinessObjectDataHelper mockBusinessObjectDataHelper = mock(BusinessObjectDataHelper.class);
    BusinessObjectDataHelper originalBusinessObjectDataHelper = (BusinessObjectDataHelper) ReflectionTestUtils.getField(downloaderController, "businessObjectDataHelper");
    ReflectionTestUtils.setField(downloaderController, "businessObjectDataHelper", mockBusinessObjectDataHelper);
    S3Service mockS3Service = mock(S3Service.class);
    S3Service originalS3Service = (S3Service) ReflectionTestUtils.getField(downloaderController, "s3Service");
    ReflectionTestUtils.setField(downloaderController, "s3Service", mockS3Service);
    StorageFileHelper mockStorageFileHelper = mock(StorageFileHelper.class);
    StorageFileHelper originalStorageFileHelper = (StorageFileHelper) ReflectionTestUtils.getField(downloaderController, "storageFileHelper");
    ReflectionTestUtils.setField(downloaderController, "storageFileHelper", mockStorageFileHelper);
    StorageHelper mockStorageHelper = mock(StorageHelper.class);
    StorageHelper originalStorageHelper = (StorageHelper) ReflectionTestUtils.getField(downloaderController, "storageHelper");
    ReflectionTestUtils.setField(downloaderController, "storageHelper", mockStorageHelper);
    /*
         * Start test
         */
    Path localPath = Files.createTempDirectory(null);
    try {
        String s3KeyPrefix = "s3KeyPrefix";
        String storageName = "storageName";
        DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
        downloaderInputManifestDto.setStorageName(storageName);
        BusinessObjectData businessObjectData = new BusinessObjectData();
        StorageUnit storageUnit = new StorageUnit(new Storage(storageName, null, null), null, null, StorageUnitStatusEntity.ENABLED, null, null, null);
        S3KeyPrefixInformation s3KeyPrefixInformation = new S3KeyPrefixInformation();
        s3KeyPrefixInformation.setS3KeyPrefix(s3KeyPrefix);
        /*
             * Mock operations on mocked dependencies
             */
        when(mockDownloaderManifestReader.readJsonManifest(any())).thenReturn(downloaderInputManifestDto);
        when(mockDownloaderWebClient.getBusinessObjectData(any())).thenReturn(businessObjectData);
        when(mockBusinessObjectDataHelper.getStorageUnitByStorageName(any(), any())).thenReturn(storageUnit);
        when(mockDownloaderWebClient.getS3KeyPrefix(any())).thenReturn(s3KeyPrefixInformation);
        when(mockDownloaderWebClient.getStorageUnitDownloadCredential(any(), any())).thenReturn(new StorageUnitDownloadCredential(new AwsCredential("awsAccessKey", "awsSecretKey", "awsSessionToken", DatatypeFactory.newInstance().newXMLGregorianCalendar())));
        when(mockS3Service.downloadDirectory(any())).then(new Answer<S3FileTransferResultsDto>() {

            @Override
            public S3FileTransferResultsDto answer(InvocationOnMock invocation) throws Throwable {
                /*
                     * Call the providers' getAwsCredentials(), just like real implementation would.
                     */
                S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = invocation.getArgument(0);
                List<HerdAWSCredentialsProvider> additionalAwsCredentialsProviders = s3FileTransferRequestParamsDto.getAdditionalAwsCredentialsProviders();
                for (HerdAWSCredentialsProvider herdAWSCredentialsProvider : additionalAwsCredentialsProviders) {
                    herdAWSCredentialsProvider.getAwsCredential();
                }
                return null;
            }
        });
        /*
             * Make the call to the method under test
             */
        RegServerAccessParamsDto regServerAccessParamsDto = null;
        File manifestPath = null;
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
        s3FileTransferRequestParamsDto.setLocalPath(localPath.toString());
        s3FileTransferRequestParamsDto.setMaxThreads(1);
        downloaderController.performDownload(regServerAccessParamsDto, manifestPath, s3FileTransferRequestParamsDto);
        // Assert that the proper delegate method is called with the expected params to retrieve credentials
        verify(mockDownloaderWebClient).getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
    } finally {
        /*
             * Restore mocked dependencies to their original implementation
             */
        ReflectionTestUtils.setField(downloaderController, "downloaderWebClient", originalDownloaderWebClient);
        ReflectionTestUtils.setField(downloaderController, "manifestReader", originalDownloaderManifestReader);
        ReflectionTestUtils.setField(downloaderController, "businessObjectDataHelper", originalBusinessObjectDataHelper);
        ReflectionTestUtils.setField(downloaderController, "s3Service", originalS3Service);
        ReflectionTestUtils.setField(downloaderController, "storageFileHelper", originalStorageFileHelper);
        ReflectionTestUtils.setField(downloaderController, "storageHelper", originalStorageHelper);
        // Clean up any temporary files
        FileUtils.deleteDirectory(localPath.toFile());
    }
}
Also used : StorageFileHelper(org.finra.herd.service.helper.StorageFileHelper) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) RegServerAccessParamsDto(org.finra.herd.model.dto.RegServerAccessParamsDto) HerdAWSCredentialsProvider(org.finra.herd.model.dto.HerdAWSCredentialsProvider) ArrayList(java.util.ArrayList) List(java.util.List) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) S3Service(org.finra.herd.service.S3Service) S3FileTransferResultsDto(org.finra.herd.model.dto.S3FileTransferResultsDto) Path(java.nio.file.Path) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Storage(org.finra.herd.model.api.xml.Storage) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StorageHelper(org.finra.herd.service.helper.StorageHelper) BusinessObjectDataHelper(org.finra.herd.service.helper.BusinessObjectDataHelper) File(java.io.File) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) Test(org.junit.Test)

Example 34 with Storage

use of org.finra.herd.model.api.xml.Storage in project herd by FINRAOS.

the class DownloaderControllerTest method testCreateDownloaderOutputManifestDto.

@Test
public void testCreateDownloaderOutputManifestDto() {
    // Initiate input parameters.
    List<String> subPartitionValues = Arrays.asList("subPartitionValue1", "subPartitionValue2", "subPartitionValue3", "subPartitionValue4");
    String s3KeyPrefix = "s3KeyPrefix";
    StorageUnit storageUnit = new StorageUnit(new Storage("storageName", s3KeyPrefix, null), null, null, StorageUnitStatusEntity.ENABLED, null, null, null);
    Attribute attribute = new Attribute("name", "value");
    BusinessObjectData businessObjectData = new BusinessObjectData(1234, "businessObjectDefinitionNamespace", "businessObjectDefinitionName", "formatUsage", "formatFileType", 2345, "partitionKey", "partitionValue", subPartitionValues, 3456, true, BusinessObjectDataStatusEntity.VALID, Arrays.asList(storageUnit), Arrays.asList(attribute), new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
    // Create a downloader output manifest DTO.
    DownloaderOutputManifestDto resultDto = downloaderController.createDownloaderOutputManifestDto(businessObjectData, storageUnit, s3KeyPrefix);
    // Validate the result DTO.
    assertEquals("businessObjectDefinitionNamespace", resultDto.getNamespace());
    assertEquals("businessObjectDefinitionName", resultDto.getBusinessObjectDefinitionName());
    assertEquals("formatUsage", resultDto.getBusinessObjectFormatUsage());
    assertEquals("formatFileType", resultDto.getBusinessObjectFormatFileType());
    assertEquals("2345", resultDto.getBusinessObjectFormatVersion());
    assertEquals("partitionKey", resultDto.getPartitionKey());
    assertEquals("partitionValue", resultDto.getPartitionValue());
    assertEquals(subPartitionValues, resultDto.getSubPartitionValues());
    assertEquals("3456", resultDto.getBusinessObjectDataVersion());
    assertEquals("storageName", resultDto.getStorageName());
    HashMap<String, String> expectedAttributes = new HashMap<>();
    expectedAttributes.put(attribute.getName(), attribute.getValue());
    assertEquals(expectedAttributes, resultDto.getAttributes());
    assertEquals(new ArrayList<>(), resultDto.getBusinessObjectDataParents());
    assertEquals(new ArrayList<>(), resultDto.getBusinessObjectDataChildren());
}
Also used : Storage(org.finra.herd.model.api.xml.Storage) Attribute(org.finra.herd.model.api.xml.Attribute) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) HashMap(java.util.HashMap) DownloaderOutputManifestDto(org.finra.herd.model.dto.DownloaderOutputManifestDto) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Test(org.junit.Test)

Example 35 with Storage

use of org.finra.herd.model.api.xml.Storage in project herd by FINRAOS.

the class StorageUnitHelperTest method testCreateStorageUnitsFromEntities.

@Test
public void testCreateStorageUnitsFromEntities() {
    // Create a business object data storage unit key.
    BusinessObjectDataStorageUnitKey businessObjectDataStorageUnitKey = new BusinessObjectDataStorageUnitKey(BDEF_NAMESPACE, BDEF_NAME, FORMAT_USAGE_CODE, FORMAT_FILE_TYPE_CODE, FORMAT_VERSION, PARTITION_VALUE, SUBPARTITION_VALUES, DATA_VERSION, STORAGE_NAME);
    Timestamp restoredExpirationOn = new Timestamp(new Date().getTime());
    // Create a storage unit entity.
    StorageUnitEntity storageUnitEntity = storageUnitDaoTestHelper.createStorageUnitEntity(businessObjectDataStorageUnitKey, STORAGE_UNIT_STATUS);
    storageUnitEntity.setDirectoryPath(STORAGE_DIRECTORY_PATH);
    storageUnitEntity.setStoragePolicyTransitionFailedAttempts(STORAGE_POLICY_TRANSITION_FAILED_ATTEMPTS);
    storageUnitEntity.setRestoreExpirationOn(restoredExpirationOn);
    // Call the method under test.
    List<StorageUnit> result = storageUnitHelper.createStorageUnitsFromEntities(Arrays.asList(storageUnitEntity), NO_INCLUDE_STORAGE_UNIT_STATUS_HISTORY);
    // Verify the external calls.
    verifyNoMoreInteractionsHelper();
    // Validate the results.
    assertEquals(Arrays.asList(new StorageUnit(new Storage(STORAGE_NAME, StoragePlatformEntity.S3, null), new StorageDirectory(STORAGE_DIRECTORY_PATH), null, STORAGE_UNIT_STATUS, NO_STORAGE_UNIT_STATUS_HISTORY, STORAGE_POLICY_TRANSITION_FAILED_ATTEMPTS, HerdDateUtils.getXMLGregorianCalendarValue(restoredExpirationOn))), result);
}
Also used : BusinessObjectDataStorageUnitKey(org.finra.herd.model.api.xml.BusinessObjectDataStorageUnitKey) Storage(org.finra.herd.model.api.xml.Storage) StorageUnitEntity(org.finra.herd.model.jpa.StorageUnitEntity) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) StorageDirectory(org.finra.herd.model.api.xml.StorageDirectory) Timestamp(java.sql.Timestamp) Date(java.util.Date) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest) Test(org.junit.Test)

Aggregations

Storage (org.finra.herd.model.api.xml.Storage)38 Test (org.junit.Test)29 StorageUnit (org.finra.herd.model.api.xml.StorageUnit)19 BusinessObjectData (org.finra.herd.model.api.xml.BusinessObjectData)17 Attribute (org.finra.herd.model.api.xml.Attribute)10 StorageKey (org.finra.herd.model.api.xml.StorageKey)10 ArrayList (java.util.ArrayList)6 StorageCreateRequest (org.finra.herd.model.api.xml.StorageCreateRequest)6 File (java.io.File)5 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)4 S3KeyPrefixInformation (org.finra.herd.model.api.xml.S3KeyPrefixInformation)4 StorageAttributesUpdateRequest (org.finra.herd.model.api.xml.StorageAttributesUpdateRequest)4 StorageDirectory (org.finra.herd.model.api.xml.StorageDirectory)4 DownloaderInputManifestDto (org.finra.herd.model.dto.DownloaderInputManifestDto)4 DownloaderOutputManifestDto (org.finra.herd.model.dto.DownloaderOutputManifestDto)4 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)4 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 AwsCredential (org.finra.herd.model.api.xml.AwsCredential)3 BusinessObjectDataStorageUnitKey (org.finra.herd.model.api.xml.BusinessObjectDataStorageUnitKey)3