1616
1717import static org .assertj .core .api .Assertions .assertThat ;
1818
19+ import java .util .Arrays ;
20+ import java .util .List ;
21+ import java .util .stream .Stream ;
1922import org .junit .jupiter .api .Test ;
23+ import org .junit .jupiter .params .ParameterizedTest ;
24+ import org .junit .jupiter .params .provider .Arguments ;
25+ import org .junit .jupiter .params .provider .MethodSource ;
2026import software .amazon .awssdk .awscore .AwsExecutionAttribute ;
2127import software .amazon .awssdk .core .SdkRequest ;
2228import software .amazon .awssdk .core .interceptor .Context ;
2632import software .amazon .awssdk .services .s3 .model .CreateBucketConfiguration ;
2733import software .amazon .awssdk .services .s3 .model .CreateBucketRequest ;
2834import software .amazon .awssdk .services .s3 .model .LocationInfo ;
35+ import software .amazon .awssdk .services .s3 .model .Tag ;
2936
3037public class CreateBucketInterceptorTest {
3138
3239 @ Test
33- public void modifyRequest_DoesNotOverrideExistingLocationConstraint () {
40+ void modifyRequest_DoesNotOverrideExistingLocationConstraint () {
3441 CreateBucketRequest request = CreateBucketRequest .builder ()
3542 .bucket ("test-bucket" )
3643 .createBucketConfiguration (CreateBucketConfiguration .builder ()
@@ -51,7 +58,7 @@ public void modifyRequest_DoesNotOverrideExistingLocationConstraint() {
5158 }
5259
5360 @ Test
54- public void modifyRequest_UpdatesLocationConstraint_When_NullCreateBucketConfiguration () {
61+ void modifyRequest_UpdatesLocationConstraint_When_NullCreateBucketConfiguration () {
5562 CreateBucketRequest request = CreateBucketRequest .builder ()
5663 .bucket ("test-bucket" )
5764 .build ();
@@ -68,7 +75,7 @@ public void modifyRequest_UpdatesLocationConstraint_When_NullCreateBucketConfigu
6875 }
6976
7077 @ Test
71- public void modifyRequest_UpdatesLocationConstraint_When_NullLocationConstraint () {
78+ void modifyRequest_UpdatesLocationConstraint_When_NullLocationConstraint () {
7279 CreateBucketRequest request = CreateBucketRequest .builder ()
7380 .bucket ("test-bucket" )
7481 .createBucketConfiguration (CreateBucketConfiguration .builder ()
@@ -87,7 +94,7 @@ public void modifyRequest_UpdatesLocationConstraint_When_NullLocationConstraint(
8794 }
8895
8996 @ Test
90- public void modifyRequest_DoesNotSetLocationConstraint_When_LocationPresent () {
97+ void modifyRequest_DoesNotSetLocationConstraint_When_LocationPresent () {
9198 LocationInfo location = LocationInfo .builder ().name ("name" ).type ("type" ).build ();
9299 CreateBucketConfiguration configuration = CreateBucketConfiguration .builder ().location (location ).build ();
93100
@@ -112,7 +119,7 @@ public void modifyRequest_DoesNotSetLocationConstraint_When_LocationPresent() {
112119 * For us-east-1 there must not be a location constraint (or containing CreateBucketConfiguration) sent.
113120 */
114121 @ Test
115- public void modifyRequest_UsEast1_UsesNullBucketConfiguration () {
122+ void modifyRequest_UsEast1_UsesNullBucketConfiguration () {
116123 CreateBucketRequest request = CreateBucketRequest .builder ()
117124 .bucket ("test-bucket" )
118125 .createBucketConfiguration (CreateBucketConfiguration .builder ()
@@ -122,14 +129,14 @@ public void modifyRequest_UsEast1_UsesNullBucketConfiguration() {
122129 Context .ModifyRequest context = () -> request ;
123130
124131 ExecutionAttributes attributes = new ExecutionAttributes ()
125- .putAttribute (AwsExecutionAttribute .AWS_REGION , Region .US_EAST_1 );
132+ .putAttribute (AwsExecutionAttribute .AWS_REGION , Region .US_EAST_1 );
126133
127134 SdkRequest modifiedRequest = new CreateBucketInterceptor ().modifyRequest (context , attributes );
128135 assertThat (((CreateBucketRequest ) modifiedRequest ).createBucketConfiguration ()).isNull ();
129136 }
130137
131138 @ Test
132- public void modifyRequest__When_NullLocationConstraint () {
139+ void modifyRequest__When_NullLocationConstraint () {
133140 CreateBucketRequest request = CreateBucketRequest .builder ()
134141 .bucket ("test-bucket" )
135142 .createBucketConfiguration (CreateBucketConfiguration .builder ()
@@ -142,8 +149,74 @@ public void modifyRequest__When_NullLocationConstraint() {
142149 .putAttribute (AwsExecutionAttribute .AWS_REGION , Region .US_WEST_2 );
143150
144151 SdkRequest modifiedRequest = new CreateBucketInterceptor ().modifyRequest (context , attributes );
145- String locationConstraint = ((CreateBucketRequest ) modifiedRequest ).createBucketConfiguration ().locationConstraintAsString ();
152+ String locationConstraint =
153+ ((CreateBucketRequest ) modifiedRequest ).createBucketConfiguration ().locationConstraintAsString ();
146154
147155 assertThat (locationConstraint ).isEqualToIgnoringCase ("us-west-2" );
148156 }
157+
158+ /*
159+ Tags on create test cases:
160+ Region has tags locationConstraint Expected CreateBucketConfiguration body
161+ ----------------------------------------------------------------
162+ us-east-1 - tags - no locationConstraint: Body with tags only
163+ us-east-1 - tags - locationConstraint: Body with both (locationConstraint=EU)
164+ us-east-1 - no tags - no locationConstraint: Empty body (null)
165+ us-east-1 - no tags - locationConstraint: Body with locationConstraint=EU
166+
167+ us-west-2 - tags - no locationConstraint: Body with tags and locationConstraint=us-west-2
168+ us-west-2 - tags - locationConstraint: Body with both (locationConstraint=EU)
169+ us-west-2 - no tags - no locationConstraint: Body with locationConstraint=us-west-2
170+ us-west-2 - no tags - locationConstraint: Body with locationConstraint=EU
171+ */
172+ public static Stream <Arguments > createWithTagsTestParam () {
173+ List <Tag > tags = Arrays .asList (
174+ Tag .builder ().key ("foo-key" ).value ("foo-value" ).build (),
175+ Tag .builder ().key ("bar-key" ).value ("bar-value" ).build ()
176+ );
177+ return Stream .of (
178+ Arguments .of (Region .US_EAST_1 , tags , null ,
179+ CreateBucketConfiguration .builder ().tags (tags ).build ()),
180+ Arguments .of (Region .US_EAST_1 , tags , BucketLocationConstraint .EU ,
181+ CreateBucketConfiguration .builder ().tags (tags ).locationConstraint (BucketLocationConstraint .EU ).build ()),
182+ Arguments .of (Region .US_EAST_1 , null , null , null ),
183+ Arguments .of (Region .US_EAST_1 , null , BucketLocationConstraint .EU ,
184+ CreateBucketConfiguration .builder ().locationConstraint (BucketLocationConstraint .EU ).build ()),
185+
186+ Arguments .of (Region .US_WEST_2 , tags , null ,
187+ CreateBucketConfiguration .builder ().tags (tags ).locationConstraint (BucketLocationConstraint .US_WEST_2 ).build ()),
188+ Arguments .of (Region .US_WEST_2 , tags , BucketLocationConstraint .EU ,
189+ CreateBucketConfiguration .builder ().tags (tags ).locationConstraint (BucketLocationConstraint .EU ).build ()),
190+ Arguments .of (Region .US_WEST_2 , null , null ,
191+ CreateBucketConfiguration .builder ().locationConstraint (BucketLocationConstraint .US_WEST_2 ).build ()),
192+ Arguments .of (Region .US_WEST_2 , null , BucketLocationConstraint .EU ,
193+ CreateBucketConfiguration .builder ().locationConstraint (BucketLocationConstraint .EU ).build ())
194+ );
195+ }
196+
197+ @ ParameterizedTest
198+ @ MethodSource ("createWithTagsTestParam" )
199+ void modifyRequest__CreateWithTags (Region region ,
200+ List <Tag > tags ,
201+ BucketLocationConstraint locationConstraint ,
202+ CreateBucketConfiguration expected ) {
203+ CreateBucketRequest request = CreateBucketRequest .builder ()
204+ .bucket ("test-bucket" )
205+ .createBucketConfiguration (
206+ CreateBucketConfiguration .builder ()
207+ .tags (tags )
208+ .locationConstraint (locationConstraint )
209+ .build ())
210+ .build ();
211+
212+ Context .ModifyRequest context = () -> request ;
213+ ExecutionAttributes attributes = new ExecutionAttributes ().putAttribute (AwsExecutionAttribute .AWS_REGION , region );
214+
215+ CreateBucketRequest modifiedRequest =
216+ (CreateBucketRequest ) new CreateBucketInterceptor ().modifyRequest (context , attributes );
217+
218+ assertThat (modifiedRequest .createBucketConfiguration ()).isEqualTo (expected );
219+
220+ }
221+
149222}
0 commit comments