|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.elasticsearch;
|
18 | 18 |
|
| 19 | +import java.net.InetSocketAddress; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.assertj.core.api.InstanceOfAssertFactories; |
19 | 24 | import org.junit.jupiter.api.Test;
|
20 | 25 |
|
21 | 26 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
24 | 29 | import org.springframework.context.annotation.Configuration;
|
25 | 30 | import org.springframework.data.elasticsearch.client.ClientConfiguration;
|
26 | 31 | import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
|
| 32 | +import org.springframework.http.HttpHeaders; |
| 33 | +import org.springframework.http.codec.CodecConfigurer.DefaultCodecConfig; |
| 34 | +import org.springframework.web.reactive.function.client.WebClient; |
27 | 35 |
|
28 | 36 | import static org.assertj.core.api.Assertions.assertThat;
|
29 | 37 | import static org.mockito.Mockito.mock;
|
@@ -57,6 +65,86 @@ void configureWhenCustomClientConfig() {
|
57 | 65 | .hasSingleBean(ClientConfiguration.class).hasBean("customClientConfiguration"));
|
58 | 66 | }
|
59 | 67 |
|
| 68 | + @Test |
| 69 | + void whenEndpointIsCustomizedThenClientConfigurationHasCustomEndpoint() { |
| 70 | + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.endpoints=localhost:9876") |
| 71 | + .run((context) -> { |
| 72 | + List<InetSocketAddress> endpoints = context.getBean(ClientConfiguration.class).getEndpoints(); |
| 73 | + assertThat(endpoints).hasSize(1); |
| 74 | + assertThat(endpoints.get(0).getHostString()).isEqualTo("localhost"); |
| 75 | + assertThat(endpoints.get(0).getPort()).isEqualTo(9876); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void whenMultipleEndpointsAreConfiguredThenClientConfigurationHasMultipleEndpoints() { |
| 81 | + this.contextRunner |
| 82 | + .withPropertyValues("spring.data.elasticsearch.client.reactive.endpoints=localhost:9876,localhost:8765") |
| 83 | + .run((context) -> { |
| 84 | + List<InetSocketAddress> endpoints = context.getBean(ClientConfiguration.class).getEndpoints(); |
| 85 | + assertThat(endpoints).hasSize(2); |
| 86 | + assertThat(endpoints.get(0).getHostString()).isEqualTo("localhost"); |
| 87 | + assertThat(endpoints.get(0).getPort()).isEqualTo(9876); |
| 88 | + assertThat(endpoints.get(1).getHostString()).isEqualTo("localhost"); |
| 89 | + assertThat(endpoints.get(1).getPort()).isEqualTo(8765); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void whenConfiguredToUseSslThenClientConfigurationUsesSsl() { |
| 95 | + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.use-ssl=true") |
| 96 | + .run((context) -> assertThat(context.getBean(ClientConfiguration.class).useSsl()).isTrue()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void whenSocketTimeoutIsNotConfiguredThenClientConfigurationUsesDefault() { |
| 101 | + this.contextRunner.run((context) -> assertThat(context.getBean(ClientConfiguration.class).getSocketTimeout()) |
| 102 | + .isEqualTo(Duration.ofSeconds(5))); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void whenConnectionTimeoutIsNotConfiguredThenClientConfigurationUsesDefault() { |
| 107 | + this.contextRunner.run((context) -> assertThat(context.getBean(ClientConfiguration.class).getConnectTimeout()) |
| 108 | + .isEqualTo(Duration.ofSeconds(10))); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void whenSocketTimeoutIsConfiguredThenClientConfigurationHasCustomSocketTimeout() { |
| 113 | + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.socket-timeout=2s") |
| 114 | + .run((context) -> assertThat(context.getBean(ClientConfiguration.class).getSocketTimeout()) |
| 115 | + .isEqualTo(Duration.ofSeconds(2))); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void whenConnectionTimeoutIsConfiguredThenClientConfigurationHasCustomConnectTimeout() { |
| 120 | + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.connection-timeout=2s") |
| 121 | + .run((context) -> assertThat(context.getBean(ClientConfiguration.class).getConnectTimeout()) |
| 122 | + .isEqualTo(Duration.ofSeconds(2))); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void whenCredentialsAreConfiguredThenClientConfigurationHasDefaultAuthorizationHeader() { |
| 127 | + this.contextRunner |
| 128 | + .withPropertyValues("spring.data.elasticsearch.client.reactive.username=alice", |
| 129 | + "spring.data.elasticsearch.client.reactive.password=secret") |
| 130 | + .run((context) -> assertThat( |
| 131 | + context.getBean(ClientConfiguration.class).getDefaultHeaders().get(HttpHeaders.AUTHORIZATION)) |
| 132 | + .containsExactly("Basic YWxpY2U6c2VjcmV0")); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void whenMaxInMemorySizeIsConfiguredThenUnderlyingWebClientHasCustomMaxInMemorySize() { |
| 137 | + this.contextRunner.withPropertyValues("spring.data.elasticsearch.client.reactive.max-in-memory-size=1MB") |
| 138 | + .run((context) -> { |
| 139 | + WebClient client = context.getBean(ClientConfiguration.class).getWebClientConfigurer() |
| 140 | + .apply(WebClient.create()); |
| 141 | + assertThat(client).extracting("exchangeFunction").extracting("strategies") |
| 142 | + .extracting("codecConfigurer").extracting("defaultCodecs") |
| 143 | + .asInstanceOf(InstanceOfAssertFactories.type(DefaultCodecConfig.class)) |
| 144 | + .extracting(DefaultCodecConfig::maxInMemorySize).isEqualTo(1024 * 1024); |
| 145 | + }); |
| 146 | + } |
| 147 | + |
60 | 148 | @Configuration(proxyBeanMethods = false)
|
61 | 149 | static class CustomClientConfiguration {
|
62 | 150 |
|
|
0 commit comments