Skip to content

Commit 162465d

Browse files
committed
Added some tests for PatchBuilder replaceInsert
With the current situation of the b3_0 sources, it looks like there are no tests for the replaceInsert functionality of the patch builder. In a recent project I found out, that the behavior of the patch builder does differ if the node does exist (replace) vs. if does not yet exist (insert: will put the whole fragment instead of the selectPath). This behavior is for the application developer a bit inconvenient, since you have to check first wether the node does exist or not, and then to decide how you structure the fragment node in question. NOTE: Therefore this test case highlights the current problem and the second assertion (L. 357, "please make me not nested") will fail.
1 parent 4e00432 commit 162465d

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/test/java/com/marklogic/client/test/JSONDocumentTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,43 @@ public void testXPathPatch() throws IOException {
324324
docMgr.delete(docId);
325325
}
326326

327+
@Test
328+
public void testXPathReplaceInsertPatch() throws IOException {
329+
String docId = "/test/testXPathReplaceInsertPatch.json";
330+
ObjectMapper mapper = new ObjectMapper();
331+
ObjectNode sourceNode = makeContent(mapper);
332+
333+
JSONDocumentManager docMgr = Common.client.newJSONDocumentManager();
334+
docMgr.write(docId, new JacksonHandle().with(sourceNode));
335+
336+
// ~~ Prepare the patch
337+
DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder();
338+
patchBldr.replaceValue("/newKey", Cardinality.ZERO_OR_MORE, "a new value");
339+
340+
ObjectNode fragmentDidNotYetExists = mapper.createObjectNode();
341+
fragmentDidNotYetExists.put("somethingNew", "the bar for the foo");
342+
patchBldr.replaceInsertFragment("/foo", "/numberKey", Position.AFTER, fragmentDidNotYetExists);
343+
344+
ObjectNode fragmentDidAlreadyExist = mapper.createObjectNode();
345+
fragmentDidAlreadyExist.put("somethingReplaced", "please make me not nested");
346+
patchBldr.replaceInsertFragment("/stringKey", "/numberKey", Position.AFTER, fragmentDidAlreadyExist);
347+
348+
DocumentPatchHandle patchHandle = patchBldr.build();
349+
logger.debug("Sending patch: " + patchHandle.toString());
350+
docMgr.patch(docId, patchHandle);
351+
352+
// ~~ Assertions on the patched document
353+
JsonNode docNode = docMgr.read(docId, new JacksonHandle()).get();
354+
assertEquals("the bar for the foo", docNode.get("somethingNew").asText());
355+
// The following assertion will fail currently (work around would be to use a TextNode,
356+
// but then there would be different behaviour for replace vs. insert)
357+
assertEquals("please make me not nested", docNode.get("stringKey").asText());
358+
assertFalse(docNode.has("newKey"));
359+
360+
// ~~ Delete the patchwork
361+
docMgr.delete(docId);
362+
}
363+
327364
@Test
328365
public void testXPathJsonMetadata() throws IOException, XpathException, SAXException {
329366
String docId = "/test/testWrite1.json";

0 commit comments

Comments
 (0)