Search in sources :

Example 6 with S3FileCopyRequestParamsDto

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

the class S3DaoTest method testCopyFileInvalidKmsId.

/**
 * Test S3 file copy with an invalid KMS Id. This should throw an AmazonServiceException.
 */
@Test
public void testCopyFileInvalidKmsId() throws InterruptedException {
    // Put a 1 byte file in S3.
    s3Operations.putObject(new PutObjectRequest(storageDaoTestHelper.getS3LoadingDockBucketName(), TARGET_S3_KEY, new ByteArrayInputStream(new byte[1]), null), null);
    try {
        S3FileCopyRequestParamsDto transferDto = new S3FileCopyRequestParamsDto();
        transferDto.setSourceBucketName(storageDaoTestHelper.getS3LoadingDockBucketName());
        transferDto.setTargetBucketName(storageDaoTestHelper.getS3ExternalBucketName());
        transferDto.setSourceObjectKey(TARGET_S3_KEY);
        transferDto.setTargetObjectKey(TARGET_S3_KEY);
        transferDto.setKmsKeyId(MockS3OperationsImpl.MOCK_KMS_ID_FAILED_TRANSFER);
        s3Dao.copyFile(transferDto);
        fail("An AmazonServiceException was expected but not thrown.");
    } catch (AmazonServiceException ex) {
        Assert.assertTrue("Invalid AmazonServiceException message returned.", ex.getMessage().contains("Key '" + MockS3OperationsImpl.MOCK_KMS_ID_FAILED_TRANSFER + "' does not exist"));
    }
}
Also used : S3FileCopyRequestParamsDto(org.finra.herd.model.dto.S3FileCopyRequestParamsDto) ByteArrayInputStream(java.io.ByteArrayInputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 7 with S3FileCopyRequestParamsDto

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

the class S3DaoTest method testCopyFileInvalidKmsIdIllegalStateException.

/**
 * Test S3 file copy with an invalid KMS Id that throws an IllegalStateException because no AmazonServiceException was found.
 */
@Test
public void testCopyFileInvalidKmsIdIllegalStateException() throws InterruptedException {
    // Put a 1 byte file in S3.
    s3Operations.putObject(new PutObjectRequest(storageDaoTestHelper.getS3LoadingDockBucketName(), TARGET_S3_KEY, new ByteArrayInputStream(new byte[1]), null), null);
    try {
        S3FileCopyRequestParamsDto transferDto = new S3FileCopyRequestParamsDto();
        transferDto.setSourceBucketName(storageDaoTestHelper.getS3LoadingDockBucketName());
        transferDto.setTargetBucketName(storageDaoTestHelper.getS3ExternalBucketName());
        transferDto.setSourceObjectKey(TARGET_S3_KEY);
        transferDto.setTargetObjectKey(TARGET_S3_KEY);
        transferDto.setKmsKeyId(MockS3OperationsImpl.MOCK_KMS_ID_FAILED_TRANSFER_NO_EXCEPTION);
        s3Dao.copyFile(transferDto);
        fail("An IllegalStateException was expected but not thrown.");
    } catch (IllegalStateException ex) {
        assertEquals("Invalid IllegalStateException message returned.", "The transfer operation \"" + MockS3OperationsImpl.MOCK_TRANSFER_DESCRIPTION + "\" failed for an unknown reason.", ex.getMessage());
    }
}
Also used : S3FileCopyRequestParamsDto(org.finra.herd.model.dto.S3FileCopyRequestParamsDto) ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 8 with S3FileCopyRequestParamsDto

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

the class S3DaoTest method testCopyFileInvalidKmsIdCancelled.

/**
 * Test S3 file copy with an invalid KMS Id that will result in a cancelled transfer.
 */
@Test
public void testCopyFileInvalidKmsIdCancelled() throws InterruptedException {
    // Put a 1 byte file in S3.
    s3Operations.putObject(new PutObjectRequest(storageDaoTestHelper.getS3LoadingDockBucketName(), TARGET_S3_KEY, new ByteArrayInputStream(new byte[1]), null), null);
    try {
        S3FileCopyRequestParamsDto transferDto = new S3FileCopyRequestParamsDto();
        transferDto.setSourceBucketName(storageDaoTestHelper.getS3LoadingDockBucketName());
        transferDto.setTargetBucketName(storageDaoTestHelper.getS3ExternalBucketName());
        transferDto.setSourceObjectKey(TARGET_S3_KEY);
        transferDto.setTargetObjectKey(TARGET_S3_KEY);
        transferDto.setKmsKeyId(MockS3OperationsImpl.MOCK_KMS_ID_CANCELED_TRANSFER);
        s3Dao.copyFile(transferDto);
        fail("An IllegalStateException was expected but not thrown.");
    } catch (IllegalStateException ex) {
        assertEquals("Invalid IllegalStateException message returned.", "The transfer operation \"" + MockS3OperationsImpl.MOCK_TRANSFER_DESCRIPTION + "\" did not complete successfully. " + "Current state: \"" + Transfer.TransferState.Canceled + "\".", ex.getMessage());
    }
}
Also used : S3FileCopyRequestParamsDto(org.finra.herd.model.dto.S3FileCopyRequestParamsDto) ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 9 with S3FileCopyRequestParamsDto

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

the class S3ServiceTest method testCopyFile.

@Test
public void testCopyFile() throws InterruptedException {
    // Create an S3 file copy request parameters DTO.
    S3FileCopyRequestParamsDto s3FileCopyRequestParamsDto = new S3FileCopyRequestParamsDto();
    // Create an S3 file transfer result DTO.
    S3FileTransferResultsDto s3FileTransferResultsDto = new S3FileTransferResultsDto();
    // Mock the external calls.
    when(s3Dao.copyFile(s3FileCopyRequestParamsDto)).thenReturn(s3FileTransferResultsDto);
    // Call the method under test.
    S3FileTransferResultsDto result = s3Service.copyFile(s3FileCopyRequestParamsDto);
    // Verify the external calls.
    verify(s3Dao).copyFile(s3FileCopyRequestParamsDto);
    verifyNoMoreInteractions(s3Dao);
    // Validate the returned object.
    assertEquals(s3FileTransferResultsDto, result);
}
Also used : S3FileCopyRequestParamsDto(org.finra.herd.model.dto.S3FileCopyRequestParamsDto) S3FileTransferResultsDto(org.finra.herd.model.dto.S3FileTransferResultsDto) Test(org.junit.Test)

Example 10 with S3FileCopyRequestParamsDto

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

the class UploadDownloadHelperServiceImpl method performFileMoveImpl.

/**
 * Moves an S3 file from the source bucket to the target bucket. Updates the target business object data status in the DTO based on the result of S3 copy
 * operation.
 *
 * @param completeUploadSingleParamsDto the DTO that contains complete upload single message parameters
 */
protected void performFileMoveImpl(CompleteUploadSingleParamsDto completeUploadSingleParamsDto) {
    // Create and initialize an S3 file copy request parameters DTO.
    S3FileCopyRequestParamsDto params = new S3FileCopyRequestParamsDto();
    params.setSourceBucketName(completeUploadSingleParamsDto.getSourceBucketName());
    params.setTargetBucketName(completeUploadSingleParamsDto.getTargetBucketName());
    params.setSourceObjectKey(completeUploadSingleParamsDto.getSourceFilePath());
    params.setTargetObjectKey(completeUploadSingleParamsDto.getTargetFilePath());
    params.setKmsKeyId(completeUploadSingleParamsDto.getKmsKeyId());
    params.setHttpProxyHost(completeUploadSingleParamsDto.getAwsParams().getHttpProxyHost());
    params.setHttpProxyPort(completeUploadSingleParamsDto.getAwsParams().getHttpProxyPort());
    String targetStatus;
    try {
        // Copy the file from source S3 bucket to target bucket, and mark the target business object data as VALID.
        s3Dao.copyFile(params);
        // Update the status of the target business object data to "VALID".
        targetStatus = BusinessObjectDataStatusEntity.VALID;
    } catch (Exception e) {
        // Log the error.
        LOGGER.error("Failed to copy the upload single file. s3Key=\"{}\" sourceS3BucketName=\"{}\" targetS3BucketName=\"{}\" " + "sourceBusinessObjectDataKey={} targetBusinessObjectDataKey={}", completeUploadSingleParamsDto.getSourceFilePath(), completeUploadSingleParamsDto.getSourceBucketName(), completeUploadSingleParamsDto.getTargetBucketName(), jsonHelper.objectToJson(completeUploadSingleParamsDto.getSourceBusinessObjectDataKey()), jsonHelper.objectToJson(completeUploadSingleParamsDto.getTargetBusinessObjectDataKey()), e);
        // Update the status of the target business object data to "INVALID".
        targetStatus = BusinessObjectDataStatusEntity.INVALID;
    }
    // Update the DTO.
    completeUploadSingleParamsDto.setTargetOldStatus(completeUploadSingleParamsDto.getTargetNewStatus());
    completeUploadSingleParamsDto.setTargetNewStatus(targetStatus);
}
Also used : S3FileCopyRequestParamsDto(org.finra.herd.model.dto.S3FileCopyRequestParamsDto) OptimisticLockException(javax.persistence.OptimisticLockException)

Aggregations

S3FileCopyRequestParamsDto (org.finra.herd.model.dto.S3FileCopyRequestParamsDto)10 Test (org.junit.Test)8 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 S3FileTransferResultsDto (org.finra.herd.model.dto.S3FileTransferResultsDto)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 Copy (com.amazonaws.services.s3.transfer.Copy)2 TransferProgress (com.amazonaws.services.s3.transfer.TransferProgress)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 MultiObjectDeleteException (com.amazonaws.services.s3.model.MultiObjectDeleteException)1 IOException (java.io.IOException)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 ObjectNotFoundException (org.finra.herd.model.ObjectNotFoundException)1 Answer (org.mockito.stubbing.Answer)1