/*
 * 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-2021 the original author or authors.
 */
package org.assertj.core.internal.paths;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.error.ShouldBeReadable.shouldBeReadable;
import static org.assertj.core.error.ShouldExist.shouldExist;
import static org.assertj.core.error.ShouldHaveBinaryContent.shouldHaveBinaryContent;
import static org.assertj.core.internal.BinaryDiffResult.noDiff;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;

import org.assertj.core.api.AssertionInfo;
import org.assertj.core.internal.BinaryDiffResult;
import org.assertj.core.internal.Paths;
import org.assertj.core.internal.PathsBaseTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
 * Tests for <code>{@link Paths#assertHasBinaryContent(AssertionInfo, Path, byte[])}</code>.
 */
class Paths_assertHasBinaryContent_Test extends PathsBaseTest {

  private static Path path;
  private static byte[] expected;
  private Path mockPath;

  @BeforeAll
  static void setUpOnce() {
    // Does not matter if the values binaryDiffer, the actual comparison is mocked in this test
    path = new File("src/test/resources/actual_file.txt").toPath();
    expected = new byte[] { 0, 1 };
  }

  @BeforeEach
  void init() {
    mockPath = mock(Path.class);
  }

  @Test
  void should_pass_if_path_has_expected_text_content() throws IOException {
    when(binaryDiff.diff(path, expected)).thenReturn(noDiff());
    when(nioFilesWrapper.exists(path)).thenReturn(true);
    when(nioFilesWrapper.isReadable(path)).thenReturn(true);
    paths.assertHasBinaryContent(someInfo(), path, expected);
  }

  @Test
  void should_throw_error_if_expected_is_null() {
    assertThatNullPointerException().isThrownBy(() -> paths.assertHasBinaryContent(someInfo(), path, null))
                                    .withMessage("The binary content to compare to should not be null");
  }

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

  @Test
  void should_fail_if_actual_path_does_not_exist() {
    AssertionInfo info = someInfo();
    when(nioFilesWrapper.exists(mockPath)).thenReturn(false);

    Throwable error = catchThrowable(() -> paths.assertHasBinaryContent(info, mockPath, expected));

    assertThat(error).isInstanceOf(AssertionError.class);
    verify(failures).failure(info, shouldExist(mockPath));
  }

  @Test
  void should_fail_if_actual_is_not_a_readable_file() {
    AssertionInfo info = someInfo();
    when(nioFilesWrapper.exists(mockPath)).thenReturn(true);
    when(nioFilesWrapper.isReadable(mockPath)).thenReturn(false);

    Throwable error = catchThrowable(() -> paths.assertHasBinaryContent(info, mockPath, expected));

    assertThat(error).isInstanceOf(AssertionError.class);
    verify(failures).failure(info, shouldBeReadable(mockPath));
  }

  @Test
  void should_throw_error_wrapping_caught_IOException() throws IOException {
    IOException cause = new IOException();
    when(binaryDiff.diff(path, expected)).thenThrow(cause);
    when(nioFilesWrapper.exists(path)).thenReturn(true);
    when(nioFilesWrapper.isReadable(path)).thenReturn(true);

    assertThatExceptionOfType(UncheckedIOException.class).isThrownBy(() -> paths.assertHasBinaryContent(someInfo(),
                                                                                                        path, expected))
                                                         .withCause(cause);
  }

  @Test
  void should_fail_if_path_does_not_have_expected_binary_content() throws IOException {
    BinaryDiffResult binaryDiffs = new BinaryDiffResult(15, (byte) 0xCA, (byte) 0xFE);
    when(binaryDiff.diff(path, expected)).thenReturn(binaryDiffs);
    when(nioFilesWrapper.exists(path)).thenReturn(true);
    when(nioFilesWrapper.isReadable(path)).thenReturn(true);
    AssertionInfo info = someInfo();

    Throwable error = catchThrowable(() -> paths.assertHasBinaryContent(info, path, expected));

    assertThat(error).isInstanceOf(AssertionError.class);
    verify(failures).failure(info, shouldHaveBinaryContent(path, binaryDiffs));
  }
}
