Skip to content

Commit

Permalink
Add method to JsonHttpTest to return n-th element from a non-definiti…
Browse files Browse the repository at this point in the history
…ve selection, needed as workaround for: json-path/JsonPath#272
  • Loading branch information
fhoeben committed May 22, 2017
1 parent 57fa835 commit 14aa578
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/main/java/nl/hsac/fitnesse/fixture/slim/JsonHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public Object jsonPath(String path) {
return getPathHelper().getJsonPath(responseString, jsonPath);
}

public Object elementOfJsonPath(int index, String path) {
List<Object> all = getAllMatches(path);
return all.get(index);
}

public int jsonPathCount(String path) {
List<Object> all = getAllMatches(path);
return all.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public void testJsonPath() {
assertEquals("Herman Melville", result);
}

@Test
public void testFindWithIndexExp() {
List<Object> result = helper.getAllJsonPath(JSON, "$.store.book[?(@.author=='Herman Melville')].title");
assertEquals("Moby Dick", result.get(0));
}

@Test
public void testJsonPathException() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
|add response|Broken|with status|500|
|$url= |get mock server url |

|script |json http test |
|get from |$url |
|check |json path count |airplanes[*] |2 |
|check |json path |airplanes.length() |2 |
|show |all json path matches|airplanes[*].airline |
|check |all json path matches|airplanes[*].airline|=~/.*KLM.*Virgin.*/|
|check |json path |airplanes[0].seats |400 |
|set json path|airplanes[0].seats |to |300 |
|check |json path |airplanes[0].seats |300 |
|show |response |
|script |json http test |
|get from |$url |
|check |json path count |airplanes[*] |2 |
|check |json path |airplanes.length() |2 |
|show |all json path matches|airplanes[*].airline |
|check |all json path matches|airplanes[*].airline|=~/.*KLM.*Virgin.*/ |
|check |json path |airplanes[0].seats |400 |
|check |element |0 |of json path|airplanes[?(@.airline=='KLM')].type|747|
|set json path|airplanes[0].seats |to |300 |
|check |json path |airplanes[0].seats |300 |
|show |response |

And now a call which returns an error status code.

Expand Down
1 change: 1 addition & 0 deletions wiki/FitNesseRoot/HsacExamples/SlimTests/HttpTests.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The commands/keywords (i.e. public methods) added by !-nl.hsac.fitnesse.fixture.
|json path <expression> |Evaluates the (definite) expression against the last response received. |
|json path count <expression> |Evaluates the (indefinite) expression against the last response received, counting the number of results. |
|all json path matches <expression> |Evaluates the expression against the last response received and returns all results. Intended to be used with 'show' command. |
|result <index> of json path <expression> |Evaluates the (indefinite) expression against the last response received, and returns the index-th element of the results. |
|set json path <expression> to <value> |Evaluates the expression against the last response received and -if exactly one key is found- replaces that key's existing value with the value that is provided.|
|repeat until json path <expression> is <value>|If 'expression' does not evaluates to 'value' for the last response: repeats last request until it does. |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ json={"key":"value"}
|check |json path |size |>0 |
|check |json path count |$.* |>=5 |
|show |all json path matches|* |
|check |element |1 |of json path |$.* |false |


Next another POST, posting of url-enoded values defined in the wiki.

Expand Down

0 comments on commit 14aa578

Please sign in to comment.