From 66722458d196e4ebd3dec0499431b785a0a731aa Mon Sep 17 00:00:00 2001 From: Olivier Lepage-Applin Date: Wed, 8 Jan 2025 12:55:24 -0500 Subject: [PATCH 1/6] create a fromInputStream static method for creating events from input stream --- .../s3/model/S3EventNotification.java | 12 ++ .../model/S3EventNotificationReaderTest.java | 125 ++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.java b/services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.java index d3e94e92c2ca..043e0162cb20 100644 --- a/services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.java +++ b/services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.java @@ -72,11 +72,23 @@ public static S3EventNotification fromJson(byte[] json) { * Any extra fields will be ignored. * @param json the notification message in json format * @return an instance of notification message S3EventNotification + * @see S3EventNotification#fromInputStream */ public S3EventNotification fromJson(InputStream json) { return S3EventNotificationReader.create().read(json); } + /** + * Converts a json representation of the notification message to an instance of S3EventNotification. Any missing fields + * of the json will be null in the resulting object. + * Any extra fields will be ignored. + * @param json the notification message in json format + * @return an instance of notification message S3EventNotification + */ + public static S3EventNotification fromInputStream(InputStream json) { + return S3EventNotificationReader.create().read(json); + } + /** * Serialize this instance to json format. {@link GlacierEventData}, {@link ReplicationEventData}, * {@link IntelligentTieringEventData} and {@link LifecycleEventData} keys diff --git a/services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java b/services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java index ff369943bc1f..1ab901639aa3 100644 --- a/services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java +++ b/services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java @@ -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; @@ -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.fromInputStream(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(); + + } } From 1a97ac4350f1d49f79eb16a9553f14e36dedeadc Mon Sep 17 00:00:00 2001 From: Olivier Lepage-Applin Date: Wed, 8 Jan 2025 13:03:44 -0500 Subject: [PATCH 2/6] changelog entry --- .changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json diff --git a/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json b/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json new file mode 100644 index 000000000000..bcbf8e9036da --- /dev/null +++ b/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json @@ -0,0 +1,6 @@ +{ + "type": "bugfix", + "category": "AWS SDK for Java v2", + "contributor": "", + "description": "add missing static method for creating S3EventNotification from input stream directly" +} From d50544df17866a7820c8a5e21abecb764232a84d Mon Sep 17 00:00:00 2001 From: Olivier Lepage-Applin Date: Thu, 9 Jan 2025 09:08:19 -0500 Subject: [PATCH 3/6] use static on original method instead --- .../s3/model/S3EventNotification.java | 14 +------------- .../s3/model/S3EventNotificationReaderTest.java | 2 +- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.java b/services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.java index 043e0162cb20..aaf506e2cb16 100644 --- a/services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.java +++ b/services-custom/s3-event-notifications/src/main/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotification.java @@ -72,20 +72,8 @@ public static S3EventNotification fromJson(byte[] json) { * Any extra fields will be ignored. * @param json the notification message in json format * @return an instance of notification message S3EventNotification - * @see S3EventNotification#fromInputStream */ - public S3EventNotification fromJson(InputStream json) { - return S3EventNotificationReader.create().read(json); - } - - /** - * Converts a json representation of the notification message to an instance of S3EventNotification. Any missing fields - * of the json will be null in the resulting object. - * Any extra fields will be ignored. - * @param json the notification message in json format - * @return an instance of notification message S3EventNotification - */ - public static S3EventNotification fromInputStream(InputStream json) { + public static S3EventNotification fromJson(InputStream json) { return S3EventNotificationReader.create().read(json); } diff --git a/services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java b/services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java index 1ab901639aa3..ef9d2c89154a 100644 --- a/services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java +++ b/services-custom/s3-event-notifications/src/test/java/software/amazon/awssdk/eventnotifications/s3/model/S3EventNotificationReaderTest.java @@ -498,7 +498,7 @@ void fromJsonInputStream_handlesCorrectly() { + " ]" + "}"; - S3EventNotification event = S3EventNotification.fromInputStream(new ByteArrayInputStream(eventJson.getBytes())); + S3EventNotification event = S3EventNotification.fromJson(new ByteArrayInputStream(eventJson.getBytes())); assertThat(event.getRecords()).hasSize(1); From 6ffb5bfc3ac3be48ffeb758914d22207a241e0be Mon Sep 17 00:00:00 2001 From: Olivier Lepage-Applin Date: Thu, 9 Jan 2025 09:50:13 -0500 Subject: [PATCH 4/6] exclude S3EventNotification from japicmp temporarily --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index cc7d8a9c8f52..a0f16c392812 100644 --- a/pom.xml +++ b/pom.xml @@ -680,6 +680,8 @@ software.amazon.awssdk.regions.* software.amazon.awssdk.utils.JavaSystemSetting + + software.amazon.awssdk.eventnotifications.s3.model.S3EventNotification true From 3602ffbae095205a43b9d4bf00c8c7ecb605aca0 Mon Sep 17 00:00:00 2001 From: Olivier Lepage-Applin Date: Thu, 9 Jan 2025 09:55:39 -0500 Subject: [PATCH 5/6] changelog --- .changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json b/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json index bcbf8e9036da..296b941f46c5 100644 --- a/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json +++ b/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json @@ -2,5 +2,5 @@ "type": "bugfix", "category": "AWS SDK for Java v2", "contributor": "", - "description": "add missing static method for creating S3EventNotification from input stream directly" + "description": "add static modifier to fromJson(InputStream) method of S3EventNotification" } From f2423ac9180724f8ddf62d550a43b6aaf09452c3 Mon Sep 17 00:00:00 2001 From: Olivier Lepage-Applin Date: Thu, 9 Jan 2025 12:34:34 -0500 Subject: [PATCH 6/6] changelog --- .changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json b/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json index 296b941f46c5..3a39e512c543 100644 --- a/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json +++ b/.changes/next-release/bugfix-AWSSDKforJavav2-8e9db48.json @@ -1,6 +1,6 @@ { "type": "bugfix", - "category": "AWS SDK for Java v2", + "category": "S3 Event Notification", "contributor": "", "description": "add static modifier to fromJson(InputStream) method of S3EventNotification" }