Skip to content

Commit 159b195

Browse files
Ajit GeorgeAjit George
authored andcommitted
Added test cases to check patch builder methods on Json documents.
1 parent 8ef8e08 commit 159b195

File tree

1 file changed

+231
-1
lines changed

1 file changed

+231
-1
lines changed

test-complete/src/test/java/com/marklogic/javaclient/TestPartialUpdate.java

Lines changed: 231 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import com.marklogic.client.io.StringHandle;
2727
import com.marklogic.client.io.DocumentMetadataHandle.Capability;
2828
import com.marklogic.client.io.marker.DocumentPatchHandle;
29+
30+
import org.json.JSONException;
2931
import org.junit.*;
32+
import org.skyscreamer.jsonassert.JSONAssert;
3033

3134
public class TestPartialUpdate extends BasicJavaClientREST {
3235
private static String dbName = "TestPartialUpdateDB";
@@ -842,7 +845,234 @@ public void testPartialUpdateCardinality() throws IOException
842845

843846
// release client
844847
client.release();
845-
}
848+
}
849+
850+
/* Purpose: This test is used to validate all of the patch builder functions on a JSON
851+
* document using JSONPath expressions.
852+
*
853+
* Function tested: replaceValue.
854+
*/
855+
@Test
856+
public void testPartialUpdateReplaceValueJSON() throws IOException, JSONException
857+
{
858+
System.out.println("Running testPartialUpdateReplaceValueJSON");
859+
860+
String[] filenames = {"json-original.json"};
861+
862+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
863+
864+
// write docs
865+
for(String filename : filenames)
866+
{
867+
writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON");
868+
}
869+
870+
String docId = "/partial-update/json-original.json";
871+
JSONDocumentManager docMgr = client.newJSONDocumentManager();
872+
DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder();
873+
patchBldr.pathLanguage(PathLanguage.JSONPATH);
874+
875+
//Replace the third employee's first name. Change it to Jack. Issue #161 - Using filters causes Bad Request Exceptions.
876+
patchBldr.replaceValue("$.employees[2].firstName", "Jack");
877+
878+
DocumentPatchHandle patchHandle = patchBldr.build();
879+
docMgr.patch(docId, patchHandle);
880+
881+
String content = docMgr.read(docId, new StringHandle()).get();
882+
883+
System.out.println(content);
884+
885+
String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," +
886+
"{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," +
887+
"{\"firstName\":\"Jack\", \"lastName\":\"Foo\"}]}";
888+
JSONAssert.assertEquals(exp, content, false);
889+
890+
// release client
891+
client.release();
892+
}
893+
894+
/* Purpose: This test is used to validate all of the patch builder functions on a JSON
895+
* document using JSONPath expressions.
896+
*
897+
* Functions tested : replaceFragment.
898+
*/
899+
@Test
900+
public void testPartialUpdateReplaceFragmentJSON() throws IOException, JSONException
901+
{
902+
System.out.println("Running testPartialUpdateReplaceValueJSON");
903+
904+
String[] filenames = {"json-original.json"};
905+
906+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
907+
908+
// write docs
909+
for(String filename : filenames)
910+
{
911+
writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON");
912+
}
913+
914+
String docId = "/partial-update/json-original.json";
915+
JSONDocumentManager docMgr = client.newJSONDocumentManager();
916+
DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder();
917+
patchBldr.pathLanguage(PathLanguage.JSONPATH);
918+
919+
//Replace the third employee. Issue #161 - Using filters causes Bad Request Exceptions.
920+
patchBldr.replaceFragment("$.employees[2]", "{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}");
921+
922+
DocumentPatchHandle patchHandle = patchBldr.build();
923+
docMgr.patch(docId, patchHandle);
924+
925+
String content = docMgr.read(docId, new StringHandle()).get();
926+
927+
System.out.println(content);
928+
929+
String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," +
930+
"{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," +
931+
"{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}]}";
932+
JSONAssert.assertEquals(exp, content, false);
933+
934+
// release client
935+
client.release();
936+
}
937+
938+
/* Purpose: This test is used to validate all of the patch builder functions on a JSON
939+
* document using JSONPath expressions.
940+
*
941+
* Functions tested : replaceInsertFragment. An new fragment is inserted when unknown index is used.
942+
*/
943+
@Test
944+
public void testPartialUpdateReplaceInsertFragmentNewJSON() throws IOException, JSONException
945+
{
946+
System.out.println("Running testPartialUpdateReplaceInsertFragmentExistingJSON");
947+
948+
String[] filenames = {"json-original.json"};
949+
950+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
951+
952+
// write docs
953+
for(String filename : filenames)
954+
{
955+
writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON");
956+
}
957+
958+
String docId = "/partial-update/json-original.json";
959+
JSONDocumentManager docMgr = client.newJSONDocumentManager();
960+
DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder();
961+
patchBldr.pathLanguage(PathLanguage.JSONPATH);
962+
963+
//Mark an unknown location in argument 1, and then insert new node relative to argument 2.
964+
patchBldr.replaceInsertFragment("$.employees[3]", "$.employees[0]", Position.BEFORE,"{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}");
965+
966+
DocumentPatchHandle patchHandle = patchBldr.build();
967+
docMgr.patch(docId, patchHandle);
968+
969+
String content = docMgr.read(docId, new StringHandle()).get();
970+
971+
System.out.println(content);
972+
973+
String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," +
974+
"{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," +
975+
"{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}," +
976+
"{\"firstName\":\"Bob\", \"lastName\":\"Foo\"}]}";
977+
JSONAssert.assertEquals(exp, content, false);
978+
979+
// release client
980+
client.release();
981+
}
982+
983+
/* Purpose: This test is used to validate all of the patch builder functions on a JSON
984+
* document using JSONPath expressions.
985+
*
986+
* Functions tested : replaceInsertFragment. An existing fragment replaced with another fragment.
987+
*/
988+
@Test
989+
public void testPartialUpdateReplaceInsertFragmentExistingJSON() throws IOException, JSONException
990+
{
991+
System.out.println("Running testPartialUpdateReplaceInsertFragmentExistingJSON");
992+
993+
String[] filenames = {"json-original.json"};
994+
995+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
996+
997+
// write docs
998+
for(String filename : filenames)
999+
{
1000+
writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON");
1001+
}
1002+
1003+
String docId = "/partial-update/json-original.json";
1004+
JSONDocumentManager docMgr = client.newJSONDocumentManager();
1005+
DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder();
1006+
patchBldr.pathLanguage(PathLanguage.JSONPATH);
1007+
1008+
//Replace the third employee. Issue #161 - Using filters causes Bad Request Exceptions.
1009+
patchBldr.replaceInsertFragment("$.employees[2]", "$.employees[2]", Position.LAST_CHILD,"{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}");
1010+
1011+
DocumentPatchHandle patchHandle = patchBldr.build();
1012+
docMgr.patch(docId, patchHandle);
1013+
1014+
String content = docMgr.read(docId, new StringHandle()).get();
1015+
1016+
System.out.println(content);
1017+
1018+
String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," +
1019+
"{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," +
1020+
"{\"firstName\":\"Albert\", \"lastName\":\"Einstein\"}]}";
1021+
JSONAssert.assertEquals(exp, content, false);
1022+
1023+
// release client
1024+
client.release();
1025+
}
1026+
1027+
/* Purpose: This test is used to validate all of the patch builder functions on a JSON
1028+
* document using JSONPath expressions.
1029+
*
1030+
* Functions tested are: delete, replaceApply, replaceFragment, replaceInsertFragment and replaceValue.
1031+
*/
1032+
1033+
/* Purpose: This test is used to validate all of the patch builder functions on a JSON
1034+
* document using JSONPath expressions.
1035+
*
1036+
* Function tested: delete.
1037+
*/
1038+
@Test
1039+
public void testPartialUpdateDeleteJSON() throws IOException, JSONException
1040+
{
1041+
System.out.println("Running testPartialUpdateReplaceValueJSON");
1042+
1043+
String[] filenames = {"json-original.json"};
1044+
1045+
DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-writer", "x", Authentication.DIGEST);
1046+
1047+
// write docs
1048+
for(String filename : filenames)
1049+
{
1050+
writeDocumentUsingInputStreamHandle(client, filename, "/partial-update/", "JSON");
1051+
}
1052+
1053+
String docId = "/partial-update/json-original.json";
1054+
JSONDocumentManager docMgr = client.newJSONDocumentManager();
1055+
DocumentPatchBuilder patchBldr = docMgr.newPatchBuilder();
1056+
patchBldr.pathLanguage(PathLanguage.JSONPATH);
1057+
1058+
//Delete the third employee's first name. Issue #161 - Using filters causes Bad Request Exceptions.
1059+
patchBldr.delete("$.employees[2].firstName", DocumentMetadataPatchBuilder.Cardinality.ZERO_OR_MORE);
1060+
1061+
DocumentPatchHandle patchHandle = patchBldr.build();
1062+
docMgr.patch(docId, patchHandle);
1063+
1064+
String content = docMgr.read(docId, new StringHandle()).get();
1065+
1066+
System.out.println(content);
1067+
1068+
String exp="{\"employees\": [{\"firstName\":\"John\", \"lastName\":\"Doe\"}," +
1069+
"{\"firstName\":\"Ann\", \"lastName\":\"Smith\"}," +
1070+
"{\"lastName\":\"Foo\"}]}";
1071+
JSONAssert.assertEquals(exp, content, false);
1072+
1073+
// release client
1074+
client.release();
1075+
}
8461076

8471077
@AfterClass
8481078
public static void tearDown() throws Exception

0 commit comments

Comments
 (0)