JsonPatch getPatch() { final List<JsonPatchOperation> list = Lists.newArrayList(); for (final DiffOperation op: diffs) list.add(op.asJsonPatchOperation()); return new JsonPatch(list); }
@Test public void cannotPatchNull() throws JsonPatchException { final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); try { patch.apply(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage( "jsonPatch.nullInput")); } }
@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 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); }