|  | 
|  | 1 | +package org.javaee8.jsonp.merge; | 
|  | 2 | + | 
|  | 3 | +import javax.json.Json; | 
|  | 4 | +import javax.json.JsonMergePatch; | 
|  | 5 | +import javax.json.JsonObject; | 
|  | 6 | +import javax.json.JsonValue; | 
|  | 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 static org.junit.Assert.assertTrue; | 
|  | 12 | +import org.junit.Test; | 
|  | 13 | +import org.junit.runner.RunWith; | 
|  | 14 | + | 
|  | 15 | +/** | 
|  | 16 | + * Class that tests and demonstrates the JSON-P 1.1 Merge Operations. | 
|  | 17 | + * @author Andrew Pielage | 
|  | 18 | + */ | 
|  | 19 | +@RunWith(Arquillian.class) | 
|  | 20 | +public class JsonpMergeTest { | 
|  | 21 | + | 
|  | 22 | + // Create a JsonObject with some values to be used in each test | 
|  | 23 | + private static final JsonObject json = Json.createObjectBuilder() | 
|  | 24 | + .add("Wibbly", "Wobbly") | 
|  | 25 | + .add("Replaced", false) | 
|  | 26 | + .add("Lexicon", Json.createArrayBuilder() | 
|  | 27 | + .add("Wibbles") | 
|  | 28 | + .add("Wobbles") | 
|  | 29 | + .build()) | 
|  | 30 | + .add("Nested", Json.createObjectBuilder() | 
|  | 31 | + .add("Birdie", "Wordie") | 
|  | 32 | + .add("Bestiary", Json.createArrayBuilder() | 
|  | 33 | + .add("Drowner") | 
|  | 34 | + .add("Werewolf") | 
|  | 35 | + .add("Chimera") | 
|  | 36 | + .build()) | 
|  | 37 | + .build()) | 
|  | 38 | + .build(); | 
|  | 39 | + | 
|  | 40 | + @Deployment | 
|  | 41 | + public static JavaArchive createDeployment() { | 
|  | 42 | + // Create a JavaArchive to deploy | 
|  | 43 | + JavaArchive jar = ShrinkWrap.create(JavaArchive.class); | 
|  | 44 | + | 
|  | 45 | + // Print out directory contents | 
|  | 46 | + System.out.println(jar.toString(true)); | 
|  | 47 | + | 
|  | 48 | + // Return Arquillian Test Archive for application server | 
|  | 49 | + return jar; | 
|  | 50 | + } | 
|  | 51 | + | 
|  | 52 | + /** | 
|  | 53 | + * Test that the JSON Merge operation replaces values as intended.  | 
|  | 54 | + */ | 
|  | 55 | + @Test | 
|  | 56 | + public void replaceTest() { | 
|  | 57 | + // Create a JSON object that we'll merge into the class variable, replacing object members and array values | 
|  | 58 | + JsonObject jsonToMerge = Json.createObjectBuilder() | 
|  | 59 | + .add("Wibbly", "Bibbly") | 
|  | 60 | + .add("Replaced", "Yes") | 
|  | 61 | + .add("Lexicon", Json.createArrayBuilder() | 
|  | 62 | + .add("Wibbles") | 
|  | 63 | + .add("Bibbles") | 
|  | 64 | + .build()) | 
|  | 65 | + .add("Nested", Json.createObjectBuilder() | 
|  | 66 | + .add("Bestiary", Json.createArrayBuilder() | 
|  | 67 | + .add("Slyzard") | 
|  | 68 | + .add("Dragon") | 
|  | 69 | + .add("Ekimmara") | 
|  | 70 | + .build()) | 
|  | 71 | + .build()) | 
|  | 72 | + .build(); | 
|  | 73 | + | 
|  | 74 | + // Create a merge patch and apply it | 
|  | 75 | + JsonMergePatch mergePatch = Json.createMergePatch(jsonToMerge); | 
|  | 76 | + JsonValue mergedJson = mergePatch.apply(json); | 
|  | 77 | + | 
|  | 78 | + // Print out to more easily see what we've done | 
|  | 79 | + System.out.println("JsonpMergeTest.replaceTest: Before Merge: " + json); | 
|  | 80 | + System.out.println("JsonpMergeTest.replaceTest: JSON to Merge: " + jsonToMerge); | 
|  | 81 | + System.out.println("JsonpMergeTest.replaceTest: After Merge: " + mergedJson); | 
|  | 82 | + | 
|  | 83 | + // Test that everything is as it should be | 
|  | 84 | + JsonObject mergedJsonObject = mergedJson.asJsonObject(); | 
|  | 85 | + assertTrue("Merged JSON didn't merge correctly!", mergedJsonObject.getString("Wibbly").equals("Bibbly")); | 
|  | 86 | + assertTrue("Merged JSON didn't merge correctly!", mergedJsonObject.getString("Replaced").equals("Yes")); | 
|  | 87 | + assertTrue("JSON Array didn't merge correctly!",  | 
|  | 88 | + mergedJsonObject.getJsonArray("Lexicon").getString(0).equals("Wibbles") | 
|  | 89 | + && mergedJsonObject.getJsonArray("Lexicon").getString(1).equals("Bibbles")); | 
|  | 90 | + assertTrue("Nested JSON didn't merge correctly!",  | 
|  | 91 | + mergedJsonObject.getJsonObject("Nested").getString("Birdie").equals("Wordie")); | 
|  | 92 | + assertTrue("Nested JSON Array didn't merge correctly!",  | 
|  | 93 | + mergedJsonObject.getJsonObject("Nested").getJsonArray("Bestiary").getString(0).equals("Slyzard") | 
|  | 94 | + && mergedJsonObject.getJsonObject("Nested").getJsonArray("Bestiary").getString(1).equals("Dragon")  | 
|  | 95 | + && mergedJsonObject.getJsonObject("Nested").getJsonArray("Bestiary").getString(2).equals("Ekimmara")); | 
|  | 96 | + } | 
|  | 97 | + | 
|  | 98 | + /** | 
|  | 99 | + * Test that the JSON Merge operation adds values as intended.  | 
|  | 100 | + */ | 
|  | 101 | + @Test | 
|  | 102 | + public void addTest() { | 
|  | 103 | + // Create a JSON object that we'll merge into the class variable, adding object members and array values | 
|  | 104 | + JsonObject jsonToMerge = Json.createObjectBuilder() | 
|  | 105 | + .add("Bibbly", "Bobbly") | 
|  | 106 | + .add("Lexicon", Json.createArrayBuilder() | 
|  | 107 | + .add("Wibbles") | 
|  | 108 | + .add("Wobbles") | 
|  | 109 | + .add("Bibbles") | 
|  | 110 | + .add("Bobbles") | 
|  | 111 | + .build()) | 
|  | 112 | + .build(); | 
|  | 113 | + | 
|  | 114 | + // Create a merge patch and apply it | 
|  | 115 | + JsonMergePatch mergePatch = Json.createMergePatch(jsonToMerge); | 
|  | 116 | + JsonValue mergedJson = mergePatch.apply(json); | 
|  | 117 | + | 
|  | 118 | + // Print out to more easily see what we've done | 
|  | 119 | + System.out.println("JsonpMergeTest.addTest: Before Merge: " + json); | 
|  | 120 | + System.out.println("JsonpMergeTest.addTest: JSON to Merge: " + jsonToMerge); | 
|  | 121 | + System.out.println("JsonpMergeTest.addTest: After Merge: " + mergedJson); | 
|  | 122 | + | 
|  | 123 | + // Test that everything is as it should be | 
|  | 124 | + JsonObject mergedJsonObject = mergedJson.asJsonObject(); | 
|  | 125 | + assertTrue("Merged JSON didn't merge correctly!", mergedJsonObject.getString("Wibbly").equals("Wobbly")); | 
|  | 126 | + assertTrue("Merged JSON didn't merge correctly!", mergedJsonObject.getString("Bibbly").equals("Bobbly")); | 
|  | 127 | + assertTrue("Merged JSON didn't merge correctly!", !mergedJsonObject.getBoolean("Replaced")); | 
|  | 128 | + assertTrue("JSON Array didn't merge correctly!",  | 
|  | 129 | + mergedJsonObject.getJsonArray("Lexicon").getString(0).equals("Wibbles") | 
|  | 130 | + && mergedJsonObject.getJsonArray("Lexicon").getString(1).equals("Wobbles") | 
|  | 131 | + && mergedJsonObject.getJsonArray("Lexicon").getString(2).equals("Bibbles") | 
|  | 132 | + && mergedJsonObject.getJsonArray("Lexicon").getString(3).equals("Bobbles")); | 
|  | 133 | + assertTrue("Nested JSON didn't merge correctly!",  | 
|  | 134 | + mergedJsonObject.getJsonObject("Nested").getString("Birdie").equals("Wordie")); | 
|  | 135 | + assertTrue("Nested JSON Array didn't merge correctly!",  | 
|  | 136 | + mergedJsonObject.getJsonObject("Nested").getJsonArray("Bestiary").getString(0).equals("Drowner") | 
|  | 137 | + && mergedJsonObject.getJsonObject("Nested").getJsonArray("Bestiary").getString(1).equals("Werewolf")  | 
|  | 138 | + && mergedJsonObject.getJsonObject("Nested").getJsonArray("Bestiary").getString(2).equals("Chimera")); | 
|  | 139 | + } | 
|  | 140 | + | 
|  | 141 | + /** | 
|  | 142 | + * Test that the JSON Merge operation removes values as intended.  | 
|  | 143 | + */ | 
|  | 144 | + @Test | 
|  | 145 | + public void removeTest() { | 
|  | 146 | + // Create a JSON object that we'll merge into the class variable, removing object members and array values | 
|  | 147 | + JsonObject jsonToMerge = Json.createObjectBuilder() | 
|  | 148 | + .addNull("Wibbly") | 
|  | 149 | + .add("Lexicon", Json.createArrayBuilder() | 
|  | 150 | + .add("Wibbles") | 
|  | 151 | + .build()) | 
|  | 152 | + .add("Nested", Json.createObjectBuilder() | 
|  | 153 | + .addNull("Bestiary") | 
|  | 154 | + .build()) | 
|  | 155 | + .build(); | 
|  | 156 | + | 
|  | 157 | + // Create a merge patch and apply it | 
|  | 158 | + JsonMergePatch mergePatch = Json.createMergePatch(jsonToMerge); | 
|  | 159 | + JsonValue mergedJson = mergePatch.apply(json); | 
|  | 160 | + | 
|  | 161 | + // Print out to more easily see what we've done | 
|  | 162 | + System.out.println("JsonpMergeTest.removeTest: Before Merge: " + json); | 
|  | 163 | + System.out.println("JsonpMergeTest.removeTest: JSON to Merge: " + jsonToMerge); | 
|  | 164 | + System.out.println("JsonpMergeTest.removeTest: After Merge: " + mergedJson); | 
|  | 165 | + | 
|  | 166 | + // Test that everything is as it should be | 
|  | 167 | + JsonObject mergedJsonObject = mergedJson.asJsonObject(); | 
|  | 168 | + assertTrue("Merged JSON didn't merge correctly!", !mergedJsonObject.containsKey("Wibbly")); | 
|  | 169 | + assertTrue("Merged JSON didn't merge correctly!", !mergedJsonObject.getBoolean("Replaced")); | 
|  | 170 | + assertTrue("JSON Array didn't merge correctly!",  | 
|  | 171 | + mergedJsonObject.getJsonArray("Lexicon").getString(0).equals("Wibbles")); | 
|  | 172 | + assertTrue("Nested JSON didn't merge correctly!",  | 
|  | 173 | + mergedJsonObject.getJsonObject("Nested").getString("Birdie").equals("Wordie")); | 
|  | 174 | + assertTrue("Nested JSON Array didn't merge correctly!",  | 
|  | 175 | + !mergedJsonObject.getJsonObject("Nested").containsKey("Bestiary")); | 
|  | 176 | + } | 
|  | 177 | +} | 
0 commit comments