
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;
    private static final String BOOKSTORE_JSON = "{\"store\":{\"book\":[{\"title\":\"Sayings of the Century\",\"price\":8.95}," +
            "{\"title\":\"Sword of Honour\",\"price\":12.99},{\"title\":\"Moby Dick\",\"price\":8.99}," +
            "{\"title\":\"The Lord of the Rings\",\"price\":22.99}]}," +
            "\"expensive\":10}";

    @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 executeInvalidJsonObject() 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));
    }

    @Test
    public void executeComplexJsonPath() {
        final Map<String, String> resultMap = jsonPathQuery.execute(BOOKSTORE_JSON, "$..book[?(@.price <= $['expensive'])]");
        assertEquals(resultMap.get(OutputNames.RETURN_CODE), ReturnCodes.SUCCESS);
        assertEquals(resultMap.get(OutputNames.RETURN_RESULT), "[{\"title\":\"Sayings of the Century\",\"price\":8.95},{\"title\":\"Moby Dick\",\"price\":8.99}]");
    }
}
@@ -442,17 +442,74 @@
 .Assert.
-*
+assertEquals;%0Aimport static org.junit.Assert.assertNotNull
 ;%0A%0A/**%0A 
@@ -996,33 +996,16 @@
  setUp()
- throws Exception
  %7B%0A     
@@ -1088,33 +1088,16 @@
 arDown()
- throws Exception
  %7B%0A     
@@ -1168,33 +1168,16 @@
 eValid()
- throws Exception
  %7B%0A     
@@ -1497,33 +1497,16 @@
 Object()
- throws Exception
  %7B%0A     
@@ -1819,32 +1819,32 @@
    %7D%0A%0A    @Test%0A
+
     public void 
@@ -1871,25 +1871,8 @@
 th()
- throws Exception
  %7B%0A 

Refactor code