|
| 1 | +/* |
| 2 | + * Copyright 2012-2014 MarkLogic Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.marklogic.client.test; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertNotNull; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | + |
| 21 | +import org.junit.AfterClass; |
| 22 | +import org.junit.BeforeClass; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 28 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 29 | +import com.marklogic.client.document.JSONDocumentManager; |
| 30 | +import com.marklogic.client.io.JacksonHandle; |
| 31 | +import com.marklogic.client.test.Common; |
| 32 | + |
| 33 | +public class JacksonHandleTest { |
| 34 | + @BeforeClass |
| 35 | + public static void beforeClass() { |
| 36 | + Common.connect(); |
| 37 | + } |
| 38 | + @AfterClass |
| 39 | + public static void afterClass() { |
| 40 | + Common.release(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testReadWrite() { |
| 45 | + // create an identifier for the database document |
| 46 | + String docId = "/example/jackson-test.json"; |
| 47 | + |
| 48 | + // create a manager for JSON documents |
| 49 | + JSONDocumentManager docMgr = Common.client.newJSONDocumentManager(); |
| 50 | + |
| 51 | + // construct a Jackson JSON structure |
| 52 | + ObjectMapper mapper = new ObjectMapper(); |
| 53 | + ObjectNode childObj = mapper.createObjectNode(); |
| 54 | + childObj.put("key", "value"); |
| 55 | + ArrayNode childArray = mapper.createArrayNode(); |
| 56 | + childArray.add("item1"); |
| 57 | + childArray.add("item2"); |
| 58 | + ObjectNode writeRoot = mapper.createObjectNode(); |
| 59 | + writeRoot.put("object", childObj); |
| 60 | + writeRoot.put("array", childArray); |
| 61 | + |
| 62 | + // create a handle for the JSON structure |
| 63 | + JacksonHandle writeHandle = new JacksonHandle(writeRoot); |
| 64 | + |
| 65 | + // write the document to the database |
| 66 | + docMgr.write(docId, writeHandle); |
| 67 | + |
| 68 | + // create a handle to receive the database content as a Jackson structure |
| 69 | + JacksonHandle readHandle = new JacksonHandle(); |
| 70 | + |
| 71 | + // read the document content from the database as a Jackson structure |
| 72 | + docMgr.read(docId, readHandle); |
| 73 | + |
| 74 | + // access the document content |
| 75 | + JsonNode readRoot = readHandle.get(); |
| 76 | + assertNotNull("Wrote null Jackson JSON structure", readRoot); |
| 77 | + assertTrue("Jackson JSON structures not equal", |
| 78 | + readRoot.equals(writeRoot)); |
| 79 | + |
| 80 | + // delete the document |
| 81 | + docMgr.delete(docId); |
| 82 | + } |
| 83 | +} |
0 commit comments