Use Provider<PeerInfo> instead of Configuration.getUrl()

By using Provider<PeerInfo> we make it possible to provide the URL of
the other peer node in a different way than reading it from the plugin's
global configuration.

To keep compatibility with the existing configuration of the secondary
peer node URL, this change implements Provider<PeerInfo> in
PluginConfigPeerInfoProvider which reads the secondary peer's URL from
the plugin's configuration file.

The assumption that there is only one secondary peer node still stays.
Introducing the concept of N peer nodes (N > 2) may be done in a future
change.

Change-Id: I2df5c16c951023df01a6b03ddb53776d52c607d7
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Module.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Module.java
index e1b4ba6..e51324a 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/Module.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/Module.java
@@ -21,6 +21,7 @@
 import com.ericsson.gerrit.plugins.highavailability.event.EventModule;
 import com.ericsson.gerrit.plugins.highavailability.forwarder.rest.RestForwarderModule;
 import com.ericsson.gerrit.plugins.highavailability.index.IndexModule;
+import com.ericsson.gerrit.plugins.highavailability.peers.PeerInfoModule;
 
 class Module extends AbstractModule {
 
@@ -31,5 +32,6 @@
     install(new EventModule());
     install(new IndexModule());
     install(new CacheModule());
+    install(new PeerInfoModule());
   }
 }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSession.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSession.java
index 574dd82..38d751b 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSession.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSession.java
@@ -19,6 +19,7 @@
 import com.google.inject.Inject;
 
 import com.ericsson.gerrit.plugins.highavailability.forwarder.rest.HttpResponseHandler.HttpResult;
+import com.ericsson.gerrit.plugins.highavailability.peers.PeerInfo;
 
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpPost;
@@ -30,13 +31,13 @@
 
 class HttpSession {
   private final CloseableHttpClient httpClient;
-  private final String url;
+  private PeerInfo peerInfo;
 
   @Inject
   HttpSession(CloseableHttpClient httpClient,
-      @ForwardUrl String url) {
+      PeerInfo peerInfo) {
     this.httpClient = httpClient;
-    this.url = url;
+    this.peerInfo = peerInfo;
   }
 
   HttpResult post(String endpoint) throws IOException {
@@ -44,7 +45,7 @@
   }
 
   HttpResult post(String endpoint, String content) throws IOException {
-    HttpPost post = new HttpPost(url + endpoint);
+    HttpPost post = new HttpPost(peerInfo.getDirectUrl() + endpoint);
     if (!Strings.isNullOrEmpty(content)) {
       post.addHeader("Content-Type", MediaType.JSON_UTF_8.toString());
       post.setEntity(new StringEntity(content, StandardCharsets.UTF_8));
@@ -53,7 +54,7 @@
   }
 
   HttpResult delete(String endpoint) throws IOException {
-    return httpClient.execute(new HttpDelete(url + endpoint),
+    return httpClient.execute(new HttpDelete(peerInfo.getDirectUrl() + endpoint),
         new HttpResponseHandler());
   }
 }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/RestForwarderModule.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/RestForwarderModule.java
index efa9d78..8bb3ce1 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/RestForwarderModule.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/RestForwarderModule.java
@@ -15,10 +15,8 @@
 package com.ericsson.gerrit.plugins.highavailability.forwarder.rest;
 
 import com.google.inject.AbstractModule;
-import com.google.inject.Provides;
 import com.google.inject.Scopes;
 
-import com.ericsson.gerrit.plugins.highavailability.Configuration;
 import com.ericsson.gerrit.plugins.highavailability.forwarder.Forwarder;
 
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -32,10 +30,4 @@
     bind(HttpSession.class);
     bind(Forwarder.class).to(RestForwarder.class);
   }
-
-  @Provides
-  @ForwardUrl
-  String forwardUrl(Configuration config) {
-    return config.getUrl();
-  }
 }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardUrl.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PeerInfo.java
similarity index 64%
rename from src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardUrl.java
rename to src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PeerInfo.java
index 72ab757..b28a4d2 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardUrl.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PeerInfo.java
@@ -1,4 +1,4 @@
-// Copyright (C) 2015 Ericsson
+// Copyright (C) 2017 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.
@@ -12,15 +12,17 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package com.ericsson.gerrit.plugins.highavailability.forwarder.rest;
+package com.ericsson.gerrit.plugins.highavailability.peers;
 
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
+public class PeerInfo {
 
-import com.google.inject.BindingAnnotation;
+  private final String directUrl;
 
-import java.lang.annotation.Retention;
+  PeerInfo(String directUrl) {
+    this.directUrl = directUrl;
+  }
 
-@Retention(RUNTIME)
-@BindingAnnotation
-@interface ForwardUrl {
+  public String getDirectUrl() {
+    return directUrl;
+  }
 }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardUrl.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PeerInfoModule.java
similarity index 62%
copy from src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardUrl.java
copy to src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PeerInfoModule.java
index 72ab757..2384b3b 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/ForwardUrl.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PeerInfoModule.java
@@ -1,4 +1,4 @@
-// Copyright (C) 2015 Ericsson
+// Copyright (C) 2017 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.
@@ -12,15 +12,13 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package com.ericsson.gerrit.plugins.highavailability.forwarder.rest;
+package com.ericsson.gerrit.plugins.highavailability.peers;
 
-import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import com.google.inject.AbstractModule;
 
-import com.google.inject.BindingAnnotation;
-
-import java.lang.annotation.Retention;
-
-@Retention(RUNTIME)
-@BindingAnnotation
-@interface ForwardUrl {
+public class PeerInfoModule extends AbstractModule {
+  @Override
+  protected void configure() {
+    bind(PeerInfo.class).toProvider(PluginConfigPeerInfoProvider.class);
+  }
 }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PluginConfigPeerInfoProvider.java b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PluginConfigPeerInfoProvider.java
new file mode 100644
index 0000000..9e937ac
--- /dev/null
+++ b/src/main/java/com/ericsson/gerrit/plugins/highavailability/peers/PluginConfigPeerInfoProvider.java
@@ -0,0 +1,35 @@
+// Copyright (C) 2017 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.ericsson.gerrit.plugins.highavailability.peers;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+import com.ericsson.gerrit.plugins.highavailability.Configuration;
+
+public class PluginConfigPeerInfoProvider implements Provider<PeerInfo> {
+
+  private final PeerInfo peerInfo;
+
+  @Inject
+  PluginConfigPeerInfoProvider(Configuration cfg) {
+    peerInfo = new PeerInfo(cfg.getUrl());
+  }
+
+  @Override
+  public PeerInfo get() {
+    return peerInfo;
+  }
+}
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpClientProviderTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpClientProviderTest.java
index 97234f0..9cac82b 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpClientProviderTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpClientProviderTest.java
@@ -42,7 +42,6 @@
 
   @Before
   public void setUp() throws Exception {
-    when(config.getUrl()).thenReturn(EMPTY);
     when(config.getUser()).thenReturn(EMPTY);
     when(config.getPassword()).thenReturn(EMPTY);
     when(config.getConnectionTimeout()).thenReturn(TIME_INTERVAL);
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSessionTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSessionTest.java
index 7b556b7..69a188c 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSessionTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/HttpSessionTest.java
@@ -25,6 +25,7 @@
 
 import com.ericsson.gerrit.plugins.highavailability.Configuration;
 import com.ericsson.gerrit.plugins.highavailability.forwarder.rest.HttpResponseHandler.HttpResult;
+import com.ericsson.gerrit.plugins.highavailability.peers.PeerInfo;
 import com.github.tomakehurst.wiremock.http.Fault;
 import com.github.tomakehurst.wiremock.junit.WireMockRule;
 import com.github.tomakehurst.wiremock.stubbing.Scenario;
@@ -62,7 +63,6 @@
   public void setUp() throws Exception {
     String url = "http://localhost:" + wireMockRule.port();
     Configuration cfg = mock(Configuration.class);
-    when(cfg.getUrl()).thenReturn(url);
     when(cfg.getUser()).thenReturn("user");
     when(cfg.getPassword()).thenReturn("pass");
     when(cfg.getMaxTries()).thenReturn(MAX_TRIES);
@@ -70,8 +70,10 @@
     when(cfg.getSocketTimeout()).thenReturn(TIMEOUT);
     when(cfg.getRetryInterval()).thenReturn(RETRY_INTERVAL);
 
+    PeerInfo peerInfo = mock(PeerInfo.class);
+    when(peerInfo.getDirectUrl()).thenReturn(url);
     httpSession =
-        new HttpSession(new HttpClientProvider(cfg).get(), url);
+        new HttpSession(new HttpClientProvider(cfg).get(), peerInfo);
     wireMockRule.resetRequests();
   }
 
diff --git a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/RestForwarderModuleTest.java b/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/RestForwarderModuleTest.java
deleted file mode 100644
index ceedd2f..0000000
--- a/src/test/java/com/ericsson/gerrit/plugins/highavailability/forwarder/rest/RestForwarderModuleTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-// Copyright (C) 2015 Ericsson
-//
-// 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.ericsson.gerrit.plugins.highavailability.forwarder.rest;
-
-import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import com.ericsson.gerrit.plugins.highavailability.Configuration;
-
-import org.junit.Test;
-
-public class RestForwarderModuleTest {
-
-  @Test
-  public void testForwardUrlProvider() {
-    Configuration configMock = mock(Configuration.class);
-    String expected = "someUrl";
-    when(configMock.getUrl()).thenReturn(expected);
-    RestForwarderModule module = new RestForwarderModule();
-    assertThat(module.forwardUrl(configMock)).isEqualTo(expected);
-  }
-}