Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add static modifier to S3EventNotification from method with InputStream #5780

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "S3 Event Notification",
"contributor": "",
"description": "add static modifier to fromJson(InputStream) method of S3EventNotification"
}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@
<exclude>software.amazon.awssdk.regions.*</exclude>
<!-- TODO revert - Temporarily disable because system setting USER_REGION was removed -->
<exclude>software.amazon.awssdk.utils.JavaSystemSetting</exclude>
<!-- TODO revert when s3EventNotification changes to static method is merged -->
<exclude>software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification</exclude>
</excludes>

<ignoreMissingOldVersion>true</ignoreMissingOldVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static S3EventNotification fromJson(byte[] json) {
* @param json the notification message in json format
* @return an instance of notification message S3EventNotification
*/
public S3EventNotification fromJson(InputStream json) {
public static S3EventNotification fromJson(InputStream json) {
return S3EventNotificationReader.create().read(json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.ByteArrayInputStream;
import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -454,4 +455,128 @@ void malformedJson_throwsException() {
String json = "{\"Records\":[], \"toto\"}";
assertThatThrownBy(() -> S3EventNotification.fromJson(json)).hasCauseInstanceOf(JsonParseException.class);
}

@Test
void fromJsonInputStream_handlesCorrectly() {
String eventJson = "{ "
+ " \"Records\":[ "
+ " { "
+ " \"eventVersion\":\"2.1\","
+ " \"eventSource\":\"aws:s3\","
+ " \"awsRegion\":\"us-west-2\","
+ " \"eventTime\":\"1970-01-01T00:00:00.000Z\","
+ " \"eventName\":\"ObjectCreated:Put\","
+ " \"userIdentity\":{ "
+ " \"principalId\":\"AIDAJDPLRKLG7UEXAMPLE\""
+ " },"
+ " \"requestParameters\":{ "
+ " \"sourceIPAddress\":\"127.0.0.1\""
+ " },"
+ " \"responseElements\":{ "
+ " \"x-amz-request-id\":\"C3D13FE58DE4C810\","
+ " \"x-amz-id-2\":\"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD\""
+ " },"
+ " \"s3\":{ "
+ " \"s3SchemaVersion\":\"1.0\","
+ " \"configurationId\":\"testConfigRule\","
+ " \"bucket\":{ "
+ " \"name\":\"mybucket\","
+ " \"ownerIdentity\":{ "
+ " \"principalId\":\"A3NL1KOZZKExample\""
+ " },"
+ " \"arn\":\"arn:aws:s3:::mybucket\""
+ " },"
+ " \"object\":{ "
+ " \"key\":\"HappyFace.jpg\","
+ " \"size\":1024,"
+ " \"eTag\":\"d41d8cd98f00b204e9800998ecf8427e\","
+ " \"versionId\":\"096fKKXTRTtl3on89fVO.nfljtsv6qko\","
+ " \"sequencer\":\"0055AED6DCD90281E5\""
+ " }"
+ " }"
+ " }"
+ " ]"
+ "}";

S3EventNotification event = S3EventNotification.fromJson(new ByteArrayInputStream(eventJson.getBytes()));

assertThat(event.getRecords()).hasSize(1);

// verify constructors
S3EventNotification expected = new S3EventNotification(
Collections.singletonList(new S3EventNotificationRecord(
"us-west-2",
"ObjectCreated:Put",
"aws:s3",
"1970-01-01T00:00:00.000Z",
"2.1",
new RequestParameters("127.0.0.1"),
new ResponseElements(
"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD", "C3D13FE58DE4C810"),
new S3(
"testConfigRule",
new S3Bucket(
"mybucket",
new UserIdentity("A3NL1KOZZKExample"),
"arn:aws:s3:::mybucket"),
new S3Object(
"HappyFace.jpg",
1024L,
"d41d8cd98f00b204e9800998ecf8427e",
"096fKKXTRTtl3on89fVO.nfljtsv6qko",
"0055AED6DCD90281E5"),
"1.0"
),
new UserIdentity("AIDAJDPLRKLG7UEXAMPLE")
))
);
assertThat(event).isEqualTo(expected);

// verify getters
S3EventNotificationRecord rec = event.getRecords().get(0);
assertThat(rec).isNotNull();
assertThat(rec.getAwsRegion()).isEqualTo("us-west-2");
assertThat(rec.getEventName()).isEqualTo("ObjectCreated:Put");
assertThat(rec.getEventTime()).isEqualTo(Instant.parse("1970-01-01T00:00:00.000Z"));
assertThat(rec.getEventVersion()).isEqualTo("2.1");

UserIdentity userIdentity = rec.getUserIdentity();
assertThat(userIdentity).isNotNull();
assertThat(userIdentity.getPrincipalId()).isEqualTo("AIDAJDPLRKLG7UEXAMPLE");

RequestParameters requestParameters = rec.getRequestParameters();
assertThat(requestParameters).isNotNull();
assertThat(requestParameters.getSourceIpAddress()).isEqualTo("127.0.0.1");

ResponseElements responseElements = rec.getResponseElements();
assertThat(responseElements).isNotNull();
assertThat(responseElements.getXAmzRequestId()).isEqualTo("C3D13FE58DE4C810");
assertThat(responseElements.getXAmzId2())
.isEqualTo("FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD");

S3 s3 = rec.getS3();
assertThat(s3).isNotNull();
assertThat(s3.getS3SchemaVersion()).isEqualTo("1.0");
assertThat(s3.getConfigurationId()).isEqualTo("testConfigRule");
S3Bucket s3Bucket = s3.getBucket();
assertThat(s3Bucket).isNotNull();
assertThat(s3Bucket.getName()).isEqualTo("mybucket");
assertThat(s3Bucket.getArn()).isEqualTo("arn:aws:s3:::mybucket");
UserIdentity ownerIdentity = s3Bucket.getOwnerIdentity();
assertThat(ownerIdentity).isNotNull();
assertThat(ownerIdentity.getPrincipalId()).isEqualTo("A3NL1KOZZKExample");
S3Object s3Object = s3.getObject();
assertThat(s3Object).isNotNull();
assertThat(s3Object.getKey()).isEqualTo("HappyFace.jpg");
assertThat(s3Object.getETag()).isEqualTo("d41d8cd98f00b204e9800998ecf8427e");
assertThat(s3Object.getSizeAsLong()).isEqualTo(1024L);
assertThat(s3Object.getVersionId()).isEqualTo("096fKKXTRTtl3on89fVO.nfljtsv6qko");
assertThat(s3Object.getSequencer()).isEqualTo("0055AED6DCD90281E5");

assertThat(rec.getGlacierEventData()).isNull();
assertThat(rec.getIntelligentTieringEventData()).isNull();
assertThat(rec.getLifecycleEventData()).isNull();
assertThat(rec.getReplicationEventData()).isNull();

}
}
Loading