package com.nogumalabs.optjson;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class OptJSONTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void usageTest() throws Exception {
        String jsonStr = "{\"key1\" : \"str1\"}";

        OptJSON json = OptJSON.parse(jsonStr);
        json.ifObject().ifPresent((optJSONObject) -> {
            optJSONObject.get("key1").ifPresent((optJSON) -> {
                optJSON.ifString().ifPresent((s -> assertEquals("str1", s)));
            });
        });
    }

    @Test
    public void arrayTest1() throws Exception {
        String jsonStr = "[1,2,3,4,5]";

        OptJSON json = OptJSON.parse(jsonStr);
        assertEquals(5, json.ifArray().get().toArrayList().size());
        long result = json.ifArray().get().toArrayList().stream().reduce(
                0L,
                (total, elm) -> total + elm.ifLong().get().longValue(),
                (total1, total2) -> total1 + total2);
        assertEquals(15L, result);
    }

    @Test
    public void arrayTest2() throws Exception {
        String jsonStr = "[1,2,3,4,\"5\"]";

        OptJSON json = OptJSON.parse(jsonStr);
        assertEquals(5, json.ifArray().get().toArrayList().size());
        long result = json.ifArray().get().toArrayList().stream().reduce(
                0L,
                (total, elm) -> {
                    return  elm.ifLong().map(aLong -> aLong + total).orElse(total);
                },
                (total1, total2) -> total1 + total2);
        assertEquals(10L, result);
    }
}
@@ -26,16 +26,44 @@
 tjson;%0A%0A
+import org.json.JSONObject;%0A
 import o
@@ -694,32 +694,781 @@
      %7D);%0A    %7D%0A%0A
+    @Test%0A    public void objectTest() throws Exception %7B%0A        JSONObject obj = new JSONObject();%0A        obj.put(%22numberA%22, 1);%0A        obj.put(%22numberB%22, 2);%0A        obj.put(%22strA%22, %221%22);%0A        OptJSON optJSON = OptJSONObject.parse(obj.toString());%0A        optJSON.ifObject().ifPresent((consumer) -%3E %7B%0A            consumer.ifObject().ifPresent((jobj) -%3E %7B%0A                long result = jobj.entrySet().stream().filter((predicate) -%3E %7B%0A                    return predicate.getKey().startsWith(%22number%22);%0A                %7D).reduce(0L,%0A                        (total, ent) -%3E total + ent.getValue().ifLong().get().longValue(),%0A                        (t1, t2) -%3Et1+t2);%0A                assertEquals(3, result);%0A            %7D);%0A        %7D);%0A    %7D%0A
     @Test%0A    pu

Add test for iterating entries in object.