Question about expected behavior with filter

28 views
Skip to first unread message

Tim Downey

unread,
Oct 4, 2016, 9:05:25 AM10/4/16
to JsonPath
Hi,

I'm curious as to what the expected behavior of filters should be.  Take the following JSON as an example:

{
   
"class": [
       
"com.adp.vantage.reference.todo.resource.ApplicationContextResource"
    ],
    "properties": {
       
"$siren4j.class": "com.adp.vantage.reference.todo.resource.ApplicationContextResource"
    },
    "actions": [
       
{
           
"name": "list:get",
            "method": "GET",
            "href": "/rest/v1/todo/lists/{listId}",
            "title": "Get a Todo Lists",
            "type": "application/x-www-form-urlencoded",
            "fields": [
               
{
                   
"name": "id",
                    "type": "TEXT",
                    "required": true,
                    "max": -1,
                    "min": -1,
                    "maxLength": -1
                }
           
]
       
}
   
],
    "links": [
       
{
           
"rel": [
               
"baseUri"
            ],
            "href": "http://localhost"
        },
        {
           
"rel": [
               
"todoContextResource"
            ],
            "href": "/rest/v1/todo"
        }
   
]
}

I'm trying to use JsonPath to write some unit tests against my webservice apis.  I've been trying to write queries using filters to assert on properties of the document.  Here are three different examples that I'm struggling with.


        @Test

       public void testFilterBehavior() {

               // What is the expected behavior here?  Result seems to be an array, but that

               // doesn't work either.

               assertThat(json, hasJsonPath("$.actions[?(@.name == 'list:get')].method", is("GET")));

       }


               @Test

        public void testFilterBehaviorAsArray() {

               // What is the expected behavior here?  Result seems to be an array, but that

               // doesn't work either.

               assertThat(json, hasJsonPath("$.actions[?(@.name == 'list:get')].method[0]", is("GET")));

       }


        @Test

       public void testFilterBehaviorTryingLast() {

               // What is the expected behavior here?  Result seems to be an array, but that

               // doesn't work either.

               assertThat(json, hasJsonPath("$.actions[?(@.name == 'list:get')].method[-1:]", is("GET")));

       }


As you can see, I'm trying to write a query matching the action with the name of 'list:get'.  The issue is what is returned.  It seems as if the query is always returning an array (should it?).  Since it's returning an array, I've tried using array index to pull out the result.  When I assert without the array dereference, I'm failing because it is saying the results are an array.  With the array dereference, I'm failing because it is saying the result is blank.

Does anyone know what should be happening?  When I try this same JSON and query using jsonpath.com, the result is returned directly as the string GET without the array.  Given that JSONPath is less prevalent then XPath, I'm not exactly sure what the right result should be.  So either I'm doing something wrong with my query (likely) or there may be a bug.

I'm using json-path-2.2.0.

Thanks for any suggestions!
Tim


JSONPathTest.java
Reply all
Reply to author
Forward
0 new messages