/*
 * 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.repository;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.function.Supplier;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class RepositoryReadOnlyCheckerTest {

  private final Repository repository = new Repository("1", "git", "hitchhiker", "HeartOfGold");

  private boolean archived = false;
  private boolean exporting = false;

  private final RepositoryArchivedCheck archivedCheck = repositoryId -> archived;
  private final RepositoryExportingCheck exportingCheck = new RepositoryExportingCheck() {
    @Override
    public boolean isExporting(String repositoryId) {
      return exporting;
    }

    @Override
    public <T> T withExportingLock(Repository repository, Supplier<T> callback) {
      return null;
    }
  };

  @BeforeEach
  void resetStates() {
    archived = false;
    exporting = false;
  }

  @AfterEach
  void unregisterStaticChecks() {
    RepositoryReadOnlyChecker.setReadOnlyChecks(Collections.emptySet());
  }

  @Nested
  class LegacyChecks {

    private final RepositoryReadOnlyChecker checker = new RepositoryReadOnlyChecker(archivedCheck, exportingCheck);

    @Test
    void shouldReturnFalseIfAllChecksFalse() {
      boolean readOnly = checker.isReadOnly(repository);

      assertThat(readOnly).isFalse();
    }

    @Test
    void shouldReturnTrueIfArchivedIsTrue() {
      archived = true;

      boolean readOnly = checker.isReadOnly(repository);

      assertThat(readOnly).isTrue();
    }

    @Test
    void shouldReturnTrueIfExportingIsTrue() {
      exporting = true;

      boolean readOnly = checker.isReadOnly(repository);

      assertThat(readOnly).isTrue();
    }

    @Test
    void shouldHandleLegacyAndStatic() {
      assertThat(checker.isReadOnly(repository)).isFalse();

      RepositoryReadOnlyChecker.setReadOnlyChecks(Collections.singleton(new SampleReadOnlyCheck(true)));
      assertThat(checker.isReadOnly(repository)).isTrue();

      RepositoryReadOnlyChecker.setReadOnlyChecks(Collections.emptySet());
      assertThat(checker.isReadOnly(repository)).isFalse();

      exporting = true;
      assertThat(checker.isReadOnly(repository)).isTrue();
    }

  }

  @Nested
  class StaticChecks {

    private final RepositoryReadOnlyChecker checker = new RepositoryReadOnlyChecker();

    @BeforeEach
    void registerStaticChecks() {
      RepositoryReadOnlyChecker.setReadOnlyChecks(Arrays.asList(
        archivedCheck, exportingCheck
      ));
    }

    @Test
    void shouldReturnFalseIfAllChecksFalse() {
      boolean readOnly = checker.isReadOnly(repository);

      assertThat(readOnly).isFalse();
    }

    @Test
    void shouldReturnTrueIfArchivedIsTrue() {
      archived = true;

      boolean readOnly = checker.isReadOnly(repository);

      assertThat(readOnly).isTrue();
    }

    @Test
    void shouldReturnTrueIfExportingIsTrue() {
      exporting = true;

      boolean readOnly = checker.isReadOnly(repository);

      assertThat(readOnly).isTrue();
    }

    @Test
    void shouldThrowReadOnlyException() {
      exporting = true;

      assertThrows(ReadOnlyException.class, () -> RepositoryReadOnlyChecker.checkReadOnly(repository));
    }

  }

  private static class SampleReadOnlyCheck implements ReadOnlyCheck {

    private final boolean readOnly;

    public SampleReadOnlyCheck(boolean readOnly) {
      this.readOnly = readOnly;
    }

    @Override
    public String getReason() {
      return "testing purposes";
    }

    @Override
    public boolean isReadOnly(String repositoryId) {
      return readOnly;
    }
  }

}
