-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Add support for Elasticsearch Sniffer #24340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,6 @@ | |
|
||
package org.springframework.boot.autoconfigure.elasticsearch; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.auth.AuthScope; | ||
import org.apache.http.auth.Credentials; | ||
|
@@ -29,21 +25,22 @@ | |
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.elasticsearch.action.get.GetRequest; | ||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.client.Node; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestClientBuilder; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.client.*; | ||
import org.elasticsearch.client.sniff.Sniffer; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.elasticsearch.ElasticsearchContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.boot.testsupport.testcontainers.DockerImageNames; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.testcontainers.elasticsearch.ElasticsearchContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
@@ -220,6 +217,53 @@ void configureUriWithUsernameAndPasswordWhenUsernameAndPasswordPropertiesSet() { | |
}); | ||
} | ||
|
||
@Test | ||
void configureShouldOnlyCreateSnifferInstance() { | ||
this.contextRunner.run((context) -> { | ||
assertThat(context).hasSingleBean(Sniffer.class); | ||
assertThat(context).doesNotHaveBean(RestClient.class).hasSingleBean(RestHighLevelClient.class); | ||
}); | ||
} | ||
|
||
@Test | ||
void configureShouldHaveSnifferInstance() { | ||
this.contextRunner.run((context) -> { | ||
assertThat(context).hasSingleBean(Sniffer.class); | ||
assertThat(context).doesNotHaveBean(RestClient.class).hasSingleBean(RestHighLevelClient.class); | ||
Assert.assertNotNull(context.getBean(Sniffer.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
void configureWithCustomSetIntervalProperties() { | ||
this.contextRunner.withPropertyValues( | ||
"spring.elasticsearch.rest.sniffInterval=1m, " + "spring.elasticsearch.rest.sniffFailureDelay=1m") | ||
.run((context) -> { | ||
Assert.assertNotNull(context.getBean(Sniffer.class)); | ||
assertThat(context).hasSingleBean(RestHighLevelClient.class); | ||
RestHighLevelClient restClient = context.getBean(RestHighLevelClient.class); | ||
assertThat(restClient.getLowLevelClient()).extracting("spring.elasticsearch.rest.sniffInterval") | ||
.isEqualTo(Math.toIntExact(Duration.ofMinutes(1L).toMillis())); | ||
assertThat(restClient.getLowLevelClient()).extracting("spring.elasticsearch.rest.sniffFailureDelay") | ||
.isEqualTo(Math.toIntExact(Duration.ofMinutes(1L).toMillis())); | ||
}); | ||
} | ||
|
||
@Test | ||
void configureWithNoAutoConfigurationPropertySet() { | ||
this.contextRunner.run((context) -> { | ||
assertThat(context).doesNotHaveBean(Sniffer.class); | ||
Assert.assertNull(context.getBean(Sniffer.class)); | ||
}); | ||
} | ||
|
||
@Test | ||
void configureWhenCustomSnifferWhenPresent() { | ||
this.contextRunner.withBean("customSniffer", Sniffer.class, () -> mock(Sniffer.class)) | ||
.run((context) -> assertThat(context).hasSingleBean(RestHighLevelClient.class) | ||
.hasSingleBean(Sniffer.class).hasBean("customSniffer")); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two tests are missing here. One that assert what happens when the library is not on the classpath, as I've indicated in my previous review. One that assert that a custom |
||
@Configuration(proxyBeanMethods = false) | ||
static class BuilderCustomizerConfiguration { | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.