/*
 * MIT License
 *
 * Copyright (c) 2020-present Cloudogu GmbH and Contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package sonia.scm.importexport;

import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.SCMContextProvider;
import sonia.scm.plugin.InstalledPlugin;
import sonia.scm.plugin.PluginManager;
import sonia.scm.version.Version;

import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class ScmEnvironmentCompatibilityCheckerTest {

  @Mock
  private PluginManager pluginManager;
  @Mock
  private SCMContextProvider scmContextProvider;

  @InjectMocks
  private ScmEnvironmentCompatibilityChecker checker;

  @BeforeEach
  void preparePluginManager() {
    InstalledPlugin first = mockPlugin("scm-first-plugin", "1.0.0");
    InstalledPlugin second = mockPlugin("scm-second-plugin", "1.1.0");
    lenient().when(pluginManager.getInstalled()).thenReturn(ImmutableList.of(first, second));
  }

  @Test
  void shouldReturnTrueIfEnvironmentIsSame() {
    when(scmContextProvider.getVersion()).thenReturn("2.0.0");
    ImmutableList<EnvironmentPluginDescriptor> plugins = ImmutableList.of(
      new EnvironmentPluginDescriptor("scm-first-plugin", "1.0.0"),
      new EnvironmentPluginDescriptor("scm-second-plugin", "1.1.0")
    );
    ScmEnvironment env = createScmEnvironment("2.0.0", "linux", "64", plugins);

    boolean compatible = checker.check(env);

    assertThat(compatible).isTrue();
  }

  @Test
  void shouldReturnTrueIfEnvironmentIsNewer() {
    when(scmContextProvider.getVersion()).thenReturn("2.1.0");
    ImmutableList<EnvironmentPluginDescriptor> plugins = ImmutableList.of(
      new EnvironmentPluginDescriptor("scm-first-plugin", "0.9.0"),
      new EnvironmentPluginDescriptor("scm-second-plugin", "1.0.1")
    );
    ScmEnvironment env = createScmEnvironment("2.0.0", "linux", "64", plugins);

    boolean compatible = checker.check(env);

    assertThat(compatible).isTrue();
  }

  @Test
  void shouldReturnFalseIfCoreVersionIncompatible() {
    when(scmContextProvider.getVersion()).thenReturn("2.0.0");
    ScmEnvironment env = createScmEnvironment("2.13.0", "linux", "64", Collections.emptyList());

    boolean compatible = checker.check(env);

    assertThat(compatible).isFalse();
  }

  @Test
  void shouldReturnFalseIfPluginIsIncompatible() {
    when(scmContextProvider.getVersion()).thenReturn("2.13.0");
    ImmutableList<EnvironmentPluginDescriptor> plugins = ImmutableList.of(new EnvironmentPluginDescriptor("scm-second-plugin", "1.2.0"));
    ScmEnvironment env = createScmEnvironment("2.13.0", "linux", "64", plugins);

    boolean compatible = checker.check(env);

    assertThat(compatible).isFalse();
  }

  @Test
  void shouldReturnTrueIfPluginDoNotMatch() {
    when(scmContextProvider.getVersion()).thenReturn("2.13.0");
    ImmutableList<EnvironmentPluginDescriptor> plugins = ImmutableList.of(new EnvironmentPluginDescriptor("scm-third-plugin", "42.0.0"));
    ScmEnvironment env = createScmEnvironment("2.13.0", "linux", "64", plugins);

    boolean compatible = checker.check(env);

    assertThat(compatible).isTrue();
  }

  private InstalledPlugin mockPlugin(String name, String version) {
    InstalledPlugin plugin = mock(InstalledPlugin.class, Answers.RETURNS_DEEP_STUBS);
    lenient().when(plugin.getDescriptor().getInformation().getName()).thenReturn(name);
    lenient().when(plugin.getDescriptor().getInformation().getVersion()).thenReturn(version);
    return plugin;
  }

  private ScmEnvironment createScmEnvironment(String coreVersion, String os, String arch, List<EnvironmentPluginDescriptor> pluginList) {
    ScmEnvironment scmEnvironment = new ScmEnvironment();
    scmEnvironment.setCoreVersion(coreVersion);
    scmEnvironment.setOs(os);
    scmEnvironment.setArch(arch);

    EnvironmentPluginsDescriptor environmentPluginsDescriptor = new EnvironmentPluginsDescriptor();
    environmentPluginsDescriptor.setPlugin(pluginList);
    scmEnvironment.setPlugins(environmentPluginsDescriptor);
    return scmEnvironment;
  }
}
