Commit 67bd2ad5 by Juho Juopperi

also fix the code that failed the test before

1 parent c3a40db9
......@@ -70,12 +70,13 @@ public class JsonUtils {
* The value associate to the key
* @return JsonObject with the key:value pair added
*/
public static JsonObject assocJsonObject(JsonObject jsonObject, String key, JsonValue value) {
public static JsonObject assocJsonObject(JsonObject jsonObject, String key,
JsonValue value) {
JsonObjectBuilder builder = Json.createObjectBuilder();
// Copy all non conflicting json entries
for (Map.Entry<String, JsonValue> v : jsonObject.entrySet()) {
if (v.getKey().equals(key)) {
if (v.getKey().equals(key) == false) {
builder = builder.add(v.getKey(), v.getValue());
}
}
......@@ -102,7 +103,7 @@ public class JsonUtils {
// Recurse?
if (keys.size() > 1) {
String firstKey = keys.get(0);
List<String> restKeys = keys.subList(0, keys.size());
List<String> restKeys = keys.subList(1, keys.size());
JsonObject subObj = jsonObject.getJsonObject(firstKey);
return assocJsonObject(jsonObject, firstKey,
assocInJsonObject(subObj, restKeys, value));
......
......@@ -64,7 +64,21 @@ public class JsonUtilsTest {
JsonObject actual = JsonUtils.alterSubObject(meta, path, newData);
JsonObject expected = jsonObject("{\"foo\":\"bar\",\"baz\":{\"quux\":\"plop\"}}");
Assert.assertEquals(expected.toString(), actual.toString());
Assert.assertEquals(actual.toString(), expected.toString());
}
@Test
public final void testAlterSubSubObject() {
JsonObject meta = jsonObject("{\"bystander1\":\"canary1\", \"module1\":{\"bystander2\":\"canary2\",\"submodule1\":{\"replacee1\":\"replacedvalue1\",\"r2\":\"rv2\"}}}");
JsonObject newData = jsonObject("{\"quux\":\"plop\"}");
ArrayList<String> path = new ArrayList<String>();
path.add("module1");
path.add("submodule1");
JsonObject actual = JsonUtils.alterSubObject(meta, path, newData);
JsonObject expected = jsonObject("{\"bystander1\":\"canary1\", \"module1\":{\"bystander2\":\"canary2\",\"submodule1\":{\"quux\":\"plop\"}}}");
Assert.assertEquals(actual.toString(), expected.toString());
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!