// Copyright (C) 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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;
import com.google.inject.ProvisionException;
import java.util.Arrays;
import org.eclipse.jgit.lib.Config;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

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");
    ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
    assertHosts(esCfg, "http://elastic:1234");
  }

  @Test
  public void serverWithoutPortSpecified() throws Exception {
    Config cfg = new Config();
    cfg.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, "http://elastic");
    ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
    assertHosts(esCfg, "http://elastic:9200");
  }

  @Test
  public void multipleServers() throws Exception {
    Config cfg = new Config();
    cfg.setStringList(
        SECTION_ELASTICSEARCH,
        null,
        KEY_SERVER,
        ImmutableList.of("http://elastic1:1234", "http://elastic2:1234"));
    ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
    assertHosts(esCfg, "http://elastic1:1234", "http://elastic2:1234");
  }

  @Test
  public void noServers() throws Exception {
    assertProvisionException(new Config());
  }

  @Test
  public void singleServerInvalid() throws Exception {
    Config cfg = new Config();
    cfg.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, "foo");
    assertProvisionException(cfg);
  }

  @Test
  public void multipleServersIncludingInvalid() throws Exception {
    Config cfg = new Config();
    cfg.setStringList(
        SECTION_ELASTICSEARCH, null, KEY_SERVER, ImmutableList.of("http://elastic1:1234", "foo"));
    ElasticConfiguration esCfg = new ElasticConfiguration(cfg);
    assertHosts(esCfg, "http://elastic1:1234");
  }

  private void assertHosts(ElasticConfiguration cfg, Object... hostURIs) throws Exception {
    assertThat(Arrays.asList(cfg.getHosts()).stream().map(h -> h.toURI()).collect(toList()))
        .containsExactly(hostURIs);
  }

  private void assertProvisionException(Config cfg) throws Exception {
    exception.expect(ProvisionException.class);
    exception.expectMessage("No valid Elasticsearch servers configured");
    new ElasticConfiguration(cfg);
  }
}

@@ -767,26 +767,273 @@
 uration.
-KEY_SERVER
+DEFAULT_USERNAME;%0Aimport static com.google.gerrit.elasticsearch.ElasticConfiguration.KEY_PASSWORD;%0Aimport static com.google.gerrit.elasticsearch.ElasticConfiguration.KEY_SERVER;%0Aimport static com.google.gerrit.elasticsearch.ElasticConfiguration.KEY_USERNAME
 ;%0Aimport
@@ -1556,16 +1556,29 @@
 leServer
+NoOtherConfig
 () throw
@@ -1603,33 +1603,32 @@
 Config cfg = new
- 
 Config();%0A    cf
@@ -1625,91 +1625,8 @@
 ();%0A
-    cfg.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, %22http://elastic:1234%22);%0A
     
@@ -1724,32 +1724,114 @@
 elastic:1234%22);%0A
+    assertThat(esCfg.username).isNull();%0A    assertThat(esCfg.password).isNull();%0A
   %7D%0A%0A  @Test%0A  p
@@ -2106,32 +2106,817 @@
 ic:9200%22);%0A  %7D%0A%0A
+  @Test%0A  public void withAuthentication() throws Exception %7B%0A    Config cfg = newConfig();%0A    cfg.setString(SECTION_ELASTICSEARCH, null, KEY_USERNAME, %22myself%22);%0A    cfg.setString(SECTION_ELASTICSEARCH, null, KEY_PASSWORD, %22s3kr3t%22);%0A    ElasticConfiguration esCfg = new ElasticConfiguration(cfg);%0A    assertThat(esCfg.username).isEqualTo(%22myself%22);%0A    assertThat(esCfg.password).isEqualTo(%22s3kr3t%22);%0A  %7D%0A%0A  @Test%0A  public void withAuthenticationPasswordOnlyUsesDefaultUsername() throws Exception %7B%0A    Config cfg = newConfig();%0A    cfg.setString(SECTION_ELASTICSEARCH, null, KEY_PASSWORD, %22s3kr3t%22);%0A    ElasticConfiguration esCfg = new ElasticConfiguration(cfg);%0A    assertThat(esCfg.username).isEqualTo(DEFAULT_USERNAME);%0A    assertThat(esCfg.password).isEqualTo(%22s3kr3t%22);%0A  %7D%0A%0A
   @Test%0A  public
@@ -3880,32 +3880,32 @@
 iguration(cfg);%0A
-
     assertHosts(
@@ -3933,32 +3933,214 @@
 c1:1234%22);%0A  %7D%0A%0A
+  private static Config newConfig() %7B%0A    Config config = new Config();%0A    config.setString(SECTION_ELASTICSEARCH, null, KEY_SERVER, %22http://elastic:1234%22);%0A    return config;%0A  %7D%0A%0A
   private void a

Add tests for elasticsearch.{username,password}