17
17
package org .springframework .cloud .config .server ;
18
18
19
19
import java .io .IOException ;
20
+ import java .util .Optional ;
20
21
22
+ import io .awspring .cloud .s3 .InMemoryBufferingS3OutputStreamProvider ;
23
+ import io .awspring .cloud .s3 .PropertiesS3ObjectContentTypeResolver ;
24
+ import io .awspring .cloud .s3 .S3ObjectContentTypeResolver ;
25
+ import io .awspring .cloud .s3 .S3OutputStreamProvider ;
26
+ import io .awspring .cloud .s3 .S3ProtocolResolver ;
21
27
import org .json .JSONException ;
22
28
import org .junit .jupiter .api .AfterAll ;
23
29
import org .junit .jupiter .api .BeforeAll ;
27
33
import org .testcontainers .junit .jupiter .Container ;
28
34
import org .testcontainers .junit .jupiter .Testcontainers ;
29
35
import org .testcontainers .utility .DockerImageName ;
36
+ import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
30
37
import software .amazon .awssdk .core .sync .RequestBody ;
38
+ import software .amazon .awssdk .regions .Region ;
31
39
import software .amazon .awssdk .services .s3 .S3Client ;
32
40
33
41
import org .springframework .boot .SpringApplication ;
34
42
import org .springframework .boot .web .client .RestTemplateBuilder ;
35
43
import org .springframework .cloud .config .environment .Environment ;
36
44
import org .springframework .cloud .config .server .test .TestConfigServerApplication ;
37
45
import org .springframework .context .ConfigurableApplicationContext ;
46
+ import org .springframework .context .annotation .Bean ;
47
+ import org .springframework .context .annotation .Import ;
38
48
import org .springframework .test .util .TestSocketUtils ;
39
49
import org .springframework .web .client .RestTemplate ;
40
50
@@ -58,7 +68,7 @@ public class AwsS3IntegrationTests {
58
68
59
69
@ BeforeAll
60
70
public static void startConfigServer () throws IOException , InterruptedException , JSONException {
61
- server = SpringApplication .run (new Class [] { TestConfigServerApplication .class },
71
+ server = SpringApplication .run (new Class [] { TestConfigServerApplication .class , S3AutoConfiguration . class },
62
72
new String [] { "--spring.config.name=server" , "--spring.profiles.active=awss3" ,
63
73
"--server.port=" + configServerPort ,
64
74
"--spring.cloud.config.server.awss3.endpoint="
@@ -71,7 +81,7 @@ public static void startConfigServer() throws IOException, InterruptedException,
71
81
"--spring.cloud.aws.credentials.secret-key=" + localstack .getSecretKey (),
72
82
"--spring.cloud.aws.region.static=" + localstack .getRegion () });
73
83
74
- if (server .containsBean (S3Client .class . getName ()) ) {
84
+ if (server .getBeanNamesForType (S3Client .class ). length > 0 ) {
75
85
s3Client = server .getBean (S3Client .class );
76
86
s3Client .createBucket ((request ) -> request .bucket ("test-bucket" ));
77
87
s3Client .putObject ((request ) -> request .bucket ("test-bucket" ).key ("data.txt" ),
@@ -86,8 +96,6 @@ public static void startConfigServer() throws IOException, InterruptedException,
86
96
87
97
@ Test
88
98
@ Disabled
89
- // TODO uncomment when we have an RC or GA release of Spring Cloud AWS with this fix
90
- // https://github.com/awspring/spring-cloud-aws/pull/652
91
99
public void context () throws IOException {
92
100
RestTemplate rest = new RestTemplateBuilder ().build ();
93
101
String configServerUrl = "http://localhost:" + configServerPort ;
@@ -104,4 +112,25 @@ public static void after() {
104
112
server .close ();
105
113
}
106
114
115
+ @ Import (S3ProtocolResolver .class )
116
+ static class S3AutoConfiguration {
117
+
118
+ @ Bean
119
+ S3Client s3Client () {
120
+ return S3Client .builder ()
121
+ .credentialsProvider (
122
+ () -> AwsBasicCredentials .create (localstack .getAccessKey (), localstack .getSecretKey ()))
123
+ .region (Region .of (localstack .getRegion ()))
124
+ .endpointOverride (localstack .getEndpointOverride (LocalStackContainer .Service .S3 )).build ();
125
+ }
126
+
127
+ @ Bean
128
+ S3OutputStreamProvider inMemoryBufferingS3StreamProvider (S3Client s3Client ,
129
+ Optional <S3ObjectContentTypeResolver > contentTypeResolver ) {
130
+ return new InMemoryBufferingS3OutputStreamProvider (s3Client ,
131
+ contentTypeResolver .orElseGet (PropertiesS3ObjectContentTypeResolver ::new ));
132
+ }
133
+
134
+ }
135
+
107
136
}
0 commit comments