package net.morimekta.providence.tools.compiler;

import net.morimekta.console.util.TerminalSize;
import net.morimekta.testing.IntegrationExecutor;
import net.morimekta.util.io.IOUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

/**
 * Created by morimekta on 4/26/16.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(TerminalSize.class)
public class CompilerIT {
    private TemporaryFolder temp;

    private IntegrationExecutor compiler;
    private File     thriftFile;
    private File     refFile;
    private File     include;
    private File     output;

    @Before
    public void setUp() throws IOException {
        mockStatic(TerminalSize.class);
        when(TerminalSize.get()).thenReturn(new TerminalSize(40, 100));

        temp = new TemporaryFolder();
        temp.create();

        include = temp.newFolder("include");
        output = temp.newFolder("output");

        refFile = new File(include, "ref.thrift");
        thriftFile = temp.newFile("test.thrift");

        FileOutputStream file = new FileOutputStream(thriftFile);
        IOUtils.copy(getClass().getResourceAsStream("/compiler/test.thrift"), file);
        file.flush();
        file.close();

        file = new FileOutputStream(refFile);
        IOUtils.copy(getClass().getResourceAsStream("/compiler/ref.thrift"), file);
        file.flush();
        file.close();

        compiler = new IntegrationExecutor("providence-tools-compiler", "providence-tools-compiler.jar");
    }

    @After
    public void tearDown() {
        temp.delete();
    }

    @Test
    public void testCompile() throws IOException {
        int exitCode = compiler.run(
                "-I", include.getAbsolutePath(),
                "--out", output.getAbsolutePath(),
                "-g", "java",
                thriftFile.getAbsolutePath());

        assertEquals("", compiler.getOutput());
        assertEquals("", compiler.getError());
        assertEquals(0, exitCode);

        // It generated the file in test.thrift.
        File service = new File(output, "net/morimekta/test/compiler/MyService.java");

        assertTrue(service.exists());
        assertTrue(service.isFile());

        // And not the one in ref.thrift
        File failure = new File(output, "net/morimekta/test/compiler_ref/Failure.java");

        assertFalse(failure.exists());
    }

    @Test
    public void testCompile_2() throws IOException {
        int exitCode = compiler.run(
                "--out", output.getAbsolutePath(),
                "-g", "java",
                refFile.getAbsolutePath(),
                thriftFile.getAbsolutePath());

        assertEquals("", compiler.getOutput());
        assertEquals("", compiler.getError());
        assertEquals(0, exitCode);

        // It generated the file in test.thrift.
        File service = new File(output, "net/morimekta/test/compiler/MyService.java");

        assertTrue(service.exists());
        assertTrue(service.isFile());

        // And not the one in ref.thrift
        File failure = new File(output, "net/morimekta/test/compiler_ref/Failure.java");

        assertTrue(failure.exists());
    }

    @Test
    public void testCompile_missingInclude() throws IOException {
        int exitCode = compiler.run(
                "--out", output.getAbsolutePath(),
                "-g", "java",
                thriftFile.getAbsolutePath());

        assertEquals("", compiler.getOutput());
        assertEquals("No such package \"ref\" exists for type \"Response\"\n", compiler.getError());
        assertEquals(1, exitCode);
    }

    @Test
    public void testCompile_badReference() throws IOException {
        FileOutputStream file = new FileOutputStream(thriftFile);
        IOUtils.copy(getClass().getResourceAsStream("/compiler/test_2.thrift"), file);
        file.flush();
        file.close();

        int exitCode = compiler.run(
                "-I", include.getAbsolutePath(),
                "--out", output.getAbsolutePath(),
                "-g", "java",
                thriftFile.getAbsolutePath());

        assertEquals("", compiler.getOutput());
        assertEquals("No such type \"Request2\" in package \"ref\"\n", compiler.getError());
        assertEquals(1, exitCode);
    }
}

@@ -47,56 +47,8 @@
 r;%0A%0A
-import net.morimekta.console.util.TerminalSize;%0A
 impo
@@ -247,160 +247,8 @@
 der;
-%0Aimport org.junit.runner.RunWith;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMockRunner;
 %0A%0Aim
@@ -464,244 +464,52 @@
 ue;%0A
-import static org.powermock.api.mockito.PowerMockito.mockStatic;%0Aimport static org.powermock.api.mockito.PowerMockito.when;%0A%0A/**%0A * Created by morimekta on 4/26/16.%0A */%0A@RunWith(PowerMockRunner.class)%0A@PrepareForTest(TerminalSize.class)
+%0A/**%0A * Created by morimekta on 4/26/16.%0A */
 %0Apub
@@ -792,121 +792,8 @@
 n %7B%0A
-        mockStatic(TerminalSize.class);%0A        when(TerminalSize.get()).thenReturn(new TerminalSize(40, 100));%0A%0A
     

Remove unused powermock setup.