Java Code Examples for com.amazonaws.services.s3.model.S3Object#setObjectContent()

The following examples show how to use com.amazonaws.services.s3.model.S3Object#setObjectContent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MockAmazonS3.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public S3Object getObject(final GetObjectRequest request) throws AmazonClientException {
    assertThat(request.getBucketName(), equalTo(bucket));

    final String blobName = request.getKey();
    final byte[] content = blobs.get(blobName);
    if (content == null) {
        AmazonS3Exception exception = new AmazonS3Exception("[" + blobName + "] does not exist.");
        exception.setStatusCode(404);
        throw exception;
    }

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(content.length);

    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new S3ObjectInputStream(new ByteArrayInputStream(content), null, false));
    s3Object.setKey(blobName);
    s3Object.setObjectMetadata(metadata);

    return s3Object;
}
 
Example 2
Source File: AmazonS3SourceMockTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws IOException {
	File remoteFolder = TEMPORARY_FOLDER.newFolder("remote");

	File aFile = new File(remoteFolder, "1.test");
	FileCopyUtils.copy("Hello".getBytes(), aFile);
	File bFile = new File(remoteFolder, "2.test");
	FileCopyUtils.copy("Bye".getBytes(), bFile);
	File otherFile = new File(remoteFolder, "otherFile");
	FileCopyUtils.copy("Other\nOther2".getBytes(), otherFile);

	S3_OBJECTS = new ArrayList<>();

	for (File file : remoteFolder.listFiles()) {
		S3Object s3Object = new S3Object();
		s3Object.setBucketName(S3_BUCKET);
		s3Object.setKey(file.getName());
		s3Object.setObjectContent(new FileInputStream(file));
		S3_OBJECTS.add(s3Object);
	}

	String localFolder = TEMPORARY_FOLDER.newFolder("local").getAbsolutePath();

	System.setProperty("s3.localDir", localFolder);
}
 
Example 3
Source File: SimpleStorageResourceTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void getInputStream_existingObject_returnsInputStreamWithContent() throws Exception {
	// Arrange
	AmazonS3 amazonS3 = mock(AmazonS3.class);
	ObjectMetadata objectMetadata = mock(ObjectMetadata.class);
	when(amazonS3.getObjectMetadata(any(GetObjectMetadataRequest.class)))
			.thenReturn(objectMetadata);

	S3Object s3Object = new S3Object();
	s3Object.setObjectMetadata(objectMetadata);
	s3Object.setObjectContent(new ByteArrayInputStream(new byte[] { 42 }));
	when(amazonS3.getObject(any(GetObjectRequest.class))).thenReturn(s3Object);

	// Act
	SimpleStorageResource simpleStorageResource = new SimpleStorageResource(amazonS3,
			"bucket", "object", new SyncTaskExecutor());

	// Assert
	assertThat(simpleStorageResource.exists()).isTrue();
	assertThat(simpleStorageResource.getInputStream().read()).isEqualTo(42);
}
 
Example 4
Source File: AwsS3EnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
private void setupS3(String fileName, String version, String propertyContent)
		throws UnsupportedEncodingException {
	final S3ObjectId s3ObjectId = new S3ObjectId("bucket1", fileName);
	final GetObjectRequest request = new GetObjectRequest(s3ObjectId);

	final S3Object s3Object = new S3Object();
	s3Object.setObjectContent(new StringInputStream(propertyContent));

	if (version != null) {
		final ObjectMetadata metadata = new ObjectMetadata();
		metadata.setHeader("x-amz-version-id", version);
		s3Object.setObjectMetadata(metadata);
	}

	when(s3Client.getObject(argThat(new GetObjectRequestMatcher(request))))
			.thenReturn(s3Object);
}
 
Example 5
Source File: S3DaoTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPropertiesHandleIllegalArgumentException() throws Exception
{
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    S3Operations mockS3Operations = mock(S3Operations.class);
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);

    JavaPropertiesHelper originalJavaPropertiesHelper = (JavaPropertiesHelper) ReflectionTestUtils.getField(s3Dao, "javaPropertiesHelper");
    JavaPropertiesHelper mockJavaPropertiesHelper = mock(JavaPropertiesHelper.class);
    ReflectionTestUtils.setField(s3Dao, "javaPropertiesHelper", mockJavaPropertiesHelper);

    try
    {
        String bucketName = "bucketName";
        String key = "key";
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();

        S3Object s3Object = new S3Object();
        s3Object.setObjectContent(new ByteArrayInputStream(new byte[] {0}));
        when(mockS3Operations.getS3Object(any(), any())).thenReturn(s3Object);
        when(mockJavaPropertiesHelper.getProperties(any(InputStream.class))).thenThrow(new IllegalArgumentException("message"));

        try
        {
            s3Dao.getProperties(bucketName, key, s3FileTransferRequestParamsDto);
            fail();
        }
        catch (Exception e)
        {
            assertEquals(IllegalArgumentException.class, e.getClass());
            assertEquals("The properties file in S3 bucket 'bucketName' and key 'key' is invalid.", e.getMessage());
        }
    }
    finally
    {
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
        ReflectionTestUtils.setField(s3Dao, "javaPropertiesHelper", originalJavaPropertiesHelper);
    }
}
 
Example 6
Source File: S3ObjectsProviderTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private static S3Object buildS3Object( Bucket bucket, String key, String dataString ) {
  S3Object s3Object = new S3Object();
  s3Object.setKey( key );
  s3Object.setBucketName( bucket.getName() );
  s3Object.setObjectContent( new ByteArrayInputStream( dataString.getBytes() ) );
  return s3Object;
}
 
Example 7
Source File: TestFetchS3Object.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetObjectVersion() throws IOException {
    runner.setProperty(FetchS3Object.REGION, "us-east-1");
    runner.setProperty(FetchS3Object.BUCKET, "request-bucket");
    runner.setProperty(FetchS3Object.VERSION_ID, "${s3.version}");
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "request-key");
    attrs.put("s3.version", "request-version");
    runner.enqueue(new byte[0], attrs);

    S3Object s3ObjectResponse = new S3Object();
    s3ObjectResponse.setBucketName("response-bucket-name");
    s3ObjectResponse.setObjectContent(new StringInputStream("Some Content"));
    ObjectMetadata metadata = Mockito.spy(ObjectMetadata.class);
    metadata.setContentDisposition("key/path/to/file.txt");
    Mockito.when(metadata.getVersionId()).thenReturn("response-version");
    s3ObjectResponse.setObjectMetadata(metadata);
    Mockito.when(mockS3Client.getObject(Mockito.any())).thenReturn(s3ObjectResponse);

    runner.run(1);

    ArgumentCaptor<GetObjectRequest> captureRequest = ArgumentCaptor.forClass(GetObjectRequest.class);
    Mockito.verify(mockS3Client, Mockito.times(1)).getObject(captureRequest.capture());
    GetObjectRequest request = captureRequest.getValue();
    assertEquals("request-bucket", request.getBucketName());
    assertEquals("request-key", request.getKey());
    assertEquals("request-version", request.getVersionId());

    runner.assertAllFlowFilesTransferred(FetchS3Object.REL_SUCCESS, 1);
    final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchS3Object.REL_SUCCESS);
    MockFlowFile ff = ffs.get(0);
    ff.assertAttributeEquals("s3.bucket", "response-bucket-name");
    ff.assertAttributeEquals(CoreAttributes.FILENAME.key(), "file.txt");
    ff.assertAttributeEquals(CoreAttributes.PATH.key(), "key/path/to");
    ff.assertAttributeEquals(CoreAttributes.ABSOLUTE_PATH.key(), "key/path/to/file.txt");
    ff.assertAttributeEquals("s3.version", "response-version");
    ff.assertContentEquals("Some Content");
}
 
Example 8
Source File: MockS3OperationsImpl.java    From herd with Apache License 2.0 5 votes vote down vote up
@Override
public S3Object getS3Object(GetObjectRequest getObjectRequest, AmazonS3 s3)
{
    MockS3Object mockS3Object = getMockS3Object(getObjectRequest.getBucketName(), getObjectRequest.getKey());

    S3Object s3Object = new S3Object();
    s3Object.setBucketName(getObjectRequest.getBucketName());
    s3Object.setKey(getObjectRequest.getKey());
    s3Object.setObjectContent(new ByteArrayInputStream(mockS3Object.getData()));
    s3Object.setObjectMetadata(mockS3Object.getObjectMetadata());

    return s3Object;
}
 
Example 9
Source File: S3DaoTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPropertiesHandleGenericException() throws Exception
{
    S3Operations originalS3Operations = (S3Operations) ReflectionTestUtils.getField(s3Dao, "s3Operations");
    S3Operations mockS3Operations = mock(S3Operations.class);
    ReflectionTestUtils.setField(s3Dao, "s3Operations", mockS3Operations);

    JavaPropertiesHelper originalJavaPropertiesHelper = (JavaPropertiesHelper) ReflectionTestUtils.getField(s3Dao, "javaPropertiesHelper");
    JavaPropertiesHelper mockJavaPropertiesHelper = mock(JavaPropertiesHelper.class);
    ReflectionTestUtils.setField(s3Dao, "javaPropertiesHelper", mockJavaPropertiesHelper);

    try
    {
        String bucketName = "bucketName";
        String key = "key";
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto = new S3FileTransferRequestParamsDto();

        S3Object s3Object = new S3Object();
        s3Object.setObjectContent(new ByteArrayInputStream(new byte[] {0}));
        when(mockS3Operations.getS3Object(any(), any())).thenReturn(s3Object);
        when(mockJavaPropertiesHelper.getProperties(any(InputStream.class))).thenThrow(new RuntimeException("message"));

        try
        {
            s3Dao.getProperties(bucketName, key, s3FileTransferRequestParamsDto);
            fail();
        }
        catch (Exception e)
        {
            assertEquals(RuntimeException.class, e.getClass());
            assertEquals("message", e.getMessage());
        }
    }
    finally
    {
        ReflectionTestUtils.setField(s3Dao, "s3Operations", originalS3Operations);
        ReflectionTestUtils.setField(s3Dao, "javaPropertiesHelper", originalJavaPropertiesHelper);
    }
}
 
Example 10
Source File: StashReaderTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Test
public void getSplit() throws Exception {
    // Intentionally create the gzip file as concatenated gzip files to verify the Java bug involving reading
    // concatenated gzip files is resolved.

    List<Map<String, Object>> expected = Lists.newArrayListWithCapacity(4);
    ByteArrayOutputStream splitOut = new ByteArrayOutputStream();

    for (int p=0; p < 2; p++) {
        for (int i=0; i < 2; i++) {
            ByteArrayOutputStream partialOut = new ByteArrayOutputStream();
            try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(partialOut)))) {
                Map<String, Object> value = ImmutableMap.<String, Object>builder()
                        .put("~id", "row" + (p * 2 + i))
                        .put("~table", "test:table")
                        .put("~version", 1)
                        .put("~signature", "3a0da59fabf298d389b7b0b59728e887")
                        .put("~lastUpdateAt", "2014-08-28T21:24:36.440Z")
                        .put("~firstUpdateAt", "2014-06-07T09:51:40.077Z")
                        .build();

                String json = JsonHelper.asJson(value);
                out.write(json);
                out.write("\n");

                expected.add(value);
            }
            partialOut.writeTo(splitOut);
        }
    }

    splitOut.close();

    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new ByteArrayInputStream(splitOut.toByteArray(), 0, splitOut.size()));
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(splitOut.size());
    s3Object.setObjectMetadata(objectMetadata);

    AmazonS3 s3 = mock(AmazonS3.class);
    when(s3.getObject(argThat(getsObject("stash-bucket", "stash/test/2015-01-01-00-00-00/test-table/split0.gz"))))
            .thenReturn(s3Object);

    StashSplit stashSplit = new StashSplit("test:table", "2015-01-01-00-00-00/test-table/split0.gz", splitOut.size());

    StandardStashReader reader = new StandardStashReader(URI.create("s3://stash-bucket/stash/test"), s3, 0);
    StashRowIterator contentIter = reader.getSplit(stashSplit);

    List<Map<String, Object>> content = ImmutableList.copyOf(contentIter);
    assertEquals(content, expected);
}
 
Example 11
Source File: AccessValidatorControllerTest.java    From herd with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateAccessNoStorageFilesZeroByteS3File() throws Exception
{
    // Create business object data without any registered storage files, but with storage unit directory path and a zero-byte S3 file.
    BusinessObjectData businessObjectData = createBusinessObjectData();
    businessObjectData.getStorageUnits().get(0).setStorageFiles(null);
    StorageDirectory storageDirectory = new StorageDirectory();
    storageDirectory.setDirectoryPath(STORAGE_DIRECTORY_PATH);
    businessObjectData.getStorageUnits().get(0).setStorageDirectory(storageDirectory);

    // Create AWS list objects response with a zero-byte S3 file.
    S3ObjectSummary s3ObjectSummary = new S3ObjectSummary();
    s3ObjectSummary.setKey(S3_KEY);
    s3ObjectSummary.setSize(FILE_SIZE_0_BYTE);
    objectListing = Mockito.mock(ObjectListing.class);
    when(objectListing.getObjectSummaries()).thenReturn(Lists.newArrayList(s3ObjectSummary));

    // Create AWS get object request.
    GetObjectRequest getObjectRequest = new GetObjectRequest(S3_BUCKET_NAME, S3_KEY).withRange(0, MAX_BYTE_DOWNLOAD);

    // Create S3 object.
    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new ByteArrayInputStream(RandomStringUtils.randomAlphabetic(MAX_BYTE_DOWNLOAD).getBytes()));

    // Create S3 object metadata.
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(MAX_BYTE_DOWNLOAD);

    // Mock the external calls.
    when(propertiesHelper.getProperty(HERD_BASE_URL_PROPERTY)).thenReturn(HERD_BASE_URL);
    when(propertiesHelper.getProperty(HERD_USERNAME_PROPERTY)).thenReturn(HERD_USERNAME);
    when(propertiesHelper.getProperty(HERD_PASSWORD_PROPERTY)).thenReturn(HERD_PASSWORD);
    when(propertiesHelper.getProperty(AWS_REGION_PROPERTY)).thenReturn(AWS_REGION_NAME_US_EAST_1);
    when(propertiesHelper.getProperty(AWS_ROLE_ARN_PROPERTY)).thenReturn(AWS_ROLE_ARN);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_VERSION_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_VERSION.toString());
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_DATA_VERSION_PROPERTY)).thenReturn(BUSINESS_OBJECT_DATA_VERSION.toString());
    when(propertiesHelper.getProperty(NAMESPACE_PROPERTY)).thenReturn(NAMESPACE);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_DEFINITION_NAME_PROPERTY)).thenReturn(BUSINESS_OBJECT_DEFINITION_NAME);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_USAGE_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_USAGE);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_FILE_TYPE_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_FILE_TYPE);
    when(propertiesHelper.getProperty(PRIMARY_PARTITION_VALUE_PROPERTY)).thenReturn(PRIMARY_PARTITION_VALUE);
    when(propertiesHelper.getProperty(SUB_PARTITION_VALUES_PROPERTY)).thenReturn(SUB_PARTITION_VALUES);
    when(herdApiClientOperations
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(SUB_PARTITION_VALUES),
            eq(BUSINESS_OBJECT_FORMAT_VERSION), eq(BUSINESS_OBJECT_DATA_VERSION), eq(null), eq(false), eq(false))).thenReturn(businessObjectData);
    when(s3Operations.listObjects(any(ListObjectsRequest.class), any(AmazonS3.class))).thenReturn(objectListing);

    // Call the method under test with message flag set to "false".
    accessValidatorController.validateAccess(new File(PROPERTIES_FILE_PATH), false);

    // Verify the external calls.
    verify(herdApiClientOperations).checkPropertiesFile(propertiesHelper, false);
    verify(propertiesHelper).loadProperties(new File(PROPERTIES_FILE_PATH));
    verify(propertiesHelper).getProperty(HERD_BASE_URL_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_USERNAME_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_PASSWORD_PROPERTY);
    verify(herdApiClientOperations).applicationGetBuildInfo(any(ApplicationApi.class));
    verify(herdApiClientOperations).currentUserGetCurrentUser(any(CurrentUserApi.class));
    verify(propertiesHelper).getProperty(AWS_REGION_PROPERTY);
    verify(propertiesHelper).getProperty(AWS_ROLE_ARN_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_VERSION_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_DATA_VERSION_PROPERTY);
    verify(propertiesHelper).getProperty(NAMESPACE_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_DEFINITION_NAME_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_USAGE_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_FILE_TYPE_PROPERTY);
    verify(propertiesHelper).getProperty(PRIMARY_PARTITION_VALUE_PROPERTY);
    verify(propertiesHelper).getProperty(SUB_PARTITION_VALUES_PROPERTY);
    verify(herdApiClientOperations)
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(SUB_PARTITION_VALUES),
            eq(BUSINESS_OBJECT_FORMAT_VERSION), eq(BUSINESS_OBJECT_DATA_VERSION), eq(null), eq(false), eq(false));
    verify(s3Operations).listObjects(any(ListObjectsRequest.class), any(AmazonS3.class));
    verifyNoMoreInteractionsHelper();
}
 
Example 12
Source File: AccessValidatorControllerTest.java    From herd with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateAccessNoStorageFiles() throws Exception
{
    // Create business object data without any registered storage files, but with storage unit directory path and an actual non zero-byte S3 file.
    BusinessObjectData businessObjectData = createBusinessObjectData();
    businessObjectData.getStorageUnits().get(0).setStorageFiles(null);
    StorageDirectory storageDirectory = new StorageDirectory();
    storageDirectory.setDirectoryPath(STORAGE_DIRECTORY_PATH);
    businessObjectData.getStorageUnits().get(0).setStorageDirectory(storageDirectory);

    // Create AWS list objects response with a non zero-byte S3 file.
    S3ObjectSummary s3ObjectSummary = new S3ObjectSummary();
    s3ObjectSummary.setKey(S3_KEY);
    s3ObjectSummary.setSize(FILE_SIZE_1_KB);
    objectListing = Mockito.mock(ObjectListing.class);
    when(objectListing.getObjectSummaries()).thenReturn(Lists.newArrayList(s3ObjectSummary));

    // Create AWS get object request.
    GetObjectRequest getObjectRequest = new GetObjectRequest(S3_BUCKET_NAME, S3_KEY).withRange(0, MAX_BYTE_DOWNLOAD);

    // Create S3 object.
    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new ByteArrayInputStream(RandomStringUtils.randomAlphabetic(MAX_BYTE_DOWNLOAD).getBytes()));

    // Create S3 object metadata.
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(MAX_BYTE_DOWNLOAD);

    // Mock the external calls.
    when(propertiesHelper.getProperty(HERD_BASE_URL_PROPERTY)).thenReturn(HERD_BASE_URL);
    when(propertiesHelper.getProperty(HERD_USERNAME_PROPERTY)).thenReturn(HERD_USERNAME);
    when(propertiesHelper.getProperty(HERD_PASSWORD_PROPERTY)).thenReturn(HERD_PASSWORD);
    when(propertiesHelper.getProperty(AWS_REGION_PROPERTY)).thenReturn(AWS_REGION_NAME_US_EAST_1);
    when(propertiesHelper.getProperty(AWS_ROLE_ARN_PROPERTY)).thenReturn(AWS_ROLE_ARN);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_VERSION_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_VERSION.toString());
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_DATA_VERSION_PROPERTY)).thenReturn(BUSINESS_OBJECT_DATA_VERSION.toString());
    when(propertiesHelper.getProperty(NAMESPACE_PROPERTY)).thenReturn(NAMESPACE);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_DEFINITION_NAME_PROPERTY)).thenReturn(BUSINESS_OBJECT_DEFINITION_NAME);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_USAGE_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_USAGE);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_FILE_TYPE_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_FILE_TYPE);
    when(propertiesHelper.getProperty(PRIMARY_PARTITION_VALUE_PROPERTY)).thenReturn(PRIMARY_PARTITION_VALUE);
    when(propertiesHelper.getProperty(SUB_PARTITION_VALUES_PROPERTY)).thenReturn(SUB_PARTITION_VALUES);
    when(herdApiClientOperations
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(SUB_PARTITION_VALUES),
            eq(BUSINESS_OBJECT_FORMAT_VERSION), eq(BUSINESS_OBJECT_DATA_VERSION), eq(null), eq(false), eq(false))).thenReturn(businessObjectData);
    when(s3Operations.listObjects(any(ListObjectsRequest.class), any(AmazonS3.class))).thenReturn(objectListing);
    when(s3Operations.getS3Object(eq(getObjectRequest), any(AmazonS3.class))).thenReturn(s3Object);

    // Call the method under test with message flag set to "false".
    accessValidatorController.validateAccess(new File(PROPERTIES_FILE_PATH), false);

    // Verify the external calls.
    verify(herdApiClientOperations).checkPropertiesFile(propertiesHelper, false);
    verify(propertiesHelper).loadProperties(new File(PROPERTIES_FILE_PATH));
    verify(propertiesHelper).getProperty(HERD_BASE_URL_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_USERNAME_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_PASSWORD_PROPERTY);
    verify(herdApiClientOperations).applicationGetBuildInfo(any(ApplicationApi.class));
    verify(herdApiClientOperations).currentUserGetCurrentUser(any(CurrentUserApi.class));
    verify(propertiesHelper).getProperty(AWS_REGION_PROPERTY);
    verify(propertiesHelper).getProperty(AWS_ROLE_ARN_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_VERSION_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_DATA_VERSION_PROPERTY);
    verify(propertiesHelper).getProperty(NAMESPACE_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_DEFINITION_NAME_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_USAGE_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_FILE_TYPE_PROPERTY);
    verify(propertiesHelper).getProperty(PRIMARY_PARTITION_VALUE_PROPERTY);
    verify(propertiesHelper).getProperty(SUB_PARTITION_VALUES_PROPERTY);
    verify(herdApiClientOperations)
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(SUB_PARTITION_VALUES),
            eq(BUSINESS_OBJECT_FORMAT_VERSION), eq(BUSINESS_OBJECT_DATA_VERSION), eq(null), eq(false), eq(false));
    verify(s3Operations).listObjects(any(ListObjectsRequest.class), any(AmazonS3.class));
    verify(s3Operations).getS3Object(eq(getObjectRequest), any(AmazonS3.class));
    verifyNoMoreInteractionsHelper();
}
 
Example 13
Source File: AccessValidatorControllerTest.java    From herd with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateAccessMissingOptionalProperties() throws Exception
{
    // Create business object data.
    BusinessObjectData businessObjectData = createBusinessObjectData();

    // Create AWS get object request.
    GetObjectRequest getObjectRequest = new GetObjectRequest(S3_BUCKET_NAME, S3_KEY).withRange(0, MAX_BYTE_DOWNLOAD);

    // Create S3 object.
    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new ByteArrayInputStream(RandomStringUtils.randomAlphabetic(MAX_BYTE_DOWNLOAD).getBytes()));

    // Create S3 object metadata.
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(MAX_BYTE_DOWNLOAD);

    // Mock the external calls.
    when(propertiesHelper.getProperty(HERD_BASE_URL_PROPERTY)).thenReturn(HERD_BASE_URL);
    when(propertiesHelper.getProperty(HERD_USERNAME_PROPERTY)).thenReturn(HERD_USERNAME);
    when(propertiesHelper.getProperty(HERD_PASSWORD_PROPERTY)).thenReturn(HERD_PASSWORD);
    when(propertiesHelper.getProperty(AWS_REGION_PROPERTY)).thenReturn(AWS_REGION_NAME_US_EAST_1);
    when(propertiesHelper.getProperty(AWS_ROLE_ARN_PROPERTY)).thenReturn(AWS_ROLE_ARN);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_VERSION_PROPERTY)).thenReturn(null);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_DATA_VERSION_PROPERTY)).thenReturn(null);
    when(propertiesHelper.getProperty(NAMESPACE_PROPERTY)).thenReturn(NAMESPACE);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_DEFINITION_NAME_PROPERTY)).thenReturn(BUSINESS_OBJECT_DEFINITION_NAME);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_USAGE_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_USAGE);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_FILE_TYPE_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_FILE_TYPE);
    when(propertiesHelper.getProperty(PRIMARY_PARTITION_VALUE_PROPERTY)).thenReturn(PRIMARY_PARTITION_VALUE);
    when(propertiesHelper.getProperty(SUB_PARTITION_VALUES_PROPERTY)).thenReturn(null);
    when(herdApiClientOperations
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(null), eq(null), eq(null),
            eq(null), eq(false), eq(false))).thenReturn(businessObjectData);
    when(s3Operations.getObjectMetadata(any(), any(), any())).thenReturn(objectMetadata);
    when(s3Operations.getS3Object(eq(getObjectRequest), any(AmazonS3.class))).thenReturn(s3Object);

    // Call the method under test with message flag set to "false".
    accessValidatorController.validateAccess(new File(PROPERTIES_FILE_PATH), false);

    // Verify the external calls.
    verify(herdApiClientOperations).checkPropertiesFile(propertiesHelper, false);
    verify(propertiesHelper).loadProperties(new File(PROPERTIES_FILE_PATH));
    verify(propertiesHelper).getProperty(HERD_BASE_URL_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_USERNAME_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_PASSWORD_PROPERTY);
    verify(herdApiClientOperations).applicationGetBuildInfo(any(ApplicationApi.class));
    verify(herdApiClientOperations).currentUserGetCurrentUser(any(CurrentUserApi.class));
    verify(propertiesHelper).getProperty(AWS_REGION_PROPERTY);
    verify(propertiesHelper).getProperty(AWS_ROLE_ARN_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_VERSION_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_DATA_VERSION_PROPERTY);
    verify(propertiesHelper).getProperty(NAMESPACE_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_DEFINITION_NAME_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_USAGE_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_FILE_TYPE_PROPERTY);
    verify(propertiesHelper).getProperty(PRIMARY_PARTITION_VALUE_PROPERTY);
    verify(propertiesHelper).getProperty(SUB_PARTITION_VALUES_PROPERTY);
    verify(herdApiClientOperations)
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(null), eq(null), eq(null),
            eq(null), eq(false), eq(false));
    verify(s3Operations).getObjectMetadata(any(), any(), any());
    verify(s3Operations).getS3Object(eq(getObjectRequest), any(AmazonS3.class));
    verifyNoMoreInteractionsHelper();
}
 
Example 14
Source File: TestFetchS3Object.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetObject() throws IOException {
    runner.setProperty(FetchS3Object.REGION, "us-east-1");
    runner.setProperty(FetchS3Object.BUCKET, "request-bucket");
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "request-key");
    runner.enqueue(new byte[0], attrs);

    S3Object s3ObjectResponse = new S3Object();
    s3ObjectResponse.setBucketName("response-bucket-name");
    s3ObjectResponse.setKey("response-key");
    s3ObjectResponse.setObjectContent(new StringInputStream("Some Content"));
    ObjectMetadata metadata = Mockito.spy(ObjectMetadata.class);
    metadata.setContentDisposition("key/path/to/file.txt");
    metadata.setContentType("text/plain");
    metadata.setContentMD5("testMD5hash");
    Date expiration = new Date();
    metadata.setExpirationTime(expiration);
    metadata.setExpirationTimeRuleId("testExpirationRuleId");
    Map<String, String> userMetadata = new HashMap<>();
    userMetadata.put("userKey1", "userValue1");
    userMetadata.put("userKey2", "userValue2");
    metadata.setUserMetadata(userMetadata);
    metadata.setSSEAlgorithm("testAlgorithm");
    Mockito.when(metadata.getETag()).thenReturn("test-etag");
    s3ObjectResponse.setObjectMetadata(metadata);
    Mockito.when(mockS3Client.getObject(Mockito.any())).thenReturn(s3ObjectResponse);

    runner.run(1);

    ArgumentCaptor<GetObjectRequest> captureRequest = ArgumentCaptor.forClass(GetObjectRequest.class);
    Mockito.verify(mockS3Client, Mockito.times(1)).getObject(captureRequest.capture());
    GetObjectRequest request = captureRequest.getValue();
    assertEquals("request-bucket", request.getBucketName());
    assertEquals("request-key", request.getKey());
    assertFalse(request.isRequesterPays());
    assertNull(request.getVersionId());

    runner.assertAllFlowFilesTransferred(FetchS3Object.REL_SUCCESS, 1);
    final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchS3Object.REL_SUCCESS);
    MockFlowFile ff = ffs.get(0);
    ff.assertAttributeEquals("s3.bucket", "response-bucket-name");
    ff.assertAttributeEquals(CoreAttributes.FILENAME.key(), "file.txt");
    ff.assertAttributeEquals(CoreAttributes.PATH.key(), "key/path/to");
    ff.assertAttributeEquals(CoreAttributes.ABSOLUTE_PATH.key(), "key/path/to/file.txt");
    ff.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "text/plain");
    ff.assertAttributeEquals("hash.value", "testMD5hash");
    ff.assertAttributeEquals("hash.algorithm", "MD5");
    ff.assertAttributeEquals("s3.etag", "test-etag");
    ff.assertAttributeEquals("s3.expirationTime", String.valueOf(expiration.getTime()));
    ff.assertAttributeEquals("s3.expirationTimeRuleId", "testExpirationRuleId");
    ff.assertAttributeEquals("userKey1", "userValue1");
    ff.assertAttributeEquals("userKey2", "userValue2");
    ff.assertAttributeEquals("s3.sseAlgorithm", "testAlgorithm");
    ff.assertContentEquals("Some Content");
}
 
Example 15
Source File: TestFetchS3Object.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetObjectWithRequesterPays() throws IOException {
    runner.setProperty(FetchS3Object.REGION, "us-east-1");
    runner.setProperty(FetchS3Object.BUCKET, "request-bucket");
    runner.setProperty(FetchS3Object.REQUESTER_PAYS, "true");
    final Map<String, String> attrs = new HashMap<>();
    attrs.put("filename", "request-key");
    runner.enqueue(new byte[0], attrs);

    S3Object s3ObjectResponse = new S3Object();
    s3ObjectResponse.setBucketName("response-bucket-name");
    s3ObjectResponse.setKey("response-key");
    s3ObjectResponse.setObjectContent(new StringInputStream("Some Content"));
    ObjectMetadata metadata = Mockito.spy(ObjectMetadata.class);
    metadata.setContentDisposition("key/path/to/file.txt");
    metadata.setContentType("text/plain");
    metadata.setContentMD5("testMD5hash");
    Date expiration = new Date();
    metadata.setExpirationTime(expiration);
    metadata.setExpirationTimeRuleId("testExpirationRuleId");
    Map<String, String> userMetadata = new HashMap<>();
    userMetadata.put("userKey1", "userValue1");
    userMetadata.put("userKey2", "userValue2");
    metadata.setUserMetadata(userMetadata);
    metadata.setSSEAlgorithm("testAlgorithm");
    Mockito.when(metadata.getETag()).thenReturn("test-etag");
    s3ObjectResponse.setObjectMetadata(metadata);
    Mockito.when(mockS3Client.getObject(Mockito.any())).thenReturn(s3ObjectResponse);

    runner.run(1);

    ArgumentCaptor<GetObjectRequest> captureRequest = ArgumentCaptor.forClass(GetObjectRequest.class);
    Mockito.verify(mockS3Client, Mockito.times(1)).getObject(captureRequest.capture());
    GetObjectRequest request = captureRequest.getValue();
    assertEquals("request-bucket", request.getBucketName());
    assertEquals("request-key", request.getKey());
    assertTrue(request.isRequesterPays());
    assertNull(request.getVersionId());

    runner.assertAllFlowFilesTransferred(FetchS3Object.REL_SUCCESS, 1);
    final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchS3Object.REL_SUCCESS);
    MockFlowFile ff = ffs.get(0);
    ff.assertAttributeEquals("s3.bucket", "response-bucket-name");
    ff.assertAttributeEquals(CoreAttributes.FILENAME.key(), "file.txt");
    ff.assertAttributeEquals(CoreAttributes.PATH.key(), "key/path/to");
    ff.assertAttributeEquals(CoreAttributes.ABSOLUTE_PATH.key(), "key/path/to/file.txt");
    ff.assertAttributeEquals(CoreAttributes.MIME_TYPE.key(), "text/plain");
    ff.assertAttributeEquals("hash.value", "testMD5hash");
    ff.assertAttributeEquals("hash.algorithm", "MD5");
    ff.assertAttributeEquals("s3.etag", "test-etag");
    ff.assertAttributeEquals("s3.expirationTime", String.valueOf(expiration.getTime()));
    ff.assertAttributeEquals("s3.expirationTimeRuleId", "testExpirationRuleId");
    ff.assertAttributeEquals("userKey1", "userValue1");
    ff.assertAttributeEquals("userKey2", "userValue2");
    ff.assertAttributeEquals("s3.sseAlgorithm", "testAlgorithm");
    ff.assertContentEquals("Some Content");
}
 
Example 16
Source File: AccessValidatorControllerTest.java    From herd with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateAccessMessageOptionEnabled() throws Exception
{
    // Create business object data.
    BusinessObjectData businessObjectData = createBusinessObjectData();

    // Get business object data key.
    BusinessObjectDataKey businessObjectDataKey =
        new BusinessObjectDataKey(NAMESPACE, BUSINESS_OBJECT_DEFINITION_NAME, BUSINESS_OBJECT_FORMAT_USAGE, BUSINESS_OBJECT_FORMAT_FILE_TYPE,
            BUSINESS_OBJECT_FORMAT_VERSION, PRIMARY_PARTITION_VALUE, Lists.newArrayList(SUB_PARTITION_VALUES), BUSINESS_OBJECT_DATA_VERSION);

    // Create AWS get object request.
    GetObjectRequest getObjectRequest = new GetObjectRequest(S3_BUCKET_NAME, S3_KEY).withRange(0, MAX_BYTE_DOWNLOAD);

    // Create S3 object.
    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new ByteArrayInputStream(RandomStringUtils.randomAlphabetic(MAX_BYTE_DOWNLOAD).getBytes()));

    // Create S3 object metadata.
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(MAX_BYTE_DOWNLOAD);

    // Mock the external calls.
    when(propertiesHelper.getProperty(HERD_BASE_URL_PROPERTY)).thenReturn(HERD_BASE_URL);
    when(propertiesHelper.getProperty(HERD_USERNAME_PROPERTY)).thenReturn(HERD_USERNAME);
    when(propertiesHelper.getProperty(HERD_PASSWORD_PROPERTY)).thenReturn(HERD_PASSWORD);
    when(propertiesHelper.getProperty(AWS_REGION_PROPERTY)).thenReturn(AWS_REGION_NAME_US_EAST_1);
    when(propertiesHelper.getProperty(AWS_ROLE_ARN_PROPERTY)).thenReturn(AWS_ROLE_ARN);
    when(propertiesHelper.getProperty(AWS_SQS_QUEUE_URL_PROPERTY)).thenReturn(AWS_SQS_QUEUE_URL);
    when(herdApiClientOperations.getBdataKeySqs(any(AmazonSQS.class), eq(AWS_SQS_QUEUE_URL))).thenReturn(businessObjectDataKey);
    when(herdApiClientOperations
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(SUB_PARTITION_VALUES),
            eq(BUSINESS_OBJECT_FORMAT_VERSION), eq(BUSINESS_OBJECT_DATA_VERSION), eq(null), eq(false), eq(false))).thenReturn(businessObjectData);
    when(s3Operations.getObjectMetadata(any(), any(), any())).thenReturn(objectMetadata);
    when(s3Operations.getS3Object(eq(getObjectRequest), any(AmazonS3.class))).thenReturn(s3Object);

    // Call the method under test with message flag set to "true".
    accessValidatorController.validateAccess(new File(PROPERTIES_FILE_PATH), true);

    // Verify the external calls.
    verify(herdApiClientOperations).checkPropertiesFile(propertiesHelper, true);
    verify(propertiesHelper).loadProperties(new File(PROPERTIES_FILE_PATH));
    verify(propertiesHelper).getProperty(HERD_BASE_URL_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_USERNAME_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_PASSWORD_PROPERTY);
    verify(herdApiClientOperations).applicationGetBuildInfo(any(ApplicationApi.class));
    verify(herdApiClientOperations).currentUserGetCurrentUser(any(CurrentUserApi.class));
    verify(propertiesHelper).getProperty(AWS_REGION_PROPERTY);
    verify(propertiesHelper).getProperty(AWS_ROLE_ARN_PROPERTY);
    verify(propertiesHelper).getProperty(AWS_SQS_QUEUE_URL_PROPERTY);
    verify(herdApiClientOperations).getBdataKeySqs(any(AmazonSQS.class), eq(AWS_SQS_QUEUE_URL));
    verify(herdApiClientOperations)
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(SUB_PARTITION_VALUES),
            eq(BUSINESS_OBJECT_FORMAT_VERSION), eq(BUSINESS_OBJECT_DATA_VERSION), eq(null), eq(false), eq(false));
    verify(s3Operations).getObjectMetadata(any(), any(), any());
    verify(s3Operations).getS3Object(eq(getObjectRequest), any(AmazonS3.class));
    verifyNoMoreInteractionsHelper();
}
 
Example 17
Source File: AccessValidatorControllerTest.java    From herd with Apache License 2.0 4 votes vote down vote up
@Test
public void testValidateAccess() throws Exception
{
    // Create business object data.
    BusinessObjectData businessObjectData = createBusinessObjectData();

    // Create AWS get object request.
    GetObjectRequest getObjectRequest = new GetObjectRequest(S3_BUCKET_NAME, S3_KEY).withRange(0, MAX_BYTE_DOWNLOAD);

    // Create S3 object.
    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new ByteArrayInputStream(RandomStringUtils.randomAlphabetic(MAX_BYTE_DOWNLOAD).getBytes()));

    // Create S3 object metadata.
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(MAX_BYTE_DOWNLOAD);

    // Mock the external calls.
    when(propertiesHelper.getProperty(HERD_BASE_URL_PROPERTY)).thenReturn(HERD_BASE_URL);
    when(propertiesHelper.getProperty(HERD_USERNAME_PROPERTY)).thenReturn(HERD_USERNAME);
    when(propertiesHelper.getProperty(HERD_PASSWORD_PROPERTY)).thenReturn(HERD_PASSWORD);
    when(propertiesHelper.getProperty(AWS_REGION_PROPERTY)).thenReturn(AWS_REGION_NAME_US_EAST_1);
    when(propertiesHelper.getProperty(AWS_ROLE_ARN_PROPERTY)).thenReturn(AWS_ROLE_ARN);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_VERSION_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_VERSION.toString());
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_DATA_VERSION_PROPERTY)).thenReturn(BUSINESS_OBJECT_DATA_VERSION.toString());
    when(propertiesHelper.getProperty(NAMESPACE_PROPERTY)).thenReturn(NAMESPACE);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_DEFINITION_NAME_PROPERTY)).thenReturn(BUSINESS_OBJECT_DEFINITION_NAME);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_USAGE_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_USAGE);
    when(propertiesHelper.getProperty(BUSINESS_OBJECT_FORMAT_FILE_TYPE_PROPERTY)).thenReturn(BUSINESS_OBJECT_FORMAT_FILE_TYPE);
    when(propertiesHelper.getProperty(PRIMARY_PARTITION_VALUE_PROPERTY)).thenReturn(PRIMARY_PARTITION_VALUE);
    when(propertiesHelper.getProperty(SUB_PARTITION_VALUES_PROPERTY)).thenReturn(SUB_PARTITION_VALUES);
    when(herdApiClientOperations
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(SUB_PARTITION_VALUES),
            eq(BUSINESS_OBJECT_FORMAT_VERSION), eq(BUSINESS_OBJECT_DATA_VERSION), eq(null), eq(false), eq(false))).thenReturn(businessObjectData);
    when(s3Operations.getObjectMetadata(any(), any(), any())).thenReturn(objectMetadata);
    when(s3Operations.getS3Object(eq(getObjectRequest), any(AmazonS3.class))).thenReturn(s3Object);

    // Call the method under test with message flag set to "false".
    accessValidatorController.validateAccess(new File(PROPERTIES_FILE_PATH), false);

    // Verify the external calls.
    verify(herdApiClientOperations).checkPropertiesFile(propertiesHelper, false);
    verify(propertiesHelper).loadProperties(new File(PROPERTIES_FILE_PATH));
    verify(propertiesHelper).getProperty(HERD_BASE_URL_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_USERNAME_PROPERTY);
    verify(propertiesHelper).getProperty(HERD_PASSWORD_PROPERTY);
    verify(herdApiClientOperations).applicationGetBuildInfo(any(ApplicationApi.class));
    verify(herdApiClientOperations).currentUserGetCurrentUser(any(CurrentUserApi.class));
    verify(propertiesHelper).getProperty(AWS_REGION_PROPERTY);
    verify(propertiesHelper).getProperty(AWS_ROLE_ARN_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_VERSION_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_DATA_VERSION_PROPERTY);
    verify(propertiesHelper).getProperty(NAMESPACE_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_DEFINITION_NAME_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_USAGE_PROPERTY);
    verify(propertiesHelper).getProperty(BUSINESS_OBJECT_FORMAT_FILE_TYPE_PROPERTY);
    verify(propertiesHelper).getProperty(PRIMARY_PARTITION_VALUE_PROPERTY);
    verify(propertiesHelper).getProperty(SUB_PARTITION_VALUES_PROPERTY);
    verify(herdApiClientOperations)
        .businessObjectDataGetBusinessObjectData(any(BusinessObjectDataApi.class), eq(NAMESPACE), eq(BUSINESS_OBJECT_DEFINITION_NAME),
            eq(BUSINESS_OBJECT_FORMAT_USAGE), eq(BUSINESS_OBJECT_FORMAT_FILE_TYPE), eq(null), eq(PRIMARY_PARTITION_VALUE), eq(SUB_PARTITION_VALUES),
            eq(BUSINESS_OBJECT_FORMAT_VERSION), eq(BUSINESS_OBJECT_DATA_VERSION), eq(null), eq(false), eq(false));
    verify(s3Operations).getObjectMetadata(any(), any(), any());
    verify(s3Operations).getS3Object(eq(getObjectRequest), any(AmazonS3.class));
    verifyNoMoreInteractionsHelper();
}
 
Example 18
Source File: StashReaderTest.java    From emodb with Apache License 2.0 4 votes vote down vote up
/**
 * Note:  This test depends on the default character set to be US-ASCII
 */
@Test
public void testUTF8ClientDecoding() throws Exception {
    if (!Charset.defaultCharset().equals(Charsets.US_ASCII)) {
        throw new SkipException("testUTF8ClientDecoding() requires default charset to be US-ASCII");
    }

    String utf8Text = "ชิงรางวัลกันจ้า";
    ByteArrayOutputStream splitOut = new ByteArrayOutputStream();

    try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(splitOut), Charsets.UTF_8))) {
        Map<String, Object> value = ImmutableMap.<String, Object>builder()
                .put("~id", "1")
                .put("~table", "test:table")
                .put("~version", 1)
                .put("~signature", "3a0da59fabf298d389b7b0b59728e887")
                .put("~lastUpdateAt", "2014-08-28T21:24:36.440Z")
                .put("~firstUpdateAt", "2014-06-07T09:51:40.077Z")
                .put("text", utf8Text)
                .build();

        String json = JsonHelper.asJson(value);
        out.write(json);
        out.write("\n");
    }

    splitOut.close();

    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(new ByteArrayInputStream(splitOut.toByteArray(), 0, splitOut.size()));
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(splitOut.size());
    s3Object.setObjectMetadata(objectMetadata);

    AmazonS3 s3 = mock(AmazonS3.class);
    when(s3.getObject(argThat(getsObject("stash-bucket", "stash/test/2015-01-01-00-00-00/test-table/split0.gz"))))
            .thenReturn(s3Object);

    StashSplit stashSplit = new StashSplit("test:table", "2015-01-01-00-00-00/test-table/split0.gz", splitOut.size());

    StandardStashReader reader = new StandardStashReader(URI.create("s3://stash-bucket/stash/test"), s3, 0);
    try (StashRowIterator rowIter = reader.getSplit(stashSplit)) {
        assertTrue(rowIter.hasNext());
        Map<String, Object> row = rowIter.next();
        assertEquals(row.get("text"), utf8Text);
    }
}