PatchProcessor { JsonNode processPatch(PatchSet patchSet) throws PatchApplicationException { String documentId = patchSet.getDocumentId(); List<PatchOperation> operations = patchSet.getOperations(); synchronized (store) { JsonNode document = store.getOrDefault(documentId, NullNode.getInstance()); JsonNode updatedDocument = patchApplier.apply(document, operations); if (updatedDocument.isMissingNode()) { log.info("Removing document {}", documentId); store.remove(documentId); } else { log.info("Updating document {}", documentId); log.info("New document : {}", updatedDocument); store.put(documentId, updatedDocument); } return updatedDocument; } }    }
@Test public void replaceExisting() throws PatchApplicationException { store.put(DOCUMENT_ID, jsonNode1); PatchSet patchSet = patchSet(REPLACE, "", singletonMap("foo", "baz")); JsonNode result = underTest.processPatch(patchSet); assertThat(result, is(jsonNode2)); assertThat(store.get(DOCUMENT_ID), is(jsonNode2)); }
@Test public void removeNonExistingRoot() throws PatchApplicationException { PatchSet patchSet = patchSet(REMOVE, "", null); JsonNode result = underTest.processPatch(patchSet); assertThat(result, is(MissingNode.getInstance())); assertThat(store.get(DOCUMENT_ID), is(nullValue())); }
@Test public void removeExistingRoot() throws PatchApplicationException { store.put(DOCUMENT_ID, jsonNode1); PatchSet patchSet = patchSet(REMOVE, "", null); JsonNode result = underTest.processPatch(patchSet); assertThat(result, is(MissingNode.getInstance())); assertThat(store.get(DOCUMENT_ID), is(nullValue())); }
@Test(expected = PatchApplicationException.class) public void removeChildFromNonExistingRoot() throws PatchApplicationException { PatchSet patchSet = patchSet(REMOVE, "/goo", null); underTest.processPatch(patchSet); }
@Test(expected = PatchApplicationException.class) public void removeChildFromExistingRoot() throws PatchApplicationException { store.put(DOCUMENT_ID, jsonNode1); PatchSet patchSet = patchSet(REMOVE, "/goo", null); underTest.processPatch(patchSet); }
@Test public void addNew() throws PatchApplicationException { PatchSet patchSet = patchSet(ADD, "", map1); JsonNode result = underTest.processPatch(patchSet); assertThat(result, is(jsonNode1)); assertThat(store.get(DOCUMENT_ID), is(jsonNode1)); }
@Test public void addExisting() throws PatchApplicationException { store.put(DOCUMENT_ID, jsonNode1); PatchSet patchSet = patchSet(ADD, "", map2); JsonNode result = underTest.processPatch(patchSet); assertThat(result, is(jsonNode2)); assertThat(store.get(DOCUMENT_ID), is(jsonNode2)); }
@Test public void replaceNew() throws PatchApplicationException { PatchSet patchSet = patchSet(REPLACE, "", map1); JsonNode result = underTest.processPatch(patchSet); assertThat(result, is(jsonNode1)); assertThat(store.get(DOCUMENT_ID), is(jsonNode1)); }