
package io.cloudslang.content.json.actions;

import io.cloudslang.content.constants.OutputNames;
import io.cloudslang.content.constants.ReturnCodes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Map;

import static io.cloudslang.content.json.utils.JsonExceptionValues.INVALID_JSONOBJECT;
import static io.cloudslang.content.json.utils.JsonExceptionValues.INVALID_JSONPATH;
import static org.junit.Assert.*;

/**
 * Created by victor on 9/12/16.
 */
public class JsonPathQueryTest {
    private JsonPathQuery jsonPathQuery;

    @Before
    public void setUp() throws Exception {
        jsonPathQuery = new JsonPathQuery();
    }

    @After
    public void tearDown() throws Exception {
        jsonPathQuery = null;
    }

    @Test
    public void executeValid() throws Exception {
        final Map<String, String> resultMap = jsonPathQuery.execute("{'a':'b', 'a1':'b1', 'a1':'b1'}", "$.a");
        assertEquals(resultMap.get(OutputNames.RETURN_CODE), ReturnCodes.SUCCESS);
        assertEquals(resultMap.get(OutputNames.RETURN_RESULT), "\"b\"");
    }

    @Test
    public void executeInvalidJsonObjecy() throws Exception {
        final Map<String, String> resultMap = jsonPathQuery.execute(null, "$.a");
        assertEquals(resultMap.get(OutputNames.RETURN_CODE), ReturnCodes.FAILURE);
        assertEquals(resultMap.get(OutputNames.RETURN_RESULT), INVALID_JSONOBJECT);
        assertNotNull(resultMap.get(OutputNames.EXCEPTION));
    }

    @Test
    public void executeInvalidJsonPath() throws Exception {
        final Map<String, String> resultMap = jsonPathQuery.execute("{'a':'b', 'a1':'b1', 'a1':'b1'}", null);
        assertEquals(resultMap.get(OutputNames.RETURN_CODE), ReturnCodes.FAILURE);
        assertEquals(resultMap.get(OutputNames.RETURN_RESULT), INVALID_JSONPATH);
        assertNotNull(resultMap.get(OutputNames.EXCEPTION));
    }

}
@@ -560,16 +560,358 @@
 thQuery;
+%0A    private static final String BOOKSTORE_JSON = %22%7B%5C%22store%5C%22:%7B%5C%22book%5C%22:%5B%7B%5C%22title%5C%22:%5C%22Sayings of the Century%5C%22,%5C%22price%5C%22:8.95%7D,%22 +%0A            %22%7B%5C%22title%5C%22:%5C%22Sword of Honour%5C%22,%5C%22price%5C%22:12.99%7D,%7B%5C%22title%5C%22:%5C%22Moby Dick%5C%22,%5C%22price%5C%22:8.99%7D,%22 +%0A            %22%7B%5C%22title%5C%22:%5C%22The Lord of the Rings%5C%22,%5C%22price%5C%22:22.99%7D%5D%7D,%22 +%0A            %22%5C%22expensive%5C%22:10%7D%22;
 %0A%0A    @B
@@ -1488,17 +1488,17 @@
 sonObjec
-y
+t
 () throw
@@ -2241,9 +2241,441 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void executeComplexJsonPath() %7B%0A        final Map%3CString, String%3E resultMap = jsonPathQuery.execute(BOOKSTORE_JSON, %22$..book%5B?(@.price %3C= $%5B'expensive'%5D)%5D%22);%0A        assertEquals(resultMap.get(OutputNames.RETURN_CODE), ReturnCodes.SUCCESS);%0A        assertEquals(resultMap.get(OutputNames.RETURN_RESULT), %22%5B%7B%5C%22title%5C%22:%5C%22Sayings of the Century%5C%22,%5C%22price%5C%22:8.95%7D,%7B%5C%22title%5C%22:%5C%22Moby Dick%5C%22,%5C%22price%5C%22:8.99%7D%5D%22);%0A    %7D%0A
 %7D

Add test on json path containing field evaluation