|
1 | 1 | package com.marklogic.javaclient; |
2 | 2 |
|
3 | | -import static org.junit.Assert.*; |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
4 | 5 |
|
5 | 6 | import java.io.File; |
6 | 7 | import java.io.IOException; |
7 | 8 |
|
| 9 | +import org.json.JSONException; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.AfterClass; |
| 12 | +import org.junit.BeforeClass; |
| 13 | +import org.junit.Test; |
| 14 | +import org.skyscreamer.jsonassert.JSONAssert; |
| 15 | + |
8 | 16 | import com.fasterxml.jackson.databind.ObjectMapper; |
9 | 17 | import com.fasterxml.jackson.databind.node.ObjectNode; |
10 | 18 | import com.marklogic.client.DatabaseClient; |
11 | 19 | import com.marklogic.client.DatabaseClientFactory; |
12 | | -import com.marklogic.client.Transaction; |
13 | 20 | import com.marklogic.client.DatabaseClientFactory.Authentication; |
| 21 | +import com.marklogic.client.Transaction; |
14 | 22 | import com.marklogic.client.admin.ExtensionLibrariesManager; |
15 | 23 | import com.marklogic.client.document.DocumentDescriptor; |
16 | 24 | import com.marklogic.client.document.DocumentMetadataPatchBuilder; |
| 25 | +import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; |
17 | 26 | import com.marklogic.client.document.DocumentPatchBuilder; |
18 | 27 | import com.marklogic.client.document.DocumentPatchBuilder.PathLanguage; |
| 28 | +import com.marklogic.client.document.DocumentPatchBuilder.Position; |
19 | 29 | import com.marklogic.client.document.DocumentUriTemplate; |
20 | 30 | import com.marklogic.client.document.JSONDocumentManager; |
21 | 31 | import com.marklogic.client.document.XMLDocumentManager; |
22 | | -import com.marklogic.client.document.DocumentMetadataPatchBuilder.Cardinality; |
23 | | -import com.marklogic.client.document.DocumentPatchBuilder.Position; |
| 32 | +import com.marklogic.client.io.DocumentMetadataHandle; |
| 33 | +import com.marklogic.client.io.DocumentMetadataHandle.Capability; |
| 34 | +import com.marklogic.client.io.DocumentMetadataHandle.DocumentCollections; |
24 | 35 | import com.marklogic.client.io.FileHandle; |
25 | 36 | import com.marklogic.client.io.Format; |
26 | 37 | import com.marklogic.client.io.StringHandle; |
27 | | -import com.marklogic.client.io.DocumentMetadataHandle.Capability; |
28 | 38 | import com.marklogic.client.io.marker.DocumentPatchHandle; |
29 | 39 |
|
30 | | -import org.json.JSONException; |
31 | | -import org.junit.*; |
32 | | -import org.skyscreamer.jsonassert.JSONAssert; |
33 | | - |
34 | 40 | public class TestPartialUpdate extends BasicJavaClientREST { |
35 | 41 | private static String dbName = "TestPartialUpdateDB"; |
36 | 42 | private static String [] fNames = {"TestPartialUpdateDB-1"}; |
@@ -1073,6 +1079,104 @@ public void testPartialUpdateDeleteJSON() throws IOException, JSONException |
1073 | 1079 | client.release(); |
1074 | 1080 | } |
1075 | 1081 |
|
| 1082 | + /* Purpose: This test is used to validate Git issue 132. |
| 1083 | + * Apply a patch to existing collections or permissions on a document using JSONPath expressions. |
| 1084 | + * |
| 1085 | + * Functions tested : replaceInsertFragment. An new fragment is inserted when unknown index is used. |
| 1086 | + */ |
| 1087 | + @Test |
| 1088 | + public void testMetaDataUpdateJSON() throws IOException, JSONException |
| 1089 | + { |
| 1090 | + System.out.println("Running testPartialUpdateReplaceInsertFragmentExistingJSON"); |
| 1091 | + |
| 1092 | + String[] filenames = {"json-original.json"}; |
| 1093 | + |
| 1094 | + DatabaseClient client = DatabaseClientFactory.newClient("localhost", uberPort, dbName, "eval-user", "x", Authentication.DIGEST); |
| 1095 | + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); |
| 1096 | + |
| 1097 | + // write docs |
| 1098 | + for(String filename : filenames) |
| 1099 | + { |
| 1100 | + writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON"); |
| 1101 | + } |
| 1102 | + |
| 1103 | + String docId = "/partial-update/json-original.json"; |
| 1104 | + JSONDocumentManager docMgr = client.newJSONDocumentManager(); |
| 1105 | + DocumentMetadataPatchBuilder patchBldr = docMgr.newPatchBuilder(Format.JSON); |
| 1106 | + |
| 1107 | + //Adding the initial meta-data, since there are none. |
| 1108 | + patchBldr.addCollection("JSONPatch1", "JSONPatch3"); |
| 1109 | + patchBldr.addPermission("test-eval", DocumentMetadataHandle.Capability.READ, DocumentMetadataHandle.Capability.EXECUTE); |
| 1110 | + |
| 1111 | + DocumentMetadataPatchBuilder.PatchHandle patchHandle = patchBldr.build(); |
| 1112 | + docMgr.patch(docId, patchHandle); |
| 1113 | + try { |
| 1114 | + Thread.sleep(5000); |
| 1115 | + } catch (InterruptedException e) { |
| 1116 | + // TODO Auto-generated catch block |
| 1117 | + e.printStackTrace(); |
| 1118 | + } |
| 1119 | + |
| 1120 | + String content = docMgr.read(docId, new StringHandle()).get(); |
| 1121 | + |
| 1122 | + System.out.println(content); |
| 1123 | + String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," + |
| 1124 | + "{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," + |
| 1125 | + "{\"lastName\":\"Foo\"}]}"; |
| 1126 | + JSONAssert.assertEquals(exp, content, false); |
| 1127 | + |
| 1128 | + // Validate the changed meta-data. |
| 1129 | + docMgr.readMetadata(docId, mhRead); |
| 1130 | + |
| 1131 | + // Collections |
| 1132 | + DocumentCollections collections = mhRead.getCollections(); |
| 1133 | + String actualCollections = getDocumentCollectionsString(collections); |
| 1134 | + System.out.println("Returned collections: " + actualCollections); |
| 1135 | + |
| 1136 | + assertTrue("Document collections difference in size value", actualCollections.contains("size:2")); |
| 1137 | + assertTrue("JSONPatch1 not found", actualCollections.contains("JSONPatch1")); |
| 1138 | + assertTrue("JSONPatch3 not found", actualCollections.contains("JSONPatch3")); |
| 1139 | + |
| 1140 | + //Construct a Patch From Raw JSON |
| 1141 | + /* This is the JSON Format of meta-data for a document: Used for debugging and JSON Path estimation. |
| 1142 | + { |
| 1143 | + "collections" : [ string ], |
| 1144 | + "permissions" : [ |
| 1145 | + { |
| 1146 | + "role-name" : string, |
| 1147 | + "capabilities" : [ string ] |
| 1148 | + } |
| 1149 | + ], |
| 1150 | + "properties" : { |
| 1151 | + property-name : property-value |
| 1152 | + }, |
| 1153 | + "quality" : integer |
| 1154 | + } |
| 1155 | + */ |
| 1156 | + |
| 1157 | + /* This is the format for INSERT patch. Refer to Guides. |
| 1158 | + { "patch": [ |
| 1159 | + { "insert": { |
| 1160 | + "context": "$.parent.child1", |
| 1161 | + "position": "before", |
| 1162 | + "content": { "INSERT1": "INSERTED1" } |
| 1163 | + }}, |
| 1164 | + */ |
| 1165 | + |
| 1166 | + /* This is the current meta-data in JSON format - For debugging purpose |
| 1167 | + {"collections":["JSONPatch1","JSONPatch3"], |
| 1168 | + "permissions":[{"role-name":"rest-writer", |
| 1169 | + "capabilities":["execute","read","update"]},{"role-name":"test-eval", |
| 1170 | + "capabilities":["execute","read"]},{"role-name":"rest-reader", |
| 1171 | + "capabilities":["read"]}], |
| 1172 | + "properties":{},"quality":0}*/ |
| 1173 | + |
| 1174 | + //String str = new String("{\"patch\": [{ \"insert\": {\"context\": \"collections\",\"position\": \"before\",\"content\": { \"shapes\":\"squares\" }}}]}"); |
| 1175 | + |
| 1176 | + // release client |
| 1177 | + client.release(); |
| 1178 | + } |
| 1179 | + |
1076 | 1180 | @AfterClass |
1077 | 1181 | public static void tearDown() throws Exception |
1078 | 1182 | { |
|
0 commit comments