// 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.googlesource.gerrit.plugins.its.phabricator;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.googlesource.gerrit.plugins.its.base.testutil.LoggingMockingTestCase;
import com.googlesource.gerrit.plugins.its.phabricator.conduit.Conduit;
import org.eclipse.jgit.lib.Config;
import org.junit.Test;

public class PhabricatorItsFacadeTest extends LoggingMockingTestCase {
  private Injector injector;
  private Config serverConfig;
  private Conduit.Factory conduitFactory;

  @Test
  public void testCreateLinkForWebUiDifferentUrlAndText() {
    mockUnconnectablePhabricator();

    PhabricatorItsFacade itsFacade = createPhabricatorItsFacade();
    String actual = itsFacade.createLinkForWebui("Test-Url", "Test-Text");

    assertThat(actual).isEqualTo("[[Test-Url|Test-Text]]");
  }

  @Test
  public void testCreateLinkForWebUiSameUrlAndText() {
    mockUnconnectablePhabricator();

    PhabricatorItsFacade itsFacade = createPhabricatorItsFacade();
    String actual = itsFacade.createLinkForWebui("Test-Url", "Test-Url");

    assertThat(actual).isEqualTo("[[Test-Url]]");
  }

  @Test
  public void testCreateLinkForWebUiNullText() {
    mockUnconnectablePhabricator();

    PhabricatorItsFacade itsFacade = createPhabricatorItsFacade();
    String actual = itsFacade.createLinkForWebui("Test-Url", null);

    assertThat(actual).isEqualTo("[[Test-Url]]");
  }

  @Test
  public void testCreateLinkForWebUiEmptyText() {
    mockUnconnectablePhabricator();

    PhabricatorItsFacade itsFacade = createPhabricatorItsFacade();
    String actual = itsFacade.createLinkForWebui("Test-Url", "");

    assertThat(actual).isEqualTo("[[Test-Url]]");
  }

  private PhabricatorItsFacade createPhabricatorItsFacade() {
    return injector.getInstance(PhabricatorItsFacade.class);
  }

  private void mockUnconnectablePhabricator() {
    when(serverConfig.getString("its-phabricator", null, "url")).thenReturn("<no-url>");
    when(serverConfig.getString("its-phabricator", null, "token")).thenReturn("none");
  }

  @Override
  public void setUp() throws Exception {
    super.setUp();

    injector = Guice.createInjector(new TestModule());
  }

  private class TestModule extends FactoryModule {
    @Override
    protected void configure() {
      serverConfig = mock(Config.class);
      conduitFactory = mock(Conduit.Factory.class);
      bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(serverConfig);
      bind(String.class).annotatedWith(PluginName.class).toInstance("its-phabricator");
      bind(Conduit.Factory.class).toInstance(conduitFactory);
    }
  }
}

@@ -755,16 +755,74 @@
 o.mock;%0A
+import static org.mockito.Mockito.verifyZeroInteractions;%0A
 import s
@@ -1442,16 +1442,43 @@
 Config;%0A
+  private Conduit conduit;%0A
   privat
@@ -1584,45 +1584,8 @@
 ) %7B%0A
-    mockUnconnectablePhabricator();%0A%0A
     
@@ -1778,24 +1778,62 @@
 st-Text%5D%5D%22);
+%0A%0A    verifyZeroInteractions(conduit);
 %0A  %7D%0A%0A  @Tes
@@ -1893,45 +1893,8 @@
 ) %7B%0A
-    mockUnconnectablePhabricator();%0A%0A
     
@@ -2072,32 +2072,70 @@
 %22%5B%5BTest-Url%5D%5D%22);
+%0A%0A    verifyZeroInteractions(conduit);
 %0A  %7D%0A%0A  @Test%0A  
@@ -2185,45 +2185,8 @@
 ) %7B%0A
-    mockUnconnectablePhabricator();%0A%0A
     
@@ -2358,32 +2358,70 @@
 %22%5B%5BTest-Url%5D%5D%22);
+%0A%0A    verifyZeroInteractions(conduit);
 %0A  %7D%0A%0A  @Test%0A  
@@ -2472,45 +2472,8 @@
 ) %7B%0A
-    mockUnconnectablePhabricator();%0A%0A
     
@@ -2651,16 +2651,54 @@
 Url%5D%5D%22);
+%0A%0A    verifyZeroInteractions(conduit);
 %0A  %7D%0A%0A  
@@ -2829,53 +2829,203 @@
 %0A%0A  
-private void mockUnconnectablePhabricator() %7B
+@Override%0A  public void setUp() throws Exception %7B%0A    super.setUp();%0A%0A    serverConfig = mock(Config.class);%0A    conduitFactory = mock(Conduit.Factory.class);%0A    conduit = mock(Conduit.class);%0A
 %0A   
@@ -3085,16 +3085,25 @@
  %22url%22))
+%0A        
 .thenRet
@@ -3111,16 +3111,32 @@
 rn(%22
-%3Cno-url%3E
+http://phab.example.org/
 %22);%0A
@@ -3218,90 +3218,108 @@
 rn(%22
-none%22);%0A  %7D%0A%0A  @Override%0A  public void setUp() throws Exception %7B%0A    super.setUp(
+cli-FOO%22);%0A    when(conduitFactory.create(%22http://phab.example.org/%22, %22cli-FOO%22)).thenReturn(conduit
 );%0A%0A
@@ -3480,101 +3480,8 @@
 ) %7B%0A
-      serverConfig = mock(Config.class);%0A      conduitFactory = mock(Conduit.Factory.class);%0A
     

Make implicit expectations about Conduit explicit in Facate tests