/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.hadoop.fs.contract;

import static org.apache.hadoop.fs.CommonPathCapabilities.LEASE_RECOVERABLE;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LeaseRecoverable;
import org.apache.hadoop.fs.Path;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

/**
 * Test lease recovery contract.
 */
public abstract class AbstractContractLeaseRecoveryTest extends
    AbstractFSContractTestBase {

  @Test
  public void testLeaseRecovery() throws Throwable {
    final Path path = methodPath();
    final FileSystem fs = getFileSystem();
    ContractTestUtils.touch(fs, path);
    LeaseRecoverable leaseRecoverableFs = verifyAndGetLeaseRecoverableInstance(fs, path);

    Assertions.assertThat(leaseRecoverableFs.recoverLease(path))
        .describedAs("Issuing lease recovery on a closed file must be successful")
        .isTrue();

    Assertions.assertThat(leaseRecoverableFs.isFileClosed(path))
        .describedAs("Get the isFileClose status on a closed file must be successful")
        .isTrue();
  }

  @Test
  public void testLeaseRecoveryFileNotExist() throws Throwable {
    final Path path = new Path("notExist");
    final FileSystem fs = getFileSystem();
    LeaseRecoverable leaseRecoverableFs = verifyAndGetLeaseRecoverableInstance(fs, path);

    Exception e = assertThrows(FileNotFoundException.class,
        () -> leaseRecoverableFs.recoverLease(path));
    Assertions.assertThat(e).hasMessageContaining("File does not exist");

    e = assertThrows(FileNotFoundException.class,
        () -> leaseRecoverableFs.isFileClosed(path));
    Assertions.assertThat(e).hasMessageContaining("File does not exist");
  }

  @Test
  public void testLeaseRecoveryFileOnDirectory() throws Throwable {
    final Path path = methodPath();
    final FileSystem fs = getFileSystem();
    LeaseRecoverable leaseRecoverableFs = verifyAndGetLeaseRecoverableInstance(fs, path);
    final Path parentDirectory = path.getParent();

    Exception e = assertThrows(FileNotFoundException.class,
        () -> leaseRecoverableFs.recoverLease(parentDirectory));
    Assertions.assertThat(e).hasMessageContaining("Path is not a file");

    e = assertThrows(FileNotFoundException.class,
        () -> leaseRecoverableFs.isFileClosed(parentDirectory));
    Assertions.assertThat(e).hasMessageContaining("Path is not a file");
  }

  private LeaseRecoverable verifyAndGetLeaseRecoverableInstance(FileSystem fs, Path path)
      throws IOException {
    Assertions.assertThat(fs.hasPathCapability(path, LEASE_RECOVERABLE))
        .describedAs("path capability %s of %s", LEASE_RECOVERABLE, path)
        .isTrue();
    Assertions.assertThat(fs)
        .describedAs("filesystem %s", fs)
        .isInstanceOf(LeaseRecoverable.class);
    return (LeaseRecoverable) fs;
  }
}
