use of org.finra.herd.core.helper.LogLevel in project herd by FINRAOS.
the class S3DaoTest method testDeleteDirectoryAssertMultiObjectDeleteExceptionLogged.
/**
* Asserts that when delete directory is called but S3 throws MultiObjectDeleteException, the exception is logged properly.
*/
@Test
// TODO: Log4J2 - This test works within an IDE, but not from Maven. We need to figure out why.
@Ignore
public void testDeleteDirectoryAssertMultiObjectDeleteExceptionLogged() throws Exception {
// Inject mock
S3Operations mockS3Operations = mock(S3Operations.class);
S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);
// Override logger with my own appender to inspect the output
String loggerName = S3DaoImpl.class.getName();
LogLevel originalLoggerLevel = getLogLevel(loggerName);
setLogLevel(loggerName, LogLevel.ERROR);
String appenderName = "TestWriterAppender";
StringWriter stringWriter = addLoggingWriterAppender(appenderName);
try {
// Set up mocked behavior
// Return a list of mock version listing
VersionListing versionListing = new VersionListing();
S3VersionSummary s3VersionSummary = new S3VersionSummary();
s3VersionSummary.setKey("s3VersionSummaryKey");
s3VersionSummary.setVersionId("s3VersionSummaryVersionId");
versionListing.setVersionSummaries(Arrays.asList(s3VersionSummary));
when(mockS3Operations.listVersions(any(), any())).thenReturn(versionListing);
// Have mock implementation throw exception
List<DeleteError> errors = new ArrayList<>();
{
DeleteError deleteError = new DeleteError();
deleteError.setCode("deleteError1Code");
deleteError.setKey("deleteError1Key");
deleteError.setMessage("deleteError1Message");
deleteError.setVersionId("deleteError1VersionId");
errors.add(deleteError);
}
{
DeleteError deleteError = new DeleteError();
deleteError.setCode("deleteError2Code");
deleteError.setKey("deleteError2Key");
deleteError.setMessage("deleteError2Message");
deleteError.setVersionId("deleteError2VersionId");
errors.add(deleteError);
}
List<DeletedObject> deletedObjects = new ArrayList<>();
MultiObjectDeleteException multiObjectDeleteException = new MultiObjectDeleteException(errors, deletedObjects);
when(mockS3Operations.deleteObjects(any(), any())).thenThrow(multiObjectDeleteException);
// try the operation and catch exception
try {
S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();
s3FileTransferRequestParamsDto.setS3KeyPrefix("/test/prefix");
s3Dao.deleteDirectory(s3FileTransferRequestParamsDto);
fail();
} catch (Exception e) {
// Inspect and assert exception
assertEquals(IllegalStateException.class, e.getClass());
assertEquals(multiObjectDeleteException, e.getCause());
}
assertEquals(String.format("Error deleting multiple objects. Below are the list of objects which failed to delete.%n" + "s3Key=\"deleteError1Key\" s3VersionId=\"deleteError1VersionId\" " + "s3DeleteErrorCode=\"deleteError1Code\" s3DeleteErrorMessage=\"deleteError1Message\"%n" + "s3Key=\"deleteError2Key\" s3VersionId=\"deleteError2VersionId\" " + "s3DeleteErrorCode=\"deleteError2Code\" s3DeleteErrorMessage=\"deleteError2Message\"%n%n"), stringWriter.toString());
} finally {
// Restore original resources
ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
setLogLevel(loggerName, originalLoggerLevel);
removeLoggingAppender(appenderName);
}
}
use of org.finra.herd.core.helper.LogLevel in project herd by FINRAOS.
the class NotificationEventServiceTest method testProcessStorageUnitStatusChangeNotificationEventSyncWithInfoLoggingEnabled.
@Test
public void testProcessStorageUnitStatusChangeNotificationEventSyncWithInfoLoggingEnabled() throws Exception {
// Get the logger and the current logger level.
LogLevel origLogLevel = getLogLevel(StorageUnitStatusChangeNotificationJobActionServiceImpl.class);
// Set logging level to INFO.
setLogLevel(StorageUnitStatusChangeNotificationJobActionServiceImpl.class, LogLevel.INFO);
// Run the test and reset the logging level back to the original value.
try {
runProcessStorageUnitStatusChangeNotificationEventSyncTest();
} finally {
setLogLevel(StorageUnitStatusChangeNotificationJobActionServiceImpl.class, origLogLevel);
}
}
use of org.finra.herd.core.helper.LogLevel in project herd by FINRAOS.
the class StopWatchAdviceTest method testLogMethodTimeWithInfoLoggingEnabled.
@Test
public void testLogMethodTimeWithInfoLoggingEnabled() throws Throwable {
// Mock a join point of the method call.
ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint(new StopWatchAdviceTest(), StopWatchAdviceTest.class.getDeclaredMethod("mockMethod"));
// Get the logger and the current logger level.
LogLevel origLogLevel = getLogLevel("org.finra.herd.core.StopWatchAdvice");
// Set logging level to INFO.
setLogLevel("org.finra.herd.core.StopWatchAdvice", LogLevel.INFO);
// Run the test and reset the logging level back to the original value.
try {
// Call the method under test.
stopWatchAdvice.logMethodTime(joinPoint);
} finally {
setLogLevel("org.finra.herd.core.StopWatchAdvice", origLogLevel);
}
}
use of org.finra.herd.core.helper.LogLevel in project herd by FINRAOS.
the class StoragePolicyProcessorHelperServiceTest method testExecuteStoragePolicyTransitionWithInfoLoggingEnabled.
@Test
public void testExecuteStoragePolicyTransitionWithInfoLoggingEnabled() {
// Get the logger and the current logger level.
LogLevel origLogLevel = getLogLevel(StoragePolicyProcessorHelperServiceImpl.class);
// Set logging level to INFO.
setLogLevel(StoragePolicyProcessorHelperServiceImpl.class, LogLevel.INFO);
// Run the test and reset the logging level back to the original value.
try {
runExecuteStoragePolicyTransitionTest();
} finally {
setLogLevel(StoragePolicyProcessorHelperServiceImpl.class, origLogLevel);
}
}
use of org.finra.herd.core.helper.LogLevel 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());
}
}
Aggregations