-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathBaseFunctionTest.java
More file actions
62 lines (53 loc) · 2.79 KB
/
BaseFunctionTest.java
File metadata and controls
62 lines (53 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.jayway.jsonpath.internal.function;
import com.jayway.jsonpath.Configuration;
import java.io.IOException;
import java.util.Scanner;
import static com.jayway.jsonpath.JsonPath.using;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Created by mattg on 6/27/15.
*/
public class BaseFunctionTest {
protected static final String NUMBER_SERIES = "{\"empty\": [], \"numbers\" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}";
protected static final String TEXT_SERIES = "{\"urls\": [\"http://api.worldbank.org/countries/all/?format=json\", \"http://api.worldbank.org/countries/all/?format=json\"], \"text\" : [ \"a\", \"b\", \"c\", \"d\", \"e\", \"f\" ], \"text_with_duplicates\" : [ \"a\", \"b\", \"b\" ]}";
protected static final String TEXT_AND_NUMBER_SERIES = "{\"text\" : [ \"a\", \"b\", \"c\", \"d\", \"e\", \"f\" ], \"numbers\" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}";
protected static final String OBJECT_SERIES = "{\"empty\":[]," +
"\"objects\":[{\"a\": \"a-val\"}, {\"b\":\"b-val\"},{ \"a\":\"a-val\"}]," +
"\"array_of_objects\":[" +
"{\"a\":[{\"a\":\"a-val\"},{\"b\":\"b-val\"}]}," +
"{\"b\":[{\"b\":\"b-val\"}]}," +
"{\"a\":[{\"a\":\"a-val\"},{\"b\":\"b-val\"}]}]," +
"\"array_of_arrays\":[" +
"[{\"a\":\"a-val\"},{\"b\":\"b-val\"}]," +
"[{\"b\":\"b-val\"}]," +
"[{\"a\":\"a-val\"},{\"b\":\"b-val\"}]]" +
"}";
/**
* Verify the function returns the correct result based on the input expectedValue
*
* @param pathExpr
* The path expression to execute
*
* @param json
* The json document (actual content) to parse
*
* @param expectedValue
* The expected value to be returned from the test
*/
protected void verifyFunction(Configuration conf, String pathExpr, String json, Object expectedValue) {
Object result = using(conf).parse(json).read(pathExpr);
assertThat(conf.jsonProvider().unwrap(result)).isEqualTo(expectedValue);
}
protected void verifyMathFunction(Configuration conf, String pathExpr, Object expectedValue) {
verifyFunction(conf, pathExpr, NUMBER_SERIES, expectedValue);
}
protected void verifyTextFunction(Configuration conf, String pathExpr, Object expectedValue) {
verifyFunction(conf, pathExpr, TEXT_SERIES, expectedValue);
}
protected void verifyTextAndNumberFunction(Configuration conf, String pathExpr, Object expectedValue) {
verifyFunction(conf, pathExpr, TEXT_AND_NUMBER_SERIES, expectedValue);
}
protected String getResourceAsText(String resourceName) throws IOException {
return new Scanner(BaseFunctionTest.class.getResourceAsStream(resourceName), "UTF-8").useDelimiter("\\A").next();
}
}