Repository: johnzon Updated Branches: refs/heads/JSONP-1.1 74cb90884 -> 0e4bd4888
JOHNZON-98 implemented JsonPatchBuilder Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/0e4bd488 Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/0e4bd488 Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/0e4bd488 Branch: refs/heads/JSONP-1.1 Commit: 0e4bd4888918bbf46db024bb62139c42de0d1bc0 Parents: 74cb908 Author: Reinhard Sandtner <[email protected]> Authored: Mon Nov 28 13:14:24 2016 +0100 Committer: Reinhard Sandtner <[email protected]> Committed: Mon Nov 28 13:14:24 2016 +0100 ---------------------------------------------------------------------- .../johnzon/core/JsonPatchBuilderImpl.java | 121 ++++- .../org/apache/johnzon/core/JsonPatchImpl.java | 125 +++++- .../apache/johnzon/core/JsonPointerImpl.java | 4 + .../johnzon/core/JsonPatchBuilderTest.java | 440 +++++++++++++++++++ .../org/apache/johnzon/core/JsonPatchTest.java | 130 +++--- 5 files changed, 724 insertions(+), 96 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/johnzon/blob/0e4bd488/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchBuilderImpl.java ---------------------------------------------------------------------- diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchBuilderImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchBuilderImpl.java index a6e7c8a..1dc5ae4 100644 --- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchBuilderImpl.java +++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchBuilderImpl.java @@ -22,110 +22,183 @@ import javax.json.JsonPatch; import javax.json.JsonPatchBuilder; import javax.json.JsonStructure; import javax.json.JsonValue; +import java.util.ArrayList; +import java.util.List; -public class JsonPatchBuilderImpl implements JsonPatchBuilder { - public JsonPatchBuilderImpl() { - super(); +class JsonPatchBuilderImpl implements JsonPatchBuilder { + + private final List<JsonPatchImpl.PatchValue> operations; + + + JsonPatchBuilderImpl() { + operations = new ArrayList<>(); } - public JsonPatchBuilderImpl(JsonArray initialData) { - super(); + JsonPatchBuilderImpl(JsonArray initialData) { + operations = new ArrayList<>(initialData.size()); + + for (JsonValue value : initialData) { + + JsonObject operation = (JsonObject) value; + + JsonPatchOperation op = JsonPatchOperation.valueOf(operation.getString("op").toUpperCase()); + String path = operation.getString("path"); + String from = operation.getString("from", null); + JsonValue jsonValue = operation.get("value"); + + operations.add(new JsonPatchImpl.PatchValue(op, + path, + from, + jsonValue)); + } } + //X TODO this should get simplified to only one method like JsonPatch @Override public JsonStructure apply(JsonStructure target) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return build().apply(target); } @Override public JsonObject apply(JsonObject target) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return build().apply(target); } @Override public JsonArray apply(JsonArray target) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return build().apply(target); } + @Override public JsonPatchBuilder add(String path, JsonValue value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return addOperation(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + path, + null, + value)); } @Override public JsonPatchBuilder add(String path, String value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return add(path, toJsonString(value)); } @Override public JsonPatchBuilder add(String path, int value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return add(path, toJsonNumber(value)); } @Override public JsonPatchBuilder add(String path, boolean value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return add(path, toJsonBoolean(value)); } + @Override public JsonPatchBuilder remove(String path) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return addOperation(new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, + path, + null, + null)); } + @Override public JsonPatchBuilder replace(String path, JsonValue value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return addOperation(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, + path, + null, + value)); } @Override public JsonPatchBuilder replace(String path, String value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return replace(path, toJsonString(value)); } @Override public JsonPatchBuilder replace(String path, int value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return replace(path, toJsonNumber(value)); } @Override public JsonPatchBuilder replace(String path, boolean value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return replace(path, toJsonBoolean(value)); } + @Override public JsonPatchBuilder move(String path, String from) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return addOperation(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, + path, + from, + null)); } + @Override public JsonPatchBuilder copy(String path, String from) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return addOperation(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, + path, + from, + null)); } + @Override public JsonPatchBuilder test(String path, JsonValue value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return addOperation(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, + path, + null, + value)); } @Override public JsonPatchBuilder test(String path, String value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return test(path, toJsonString(value)); } @Override public JsonPatchBuilder test(String path, int value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return test(path, toJsonNumber(value)); } @Override public JsonPatchBuilder test(String path, boolean value) { - throw new UnsupportedOperationException("JSON-P 1.1"); + return test(path, toJsonBoolean(value)); } + @Override public JsonPatch build() { - throw new UnsupportedOperationException("JSON-P 1.1"); + + // add operations to another list + // so we can clear and reuse the builder + JsonPatchImpl patch = new JsonPatchImpl(new ArrayList<>(operations)); + operations.clear(); + + return patch; + } + + + private JsonPatchBuilder addOperation(JsonPatchImpl.PatchValue operation) { + operations.add(operation); + return this; + } + + private static JsonValue toJsonBoolean(boolean value) { + return value ? JsonValue.TRUE : JsonValue.FALSE; + } + + private static JsonValue toJsonString(String value) { + return value == null ? JsonValue.NULL : new JsonStringImpl(value); + } + + private static JsonValue toJsonNumber(int value) { + return new JsonLongImpl(value); + } + } http://git-wip-us.apache.org/repos/asf/johnzon/blob/0e4bd488/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchImpl.java ---------------------------------------------------------------------- diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchImpl.java index 0aaedf9..813dba0 100644 --- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchImpl.java +++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPatchImpl.java @@ -20,11 +20,16 @@ package org.apache.johnzon.core; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import javax.json.Json; +import javax.json.JsonArray; +import javax.json.JsonArrayBuilder; import javax.json.JsonException; +import javax.json.JsonObject; +import javax.json.JsonObjectBuilder; import javax.json.JsonPatch; -import javax.json.JsonPointer; import javax.json.JsonStructure; import javax.json.JsonValue; @@ -37,6 +42,14 @@ class JsonPatchImpl implements JsonPatch { this.patches = Arrays.asList(patches); } + JsonPatchImpl(List<PatchValue> patches) { + if (patches == null) { + this.patches = Collections.emptyList(); + } else { + this.patches = Collections.unmodifiableList(patches); + } + } + @Override public <T extends JsonStructure> T apply(T target) { @@ -81,21 +94,119 @@ class JsonPatchImpl implements JsonPatch { } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + JsonPatchImpl jsonPatch = (JsonPatchImpl) o; + + return patches.equals(jsonPatch.patches); + } + + @Override + public int hashCode() { + return patches.hashCode(); + } + + + JsonArray toJsonArray() { + + JsonArrayBuilder builder = Json.createArrayBuilder(); + for (PatchValue patch : patches) { + builder.add(patch.toJson()); + } + + return builder.build(); + } + + static class PatchValue { private final JsonPatchOperation operation; - private final JsonPointer path; - private final JsonPointer from; + private final JsonPointerImpl path; + private final JsonPointerImpl from; private final JsonValue value; PatchValue(JsonPatchOperation operation, - JsonPointer path, - JsonPointer from, + String path, + String from, JsonValue value) { this.operation = operation; - this.path = path; - this.from = from; + this.path = new JsonPointerImpl(path); + + // ignore from if we do not need it + if (operation == JsonPatchOperation.MOVE || operation == JsonPatchOperation.COPY) { + this.from = new JsonPointerImpl(from); + } else { + this.from = null; + } + this.value = value; } + + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + PatchValue that = (PatchValue) o; + + if (operation != that.operation) { + return false; + } + if (!path.equals(that.path)) { + return false; + } + if (from != null ? !from.equals(that.from) : that.from != null) { + return false; + } + return value != null ? value.equals(that.value) : that.value == null; + } + + @Override + public int hashCode() { + int result = operation.hashCode(); + result = 31 * result + path.hashCode(); + result = 31 * result + (from != null ? from.hashCode() : 0); + result = 31 * result + (value != null ? value.hashCode() : 0); + return result; + } + + + @Override + public String toString() { + return "{" + + "op: " + operation + + ", path: " + path + + ", from: " + from + + ", value: " + value + + '}'; + } + + JsonObject toJson() { + JsonObjectBuilder builder = Json.createObjectBuilder() + .add("op", operation.name().toLowerCase()) + .add("path", path.getJsonPointer()); + + if (from != null) { + builder.add("from", from.getJsonPointer()); + } + + if (value != null) { + builder.add("value", value); + } + + return builder.build(); + } } } http://git-wip-us.apache.org/repos/asf/johnzon/blob/0e4bd488/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java ---------------------------------------------------------------------- diff --git a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java index 8057f95..0b01c3d 100644 --- a/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java +++ b/johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java @@ -289,6 +289,10 @@ public class JsonPointerImpl implements JsonPointer { return (JsonArray) remove(target, 1, referenceTokens.size() - 1); } + String getJsonPointer() { + return jsonPointer; + } + private void validateAdd(JsonValue target) { validateJsonPointer(target, referenceTokens.size() - 1); } http://git-wip-us.apache.org/repos/asf/johnzon/blob/0e4bd488/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchBuilderTest.java ---------------------------------------------------------------------- diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchBuilderTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchBuilderTest.java new file mode 100644 index 0000000..7fd2af0 --- /dev/null +++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchBuilderTest.java @@ -0,0 +1,440 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.johnzon.core; + +import org.junit.Test; + +import javax.json.Json; +import javax.json.JsonPatch; +import javax.json.JsonValue; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class JsonPatchBuilderTest { + + @Test + public void testPatchBuilderAddString() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/foo", + null, + new JsonStringImpl("bar"))); + + JsonPatch patch = new JsonPatchBuilderImpl().add("/foo", "bar") + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderAddStringNull() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/foo", + null, + JsonValue.NULL)); + + String nullString = null; + JsonPatch patch = new JsonPatchBuilderImpl().add("/foo", nullString) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderAddJsonObject() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/foo", + null, + Json.createObjectBuilder() + .add("bar", "qux") + .build())); + + JsonPatch patch = new JsonPatchBuilderImpl().add("/foo", Json.createObjectBuilder() + .add("bar", "qux") + .build()) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderAddJsonArray() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/path", + null, + Json.createArrayBuilder() + .add("test") + .build())); + + JsonPatch patch = new JsonPatchBuilderImpl().add("/path", Json.createArrayBuilder() + .add("test") + .build()) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderAddJsonValueNull() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/path", + null, + JsonValue.NULL)); + + JsonPatch patch = new JsonPatchBuilderImpl().add("/path", JsonValue.NULL) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderAddInt() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/foo", + null, + new JsonStringImpl("bar"))); + + JsonPatch patch = new JsonPatchBuilderImpl().add("/foo", "bar") + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderAddBoolean() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/path/true", + null, + JsonValue.TRUE), + new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/path/false", + null, + JsonValue.FALSE)); + + JsonPatch patch = new JsonPatchBuilderImpl().add("/path/true", true) + .add("/path/false", false) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test(expected = NullPointerException.class) + public void testPatchBuilderAddMissingPath() { + new JsonPatchBuilderImpl().add(null, 0); + } + + + @Test + public void testPatchBuilderRemove() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, + "/path/to/remove", + null, + null)); + + JsonPatch patch = new JsonPatchBuilderImpl().remove("/path/to/remove") + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test(expected = NullPointerException.class) + public void testPatchBuilderRemoveMissingPath() { + new JsonPatchBuilderImpl().remove(null); + } + + + @Test + public void testPatchBuilderReplaceString() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, + "/path/to/replace", + null, + new JsonStringImpl("new value"))); + + JsonPatch patch = new JsonPatchBuilderImpl().replace("/path/to/replace", "new value") + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderReplaceInt() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, + "/replace/me", + null, + new JsonLongImpl(42))); + + JsonPatch patch = new JsonPatchBuilderImpl().replace("/replace/me", 42) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderReplaceBoolean() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, + "/true/to/replace", + null, + JsonValue.FALSE), + new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, + "/false/to/replace", + null, + JsonValue.TRUE)); + + JsonPatch patch = new JsonPatchBuilderImpl().replace("/true/to/replace", false) + .replace("/false/to/replace", true) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderReplaceJsonObject() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, + "/replace/the/object", + null, + Json.createObjectBuilder() + .add("foo", "bar") + .build())); + + JsonPatch patch = new JsonPatchBuilderImpl().replace("/replace/the/object", Json.createObjectBuilder() + .add("foo", "bar") + .build()) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderReplaceJsonArray() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, + "/replace/my/array", + null, + Json.createArrayBuilder() + .add("test") + .build())); + + JsonPatch patch = new JsonPatchBuilderImpl().replace("/replace/my/array", Json.createArrayBuilder() + .add("test") + .build()) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test(expected = NullPointerException.class) + public void testPatchBuilderReplaceMissingPath() { + new JsonPatchBuilderImpl().replace(null, "ignored"); + } + + + @Test + public void testPatchBuilderMove() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, + "/move/to", + "/move/from", + null)); + + JsonPatch patch = new JsonPatchBuilderImpl().move("/move/to", "/move/from") + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test(expected = NullPointerException.class) + public void testPatchBuilderMoveMissingPath() { + new JsonPatchBuilderImpl().move(null, "/ignored"); + } + + @Test(expected = NullPointerException.class) + public void testPatchBuilderMoveMissingFrom() { + new JsonPatchBuilderImpl().move("/the/path", null); + } + + + @Test + public void testPatchBuilderCopy() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, + "/to", + "/from", + null)); + + JsonPatch patch = new JsonPatchBuilderImpl().copy("/to", "/from") + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test(expected = NullPointerException.class) + public void testPatchBuilderCopyMissingPath() { + new JsonPatchBuilderImpl().copy(null, "/ignored"); + } + + @Test(expected = NullPointerException.class) + public void testPatchBuilderCopyMissingFrom() { + new JsonPatchBuilderImpl().copy("/the/path", null); + } + + + @Test + public void testPatchBuilderTestString() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, + "/to/test", + null, + new JsonStringImpl("value"))); + + JsonPatch patch = new JsonPatchBuilderImpl().test("/to/test", "value") + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderTestBoolean() { + + JsonPatchImpl exptected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, + "/true/to/test", + null, + JsonValue.TRUE), + new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, + "/false/to/test", + null, + JsonValue.FALSE)); + + JsonPatch patch = new JsonPatchBuilderImpl().test("/true/to/test", true) + .test("/false/to/test", false) + .build(); + assertNotNull(patch); + assertEquals(exptected, patch); + } + + @Test + public void testPatchBuilderTestInt() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, + "/test/int", + null, + new JsonLongImpl(16))); + + JsonPatch patch = new JsonPatchBuilderImpl().test("/test/int", 16) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderTestJsonValue() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, + "/test/value", + null, + JsonValue.NULL)); + + JsonPatch patch = new JsonPatchBuilderImpl().test("/test/value", JsonValue.NULL) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderTestJsonObject() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, + "/test/the/object", + null, + Json.createObjectBuilder() + .add("foo", "bar") + .build())); + + JsonPatch patch = new JsonPatchBuilderImpl().test("/test/the/object", Json.createObjectBuilder() + .add("foo", "bar") + .build()) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test + public void testPatchBuilderTestJsonArray() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, + "/test/my/array", + null, + Json.createArrayBuilder() + .add("element") + .build())); + + JsonPatch patch = new JsonPatchBuilderImpl().test("/test/my/array", Json.createArrayBuilder() + .add("element") + .build()) + .build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + @Test(expected = NullPointerException.class) + public void testPatchBuilderTestMissingPath() { + new JsonPatchBuilderImpl().test(null, "ignored"); + } + + + @Test + public void testPatchBuilderWithinitialData() { + + JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, + "/add/an/object", + null, + Json.createObjectBuilder() + .add("name", "Cassius") + .build()), + new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, + "/replace/me", + null, + Json.createArrayBuilder() + .add(16) + .add(27) + .add("test") + .build()), + new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, + "/remove/it", + null, + null)); + + JsonPatch patch = new JsonPatchBuilderImpl(expected.toJsonArray()).build(); + assertNotNull(patch); + assertEquals(expected, patch); + } + + +} http://git-wip-us.apache.org/repos/asf/johnzon/blob/0e4bd488/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchTest.java ---------------------------------------------------------------------- diff --git a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchTest.java b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchTest.java index b4665ed..7e8ad67 100644 --- a/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchTest.java +++ b/johnzon-core/src/test/java/org/apache/johnzon/core/JsonPatchTest.java @@ -44,7 +44,7 @@ public class JsonPatchTest { .readObject(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/baz"), + "/baz", null, // no from new JsonStringImpl("qux"))); @@ -66,7 +66,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/foo/1"), + "/foo/1", null, // no from new JsonStringImpl("qux"))); @@ -92,7 +92,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/foo/-"), + "/foo/-", null, // no from new JsonStringImpl("qux"))); @@ -116,7 +116,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/-"), + "/-", null, // no from new JsonStringImpl("qux"))); @@ -138,7 +138,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/baz/bat"), + "/baz/bat", null, // no from new JsonStringImpl("qux"))); @@ -153,7 +153,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/5"), + "/5", null, new JsonStringImpl("baz"))); @@ -170,7 +170,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, - Json.createJsonPointer("/baz"), + "/baz", null, null)); @@ -193,7 +193,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, - Json.createJsonPointer("/foo/1"), + "/foo/1", null, null)); @@ -219,7 +219,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, - Json.createJsonPointer("/1"), + "/1", null, null)); @@ -241,7 +241,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, - Json.createJsonPointer("/nomatch"), + "/nomatch", null, null)); @@ -256,7 +256,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, - Json.createJsonPointer("/5"), + "/5", null, null)); @@ -273,7 +273,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, - Json.createJsonPointer("/baz"), + "/baz", null, new JsonStringImpl("boo"))); @@ -296,7 +296,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, - Json.createJsonPointer("/foo/1"), + "/foo/1", null, new JsonStringImpl("boo"))); @@ -323,7 +323,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, - Json.createJsonPointer("/0"), + "/0", null, new JsonStringImpl("boo"))); @@ -345,7 +345,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, - Json.createJsonPointer("/nomatch"), + "/nomatch", null, new JsonStringImpl("notneeded"))); @@ -360,7 +360,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.REPLACE, - Json.createJsonPointer("/1"), + "/1", null, new JsonStringImpl("notneeded"))); @@ -380,8 +380,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, - Json.createJsonPointer("/qux/thud"), - Json.createJsonPointer("/foo/waldo"), + "/qux/thud", + "/foo/waldo", null)); JsonObject patched = patch.apply(object); @@ -413,8 +413,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, - Json.createJsonPointer("/foo/3"), - Json.createJsonPointer("/foo/1"), + "/foo/3", + "/foo/1", null)); JsonObject patched = patch.apply(object); @@ -442,8 +442,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, - Json.createJsonPointer("/0"), - Json.createJsonPointer("/3"), + "/0", + "/3", null)); JsonArray patched = patch.apply(array); @@ -466,8 +466,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, - Json.createJsonPointer("/bar"), - Json.createJsonPointer("/foo/2"), + "/bar", + "/foo/2", null)); JsonObject patched = patch.apply(object); @@ -492,8 +492,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, - Json.createJsonPointer("/baz"), - Json.createJsonPointer("/nomatch"), + "/baz", + "/nomatch", null)); patch.apply(object); @@ -508,8 +508,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, - Json.createJsonPointer("/nomatch/child"), - Json.createJsonPointer("/foo"), + "/nomatch/child", + "/foo", null)); patch.apply(object); @@ -524,8 +524,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, - Json.createJsonPointer("/object/key"), - Json.createJsonPointer("/object"), + "/object/key", + "/object", null)); patch.apply(object); @@ -540,8 +540,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/baz"), - Json.createJsonPointer("/foo"), + "/baz", + "/foo", null)); JsonObject patched = patch.apply(object); @@ -563,8 +563,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/foo/-"), - Json.createJsonPointer("/foo/0"), + "/foo/-", + "/foo/0", null)); JsonObject patched = patch.apply(object); @@ -589,8 +589,8 @@ public class JsonPatchTest { JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/0"), - Json.createJsonPointer("/1"), + "/0", + "/1", null)); JsonArray patched = patch.apply(array); @@ -615,8 +615,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/partner/partner/name"), - Json.createJsonPointer("/name"), + "/partner/partner/name", + "/name", null)); JsonObject patched = patch.apply(object); @@ -641,8 +641,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/notneeded"), - Json.createJsonPointer("/nomatch"), + "/notneeded", + "/nomatch", null)); patch.apply(object); @@ -656,8 +656,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/path/nomatch"), - Json.createJsonPointer("/foo"), + "/path/nomatch", + "/foo", null)); patch.apply(object); @@ -672,8 +672,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/-"), - Json.createJsonPointer("/2"), + "/-", + "/2", null)); patch.apply(array); @@ -687,8 +687,8 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/1"), - Json.createJsonPointer("/-"), + "/1", + "/-", null)); patch.apply(array); @@ -703,7 +703,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/foo"), + "/foo", null, new JsonStringImpl("qux"))); @@ -720,7 +720,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/foo"), + "/foo", null, Json.createArrayBuilder().build())); @@ -738,7 +738,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/parents"), + "/parents", null, Json.createArrayBuilder() // yessss, we really want to create a new JsonArray ;) .add("Odin") @@ -761,7 +761,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/numbers"), + "/numbers", null, Json.createArrayBuilder() // different ordering .add(2) @@ -781,7 +781,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/foo/1"), + "/foo/1", null, new JsonStringImpl("baz"))); @@ -800,7 +800,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/2"), + "/2", null, new JsonStringImpl("qux"))); @@ -819,7 +819,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/0"), + "/0", null, new JsonStringImpl("bar"))); @@ -830,7 +830,7 @@ public class JsonPatchTest { public void testTestingObjectMemeberNonexistentTarget() { JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/nomatch"), + "/nomatch", null, JsonValue.EMPTY_JSON_OBJECT)); @@ -841,7 +841,7 @@ public class JsonPatchTest { public void testTestingArrayElementIndexOutOfBounds() { JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.TEST, - Json.createJsonPointer("/3"), + "/3", null, JsonValue.EMPTY_JSON_OBJECT)); @@ -858,7 +858,7 @@ public class JsonPatchTest { .build(); JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/foo"), + "/foo", null, new JsonStringImpl("abcd"))); @@ -875,7 +875,7 @@ public class JsonPatchTest { public void testAddArrayElementToEmptyArray() { JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/-"), + "/-", null, new JsonStringImpl("foo"))); @@ -896,33 +896,33 @@ public class JsonPatchTest { // i know this can be done with PatchBuilder but // currently it's not implemented and its fun ;) JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/family/father"), + "/family/father", null, Json.createObjectBuilder() .add("name", "Gaio Modry Effect") .build()), new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/family/mother"), + "/family/mother", null, Json.createObjectBuilder() .add("name", "Cassius vom Hause Clarabella") .build()), new JsonPatchImpl.PatchValue(JsonPatchOperation.MOVE, - Json.createJsonPointer("/family/children/0"), - Json.createJsonPointer("/family/mother"), + "/family/children/0", + "/family/mother", null), new JsonPatchImpl.PatchValue(JsonPatchOperation.ADD, - Json.createJsonPointer("/family/mother"), + "/family/mother", null, Json.createObjectBuilder() .add("name", "Aimee vom Hause Clarabella") .build()), new JsonPatchImpl.PatchValue(JsonPatchOperation.COPY, - Json.createJsonPointer("/pedigree"), - Json.createJsonPointer("/family"), + "/pedigree", + "/family", null), new JsonPatchImpl.PatchValue(JsonPatchOperation.REMOVE, - Json.createJsonPointer("/family"), + "/family", null, null));
