|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.elasticsearch;
|
18 | 18 |
|
19 |
| -import org.junit.jupiter.api.AfterEach; |
20 | 19 | import org.junit.jupiter.api.Test;
|
21 | 20 | import org.testcontainers.junit.jupiter.Container;
|
22 | 21 | import org.testcontainers.junit.jupiter.Testcontainers;
|
23 | 22 |
|
24 |
| -import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
25 |
| -import org.springframework.boot.test.util.TestPropertyValues; |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 24 | +import org.springframework.boot.autoconfigure.elasticsearch.rest.RestClientAutoConfiguration; |
| 25 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
26 | 26 | import org.springframework.boot.testsupport.testcontainers.ElasticsearchContainer;
|
27 |
| -import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; |
28 | 30 | import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
29 | 31 | import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
|
30 | 32 | import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
|
31 | 33 |
|
32 | 34 | import static org.assertj.core.api.Assertions.assertThat;
|
| 35 | +import static org.mockito.Mockito.mock; |
33 | 36 |
|
34 | 37 | /**
|
35 | 38 | * Tests for {@link ElasticsearchDataAutoConfiguration}.
|
36 | 39 | *
|
37 | 40 | * @author Phillip Webb
|
38 | 41 | * @author Artur Konczak
|
| 42 | + * @author Brian Clozel |
39 | 43 | */
|
40 |
| -@SuppressWarnings("deprecation") |
41 | 44 | @Testcontainers
|
42 | 45 | public class ElasticsearchDataAutoConfigurationTests {
|
43 | 46 |
|
44 | 47 | @Container
|
45 | 48 | public static ElasticsearchContainer elasticsearch = new ElasticsearchContainer();
|
46 | 49 |
|
47 |
| - private AnnotationConfigApplicationContext context; |
| 50 | + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 51 | + .withConfiguration(AutoConfigurations.of(ElasticsearchAutoConfiguration.class, |
| 52 | + RestClientAutoConfiguration.class, |
| 53 | + ElasticsearchDataAutoConfiguration.class)); |
48 | 54 |
|
49 |
| - @AfterEach |
50 |
| - public void close() { |
51 |
| - if (this.context != null) { |
52 |
| - this.context.close(); |
53 |
| - } |
| 55 | + @Test |
| 56 | + public void defaultTransportBeansAreRegistered() { |
| 57 | + this.contextRunner |
| 58 | + .withPropertyValues( |
| 59 | + "spring.data.elasticsearch.cluster-nodes:localhost:" |
| 60 | + + elasticsearch.getMappedTransportPort(), |
| 61 | + "spring.data.elasticsearch.cluster-name:docker-cluster") |
| 62 | + .run((context) -> assertThat(context) |
| 63 | + .hasSingleBean(ElasticsearchTemplate.class) |
| 64 | + .hasSingleBean(SimpleElasticsearchMappingContext.class) |
| 65 | + .hasSingleBean(ElasticsearchConverter.class)); |
54 | 66 | }
|
55 | 67 |
|
56 | 68 | @Test
|
57 |
| - public void templateBackOffWithNoClient() { |
58 |
| - this.context = new AnnotationConfigApplicationContext( |
59 |
| - ElasticsearchDataAutoConfiguration.class); |
60 |
| - assertThat(this.context.getBeansOfType(ElasticsearchTemplate.class)).isEmpty(); |
| 69 | + public void defaultTransportBeansNotRegisteredIfNoTransportClient() { |
| 70 | + this.contextRunner.run((context) -> assertThat(context) |
| 71 | + .doesNotHaveBean(ElasticsearchTemplate.class)); |
61 | 72 | }
|
62 | 73 |
|
63 | 74 | @Test
|
64 |
| - public void templateExists() { |
65 |
| - this.context = new AnnotationConfigApplicationContext(); |
66 |
| - TestPropertyValues |
67 |
| - .of("spring.data.elasticsearch.cluster-nodes:localhost:" |
68 |
| - + elasticsearch.getMappedTransportPort(), |
69 |
| - "spring.data.elasticsearch.cluster-name:docker-cluster") |
70 |
| - .applyTo(this.context); |
71 |
| - this.context.register(PropertyPlaceholderAutoConfiguration.class, |
72 |
| - ElasticsearchAutoConfiguration.class, |
73 |
| - ElasticsearchDataAutoConfiguration.class); |
74 |
| - this.context.refresh(); |
75 |
| - assertHasSingleBean(ElasticsearchTemplate.class); |
| 75 | + public void defaultRestBeansRegistered() { |
| 76 | + this.contextRunner.run((context) -> assertThat(context) |
| 77 | + .hasSingleBean(ElasticsearchRestTemplate.class) |
| 78 | + .hasSingleBean(ElasticsearchConverter.class)); |
76 | 79 | }
|
77 | 80 |
|
78 | 81 | @Test
|
79 |
| - public void mappingContextExists() { |
80 |
| - this.context = new AnnotationConfigApplicationContext(); |
81 |
| - TestPropertyValues |
82 |
| - .of("spring.data.elasticsearch.cluster-nodes:localhost:" |
83 |
| - + elasticsearch.getMappedTransportPort(), |
84 |
| - "spring.data.elasticsearch.cluster-name:docker-cluster") |
85 |
| - .applyTo(this.context); |
86 |
| - this.context.register(PropertyPlaceholderAutoConfiguration.class, |
87 |
| - ElasticsearchAutoConfiguration.class, |
88 |
| - ElasticsearchDataAutoConfiguration.class); |
89 |
| - this.context.refresh(); |
90 |
| - assertHasSingleBean(SimpleElasticsearchMappingContext.class); |
| 82 | + public void customTransportTemplateShouldBeUsed() { |
| 83 | + this.contextRunner.withUserConfiguration(CustomTransportTemplate.class) |
| 84 | + .run((context) -> assertThat(context) |
| 85 | + .getBeanNames(ElasticsearchTemplate.class).hasSize(1) |
| 86 | + .contains("elasticsearchTemplate")); |
91 | 87 | }
|
92 | 88 |
|
93 | 89 | @Test
|
94 |
| - public void converterExists() { |
95 |
| - this.context = new AnnotationConfigApplicationContext(); |
96 |
| - TestPropertyValues |
97 |
| - .of("spring.data.elasticsearch.cluster-nodes:localhost:" |
98 |
| - + elasticsearch.getMappedTransportPort(), |
99 |
| - "spring.data.elasticsearch.cluster-name:docker-cluster") |
100 |
| - .applyTo(this.context); |
101 |
| - this.context.register(PropertyPlaceholderAutoConfiguration.class, |
102 |
| - ElasticsearchAutoConfiguration.class, |
103 |
| - ElasticsearchDataAutoConfiguration.class); |
104 |
| - this.context.refresh(); |
105 |
| - assertHasSingleBean(ElasticsearchConverter.class); |
| 90 | + public void customRestTemplateShouldBeUsed() { |
| 91 | + this.contextRunner.withUserConfiguration(CustomRestTemplate.class) |
| 92 | + .run((context) -> assertThat(context) |
| 93 | + .getBeanNames(ElasticsearchRestTemplate.class).hasSize(1) |
| 94 | + .contains("elasticsearchTemplate")); |
| 95 | + } |
| 96 | + |
| 97 | + @Configuration |
| 98 | + static class CustomTransportTemplate { |
| 99 | + |
| 100 | + @Bean |
| 101 | + ElasticsearchTemplate elasticsearchTemplate() { |
| 102 | + return mock(ElasticsearchTemplate.class); |
| 103 | + } |
| 104 | + |
106 | 105 | }
|
107 | 106 |
|
108 |
| - private void assertHasSingleBean(Class<?> type) { |
109 |
| - assertThat(this.context.getBeanNamesForType(type)).hasSize(1); |
| 107 | + @Configuration |
| 108 | + static class CustomRestTemplate { |
| 109 | + |
| 110 | + @Bean |
| 111 | + ElasticsearchRestTemplate elasticsearchTemplate() { |
| 112 | + return mock(ElasticsearchRestTemplate.class); |
| 113 | + } |
| 114 | + |
110 | 115 | }
|
111 | 116 |
|
112 | 117 | }
|
0 commit comments