|
| 1 | +/* |
| 2 | + * Copyright (C) 2015 ArangoDB GmbH |
| 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 | + |
| 17 | +package com.arangodb.example.document; |
| 18 | + |
| 19 | +import org.json.JSONML; |
| 20 | +import org.json.JSONObject; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import com.arangodb.ArangoDriver; |
| 26 | +import com.arangodb.ArangoException; |
| 27 | +import com.arangodb.entity.DocumentEntity; |
| 28 | + |
| 29 | +public class RawDocumentExample extends BaseExample { |
| 30 | + |
| 31 | + private static final String DATABASE_NAME = "RawDocument"; |
| 32 | + |
| 33 | + private static final String COLLECTION_NAME = "RawDocument"; |
| 34 | + |
| 35 | + public ArangoDriver arangoDriver; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void _before() { |
| 39 | + removeTestDatabase(DATABASE_NAME); |
| 40 | + |
| 41 | + arangoDriver = getArangoDriver(getConfiguration()); |
| 42 | + createDatabase(arangoDriver, DATABASE_NAME); |
| 43 | + createCollection(arangoDriver, COLLECTION_NAME); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void ReadDocuments() { |
| 48 | + // |
| 49 | + // You can find the ArangoDB Web interface here: |
| 50 | + // http://127.0.0.1:8529/ |
| 51 | + // |
| 52 | + // change the log level to "debug" in /src/test/resource/logback.xml to |
| 53 | + // see the HTTP communication |
| 54 | + |
| 55 | + // |
| 56 | + printHeadline("create example document 1"); |
| 57 | + // |
| 58 | + |
| 59 | + String documentHandle1 = null; |
| 60 | + String documentHandle2 = null; |
| 61 | + String documentHandle3 = null; |
| 62 | + |
| 63 | + String x = "{\"test\":123}"; |
| 64 | + try { |
| 65 | + DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, x, true, false); |
| 66 | + // the DocumentEntity contains the key, document handle and revision |
| 67 | + System.out.println("Key: " + entity.getDocumentKey()); |
| 68 | + System.out.println("Id: " + entity.getDocumentHandle()); |
| 69 | + System.out.println("Revision: " + entity.getDocumentRevision()); |
| 70 | + documentHandle1 = entity.getDocumentHandle(); |
| 71 | + } catch (ArangoException e) { |
| 72 | + Assert.fail("Failed to create document. " + e.getMessage()); |
| 73 | + } |
| 74 | + |
| 75 | + // |
| 76 | + printHeadline("read example document 1"); |
| 77 | + // |
| 78 | + |
| 79 | + try { |
| 80 | + String str = arangoDriver.getDocumentRaw(documentHandle1, null, null); |
| 81 | + System.out.println("value: " + str); |
| 82 | + } catch (ArangoException e) { |
| 83 | + Assert.fail("Failed to read document. " + e.getMessage()); |
| 84 | + } |
| 85 | + |
| 86 | + // |
| 87 | + printHeadline("create example document 2 with key"); |
| 88 | + // |
| 89 | + |
| 90 | + x = "{\"_key\":\"key2\",\"test\":123}"; |
| 91 | + try { |
| 92 | + DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, x, true, false); |
| 93 | + // the DocumentEntity contains the key, document handle and revision |
| 94 | + System.out.println("Key: " + entity.getDocumentKey()); |
| 95 | + System.out.println("Id: " + entity.getDocumentHandle()); |
| 96 | + System.out.println("Revision: " + entity.getDocumentRevision()); |
| 97 | + documentHandle2 = entity.getDocumentHandle(); |
| 98 | + } catch (ArangoException e) { |
| 99 | + Assert.fail("Failed to create document. " + e.getMessage()); |
| 100 | + } |
| 101 | + |
| 102 | + // |
| 103 | + printHeadline("read example document 2"); |
| 104 | + // |
| 105 | + |
| 106 | + try { |
| 107 | + String str = arangoDriver.getDocumentRaw(documentHandle2, null, null); |
| 108 | + System.out.println("value: " + str); |
| 109 | + } catch (ArangoException e) { |
| 110 | + Assert.fail("Failed to read document. " + e.getMessage()); |
| 111 | + } |
| 112 | + |
| 113 | + // |
| 114 | + printHeadline("using org.json.JSONML to save a xml file"); |
| 115 | + // |
| 116 | + String string = "<recipe name=\"bread\" prep_time=\"5 mins\" cook_time=\"3 hours\"> <title>Basic bread</title> <ingredient amount=\"8\" unit=\"dL\">Flour</ingredient> <ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient> <ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient> <ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient> <instructions> <step>Mix all ingredients together.</step> <step>Knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Knead again.</step> <step>Place in a bread baking tin.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Bake in the oven at 180(degrees)C for 30 minutes.</step> </instructions> </recipe> "; |
| 117 | + System.out.println("Orig XML value: " + string); |
| 118 | + JSONObject jsonObject = JSONML.toJSONObject(string); |
| 119 | + try { |
| 120 | + DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, jsonObject.toString(), true, |
| 121 | + false); |
| 122 | + // the DocumentEntity contains the key, document handle and revision |
| 123 | + System.out.println("Key: " + entity.getDocumentKey()); |
| 124 | + System.out.println("Id: " + entity.getDocumentHandle()); |
| 125 | + System.out.println("Revision: " + entity.getDocumentRevision()); |
| 126 | + documentHandle3 = entity.getDocumentHandle(); |
| 127 | + } catch (ArangoException e) { |
| 128 | + Assert.fail("Failed to create document. " + e.getMessage()); |
| 129 | + } |
| 130 | + |
| 131 | + // |
| 132 | + printHeadline("read example and convert it back to XML"); |
| 133 | + // |
| 134 | + |
| 135 | + try { |
| 136 | + String str = arangoDriver.getDocumentRaw(documentHandle3, null, null); |
| 137 | + System.out.println("JSON value: " + str); |
| 138 | + JSONObject jsonObject2 = new JSONObject(str); |
| 139 | + System.out.println("XML value: " + JSONML.toString(jsonObject2)); |
| 140 | + } catch (ArangoException e) { |
| 141 | + Assert.fail("Failed to read document. " + e.getMessage()); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments