package io.github.mattthomson.depijp;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import io.github.mattthomson.depijp.tap.TextLineDePijpTap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ExpectedSystemExit;

import java.io.File;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class DePijpTest {
    @Rule
    public final ExpectedSystemExit exit = ExpectedSystemExit.none();

    @Test
    public void shouldRunFlow() throws Exception {
        File outputFile = File.createTempFile("output", "txt");
        outputFile.deleteOnExit();

        exit.expectSystemExitWithStatus(0);
        DePijp.main(new String[]{SimpleFlow.class.getName(), outputFile.getPath()});

        List<String> result = Files.readLines(outputFile, Charsets.UTF_8);
        assertThat(result).containsExactly("one", "two", "three");
    }

    @Test
    public void shouldReturnNonZeroExitCodeIfFlowFails() throws Exception {
        File outputFile = File.createTempFile("output", "txt");
        outputFile.deleteOnExit();

        exit.expectSystemExitWithStatus(1);
        DePijp.main(new String[]{InvalidFlow.class.getName(), outputFile.getPath()});
    }

    public static class SimpleFlow implements DePijpFlow {
        @Override
        public void flow(PijpBuilder pijpBuilder, String[] args) {
            pijpBuilder
                    .read(new TextLineDePijpTap("src/test/resources/values.txt"))
                    .write(new TextLineDePijpTap(args[1]));
        }
    }

    public static class InvalidFlow implements DePijpFlow {
        @Override
        public void flow(PijpBuilder pijpBuilder, String[] args) {
            pijpBuilder
                    .read(new TextLineDePijpTap("src/test/resources/values.txt"));
        }
    }
}
@@ -703,86 +703,50 @@
     
-DePijp.main(new String%5B%5D%7BSimpleFlow.class.getName(), outputFile.getPath()%7D);%0A%0A
+exit.checkAssertionAfterwards(() -%3E %7B%0A    
     
@@ -824,16 +824,20 @@
         
+    
 assertTh
@@ -883,24 +883,122 @@
 , %22three%22);%0A
+        %7D);%0A%0A        DePijp.main(new String%5B%5D%7BSimpleFlow.class.getName(), outputFile.getPath()%7D);%0A
     %7D%0A%0A    @
@@ -1222,16 +1222,25 @@
 tus(1);%0A
+        %0A
         

Verify assertion after exit.