Skip to content

Commit bd5a42b

Browse files
committed
Add tests for objectAtPathText
1 parent 6f555e5 commit bd5a42b

File tree

6 files changed

+90
-7
lines changed

6 files changed

+90
-7
lines changed

integration-tests/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@
4040
<dependency>
4141
<groupId>junit</groupId>
4242
<artifactId>junit</artifactId>
43-
<version>4.12</version>
43+
<scope>test</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.fasterxml.jackson.core</groupId>
48+
<artifactId>jackson-databind</artifactId>
4449
<scope>test</scope>
4550
</dependency>
4651
</dependencies>

integration-tests/src/test/java/com/github/t9t/jooq/json/AbstractJsonDSLTest.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.t9t.jooq.json;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
35
import org.jooq.DSLContext;
46
import org.jooq.Field;
57
import org.jooq.SQLDialect;
@@ -9,17 +11,20 @@
911
import org.junit.runner.RunWith;
1012
import org.junit.runners.Parameterized;
1113

14+
import java.io.IOException;
1215
import java.util.ArrayList;
1316
import java.util.Arrays;
1417
import java.util.List;
1518
import java.util.function.BiFunction;
1619

1720
import static com.github.t9t.jooq.generated.Tables.JSON_TEST;
1821
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertNull;
1923

2024
@RunWith(Parameterized.class)
2125
public abstract class AbstractJsonDSLTest {
2226
private static final DSLContext dsl = DSL.using(TestDb.createDataSource(), SQLDialect.POSTGRES_10);
27+
private static final ObjectMapper om = new ObjectMapper();
2328

2429
static final String genericRow = "json-dsl";
2530
static final String arrayRow = "array";
@@ -29,7 +34,7 @@ public abstract class AbstractJsonDSLTest {
2934
@Parameterized.Parameter(1)
3035
public String rowName;
3136
@Parameterized.Parameter(2)
32-
public String expected;
37+
public Object expected;
3338
@Parameterized.Parameter(3)
3439
public Field<String> fieldToSelect;
3540

@@ -46,15 +51,15 @@ static List<Object[]> generateParams(String baseName, BiFunction<String, Field<S
4651
return params;
4752
}
4853

49-
static Params test(String name, String expected, Field<String> field) {
54+
static Params test(String name, Object expected, Field<String> field) {
5055
return params(name, genericRow, expected, field);
5156
}
5257

5358
static Params arrayTest(String name, String expected, Field<String> field) {
5459
return params(name, arrayRow, expected, field);
5560
}
5661

57-
private static Params params(String name, String rowName, String expected, Field<String> field) {
62+
private static Params params(String name, String rowName, Object expected, Field<String> field) {
5863
return new Params(name, rowName, expected, field);
5964
}
6065

@@ -74,7 +79,25 @@ public void setUp() {
7479

7580
@Test
7681
public void test() {
77-
assertEquals(expected, select(rowName, fieldToSelect));
82+
String data = select(rowName, fieldToSelect);
83+
84+
if (expected == null) {
85+
assertNull(data);
86+
} else if (expected instanceof String) {
87+
assertEquals(expected, data);
88+
} else if (expected instanceof JsonNode) {
89+
assertEquals(expected, toNode(data));
90+
} else {
91+
throw new IllegalArgumentException("Cannot assert object: " + expected.getClass());
92+
}
93+
}
94+
95+
static JsonNode toNode(String s) {
96+
try {
97+
return om.readTree(s);
98+
} catch (IOException e) {
99+
throw new AssertionError("Unable to parse JSON: " + s, e);
100+
}
78101
}
79102

80103
private static String select(String rowName, Field<String> field) {
@@ -87,10 +110,10 @@ private static String select(String rowName, Field<String> field) {
87110
static class Params {
88111
final String name;
89112
final String dataSet;
90-
final String expected;
113+
final Object expected;
91114
final Field<String> fieldToSelect;
92115

93-
public Params(String name, String dataSet, String expected, Field<String> fieldToSelect) {
116+
public Params(String name, String dataSet, Object expected, Field<String> fieldToSelect) {
94117
this.name = name;
95118
this.dataSet = dataSet;
96119
this.expected = expected;

integration-tests/src/test/java/com/github/t9t/jooq/json/JsonDSLObjectAtPathIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class JsonDSLObjectAtPathIT extends AbstractJsonDSLTest {
1010
public static List<Object[]> params() {
1111
return generateParams("objectAtPath", (type, f) -> Arrays.asList(
1212
test("oneLevel", "\"Hello, " + type + " world!\"", JsonDSL.objectAtPath(f, "str")),
13+
test("obj", toNode("{\"i\": 5521, \"b\": true}"), JsonDSL.objectAtPath(f, "obj")),
1314
test("deepVarargs", "4408", JsonDSL.objectAtPath(f, "arr", "0", "d")),
1415
test("deepCollection", "4408", JsonDSL.objectAtPath(f, Arrays.asList("arr", "0", "d"))),
1516
test("notExistingPath", null, JsonDSL.objectAtPath(f, "not", "existing", "path"))

integration-tests/src/test/java/com/github/t9t/jooq/json/JsonDSLObjectAtPathTextIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class JsonDSLObjectAtPathTextIT extends AbstractJsonDSLTest {
1010
public static List<Object[]> params() {
1111
return generateParams("objectAtPath", (type, f) -> Arrays.asList(
1212
test("oneLevel", "Hello, " + type + " world!", JsonDSL.objectAtPathText(f, "str")),
13+
test("obj", toNode("{\"i\": 5521, \"b\": true}"), JsonDSL.objectAtPathText(f, "obj")),
1314
test("deepVarargs", "4408", JsonDSL.objectAtPathText(f, "arr", "0", "d")),
1415
test("deepCollection", "4408", JsonDSL.objectAtPathText(f, Arrays.asList("arr", "0", "d"))),
1516
test("notExistingPath", null, JsonDSL.objectAtPathText(f, "not", "existing", "path"))

jooq-postgresql-json/src/main/java/com/github/t9t/jooq/json/JsonDSL.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,64 @@ public static Field<String> fieldByKeyText(Field<String> jsonField, String key)
7070
return DSL.field("{0}->>{1}", String.class, jsonField, key);
7171
}
7272

73+
/**
74+
* <p>Get JSON object at specified path using the <code>#&gt;</code> operator</p>
75+
*
76+
* <p>Example: <code>'{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'</code></p>
77+
* <p>Example result: <code>{"c": "foo"}</code></p>
78+
*
79+
* @param jsonField The JSON {@code Field} to extract the path from
80+
* @param path Path to the the object to return
81+
* @return A {@code Field} representing the object at the specified path
82+
* @see #objectAtPath(Field, Collection)
83+
*/
7384
public static Field<String> objectAtPath(Field<String> jsonField, String... path) {
7485
return DSL.field("{0}#>{1}", String.class, jsonField, DSL.array(path));
7586
}
7687

88+
/**
89+
* <p>Get JSON object at specified path using the <code>#&gt;</code> operator</p>
90+
*
91+
* <p>Example: <code>'{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'</code></p>
92+
* <p>Example result: <code>{"c": "foo"}</code></p>
93+
*
94+
* @param jsonField The JSON {@code Field} to extract the path from
95+
* @param path Path to the the object to return
96+
* @return A {@code Field} representing the object at the specified path
97+
* @see #objectAtPath(Field, String...)
98+
*/
7799
public static Field<String> objectAtPath(Field<String> jsonField, Collection<String> path) {
78100
return objectAtPath(jsonField, path.toArray(new String[0]));
79101
}
80102

103+
/**
104+
* <p>Get JSON object at specified path as {@code text} rather than {@code json(b)}, using the <code>#&gt;</code>
105+
* operator</p>
106+
*
107+
* <p>Example: <code>'{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'</code></p>
108+
* <p>Example result: <code>3</code></p>
109+
*
110+
* @param jsonField The JSON {@code Field} to extract the path from
111+
* @param path Path to the the object to return
112+
* @return A {@code Field} representing the object at the specified path, as text
113+
* @see #objectAtPathText(Field, Collection)
114+
*/
81115
public static Field<String> objectAtPathText(Field<String> jsonField, String... path) {
82116
return DSL.field("{0}#>>{1}", String.class, jsonField, DSL.array(path));
83117
}
84118

119+
/**
120+
* <p>Get JSON object at specified path as {@code text} rather than {@code json(b)}, using the <code>#&gt;</code>
121+
* operator</p>
122+
*
123+
* <p>Example: <code>'{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'</code></p>
124+
* <p>Example result: <code>3</code></p>
125+
*
126+
* @param jsonField The JSON {@code Field} to extract the path from
127+
* @param path Path to the the object to return
128+
* @return A {@code Field} representing the object at the specified path, as text
129+
* @see #objectAtPath(Field, String...)
130+
*/
85131
public static Field<String> objectAtPathText(Field<String> jsonField, Collection<String> path) {
86132
return objectAtPathText(jsonField, path.toArray(new String[0]));
87133
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@
5151
<version>4.12</version>
5252
<scope>test</scope>
5353
</dependency>
54+
55+
<dependency>
56+
<groupId>com.fasterxml.jackson.core</groupId>
57+
<artifactId>jackson-databind</artifactId>
58+
<version>2.9.8</version>
59+
<scope>test</scope>
60+
</dependency>
5461
</dependencies>
5562
</dependencyManagement>
5663

0 commit comments

Comments
 (0)