Skip to content

Commit d27b903

Browse files
committed
Create Jsonp modules, and create Patch tests
1 parent 4e150e6 commit d27b903

File tree

7 files changed

+310
-1
lines changed

7 files changed

+310
-1
lines changed

json-p/merge/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>json-p</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
<groupId>org.javaee8.jsonp</groupId>
10+
<artifactId>merge</artifactId>
11+
<packaging>jar</packaging>
12+
</project>

json-p/patch/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>json-p</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
<groupId>org.javaee8.jsonp</groupId>
10+
<artifactId>patch</artifactId>
11+
<packaging>jar</packaging>
12+
</project>
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package org.javaee8.jsonp;
2+
3+
import javax.json.Json;
4+
import javax.json.JsonException;
5+
import javax.json.JsonObject;
6+
import javax.json.JsonPatch;
7+
import org.jboss.arquillian.container.test.api.Deployment;
8+
import org.jboss.arquillian.junit.Arquillian;
9+
import org.jboss.shrinkwrap.api.ShrinkWrap;
10+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
11+
import org.junit.Assert;
12+
import static org.junit.Assert.assertTrue;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
16+
/**
17+
* Class that tests and demonstrates the JSON-P 1.1 Patch Operations.
18+
* @author Andrew Pielage
19+
*/
20+
@RunWith(Arquillian.class)
21+
public class JsonpPatchTest {
22+
23+
// Create a JsonObject with some values to be used in each test
24+
private static JsonObject json = Json.createObjectBuilder()
25+
.add("Wibbly", "Wobbly")
26+
.add("Replaced", false)
27+
.add("Lexicon", Json.createArrayBuilder()
28+
.add("Wibbles")
29+
.add("Wobbles")
30+
.build())
31+
.add("Nested", Json.createObjectBuilder()
32+
.add("Birdie", "Wordie")
33+
.add("Bestiary", Json.createArrayBuilder()
34+
.add("Drowner")
35+
.add("Werewolf")
36+
.add("Chimera")
37+
.build())
38+
.build())
39+
.build();
40+
41+
@Deployment
42+
public static JavaArchive createDeployment() {
43+
// Create a JavaArchive to deploy
44+
JavaArchive jar = ShrinkWrap.create(JavaArchive.class);
45+
46+
// Print out directory contents
47+
System.out.println(jar.toString(true));
48+
49+
// Return Arquillian Test Archive for application server
50+
return jar;
51+
}
52+
53+
/**
54+
* Test that the JSON Patch add operation works as intended.
55+
*/
56+
@Test
57+
public void addTest() {
58+
// Create a patch that adds some object members, replaces the value of an already existing object member, and
59+
// adds some extra elements to an array
60+
JsonPatch patch = Json.createPatchBuilder()
61+
.add("/Timey", "Wimey")
62+
.add("/Replaced", true)
63+
.add("/FunnyReference", true)
64+
.add("/FunScore", 100)
65+
.add("/Lexicon/2", "Toddles")
66+
.add("/Lexicon/2", "Tiddles")
67+
.build();
68+
69+
// Apply the patch
70+
JsonObject patchedJson = patch.apply(json);
71+
72+
// Print out to more easily see what we've done
73+
System.out.println("JsonpPatchTest.addTest: Before Patch: " + json);
74+
System.out.println("JsonpPatchTest.addTest: After Patch: " + patchedJson);
75+
76+
// Test that everything is as it should be
77+
assertTrue("Patched JSON doesn't match!", patchedJson.getString("Wibbly").equals("Wobbly"));
78+
assertTrue("Patched JSON doesn't match!", patchedJson.getString("Timey").equals("Wimey"));
79+
assertTrue("Patched JSON doesn't match!", patchedJson.getBoolean("FunnyReference"));
80+
assertTrue("Patched JSON doesn't match!", patchedJson.getInt("FunScore") == 100);
81+
assertTrue("Patched JSON doesn't match!", patchedJson.getBoolean("Replaced"));
82+
assertTrue("Patched JSON doesn't match!", patchedJson.getJsonArray("Lexicon").getString(0)
83+
.equals("Wibbles"));
84+
assertTrue("Patched JSON doesn't match!", patchedJson.getJsonArray("Lexicon").getString(1)
85+
.equals("Wobbles"));
86+
assertTrue("Patched JSON doesn't match!", patchedJson.getJsonArray("Lexicon").getString(2)
87+
.equals("Tiddles"));
88+
assertTrue("Patched JSON doesn't match!", patchedJson.getJsonArray("Lexicon").getString(3)
89+
.equals("Toddles"));
90+
}
91+
92+
/**
93+
* Test that the JSON Patch remove operation works as intended.
94+
*/
95+
@Test
96+
public void removeTest() {
97+
// Create a patch that removes an object member and an array element
98+
JsonPatch patch = Json.createPatchBuilder()
99+
.remove("/Replaced")
100+
.remove("/Lexicon/1")
101+
.build();
102+
103+
// Apply the patch
104+
JsonObject patchedJson = patch.apply(json);
105+
106+
// Print out to more easily see what we've done
107+
System.out.println("JsonpPatchTest.removeTest: Before Patch: " + json);
108+
System.out.println("JsonpPatchTest.removeTest: After Patch: " + patchedJson);
109+
110+
assertTrue("Patched JSON still contains object member!", !patchedJson.containsKey("Replaced"));
111+
assertTrue("Patched JSON still contains object member!", ((patchedJson.getJsonArray("Lexicon").size() == 1)
112+
&& (patchedJson.getJsonArray("Lexicon").getString(0).equals("Wibbles"))));
113+
}
114+
115+
/**
116+
* Test that the JSON Patch replace operation works as intended.
117+
*/
118+
@Test
119+
public void replaceTest() {
120+
// Create a patch that replaces an object member and an array element
121+
JsonPatch patch = Json.createPatchBuilder()
122+
.replace("/Replaced", true)
123+
.replace("/Lexicon/0", "Tiddles")
124+
.replace("/Lexicon/1", "Toddles")
125+
.build();
126+
127+
// Apply the patch
128+
JsonObject patchedJson = patch.apply(json);
129+
130+
// Print out to more easily see what we've done
131+
System.out.println("JsonpPatchTest.replaceTest: Before Patch: " + json);
132+
System.out.println("JsonpPatchTest.replaceTest: After Patch: " + patchedJson);
133+
134+
assertTrue("Patched JSON still contains original value!", patchedJson.getBoolean("Replaced"));
135+
assertTrue("Patched JSON still contains original values!",
136+
((patchedJson.getJsonArray("Lexicon").getString(0).equals("Tiddles"))
137+
&& (patchedJson.getJsonArray("Lexicon").getString(1).equals("Toddles"))));
138+
}
139+
140+
/**
141+
* Test that the JSON Patch move operation works as intended.
142+
*/
143+
@Test
144+
public void moveTest() {
145+
// Create a patch that moves an object member, moves an array element, and reorders an array
146+
JsonPatch patch = Json.createPatchBuilder()
147+
.move("/Nested/Tibbly", "/Wibbly")
148+
.move("/Nested/Bestiary/2", "/Lexicon/1")
149+
.move("/Nested/Bestiary/3", "/Nested/Bestiary/0")
150+
.build();
151+
152+
// Apply the patch
153+
JsonObject patchedJson = patch.apply(json);
154+
155+
// Print out to more easily see what we've done
156+
System.out.println("JsonpPatchTest.moveTest: Before Patch: " + json);
157+
System.out.println("JsonpPatchTest.moveTest: After Patch: " + patchedJson);
158+
159+
assertTrue("Patched JSON hasn't moved value!", (!patchedJson.containsKey("Wibbly")
160+
&& patchedJson.getJsonObject("Nested").getString("Tibbly").equals("Wobbly")));
161+
assertTrue("Patched JSON hasn't moved value!", ((patchedJson.getJsonArray("Lexicon").size() == 1))
162+
&& (patchedJson.getJsonObject("Nested").getJsonArray("Bestiary").getString(2).equals("Chimera"))
163+
&& (patchedJson.getJsonObject("Nested").getJsonArray("Bestiary").getString(0).equals("Werewolf")));
164+
}
165+
166+
/**
167+
* Test that the JSON Patch copy operation works as intended.
168+
*/
169+
@Test
170+
public void copyTest() {
171+
// Create a patch that copies an object member and an array element
172+
JsonPatch patch = Json.createPatchBuilder()
173+
.copy("/Nested/Tobbly", "/Wibbly")
174+
.copy("/Nested/Bestiary/2", "/Lexicon/0")
175+
.build();
176+
177+
// Apply the patch
178+
JsonObject patchedJson = patch.apply(json);
179+
180+
// Print out to more easily see what we've done
181+
System.out.println("JsonpPatchTest.copyTest: Before Patch: " + json);
182+
System.out.println("JsonpPatchTest.copyTest: After Patch: " + patchedJson);
183+
184+
assertTrue("Patched JSON hasn't moved value!", (patchedJson.containsKey("Wibbly")
185+
&& patchedJson.getJsonObject("Nested").getString("Tobbly").equals("Wobbly")));
186+
assertTrue("Patched JSON hasn't moved value!", ((patchedJson.getJsonArray("Lexicon").size() == 2))
187+
&& (patchedJson.getJsonObject("Nested").getJsonArray("Bestiary").getString(2).equals("Wibbles")));
188+
}
189+
190+
/**
191+
* Test that the JSON Patch test operation works as intended.
192+
*/
193+
@Test
194+
public void testTest() {
195+
// Create a patch that should test positive
196+
JsonPatch patch = Json.createPatchBuilder()
197+
.test("/Wibbly", "Wobbly")
198+
.test("/Lexicon/0", "Wibbles")
199+
.build();
200+
201+
// Apply the patch
202+
JsonObject patchedJson = patch.apply(json);
203+
204+
// Create a patch that should fail
205+
patch = Json.createPatchBuilder()
206+
.test("/Wibbly", "Tobbly")
207+
.build();
208+
209+
try {
210+
patchedJson = patch.apply(json);
211+
} catch (JsonException ex) {
212+
return;
213+
}
214+
215+
Assert.fail("Should have caught a JsonException and exited!");
216+
}
217+
218+
// TODO: Ignore unrecognised elements; Adding to a non-existent target; Adding a nested member object; Error Handling
219+
}

json-p/pointer/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>json-p</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
<groupId>org.javaee8.jsonp</groupId>
10+
<artifactId>pointer</artifactId>
11+
<packaging>jar</packaging>
12+
</project>

json-p/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee8</groupId>
6+
<artifactId>samples-parent</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>json-p</artifactId>
11+
<packaging>pom</packaging>
12+
<name>Java EE 8 Samples: JSON-P</name>
13+
14+
<modules>
15+
<module>patch</module>
16+
<module>pointer</module>
17+
<module>merge</module>
18+
</modules>
19+
20+
<properties>
21+
<json.version>1.1</json.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.javaee8</groupId>
27+
<artifactId>test-utils</artifactId>
28+
<version>${project.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>javax.json</groupId>
32+
<artifactId>javax.json-api</artifactId>
33+
<version>${json.version}</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.glassfish</groupId>
39+
<artifactId>javax.json</artifactId>
40+
<version>${json.version}</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "Andrew Pielage",
3+
"age": "26",
4+
"physicalAttributes": {
5+
"hair": ["blond", "short"],
6+
"height": "187cm"
7+
}
8+
}

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@
9898
<module>cdi</module>
9999
<module>jpa</module>
100100
<module>jsonb</module>
101-
<module>validation</module>
101+
<module>validation</module>
102+
<module>json-p</module>
102103
</modules>
103104

104105
<dependencyManagement>

0 commit comments

Comments
 (0)