ElasticConfiguration: Extract constants to statics
Create static constants for the elasticsearch section name, the keys, and default values. Make them package visible so that they can be used in tests. Update the configuration tests to use them. Change-Id: I150e584f82825a25ae32604481b4b8e919850cb1
This commit is contained in:
@@ -35,8 +35,16 @@ import org.slf4j.LoggerFactory;
|
||||
class ElasticConfiguration {
|
||||
private static final Logger log = LoggerFactory.getLogger(ElasticConfiguration.class);
|
||||
|
||||
private static final String DEFAULT_PORT = "9200";
|
||||
private static final String DEFAULT_USERNAME = "elastic";
|
||||
static final String SECTION_ELASTICSEARCH = "elasticsearch";
|
||||
static final String KEY_PASSWORD = "password";
|
||||
static final String KEY_USERNAME = "username";
|
||||
static final String KEY_MAX_RETRY_TIMEOUT = "maxRetryTimeout";
|
||||
static final String KEY_PREFIX = "prefix";
|
||||
static final String KEY_SERVER = "server";
|
||||
static final String DEFAULT_PORT = "9200";
|
||||
static final String DEFAULT_USERNAME = "elastic";
|
||||
static final int DEFAULT_MAX_RETRY_TIMEOUT_MS = 30000;
|
||||
static final TimeUnit MAX_RETRY_TIMEOUT_UNIT = TimeUnit.MILLISECONDS;
|
||||
|
||||
private final Config cfg;
|
||||
private final List<HttpHost> hosts;
|
||||
@@ -49,17 +57,23 @@ class ElasticConfiguration {
|
||||
@Inject
|
||||
ElasticConfiguration(@GerritServerConfig Config cfg) {
|
||||
this.cfg = cfg;
|
||||
this.password = cfg.getString("elasticsearch", null, "password");
|
||||
this.password = cfg.getString(SECTION_ELASTICSEARCH, null, KEY_PASSWORD);
|
||||
this.username =
|
||||
password == null
|
||||
? null
|
||||
: firstNonNull(cfg.getString("elasticsearch", null, "username"), DEFAULT_USERNAME);
|
||||
: firstNonNull(
|
||||
cfg.getString(SECTION_ELASTICSEARCH, null, KEY_USERNAME), DEFAULT_USERNAME);
|
||||
this.maxRetryTimeout =
|
||||
(int)
|
||||
cfg.getTimeUnit("elasticsearch", null, "maxRetryTimeout", 30000, TimeUnit.MILLISECONDS);
|
||||
this.prefix = Strings.nullToEmpty(cfg.getString("elasticsearch", null, "prefix"));
|
||||
cfg.getTimeUnit(
|
||||
SECTION_ELASTICSEARCH,
|
||||
null,
|
||||
KEY_MAX_RETRY_TIMEOUT,
|
||||
DEFAULT_MAX_RETRY_TIMEOUT_MS,
|
||||
MAX_RETRY_TIMEOUT_UNIT);
|
||||
this.prefix = Strings.nullToEmpty(cfg.getString(SECTION_ELASTICSEARCH, null, KEY_PREFIX));
|
||||
this.hosts = new ArrayList<>();
|
||||
for (String server : cfg.getStringList("elasticsearch", null, "server")) {
|
||||
for (String server : cfg.getStringList(SECTION_ELASTICSEARCH, null, KEY_SERVER)) {
|
||||
try {
|
||||
URI uri = new URI(server);
|
||||
int port = uri.getPort();
|
||||
|
@@ -15,6 +15,8 @@
|
||||
package com.google.gerrit.elasticsearch;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.gerrit.elasticsearch.ElasticConfiguration.KEY_SERVER;
|
||||
import static com.google.gerrit.elasticsearch.ElasticConfiguration.SECTION_ELASTICSEARCH;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
@@ -31,7 +33,7 @@ public class ElasticConfigurationTest {
|
||||
@Test
|
||||
public void singleServer() throws Exception {
|
||||
Config cfg = new Config();
|
||||
cfg.setString("elasticsearch", null, "server", "http://elastic:1234");
|
||||
cfg.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, "http://elastic:1234");
|
||||
ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
|
||||
assertHosts(esCfg, "http://elastic:1234");
|
||||
}
|
||||
@@ -39,7 +41,7 @@ public class ElasticConfigurationTest {
|
||||
@Test
|
||||
public void serverWithoutPortSpecified() throws Exception {
|
||||
Config cfg = new Config();
|
||||
cfg.setString("elasticsearch", null, "server", "http://elastic");
|
||||
cfg.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, "http://elastic");
|
||||
ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
|
||||
assertHosts(esCfg, "http://elastic:9200");
|
||||
}
|
||||
@@ -48,9 +50,9 @@ public class ElasticConfigurationTest {
|
||||
public void multipleServers() throws Exception {
|
||||
Config cfg = new Config();
|
||||
cfg.setStringList(
|
||||
"elasticsearch",
|
||||
SECTION_ELASTICSEARCH,
|
||||
null,
|
||||
"server",
|
||||
KEY_SERVER,
|
||||
ImmutableList.of("http://elastic1:1234", "http://elastic2:1234"));
|
||||
ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
|
||||
assertHosts(esCfg, "http://elastic1:1234", "http://elastic2:1234");
|
||||
@@ -64,7 +66,7 @@ public class ElasticConfigurationTest {
|
||||
@Test
|
||||
public void singleServerInvalid() throws Exception {
|
||||
Config cfg = new Config();
|
||||
cfg.setString("elasticsearch", null, "server", "foo");
|
||||
cfg.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, "foo");
|
||||
assertProvisionException(cfg);
|
||||
}
|
||||
|
||||
@@ -72,7 +74,7 @@ public class ElasticConfigurationTest {
|
||||
public void multipleServersIncludingInvalid() throws Exception {
|
||||
Config cfg = new Config();
|
||||
cfg.setStringList(
|
||||
"elasticsearch", null, "server", ImmutableList.of("http://elastic1:1234", "foo"));
|
||||
SECTION_ELASTICSEARCH, null, KEY_SERVER, ImmutableList.of("http://elastic1:1234", "foo"));
|
||||
ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
|
||||
assertHosts(esCfg, "http://elastic1:1234");
|
||||
}
|
||||
|
Reference in New Issue
Block a user