package org.geometrycommands;

import org.geometrycommands.DrawBase64Command.DrawOptions;
import org.junit.Test;

import java.io.File;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;

import static org.junit.Assert.assertTrue;

/**
 * The DrawBase64Command UnitTest
 *
 * @author Jared Erickson
 */
public class DrawBase64CommandTest extends BaseTest {

    @Test
    public void execute() throws Exception {
        // PNG no prefix
        String inputGeometry = "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))";
        DrawOptions options = new DrawOptions();
        options.setGeometry(inputGeometry);

        Reader reader = new StringReader(inputGeometry);
        StringWriter writer = new StringWriter();

        DrawBase64Command command = new DrawBase64Command();
        command.execute(options, reader, writer);
        String result = writer.toString().trim();
        assertTrue(result.startsWith("iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAAGoUlEQVR42u3VsQkAIAxFQXfN"));

        // JPEG with prefix
        options = new DrawOptions();
        options.setGeometry(inputGeometry);
        options.setType("jpeg");
        options.setIncludingPrefix(true);

        reader = new StringReader(inputGeometry);
        writer = new StringWriter();

        command = new DrawBase64Command();
        command.execute(options, reader, writer);
        result = writer.toString().trim();
        assertTrue(result.startsWith("data:image/jpeg;base64,/9j/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH"));
    }

    @Test
    public void run() throws Exception {
        // PNG, no prefix, geometry from options
        String result = runApp(new String[]{
                "drawbase64",
                "-g", "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))"
        }, null).trim();
        assertTrue(result.startsWith("iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAAGoUlEQVR42u3VsQkAIAxFQXfN"));

        // JPEG with prefix, geometry from input stream
        result = runApp(new String[]{
                "drawbase64",
                "-y", "jpeg",
                "-p",
                "-w", "300",
                "-h", "250",
                "-b", "0,0,0",
                "-s", "255,255,255",
                "-t", "0.75",
                "-l", "255,255,0",
                "-o", "0.55",
                "-m", "square",
                "-z", "12",
                "-e", "0,0,10,10"
        }, "POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))").trim();
        assertTrue(result.startsWith("data:image/jpeg;base64,/9j/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH"));
    }
}

@@ -210,16 +210,60 @@
 riter;%0A%0A
+import static org.junit.Assert.assertFalse;%0A
 import s
@@ -297,16 +297,16 @@
 rtTrue;%0A
-
 %0A/**%0A * 
@@ -924,38 +924,72 @@
 riter.toString()
-.trim(
+;%0A        assertTrue(result.length() %3E 0
 );%0A        asser
@@ -981,35 +981,36 @@
 ;%0A        assert
-Tru
+Fals
 e(result.startsW
@@ -1018,80 +1018,30 @@
 th(%22
-iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAAGoUlEQVR42u3VsQkAIAxFQXfN
+data:image/jpeg;base64
 %22));
@@ -1443,32 +1443,73 @@
 tring().trim();%0A
+        assertTrue(result.length() %3E 0);%0A
         assertTr
@@ -1556,62 +1556,8 @@
 se64
-,/9j/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH
 %22));
@@ -1858,92 +1858,84 @@
 ult.
-startsWith(%22iVBORw0KGgoAAAANSUhEUgAAAZAAAAGQCAYAAACAvzbMAAAGoUlEQVR42u3VsQkAIAxFQXfN
+length() %3E 0);%0A        assertFalse(result.startsWith(%22data:image/jpeg;base64
 %22));
@@ -2481,32 +2481,73 @@
 0 0))%22).trim();%0A
+        assertTrue(result.length() %3E 0);%0A
         assertTr
@@ -2594,62 +2594,8 @@
 se64
-,/9j/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH
 %22));

Fix base 64 units tests for travis ci