/** * Apply this patch to a JSON value * * @param node the value to apply the patch to * @return the patched JSON value * @throws JsonPatchException failed to apply patch * @throws NullPointerException input is null */ public JsonNode apply(final JsonNode node) throws JsonPatchException { BUNDLE.checkNotNull(node, "jsonPatch.nullInput"); JsonNode ret = node; for (final JsonPatchOperation operation: operations) ret = operation.apply(ret); return ret; }
@Override public void serialize(final JsonGenerator jgen, final SerializerProvider provider) throws IOException { jgen.writeStartArray(); for (final JsonPatchOperation op: operations) op.serialize(jgen, provider); jgen.writeEndArray(); }
@Test(dataProvider = "getErrors") public final void errorsAreCorrectlyReported(final JsonNode patch, final JsonNode node, final String message) throws IOException { final JsonPatchOperation op = reader.readValue(patch); try { op.apply(node); fail("No exception thrown!!"); } catch (JsonPatchException e) { assertEquals(e.getMessage(), message); } }
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { if (from.equals(path)) return node.deepCopy(); final JsonNode movedNode = from.path(node); if (movedNode.isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); final JsonPatchOperation remove = new RemoveOperation(from); final JsonPatchOperation add = new AddOperation(path, movedNode); return add.apply(remove.apply(node)); } }
@Test public void operationsAreCalledInOrder() throws JsonPatchException { final JsonNode node1 = FACTORY.textNode("hello"); final JsonNode node2 = FACTORY.textNode("world"); when(op1.apply(node1)).thenReturn(node2); final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); final ArgumentCaptor<JsonNode> captor = ArgumentCaptor.forClass(JsonNode.class); patch.apply(node1); verify(op1, only()).apply(same(node1)); verify(op2, only()).apply(captor.capture()); assertSame(captor.getValue(), node2); }
@Test public void whenOneOperationFailsNextOperationIsNotCalled() throws JsonPatchException { final String message = "foo"; when(op1.apply(any(JsonNode.class))) .thenThrow(new JsonPatchException(message)); final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); try { patch.apply(FACTORY.nullNode()); fail("No exception thrown!!"); } catch (JsonPatchException e) { assertEquals(e.getMessage(), message); } verifyZeroInteractions(op2); } }
@Test(dataProvider = "getOps") public final void operationsYieldExpectedResults(final JsonNode patch, final JsonNode node, final JsonNode expected) throws IOException, JsonPatchException { final JsonPatchOperation op = reader.readValue(patch); final JsonNode actual = op.apply(node); assertTrue(EQUIVALENCE.equivalent(actual, expected), "patched node differs from expectations: expected " + expected + " but found " + actual); if (EQUIVALENCE.equivalent(node, actual) && node.isContainerNode()) assertNotSame(node, actual, "operation didn't make a copy of the input node"); } }
@Test public void removingRootReturnsMissingNode() throws JsonPatchException { final JsonNode node = JacksonUtils.nodeFactory().nullNode(); final JsonPatchOperation op = new RemoveOperation(JsonPointer.empty()); final JsonNode ret = op.apply(node); assertTrue(ret.isMissingNode()); } }