/
JsonNodePathTest.java
125 lines (106 loc) · 4.3 KB
/
JsonNodePathTest.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.pallasathenagroup.querydsl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.pallasathenagroup.querydsl.json.JsonExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static com.pallasathenagroup.querydsl.QJsonNodeEntity.jsonNodeEntity;
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
import static org.junit.Assert.assertEquals;
public class JsonNodePathTest extends BaseCoreFunctionalTestCase {
private final ObjectMapper objectMapper = new ObjectMapper();
private JsonNodeEntity entity;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { JsonNodeEntity.class };
}
@Before
public void setUp() {
doInJPA(this::sessionFactory, entityManager -> {
entity = new JsonNodeEntity();
entity.jsonNode = objectMapper.valueToTree(ImmutableMap.of("a", 123));
entityManager.persist(entity);
});
}
@Override
protected boolean isCleanupTestDataRequired() {
return true;
}
@Test
public void getFieldAsText() {
doInJPA(this::sessionFactory, entityManager -> {
String result = new JPAQuery<RangeEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(jsonNodeEntity)
.select(
jsonNodeEntity.jsonNode.get("a").asText()
)
.fetchOne();
assertEquals("123", result);
});
}
@Test
public void getFieldAsNode() {
doInJPA(this::sessionFactory, entityManager -> {
JsonNode result = new JPAQuery<RangeEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(jsonNodeEntity)
.select(
jsonNodeEntity.jsonNode.get("a")
)
.fetchOne();
assertEquals(objectMapper.valueToTree(123), result);
});
}
@Test
public void getKeys() {
doInJPA(this::sessionFactory, entityManager -> {
List<String> result = new JPAQuery<RangeEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(jsonNodeEntity)
.select(
jsonNodeEntity.jsonNode.keys()
)
.fetch();
assertEquals(ImmutableList.of("a"), result);
});
}
@Test
public void buildJsonObject() {
doInJPA(this::sessionFactory, entityManager -> {
ObjectNode result = new JPAQuery<RangeEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(jsonNodeEntity)
.select(
JsonExpressions.buildJsonObject(jsonNodeEntity.id.as("id"), jsonNodeEntity.id.as("id2"))
)
.fetchOne();
try {
assertEquals(ImmutableMap.of("id", entity.id.intValue(), "id2", entity.id.intValue()), objectMapper.treeToValue(result, Map.class));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}
@Test
public void buildJsonObject2() {
doInJPA(this::sessionFactory, entityManager -> {
ObjectNode result = new JPAQuery<RangeEntity>(entityManager, ExtendedHQLTemplates.DEFAULT)
.from(jsonNodeEntity)
.select(
JsonExpressions.buildJsonObject(ImmutableMap.of("id", jsonNodeEntity.id, "id2", jsonNodeEntity.id))
)
.fetchOne();
try {
assertEquals(ImmutableMap.of("id", entity.id.intValue(), "id2", entity.id.intValue()), objectMapper.treeToValue(result, Map.class));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}
}