Search in sources :

Example 1 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderControllerTest method testPerformDownloadWithStorageName.

@Test
public void testPerformDownloadWithStorageName() throws Exception {
    DownloaderInputManifestDto testDownloaderInputManifestDto = getTestDownloaderInputManifestDto();
    testDownloaderInputManifestDto.setStorageName("S3_MANAGED");
    runDownload(testDownloaderInputManifestDto, LOCAL_TEMP_PATH_OUTPUT.toString(), DownloaderController.MIN_THREADS, null);
}
Also used : DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) Test(org.junit.Test)

Example 2 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataAssertNoAuthorizationHeaderWhenNoSsl.

@Test
public void testGetBusinessObjectDataAssertNoAuthorizationHeaderWhenNoSsl() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
        when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse);
        when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
        when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new BusinessObjectData())));
        DownloaderInputManifestDto manifest = new DownloaderInputManifestDto();
        downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(false);
        downloaderWebClient.getBusinessObjectData(manifest);
        verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> httpUriRequest.getFirstHeader("Authorization") == null));
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : HttpVersion(org.apache.http.HttpVersion) Arrays(java.util.Arrays) BasicStatusLine(org.apache.http.message.BasicStatusLine) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Mockito.doThrow(org.mockito.Mockito.doThrow) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) UploaderInputManifestDto(org.finra.herd.model.dto.UploaderInputManifestDto) Command(org.finra.herd.core.Command) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) Storage(org.finra.herd.model.api.xml.Storage) DataBridgeWebClient(org.finra.herd.tools.common.databridge.DataBridgeWebClient) Assert.fail(org.junit.Assert.fail) Objects(com.google.common.base.Objects) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) StringEntity(org.apache.http.entity.StringEntity) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RegServerAccessParamsDto(org.finra.herd.model.dto.RegServerAccessParamsDto) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) Matchers.argThat(org.mockito.Matchers.argThat) Assert(org.junit.Assert) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) Assert.assertEquals(org.junit.Assert.assertEquals) StorageEntity(org.finra.herd.model.jpa.StorageEntity) Mockito.mock(org.mockito.Mockito.mock) BusinessObjectDataStatusEntity(org.finra.herd.model.jpa.BusinessObjectDataStatusEntity) StringEntity(org.apache.http.entity.StringEntity) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 3 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredentialAssertHttpClientClosedWhenIOException.

@Test
public void testGetBusinessObjectDataDownloadCredentialAssertHttpClientClosedWhenIOException() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        IOException expectedException = new IOException();
        CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
        when(mockHttpClientOperations.createHttpClient()).thenReturn(closeableHttpClient);
        when(mockHttpClientOperations.execute(any(), any())).thenThrow(expectedException);
        DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
        String storageName = "storageName";
        try {
            downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
            verify(closeableHttpClient).close();
            fail();
        } catch (Exception e) {
            assertEquals(expectedException, e);
        }
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) IOException(java.io.IOException) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderWebClientTest method testGetBusinessObjectDataAssertNamespaceOptional.

@Test
public void testGetBusinessObjectDataAssertNamespaceOptional() throws Exception {
    HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
    HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
    ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
    try {
        String expectedHttpMethod = "GET";
        String expectedUri = "https://testWebServiceHostname:1234/herd-app/rest/businessObjectData" + "/businessObjectDefinitionNames/businessObjectDefinitionName/businessObjectFormatUsages/businessObjectFormatUsage" + "/businessObjectFormatFileTypes/businessObjectFormatFileType?partitionKey=partitionKey&partitionValue=partitionValue&" + "businessObjectFormatVersion=0&businessObjectDataVersion=1";
        CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
        when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse);
        when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
        BusinessObjectData expectedBusinessObjectData = new BusinessObjectData();
        expectedBusinessObjectData.setId(1234);
        StringEntity httpEntity = new StringEntity(xmlHelper.objectToXml(expectedBusinessObjectData));
        when(closeableHttpResponse.getEntity()).thenReturn(httpEntity);
        DownloaderInputManifestDto manifest = new DownloaderInputManifestDto();
        manifest.setBusinessObjectDefinitionName("businessObjectDefinitionName");
        manifest.setBusinessObjectFormatUsage("businessObjectFormatUsage");
        manifest.setBusinessObjectFormatFileType("businessObjectFormatFileType");
        manifest.setBusinessObjectFormatVersion("0");
        manifest.setPartitionKey("partitionKey");
        manifest.setPartitionValue("partitionValue");
        manifest.setBusinessObjectDataVersion("1");
        assertEquals(expectedBusinessObjectData.getId(), downloaderWebClient.getBusinessObjectData(manifest).getId());
        verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> Objects.equal(expectedHttpMethod, httpUriRequest.getMethod()) && Objects.equal(expectedUri, httpUriRequest.getURI().toString())));
    } finally {
        ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
    }
}
Also used : HttpVersion(org.apache.http.HttpVersion) Arrays(java.util.Arrays) BasicStatusLine(org.apache.http.message.BasicStatusLine) StorageUnit(org.finra.herd.model.api.xml.StorageUnit) Mockito.doThrow(org.mockito.Mockito.doThrow) BusinessObjectDataKey(org.finra.herd.model.api.xml.BusinessObjectDataKey) UploaderInputManifestDto(org.finra.herd.model.dto.UploaderInputManifestDto) Command(org.finra.herd.core.Command) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) S3KeyPrefixInformation(org.finra.herd.model.api.xml.S3KeyPrefixInformation) Storage(org.finra.herd.model.api.xml.Storage) DataBridgeWebClient(org.finra.herd.tools.common.databridge.DataBridgeWebClient) Assert.fail(org.junit.Assert.fail) Objects(com.google.common.base.Objects) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) StorageUnitDownloadCredential(org.finra.herd.model.api.xml.StorageUnitDownloadCredential) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) StringEntity(org.apache.http.entity.StringEntity) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) RegServerAccessParamsDto(org.finra.herd.model.dto.RegServerAccessParamsDto) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) Matchers.argThat(org.mockito.Matchers.argThat) Assert(org.junit.Assert) AwsCredential(org.finra.herd.model.api.xml.AwsCredential) Assert.assertEquals(org.junit.Assert.assertEquals) StorageEntity(org.finra.herd.model.jpa.StorageEntity) Mockito.mock(org.mockito.Mockito.mock) BusinessObjectDataStatusEntity(org.finra.herd.model.jpa.BusinessObjectDataStatusEntity) StringEntity(org.apache.http.entity.StringEntity) BusinessObjectData(org.finra.herd.model.api.xml.BusinessObjectData) DownloaderInputManifestDto(org.finra.herd.model.dto.DownloaderInputManifestDto) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientOperations(org.finra.herd.dao.HttpClientOperations) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 5 with DownloaderInputManifestDto

use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.

the class DownloaderControllerTest method testPerformDownloadAssertCleanTargetDirectoryWhenError.

/**
 * Asserts that the target directory is cleared (ie. all files under the directory is removed recursively) when there is an error during download.
 */
@Test
public void testPerformDownloadAssertCleanTargetDirectoryWhenError() 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";
        IOException expectedException = new IOException();
        Path targetDirectoryPath = localPath.resolve(s3KeyPrefix);
        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(targetDirectoryPath.resolve("file"));
                throw expectedException;
            }
        });
        /*
             * 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);
        try {
            downloaderController.performDownload(regServerAccessParamsDto, manifestPath, s3FileTransferRequestParamsDto);
            // Expect an exception, fail if no exception
            fail();
        } catch (Exception e) {
            // Assert that the exception thrown by the mock is what is actually thrown
            assertEquals(expectedException, e);
            // Assert that the target directory is cleaned
            assertEquals(0, targetDirectoryPath.toFile().list().length);
        }
    } 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) 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) IOException(java.io.IOException) IOException(java.io.IOException) 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) 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)

Aggregations

DownloaderInputManifestDto (org.finra.herd.model.dto.DownloaderInputManifestDto)16 Test (org.junit.Test)14 IOException (java.io.IOException)8 BusinessObjectData (org.finra.herd.model.api.xml.BusinessObjectData)8 AwsCredential (org.finra.herd.model.api.xml.AwsCredential)7 S3KeyPrefixInformation (org.finra.herd.model.api.xml.S3KeyPrefixInformation)7 Storage (org.finra.herd.model.api.xml.Storage)7 StorageUnit (org.finra.herd.model.api.xml.StorageUnit)7 StorageUnitDownloadCredential (org.finra.herd.model.api.xml.StorageUnitDownloadCredential)7 RegServerAccessParamsDto (org.finra.herd.model.dto.RegServerAccessParamsDto)6 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 HttpClientOperations (org.finra.herd.dao.HttpClientOperations)5 File (java.io.File)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 StringEntity (org.apache.http.entity.StringEntity)4 BasicStatusLine (org.apache.http.message.BasicStatusLine)4 Command (org.finra.herd.core.Command)4 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)4 UploaderInputManifestDto (org.finra.herd.model.dto.UploaderInputManifestDto)4 Objects (com.google.common.base.Objects)3