1
1
package com .example .springbootelasticsearch ;
2
2
3
- import org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
4
- import org .elasticsearch .client .RestClientBuilder ;
5
3
import org .junit .jupiter .api .Test ;
6
4
import org .springframework .beans .factory .annotation .Autowired ;
7
- import org .springframework .boot .autoconfigure .elasticsearch .RestClientBuilderCustomizer ;
8
5
import org .springframework .boot .test .autoconfigure .data .elasticsearch .DataElasticsearchTest ;
9
- import org .springframework .boot .test . context . TestConfiguration ;
10
- import org .springframework .context . annotation . Bean ;
6
+ import org .springframework .boot .testcontainers . service . connection . ServiceConnection ;
7
+ import org .springframework .boot . testcontainers . service . connection . Ssl ;
11
8
import org .springframework .data .elasticsearch .client .elc .ElasticsearchTemplate ;
12
- import org .springframework .test .context .DynamicPropertyRegistry ;
13
- import org .springframework .test .context .DynamicPropertySource ;
14
9
import org .testcontainers .elasticsearch .ElasticsearchContainer ;
15
10
import org .testcontainers .junit .jupiter .Container ;
16
11
import org .testcontainers .junit .jupiter .Testcontainers ;
19
14
20
15
import static org .assertj .core .api .Assertions .assertThat ;
21
16
22
- @ DataElasticsearchTest (
23
- properties = { "spring.elasticsearch.username=elastic" , "spring.elasticsearch.password=changeme" })
17
+ @ DataElasticsearchTest
24
18
@ Testcontainers
25
19
class BookRepositoryElasticsearch8Tests {
26
20
27
21
@ Container
22
+ @ ServiceConnection
23
+ @ Ssl
28
24
private static ElasticsearchContainer elasticsearch = new ElasticsearchContainer (
29
25
"docker.elastic.co/elasticsearch/elasticsearch:8.7.1" );
30
26
31
- @ DynamicPropertySource
32
- static void properties (DynamicPropertyRegistry registry ) {
33
- registry .add ("spring.elasticsearch.uris" , () -> "https://" + elasticsearch .getHttpHostAddress ());
34
- }
35
-
36
27
@ Autowired
37
28
private BookRepository bookRepository ;
38
29
@@ -47,24 +38,4 @@ void contextLoads() {
47
38
assertThat (this .elasticsearchTemplate .get (id , Book .class )).extracting ("title" ).isEqualTo ("Spring Boot Testing" );
48
39
}
49
40
50
- @ TestConfiguration
51
- static class SSL {
52
-
53
- @ Bean
54
- public RestClientBuilderCustomizer customizer () {
55
- return new RestClientBuilderCustomizer () {
56
- @ Override
57
- public void customize (RestClientBuilder builder ) {
58
-
59
- }
60
-
61
- @ Override
62
- public void customize (HttpAsyncClientBuilder builder ) {
63
- builder .setSSLContext (elasticsearch .createSslContextFromCa ());
64
- }
65
- };
66
- }
67
-
68
- }
69
-
70
41
}
0 commit comments