/*
 * Copyright (C) 2016 Google Inc.
 *
 * 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 com.google.copybara.transform;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.file.Paths.get;

import com.google.copybara.TransformWork;
import com.google.copybara.Transformation;
import com.google.copybara.exception.NonReversibleValidationException;
import com.google.copybara.exception.ValidationException;
import com.google.copybara.exception.VoidOperationException;
import com.google.copybara.testing.TransformWorks;
import com.google.copybara.util.console.testing.TestingConsole;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class ExplicitReversalTest {
  private final TestingConsole console = new TestingConsole();

  private class MockTransform implements Transformation {

    String name;

    @Override
    public void transform(TransformWork work) {
      invokedTransforms.add(name);
    }

    @Override
    public Transformation reverse() {
      throw new UnsupportedOperationException();
    }

    @Override
    public String describe() {
      return name;
    }
  }

  private List<String> invokedTransforms = new ArrayList<>();
  private MockTransform transform1 = new MockTransform();
  private MockTransform transform2 = new MockTransform();
  private ExplicitReversal explicit = new ExplicitReversal(transform1, transform2);

  @Before
  public void setup() {
    transform1.name = "t1-foo";
    transform2.name = "t2-bar";
  }

  @Test
  public void describeMatchesForwardDescribe() {
    assertThat(explicit.describe()).isEqualTo("t1-foo");
  }

  private void transform(Transformation transformation) throws Exception {
    transformation.transform(
        TransformWorks.of(get("/foo"), "test msg", console)
    );
  }

  @Test
  public void reverseToRunReverseTransform() throws Exception {
    transform(explicit.reverse());
    assertThat(invokedTransforms)
        .containsExactly("t2-bar");
  }

  @Test
  public void delegatesToForward() throws Exception {
    transform(explicit);
    assertThat(invokedTransforms)
        .containsExactly("t1-foo");
  }

  @Test
  public void reverseTwiceForSameOperation() throws Exception {
    transform(explicit.reverse().reverse());
    assertThat(invokedTransforms)
        .containsExactly("t1-foo");
  }

  private static class NoopTransform implements Transformation {

    @Override
    public void transform(TransformWork work) throws IOException, ValidationException {
      throw new VoidOperationException("Nothing happened");
    }

    @Override
    public Transformation reverse() throws NonReversibleValidationException {
      return this;
    }

    @Override
    public String describe() {
      return "noop";
    }
  }
}

@@ -814,198 +814,8 @@
 on;%0A
-import com.google.copybara.exception.NonReversibleValidationException;%0Aimport com.google.copybara.exception.ValidationException;%0Aimport com.google.copybara.exception.VoidOperationException;%0A
 impo
@@ -929,36 +929,8 @@
 le;%0A
-import java.io.IOException;%0A
 impo
@@ -2784,437 +2784,7 @@
 %0A  %7D
-%0A%0A  private static class NoopTransform implements Transformation %7B%0A%0A    @Override%0A    public void transform(TransformWork work) throws IOException, ValidationException %7B%0A      throw new VoidOperationException(%22Nothing happened%22);%0A    %7D%0A%0A    @Override%0A    public Transformation reverse() throws NonReversibleValidationException %7B%0A      return this;%0A    %7D%0A%0A    @Override%0A    public String describe() %7B%0A      return %22noop%22;%0A    %7D%0A  %7D
 %0A%7D%0A

Remove unused inner class and imports