/*
 * 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.
 *
 * Copyright 2012-2023 the original author or authors.
 */
package org.assertj.core.internal.iterables;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.error.ShouldNotHaveDuplicates.shouldNotHaveDuplicates;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.Arrays.array;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.core.util.Sets.newLinkedHashSet;
import static org.mockito.Mockito.verify;

import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

import org.assertj.core.api.AssertionInfo;
import org.assertj.core.internal.Iterables;
import org.assertj.core.internal.IterablesBaseTest;
import org.junit.jupiter.api.Test;

/**
 * Tests for <code>{@link Iterables#assertDoesNotHaveDuplicates(AssertionInfo, Collection)}</code>.
 * 
 * @author Alex Ruiz
 * @author Joel Costigliola
 */
class Iterables_assertDoesNotHaveDuplicates_Test extends IterablesBaseTest {

  private static final int GENERATED_OBJECTS_NUMBER = 50000;
  private final List<String> actual = newArrayList("Luke", "Yoda", "Leia");

  @Test
  void should_pass_if_actual_does_not_have_duplicates() {
    iterables.assertDoesNotHaveDuplicates(someInfo(), actual);
  }

  @Test
  void should_pass_if_actual_is_empty() {
    iterables.assertDoesNotHaveDuplicates(someInfo(), emptyList());
  }

  @Test
  void should_fail_if_actual_is_null() {
    assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> iterables.assertDoesNotHaveDuplicates(someInfo(), null))
                                                   .withMessage(actualIsNull());
  }

  @Test
  void should_fail_if_actual_contains_duplicates() {
    AssertionInfo info = someInfo();
    Collection<String> duplicates = newLinkedHashSet("Luke", "Yoda");
    actual.addAll(duplicates);

    Throwable error = catchThrowable(() -> iterables.assertDoesNotHaveDuplicates(info, actual));

    assertThat(error).isInstanceOf(AssertionError.class);
    verify(failures).failure(info, shouldNotHaveDuplicates(actual, duplicates));
  }

  @Test
  void should_pass_within_time_constraints() {
    List<UUID> generated = Stream.generate(UUID::randomUUID).limit(GENERATED_OBJECTS_NUMBER).collect(toList());

    long time = System.currentTimeMillis();
    iterables.assertDoesNotHaveDuplicates(someInfo(), generated);
    // check that it takes less than 2 seconds, usually it takes 100ms on an average computer
    // with the previous implementation, it would take minutes ...
    System.out.println("Time elapsed in ms for assertDoesNotHaveDuplicates : " + (System.currentTimeMillis() - time));
    assertThat((System.currentTimeMillis() - time)).isLessThan(2000);
  }

  @Test
  void should_fail_if_actual_contains_duplicates_array() {
    Collection<String[]> actual = newArrayList(array("Luke", "Yoda"), array("Luke", "Yoda"));
    // duplicates is commented, because mockito is not smart enough to compare arrays contents 
    // Collection<String[]> duplicates = newLinkedHashSet();
    // duplicates.add(array("Luke", "Yoda"));
    Throwable error = catchThrowable(() -> iterables.assertDoesNotHaveDuplicates(someInfo(), actual));

    assertThat(error).isInstanceOf(AssertionError.class);
    // can't use verify since mockito not smart enough to compare arrays contents
    // verify(failures).failure(info, shouldNotHaveDuplicates(actual, duplicates));
  }

  // ------------------------------------------------------------------------------------------------------------------
  // tests using a custom comparison strategy
  // ------------------------------------------------------------------------------------------------------------------

  @Test
  void should_pass_if_actual_does_not_have_duplicates_according_to_custom_comparison_strategy() {
    iterablesWithCaseInsensitiveComparisonStrategy.assertDoesNotHaveDuplicates(someInfo(), actual);
  }

  @Test
  void should_fail_if_actual_contains_duplicates_according_to_custom_comparison_strategy() {
    AssertionInfo info = someInfo();
    Collection<String> duplicates = newLinkedHashSet("LUKE", "yoda");
    actual.addAll(duplicates);

    Throwable error = catchThrowable(() -> iterablesWithCaseInsensitiveComparisonStrategy.assertDoesNotHaveDuplicates(info, actual));

    assertThat(error).isInstanceOf(AssertionError.class);
    verify(failures).failure(info, shouldNotHaveDuplicates(actual, duplicates, comparisonStrategy));
  }
  
  @Test
  void should_pass_within_time_constraints_with_custom_comparison_strategy() {
    List<String> generated = Stream.generate(() -> UUID.randomUUID().toString()).limit(GENERATED_OBJECTS_NUMBER).collect(toList());
    long time = System.currentTimeMillis();
    iterablesWithCaseInsensitiveComparisonStrategy.assertDoesNotHaveDuplicates(someInfo(), generated);
    // check that it takes less than 10 seconds, usually it takes 1000ms on an average computer
    // with the previous implementation, it would take minutes ...
    System.out.println("Time elapsed in ms for assertDoesNotHaveDuplicates with custom comparison strategy : " + (System.currentTimeMillis() - time));
    assertThat((System.currentTimeMillis() - time)).isLessThan(10000);
  }

}
