// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.tools.server;

import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.createTlds;
import static google.registry.testing.DatabaseHelper.persistActiveDomain;

import com.google.common.collect.ImmutableSet;
import google.registry.testing.FakeClock;
import java.time.Instant;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/** Unit tests for {@link ListDomainsAction}. */
class ListDomainsActionTest extends ListActionTestCase {

  private ListDomainsAction action;

  @BeforeEach
  void beforeEach() {
    createTld("foo");
    action = new ListDomainsAction();
    action.clock = new FakeClock(Instant.parse("2018-01-01T00:00:00Z"));
    action.limit = Integer.MAX_VALUE;
  }

  @Test
  void testRun_invalidRequest_missingTlds() {
    action.tlds = ImmutableSet.of();
    testRunError(
        action,
        Optional.empty(),
        Optional.empty(),
        Optional.empty(),
        "^Must specify TLDs to query$");
  }

  @Test
  void testRun_invalidRequest_invalidTld() {
    action.tlds = ImmutableSet.of("%%%badtld%%%");
    testRunError(
        action,
        Optional.empty(),
        Optional.empty(),
        Optional.empty(),
        "^TLDs do not exist: %%%badtld%%%$");
  }

  @Test
  void testRun_noParameters() {
    action.tlds = ImmutableSet.of("foo");
    testRunSuccess(action, Optional.empty(), Optional.empty(), Optional.empty());
  }

  @Test
  void testRun_twoLinesWithIdOnly() {
    action.tlds = ImmutableSet.of("foo");
    createTlds("bar", "sim");
    persistActiveDomain("dontlist.bar", Instant.parse("2015-02-14T15:15:15Z"));
    persistActiveDomain("example1.foo", Instant.parse("2015-02-15T15:15:15Z"));
    persistActiveDomain("example2.foo", Instant.parse("2015-02-16T15:15:15Z"));
    persistActiveDomain("notlistedaswell.sim", Instant.parse("2015-02-17T15:15:15Z"));
    // Only list the two domains in .foo, not the .bar or .sim ones.
    testRunSuccess(
        action,
        Optional.empty(),
        Optional.empty(),
        Optional.empty(),
        "^example1.foo$",
        "^example2.foo$");
  }

  @Test
  void testRun_multipleTlds() {
    action.tlds = ImmutableSet.of("bar", "foo");
    createTlds("bar", "sim");
    persistActiveDomain("dolist.bar", Instant.parse("2015-01-15T15:15:15Z"));
    persistActiveDomain("example1.foo", Instant.parse("2015-02-15T15:15:15Z"));
    persistActiveDomain("example2.foo", Instant.parse("2015-03-15T15:15:15Z"));
    persistActiveDomain("notlistedaswell.sim", Instant.parse("2015-04-15T15:15:15Z"));
    testRunSuccess(
        action,
        Optional.empty(),
        Optional.empty(),
        Optional.empty(),
        "^dolist.bar",
        "^example1.foo$",
        "^example2.foo$");
  }

  @Test
  void testRun_twoLinesWithIdOnlyNoHeader() {
    action.tlds = ImmutableSet.of("foo");
    persistActiveDomain("example1.foo", Instant.parse("2010-03-04T16:00:00Z"));
    persistActiveDomain("example2.foo", Instant.parse("2011-03-04T16:00:00Z"));
    testRunSuccess(
        action,
        Optional.empty(),
        Optional.of(false),
        Optional.empty(),
        "^example1.foo$",
        "^example2.foo$");
  }

  @Test
  void testRun_twoLinesWithIdOnlyExplicitHeader() {
    action.tlds = ImmutableSet.of("foo");
    persistActiveDomain("test1.foo", Instant.parse("2010-03-04T16:00:00Z"));
    persistActiveDomain("test2.foo", Instant.parse("2011-03-04T16:00:00Z"));
    testRunSuccess(
        action,
        Optional.empty(),
        Optional.of(true),
        Optional.empty(),
        "^domainName$",
        "^-+\\s*$",
        "^test1.foo\\s*$",
        "^test2.foo\\s*$");
  }

  @Test
  void testRun_twoLinesWithRepoId() {
    action.tlds = ImmutableSet.of("foo");
    persistActiveDomain("example1.foo", Instant.parse("2010-03-04T16:00:00Z"));
    persistActiveDomain("example3.foo", Instant.parse("2011-03-04T16:00:00Z"));
    testRunSuccess(
        action,
        Optional.of("repoId"),
        Optional.empty(),
        Optional.empty(),
        "^domainName\\s+repoId\\s*$",
        "^-+\\s+-+\\s*$",
        "^example1.foo\\s+2-FOO\\s*$",
        "^example3.foo\\s+3-FOO\\s*$");
  }

  @Test
  void testRun_twoLinesWithRepoIdNoHeader() {
    action.tlds = ImmutableSet.of("foo");
    persistActiveDomain("example1.foo", Instant.parse("2010-03-04T16:00:00Z"));
    persistActiveDomain("example3.foo", Instant.parse("2011-03-04T16:00:00Z"));
    testRunSuccess(
        action,
        Optional.of("repoId"),
        Optional.of(false),
        Optional.empty(),
        "^example1.foo  2-FOO$",
        "^example3.foo  3-FOO$");
  }

  @Test
  void testRun_twoLinesWithRepoIdExplicitHeader() {
    action.tlds = ImmutableSet.of("foo");
    persistActiveDomain("example1.foo", Instant.parse("2010-03-04T16:00:00Z"));
    persistActiveDomain("example3.foo", Instant.parse("2011-03-04T16:00:00Z"));
    testRunSuccess(
        action,
        Optional.of("repoId"),
        Optional.of(true),
        Optional.empty(),
        "^domainName\\s+repoId\\s*$",
        "^-+\\s+-+\\s*$",
        "^example1.foo\\s+2-FOO\\s*$",
        "^example3.foo\\s+3-FOO\\s*$");
  }

  @Test
  void testRun_twoLinesWithWildcard() {
    action.tlds = ImmutableSet.of("foo");
    persistActiveDomain("example1.foo", Instant.parse("2010-03-04T16:00:00Z"));
    persistActiveDomain("example3.foo", Instant.parse("2010-03-05T16:00:00Z"));
    testRunSuccess(
        action,
        Optional.of("*"),
        Optional.empty(),
        Optional.empty(),
        "^domainName\\s+.*repoId",
        "^-+\\s+-+",
        "^example1.foo\\s+.*2-FOO",
        "^example3.foo\\s+.*3-FOO");
  }

  @Test
  void testRun_twoLinesWithWildcardAndAnotherField() {
    action.tlds = ImmutableSet.of("foo");
    persistActiveDomain("example1.foo", Instant.parse("2010-03-04T16:00:00Z"));
    persistActiveDomain("example3.foo", Instant.parse("2010-03-04T17:00:00Z"));
    testRunSuccess(
        action,
        Optional.of("*,repoId"),
        Optional.empty(),
        Optional.empty(),
        "^domainName\\s+.*repoId",
        "^-+\\s+-+",
        "^example1.foo\\s+.*2-FOO",
        "^example3.foo\\s+.*3-FOO");
  }

  @Test
  void testRun_withBadField_returnsError() {
    action.tlds = ImmutableSet.of("foo");
    persistActiveDomain("example2.foo");
    persistActiveDomain("example1.foo");
    testRunError(
        action,
        Optional.of("badfield"),
        Optional.empty(),
        Optional.empty(),
        "^Field 'badfield' not found - recognized fields are:");
  }

  @Test
  void testRun_limitFiltersOutOldestDomains() {
    createTlds("bar", "baz");
    action.tlds = ImmutableSet.of("foo", "bar");
    action.limit = 2;
    persistActiveDomain("example4.foo", Instant.parse("2017-04-01T00:00:00Z"));
    persistActiveDomain("example1.foo", Instant.parse("2017-01-01T00:00:00Z"));
    persistActiveDomain("example2.bar", Instant.parse("2017-02-01T00:00:00Z"));
    persistActiveDomain("example3.bar", Instant.parse("2017-03-01T00:00:00Z"));
    persistActiveDomain("example5.baz", Instant.parse("2018-01-01T00:00:00Z"));
    testRunSuccess(
        action,
        Optional.empty(),
        Optional.empty(),
        Optional.empty(),
        "^example3.bar$",
        "^example4.foo$");
  }
}
