Skip to content

Commit 6115108

Browse files
#871 - Corrected the serialization and added unit tests for the same
1 parent a867a50 commit 6115108

File tree

5 files changed

+83
-10
lines changed

5 files changed

+83
-10
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/impl/DocumentMetadataPatchBuilderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ public void writeCall(JSONStringWriter serializer, CallImpl call) {
252252
} else {
253253
serializer.writeStartArray();
254254
for (Object value: call.args) {
255-
serializer.writeStartObject();
256-
serializer.writeStartEntry("value");
255+
serializer.writeStartObjectInLoop();
256+
serializer.writeStartEntry("$value");
257257
serializer.writeStringValue(value);
258258
serializer.writeEndObject();
259259
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/JSONStringWriter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public void writeStartObject() {
3838
builder.append("{");
3939
isFirst = true;
4040
}
41+
public void writeStartObjectInLoop() {
42+
if (isFirst)
43+
isFirst = false;
44+
else
45+
builder.append(", ");
46+
writeStartObject();
47+
}
4148
public void writeStartEntry(String key) {
4249
if (isFirst)
4350
isFirst = false;

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.Reader;
2525
import java.nio.charset.Charset;
2626

27+
import com.marklogic.client.admin.ExtensionLibrariesManager;
2728
import org.custommonkey.xmlunit.exceptions.XpathException;
2829
import org.junit.AfterClass;
2930
import org.junit.BeforeClass;
@@ -53,6 +54,7 @@
5354

5455
public class JSONDocumentTest {
5556

57+
private static ExtensionLibrariesManager libsMgr = null;
5658
final static String metadata =
5759
" {\"collections\":\n" +
5860
" [\"collection1\",\n" +
@@ -73,11 +75,20 @@ public class JSONDocumentTest {
7375

7476
@BeforeClass
7577
public static void beforeClass() {
78+
Common.connectAdmin();
79+
// get a manager
80+
libsMgr = Common.adminClient
81+
.newServerConfigManager().newExtensionLibrariesManager();
82+
83+
// write XQuery file to the modules database
84+
libsMgr.write("/ext/my-lib.xqy", new FileHandle(
85+
new File("src/test/resources/my-lib.xqy")).withFormat(Format.TEXT));
7686
Common.connect();
7787
}
7888

7989
@AfterClass
8090
public static void afterClass() {
91+
libsMgr.delete("/ext/my-lib.xqy");
8192
}
8293

8394
@Test
@@ -124,6 +135,7 @@ public void testJsonPathPatch() throws IOException {
124135
String docId = "/test/testWrite1.json";
125136
ObjectMapper mapper = new ObjectMapper();
126137
ObjectNode sourceNode = makeContent(mapper);
138+
sourceNode.put("numberKey3" , 31);
127139
String content = mapper.writeValueAsString(sourceNode);
128140
JSONDocumentManager docMgr = Common.client.newJSONDocumentManager();
129141
docMgr.write(docId, new StringHandle().with(content));
@@ -156,6 +168,10 @@ public void testJsonPathPatch() throws IOException {
156168
patchBldr.replaceValue("$.booleanKey", true);
157169
patchBldr.replaceValue("$.numberKey2", 2);
158170
patchBldr.replaceValue("$.nullKey", null);
171+
patchBldr.library("http://marklogic.com/java-unit-test/my-lib",
172+
"/ext/my-lib.xqy");
173+
patchBldr.replaceApply("$.numberKey3",
174+
patchBldr.call().applyLibraryValues("getMin", 18, 21));
159175

160176
DocumentPatchHandle patchHandle = patchBldr.pathLanguage(
161177
PathLanguage.JSONPATH).build();
@@ -185,7 +201,7 @@ public void testJsonPathPatch() throws IOException {
185201
expectedNode.put("booleanKey", true);
186202
expectedNode.put("numberKey2", 2);
187203
expectedNode.putNull("nullKey");
188-
204+
expectedNode.put("numberKey3" , 18);
189205

190206
String docText = docMgr.read(docId, new StringHandle()).get();
191207
assertNotNull("Read null string for patched JSON content", docText);
@@ -268,6 +284,7 @@ public void testXPathPatch() throws IOException {
268284
String docId = "/test/testWrite1.json";
269285
ObjectMapper mapper = new ObjectMapper();
270286
ObjectNode sourceNode = makeContent(mapper);
287+
sourceNode.put("numberKey3", 31);
271288
String content = mapper.writeValueAsString(sourceNode);
272289

273290
JSONDocumentManager docMgr = Common.client.newJSONDocumentManager();
@@ -292,7 +309,10 @@ public void testXPathPatch() throws IOException {
292309
patchBldr.replaceValue("/booleanKey", true);
293310
patchBldr.replaceValue("/numberKey2", 2);
294311
patchBldr.replaceValue("/nullKey", null);
295-
312+
patchBldr.library("http://marklogic.com/java-unit-test/my-lib",
313+
"/ext/my-lib.xqy");
314+
patchBldr.replaceApply("/numberKey3",
315+
patchBldr.call().applyLibraryValues("getMin", 18, 21));
296316
//patchBldr.replaceApply("/node()/arrayKey/node()[string(.) eq '3']",
297317
// patchBldr.call().add(2));
298318

@@ -329,6 +349,7 @@ public void testXPathPatch() throws IOException {
329349
expectedNode.put("booleanKey", true);
330350
expectedNode.put("numberKey2", 2);
331351
expectedNode.putNull("nullKey");
352+
expectedNode.put("numberKey3", 18);
332353

333354
String docText = docMgr.read(docId, new StringHandle()).get();
334355

marklogic-client-api/src/test/java/com/marklogic/client/test/XMLDocumentTest.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertNotNull;
2020
import static org.junit.Assert.assertTrue;
2121

22+
import java.io.File;
2223
import java.io.IOException;
2324
import java.io.StringReader;
2425
import java.util.HashMap;
@@ -40,6 +41,8 @@
4041
import javax.xml.validation.Schema;
4142
import javax.xml.validation.SchemaFactory;
4243

44+
import com.marklogic.client.admin.ExtensionLibrariesManager;
45+
import com.marklogic.client.io.*;
4346
import org.junit.AfterClass;
4447
import org.junit.BeforeClass;
4548
import org.junit.Test;
@@ -57,23 +60,29 @@
5760
import com.marklogic.client.document.XMLDocumentManager;
5861
import com.marklogic.client.document.DocumentPatchBuilder.Position;
5962
import com.marklogic.client.document.XMLDocumentManager.DocumentRepair;
60-
import com.marklogic.client.io.DOMHandle;
61-
import com.marklogic.client.io.InputSourceHandle;
62-
import com.marklogic.client.io.SourceHandle;
63-
import com.marklogic.client.io.StringHandle;
64-
import com.marklogic.client.io.XMLEventReaderHandle;
65-
import com.marklogic.client.io.XMLStreamReaderHandle;
6663
import com.marklogic.client.io.marker.DocumentPatchHandle;
6764
import com.marklogic.client.util.EditableNamespaceContext;
6865
import java.util.Map;
6966

7067
public class XMLDocumentTest {
68+
69+
private static ExtensionLibrariesManager libsMgr = null;
70+
7171
@BeforeClass
7272
public static void beforeClass() {
73+
Common.connectAdmin();
74+
// get a manager
75+
libsMgr = Common.adminClient
76+
.newServerConfigManager().newExtensionLibrariesManager();
77+
78+
// write XQuery file to the modules database
79+
libsMgr.write("/ext/my-lib.xqy", new FileHandle(
80+
new File("src/test/resources/my-lib.xqy")).withFormat(Format.TEXT));
7381
Common.connect();
7482
}
7583
@AfterClass
7684
public static void afterClass() {
85+
libsMgr.delete("/ext/my-lib.xqy");
7786
}
7887

7988
@SuppressWarnings("unchecked")
@@ -333,6 +342,11 @@ public void testPatch() throws Exception {
333342
patchBldr.delete("fourthChild");
334343
patchBldr.replaceApply("fifthChild", Cardinality.ONE, patchBldr.call().multiply(3));
335344

345+
patchBldr.library("http://marklogic.com/java-unit-test/my-lib",
346+
"/ext/my-lib.xqy");
347+
patchBldr.replaceApply("/root/sixthChild",
348+
patchBldr.call().applyLibraryValues("getMin", 18, 21));
349+
336350
DocumentPatchHandle patchHandle = patchBldr.build();
337351

338352
for (int i=0; i < 2; i++) {
@@ -352,6 +366,9 @@ public void testPatch() throws Exception {
352366
Element fifthChild = domDocument.createElement("fifthChild");
353367
fifthChild.setTextContent("5");
354368
root.appendChild(fifthChild);
369+
Element sixthChild = domDocument.createElement("sixthChild");
370+
sixthChild.setTextContent("31");
371+
root.appendChild(sixthChild);
355372
domDocument.appendChild(root);
356373

357374
docMgr.write(docId, new DOMHandle().with(domDocument));
@@ -379,6 +396,7 @@ public void testPatch() throws Exception {
379396
root.replaceChild(domDocument.createElement("replacedSecondChild"), secondChild);
380397
thirdChild.setTextContent("new value");
381398
fifthChild.setTextContent("15");
399+
sixthChild.setTextContent("18");
382400
root.removeChild(fourthChild);
383401

384402
String expected = Common.testDocumentToString(domDocument);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
xquery version "1.0-ml";
2+
3+
module namespace my-lib = "http://marklogic.com/java-unit-test/my-lib";
4+
5+
(: Find the minimum value in a sequence of value composed of :)
6+
(: the current node and a set of input values. :)
7+
declare function my-lib:getMin(
8+
$current as node()?,
9+
$args as item()*
10+
) as node()*
11+
{
12+
if ($current/data() castable as xs:decimal)
13+
then
14+
let $new-value := fn:min(($current, $args))
15+
return
16+
typeswitch($current)
17+
case element() (: XML :)
18+
return element {fn:node-name($current)} {$new-value}
19+
case number-node() (: JSON :)
20+
return number-node {$new-value}
21+
default return fn:error((), "RESTAPI-SRVEXERR",
22+
("400", "Bad Request",
23+
fn:concat("Not an element or number node: ",
24+
xdmp:path($current))))
25+
else fn:error((), "RESTAPI-SRVEXERR", ("400", "Bad Request",
26+
fn:concat("Non-decimal data: ", $current)))
27+
};

0 commit comments

Comments
 (0)