use of org.finra.herd.model.dto.DownloaderInputManifestDto 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());
}
}
use of org.finra.herd.model.dto.DownloaderInputManifestDto 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());
}
}
use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.
the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredential1.
@Test
public void testGetBusinessObjectDataDownloadCredential1() throws Exception {
DownloaderInputManifestDto manifest = new DownloaderInputManifestDto();
manifest.setNamespace("test1");
manifest.setBusinessObjectDefinitionName("test2");
manifest.setBusinessObjectFormatUsage("test3");
manifest.setBusinessObjectFormatFileType("test4");
manifest.setBusinessObjectFormatVersion("test5");
manifest.setPartitionValue("test6");
manifest.setSubPartitionValues(Arrays.asList("test7", "test8"));
manifest.setBusinessObjectDataVersion("test9");
String storageName = "test10";
StorageUnitDownloadCredential storageUnitDownloadCredential = downloaderWebClient.getStorageUnitDownloadCredential(manifest, storageName);
Assert.assertNotNull(storageUnitDownloadCredential);
AwsCredential awsCredential = storageUnitDownloadCredential.getAwsCredential();
Assert.assertNotNull(awsCredential);
Assert.assertEquals("https://testWebServiceHostname:1234/herd-app/rest/storageUnits/download/credential/namespaces/test1" + "/businessObjectDefinitionNames/test2/businessObjectFormatUsages/test3/businessObjectFormatFileTypes/test4/businessObjectFormatVersions/test5" + "/partitionValues/test6/businessObjectDataVersions/test9/storageNames/test10?subPartitionValues=test7%7Ctest8", awsCredential.getAwsAccessKey());
}
use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.
the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredentialAssertNoAuthorizationHeaderWhenNoSsl.
@Test
public void testGetBusinessObjectDataDownloadCredentialAssertNoAuthorizationHeaderWhenNoSsl() 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, "SUCCESS"));
when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential())));
DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
String storageName = "storageName";
boolean useSsl = false;
downloaderWebClient.getRegServerAccessParamsDto().setUseSsl(useSsl);
downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
verify(mockHttpClientOperations).execute(any(), argThat(httpUriRequest -> httpUriRequest.getFirstHeader("Authorization") == null));
} finally {
ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
}
}
use of org.finra.herd.model.dto.DownloaderInputManifestDto in project herd by FINRAOS.
the class DownloaderWebClientTest method testGetBusinessObjectDataDownloadCredentialAssertThrowIOExceptionWhenClosingHttpClientThrowsIOException.
/**
* Asserts that the http client is closed and if an exception is thrown during closing of the http client, the same exception is bubbled up.
*/
@Test
public void testGetBusinessObjectDataDownloadCredentialAssertThrowIOExceptionWhenClosingHttpClientThrowsIOException() throws Exception {
HttpClientOperations mockHttpClientOperations = mock(HttpClientOperations.class);
HttpClientOperations originalHttpClientOperations = (HttpClientOperations) ReflectionTestUtils.getField(downloaderWebClient, "httpClientOperations");
ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", mockHttpClientOperations);
try {
CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
when(mockHttpClientOperations.createHttpClient()).thenReturn(closeableHttpClient);
doThrow(IOException.class).when(closeableHttpClient).close();
CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
when(mockHttpClientOperations.execute(any(), any())).thenReturn(closeableHttpResponse);
when(closeableHttpResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "SUCCESS"));
when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity(xmlHelper.objectToXml(new StorageUnitDownloadCredential())));
DownloaderInputManifestDto downloaderInputManifestDto = new DownloaderInputManifestDto();
String storageName = "storageName";
try {
downloaderWebClient.getStorageUnitDownloadCredential(downloaderInputManifestDto, storageName);
verify(closeableHttpClient).close();
fail();
} catch (Exception e) {
assertEquals(IOException.class, e.getClass());
}
} finally {
ReflectionTestUtils.setField(downloaderWebClient, "httpClientOperations", originalHttpClientOperations);
}
}
Aggregations