package com.brettonw.bedrock.bag;

import com.brettonw.bedrock.bag.formats.MimeType;
import org.junit.Test;

import java.io.IOException;

public class FromUrlTest {
    @Test
    public void test () {

    }

    @Test
    public void testGet () throws IOException {
        BagObject brettonw = BagObjectFrom.url ("http://bedrock.brettonw.com/api?event=ip", () -> null);
        BagTest.report (brettonw.getString ("response/ip") != null, true, "Got a valid BagObject");

        // XXX should add an array processor to the test API for when github throttles the API
        /*
        BagArray arrayEcho = BagArrayFrom.url ("http://bedrock.brettonw.com/api?event=echo-array", () -> null);
        BagTest.report (repos.getCount () > 0, true, "Got a valid BagArray");
        */

        BagArray repos = BagArrayFrom.url ("https://api.github.com/users/brettonw/repos", () -> null);
        BagTest.report (repos.getCount () > 0, true, "Got a valid BagArray");
    }

    @Test
    public void testPost () throws IOException {
        BagObject postResponseBagObject = BagObjectFrom.url ("http://bedrock.brettonw.com/api?event=echo",
                new BagObject ()
                        .put ("login", "brettonw")
                        .put ("First Name", "Bretton")
                        .put ("Last Name", "Wade"),
                MimeType.JSON,
                () -> null
        );
        BagTest.report (postResponseBagObject.getString ("post-data/login"), "brettonw", "Got a valid BagObject - 1");

        postResponseBagObject = BagObjectFrom.url ("http://bedrock.brettonw.com/api?event=echo",
                new BagObject ()
                        .put ("login", "brettonw")
                        .put ("First Name", "Bretton")
                        .put ("Last Name", "Wade"),
                MimeType.JSON
        );
        BagTest.report (postResponseBagObject.getString ("post-data/login"), "brettonw", "Got a valid BagObject - 2");

        BagArray postResponseBagArray = BagArrayFrom.url ("http://bedrock.brettonw.com/api?event=post-data",
                new BagArray ()
                        .add ("login")
                        .add ("brettonw")
                        .add ("First Name")
                        .add ("Bretton")
                        .add ("Last Name")
                        .add ("Wade"),
                MimeType.JSON
        );
        BagTest.report (postResponseBagArray.getString (1), "brettonw", "Got a valid BagArray - 1");

        postResponseBagArray = BagArrayFrom.url ("http://bedrock.brettonw.com/api?event=post-data",
                new BagArray ()
                        .add ("login")
                        .add ("brettonw")
                        .add ("First Name")
                        .add ("Bretton")
                        .add ("Last Name")
                        .add ("Wade"),
                MimeType.JSON,
                () -> null
        );
        BagTest.report (postResponseBagArray.getString (1), "brettonw", "Got a valid BagArray - 2");
    }

    @Test
    public void testBogusGet () throws IOException {
        BagObject bogus = BagObjectFrom.url ("http://gojsonogle.com", () -> null);
        BagTest.report (bogus, null, "Not a valid URL");
    }

    @Test
    public void testBogusPost () throws IOException {
        BagObject bogus = BagObjectFrom.url ("http://gojsonogle.com", BagObject.open  ("a", "b"), MimeType.JSON, () -> null);
        BagTest.report (bogus, null, "Not a valid URL");
    }
}

@@ -305,32 +305,33 @@
 tFrom.url (%22http
+s
 ://bedrock.brett
@@ -358,32 +358,32 @@
 %22, () -%3E null);%0A
-
         BagTest.
@@ -566,19 +566,8 @@
 API%0A
-        /*%0A
     
@@ -606,32 +606,33 @@
 yFrom.url (%22http
+s
 ://bedrock.brett
@@ -703,206 +703,17 @@
 rt (
-repos.getCount () %3E 0, true, %22Got a valid BagArray%22);%0A        */%0A%0A        BagArray repos = BagArrayFrom.url (%22https://api.github.com/users/brettonw/repos%22, () -%3E null);%0A        BagTest.report (repos
+arrayEcho
 .get

revise tests for new deployment