Search in sources :

Example 11 with LogLevel

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);
    }
}
Also used : DeleteError(com.amazonaws.services.s3.model.MultiObjectDeleteException.DeleteError) S3FileTransferRequestParamsDto(org.finra.herd.model.dto.S3FileTransferRequestParamsDto) VersionListing(com.amazonaws.services.s3.model.VersionListing) ArrayList(java.util.ArrayList) LogLevel(org.finra.herd.core.helper.LogLevel) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) StringWriter(java.io.StringWriter) S3VersionSummary(com.amazonaws.services.s3.model.S3VersionSummary) MultiObjectDeleteException(com.amazonaws.services.s3.model.MultiObjectDeleteException) DeletedObject(com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with LogLevel

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);
    }
}
Also used : LogLevel(org.finra.herd.core.helper.LogLevel) Test(org.junit.Test)

Example 13 with LogLevel

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);
    }
}
Also used : MockProceedingJoinPoint(org.finra.herd.core.MockProceedingJoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) LogLevel(org.finra.herd.core.helper.LogLevel) Test(org.junit.Test) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest)

Example 14 with LogLevel

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);
    }
}
Also used : LogLevel(org.finra.herd.core.helper.LogLevel) Test(org.junit.Test)

Example 15 with LogLevel

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());
    }
}
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)

Aggregations

LogLevel (org.finra.herd.core.helper.LogLevel)15 Test (org.junit.Test)15 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)4 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)3 StringWriter (java.io.StringWriter)2 MockProceedingJoinPoint (org.finra.herd.core.MockProceedingJoinPoint)2 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 DeletedObject (com.amazonaws.services.s3.model.DeleteObjectsResult.DeletedObject)1 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)1 DeleteError (com.amazonaws.services.s3.model.MultiObjectDeleteException.DeleteError)1 S3VersionSummary (com.amazonaws.services.s3.model.S3VersionSummary)1 VersionListing (com.amazonaws.services.s3.model.VersionListing)1 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)1