ElasticConfigurationTest: Add tests for elasticsearch.{username,password}

Test that the configuration reads the username and password settings
in the expected way.

Bug: Issue 9095
Change-Id: Icb9eccd38a5fdadf478d3b5492a509189e58b0a1
This commit is contained in:
David Pursehouse
2018-07-04 13:26:09 +09:00
parent 12a75b2815
commit 7225fbd6c9

View File

@@ -15,7 +15,10 @@
package com.google.gerrit.elasticsearch;
import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.elasticsearch.ElasticConfiguration.DEFAULT_USERNAME;
import static com.google.gerrit.elasticsearch.ElasticConfiguration.KEY_PASSWORD;
import static com.google.gerrit.elasticsearch.ElasticConfiguration.KEY_SERVER;
import static com.google.gerrit.elasticsearch.ElasticConfiguration.KEY_USERNAME;
import static com.google.gerrit.elasticsearch.ElasticConfiguration.SECTION_ELASTICSEARCH;
import static java.util.stream.Collectors.toList;
@@ -31,11 +34,12 @@ public class ElasticConfigurationTest {
@Rule public ExpectedException exception = ExpectedException.none();
@Test
public void singleServer() throws Exception {
Config cfg = new Config();
cfg.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, "http://elastic:1234");
public void singleServerNoOtherConfig() throws Exception {
Config cfg = newConfig();
ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
assertHosts(esCfg, "http://elastic:1234");
assertThat(esCfg.username).isNull();
assertThat(esCfg.password).isNull();
}
@Test
@@ -46,6 +50,25 @@ public class ElasticConfigurationTest {
assertHosts(esCfg, "http://elastic:9200");
}
@Test
public void withAuthentication() throws Exception {
Config cfg = newConfig();
cfg.setString(SECTION_ELASTICSEARCH, null, KEY_USERNAME, "myself");
cfg.setString(SECTION_ELASTICSEARCH, null, KEY_PASSWORD, "s3kr3t");
ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
assertThat(esCfg.username).isEqualTo("myself");
assertThat(esCfg.password).isEqualTo("s3kr3t");
}
@Test
public void withAuthenticationPasswordOnlyUsesDefaultUsername() throws Exception {
Config cfg = newConfig();
cfg.setString(SECTION_ELASTICSEARCH, null, KEY_PASSWORD, "s3kr3t");
ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
assertThat(esCfg.username).isEqualTo(DEFAULT_USERNAME);
assertThat(esCfg.password).isEqualTo("s3kr3t");
}
@Test
public void multipleServers() throws Exception {
Config cfg = new Config();
@@ -79,6 +102,12 @@ public class ElasticConfigurationTest {
assertHosts(esCfg, "http://elastic1:1234");
}
private static Config newConfig() {
Config config = new Config();
config.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, "http://elastic:1234");
return config;
}
private void assertHosts(ElasticConfiguration cfg, Object... hostURIs) throws Exception {
assertThat(Arrays.asList(cfg.getHosts()).stream().map(h -> h.toURI()).collect(toList()))
.containsExactly(hostURIs);