/*
 * 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.beam.sdk.testing;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

import com.google.api.client.util.BackOff;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.util.IOChannelUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.Mockito;

/** Tests for {@link FileChecksumMatcher}. */
@RunWith(JUnit4.class)
public class FileChecksumMatcherTest {
  @Rule
  public TemporaryFolder tmpFolder = new TemporaryFolder();
  @Rule
  public ExpectedException thrown = ExpectedException.none();
  @Rule
  public FastNanoClockAndSleeper fastClock = new FastNanoClockAndSleeper();

  @Mock
  private PipelineResult pResult = Mockito.mock(PipelineResult.class);

  private BackOff backOff = FileChecksumMatcher.BACK_OFF_FACTORY.backoff();

  @Test
  public void testPreconditionChecksumIsNull() throws IOException {
    String tmpPath = tmpFolder.newFile().getPath();

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage(containsString("Expected valid checksum, but received"));
    new FileChecksumMatcher(null, tmpPath);
  }

  @Test
  public void testPreconditionChecksumIsEmpty() throws IOException {
    String tmpPath = tmpFolder.newFile().getPath();

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage(containsString("Expected valid checksum, but received"));
    new FileChecksumMatcher("", tmpPath);
  }

  @Test
  public void testPreconditionFilePathIsEmpty() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage(containsString("Expected valid file path, but received"));
    new FileChecksumMatcher("checksumString", "");
  }

  @Test
  public void testPreconditionShardTemplateIsNull() throws IOException {
    String tmpPath = tmpFolder.newFile().getPath();

    thrown.expect(NullPointerException.class);
    thrown.expectMessage(
        containsString(
            "Expected non-null shard pattern. "
                + "Please call the other constructor to use default pattern:"));
    new FileChecksumMatcher("checksumString", tmpPath, null);
  }

  @Test
  public void testMatcherThatVerifiesSingleFile() throws IOException{
    File tmpFile = tmpFolder.newFile("result-000-of-001");
    Files.write("Test for file checksum verifier.", tmpFile, StandardCharsets.UTF_8);
    FileChecksumMatcher matcher =
        new FileChecksumMatcher("a8772322f5d7b851777f820fc79d050f9d302915", tmpFile.getPath());

    assertThat(pResult, matcher);
  }

  @Test
  public void testMatcherThatVerifiesMultipleFiles() throws IOException {
    File tmpFile1 = tmpFolder.newFile("result-000-of-002");
    File tmpFile2 = tmpFolder.newFile("result-001-of-002");
    File tmpFile3 = tmpFolder.newFile("tmp");
    Files.write("To be or not to be, ", tmpFile1, StandardCharsets.UTF_8);
    Files.write("it is not a question.", tmpFile2, StandardCharsets.UTF_8);
    Files.write("tmp", tmpFile3, StandardCharsets.UTF_8);

    FileChecksumMatcher matcher =
        new FileChecksumMatcher(
            "90552392c28396935fe4f123bd0b5c2d0f6260c8",
            IOChannelUtils.resolve(tmpFolder.getRoot().getPath(), "result-*"));

    assertThat(pResult, matcher);
  }

  @Test
  public void testMatcherThatVerifiesFileWithEmptyContent() throws IOException {
    File emptyFile = tmpFolder.newFile("result-000-of-001");
    Files.write("", emptyFile, StandardCharsets.UTF_8);
    FileChecksumMatcher matcher =
        new FileChecksumMatcher(
            "da39a3ee5e6b4b0d3255bfef95601890afd80709",
            IOChannelUtils.resolve(tmpFolder.getRoot().getPath(), "*"));

    assertThat(pResult, matcher);
  }

  @Test
  public void testMatcherThatUsesCustomizedTemplate() throws Exception {
    // Customized template: resultSSS-totalNNN
    File tmpFile1 = tmpFolder.newFile("result0-total2");
    File tmpFile2 = tmpFolder.newFile("result1-total2");
    Files.write("To be or not to be, ", tmpFile1, StandardCharsets.UTF_8);
    Files.write("it is not a question.", tmpFile2, StandardCharsets.UTF_8);

    Pattern customizedTemplate =
        Pattern.compile("(?x) result (?<shardnum>\\d+) - total (?<numshards>\\d+)");
    FileChecksumMatcher matcher = new FileChecksumMatcher(
        "90552392c28396935fe4f123bd0b5c2d0f6260c8",
        IOChannelUtils.resolve(tmpFolder.getRoot().getPath(), "*"),
        customizedTemplate);

    assertThat(pResult, matcher);
  }
}

@@ -1191,56 +1191,8 @@
 lt;%0A
-import org.apache.beam.sdk.util.IOChannelUtils;%0A
 impo
@@ -4208,39 +4208,16 @@
         
-IOChannelUtils.resolve(
 tmpFolde
@@ -4228,27 +4228,33 @@
 tRoot().
-ge
 t
+o
 Path()
-, 
+.resolve(
 %22result-
@@ -4256,16 +4256,27 @@
 sult-*%22)
+.toString()
 );%0A%0A    
@@ -4651,39 +4651,16 @@
         
-IOChannelUtils.resolve(
 tmpFolde
@@ -4671,30 +4671,47 @@
 tRoot().
-ge
 t
+o
 Path()
-, %22*%22
+.resolve(%22*%22).toString(
 ));%0A%0A   
@@ -5381,31 +5381,8 @@
     
-IOChannelUtils.resolve(
 tmpF
@@ -5401,22 +5401,39 @@
 t().
-ge
 t
+o
 Path()
-, %22*%22
+.resolve(%22*%22).toString(
 ),%0A 

switch from IOChannelUtils to java file API