|
19 | 19 | */ |
20 | 20 | package com.marklogic.client.functionaltest; |
21 | 21 |
|
| 22 | +import static org.junit.Assert.assertEquals; |
22 | 23 | import static org.junit.Assert.assertTrue; |
23 | 24 |
|
24 | 25 | import java.security.KeyManagementException; |
25 | 26 | import java.security.NoSuchAlgorithmException; |
26 | 27 | import java.util.Calendar; |
27 | 28 |
|
28 | | -import org.junit.After; |
29 | | -import org.junit.AfterClass; |
30 | | -import org.junit.Before; |
31 | | -import org.junit.BeforeClass; |
32 | | -import org.junit.Test; |
| 29 | +import org.junit.*; |
33 | 30 | import org.skyscreamer.jsonassert.JSONAssert; |
34 | 31 |
|
35 | 32 | import com.fasterxml.jackson.databind.JsonNode; |
@@ -534,4 +531,141 @@ public void testWriteMultipleTextDocWithChangedMetadataCollections() { |
534 | 531 | } |
535 | 532 | validateUpdatedMetadataCollections(metadataHandleRead); |
536 | 533 | } |
537 | | -} |
| 534 | + |
| 535 | + // Test reset of metadata collections in a transaction. |
| 536 | + @Test |
| 537 | + public void testDefaultMetadataOnDocManager() throws InterruptedException { |
| 538 | + System.out.println("Running testDefaultMetadataOnDocManager"); |
| 539 | + Transaction t1 = null, tReset = null; |
| 540 | + try { |
| 541 | + String docId[] = {"/foo/test/myFoo1.txt", "/foo/test/myFoo2.txt", "/foo/test/myFoo3.txt", |
| 542 | + }; |
| 543 | + TextDocumentManager docMgr = client.newTextDocumentManager(); |
| 544 | + |
| 545 | + DocumentWriteSet writeset = docMgr.newWriteSet(); |
| 546 | + // put meta-data |
| 547 | + DocumentMetadataHandle mh = new DocumentMetadataHandle(); |
| 548 | + DocumentMetadataHandle mhRead = new DocumentMetadataHandle(); |
| 549 | + |
| 550 | + mh.getCollections().addAll("groupCollections"); |
| 551 | + DocumentMetadataHandle docSpecificMetadata = |
| 552 | + new DocumentMetadataHandle().withCollections("specificDocCollection"); |
| 553 | + writeset.addDefault(mh); |
| 554 | + writeset.add(docId[0], docSpecificMetadata, new StringHandle().with("This is so foo1")); |
| 555 | + writeset.add(docId[1], new StringHandle().with("This is so foo2")); |
| 556 | + writeset.add(docId[2], new StringHandle().with("This is so foo3")); |
| 557 | + docMgr.write(writeset); |
| 558 | + |
| 559 | + StringHandle sh = docMgr.read(docId[0], new StringHandle()); |
| 560 | + assertEquals("Doc content incorrect", "This is so foo1", sh.get()); |
| 561 | + docMgr.readMetadata(docId[0], mhRead); |
| 562 | + |
| 563 | + DocumentCollections collections = mhRead.getCollections(); |
| 564 | + String actualCollections = getDocumentCollectionsString(collections); |
| 565 | + System.out.println(actualCollections); |
| 566 | + |
| 567 | + String expectedCollections1 = "size:1|specificDocCollection|"; |
| 568 | + assertEquals("Document collections difference", expectedCollections1, actualCollections); |
| 569 | + |
| 570 | + // Verify that docId[1] does not have specificDoccollection |
| 571 | + mhRead = new DocumentMetadataHandle(); |
| 572 | + docMgr.readMetadata(docId[1], mhRead); |
| 573 | + collections = mhRead.getCollections(); |
| 574 | + actualCollections = getDocumentCollectionsString(collections); |
| 575 | + System.out.println(actualCollections); |
| 576 | + |
| 577 | + String expectedCollections2 = "size:1|groupCollections|"; |
| 578 | + assertEquals("Document collections difference", expectedCollections2, actualCollections); |
| 579 | + // Reset metadata |
| 580 | + docMgr.writeDefaultMetadata(docId); |
| 581 | + Thread.sleep(5000); |
| 582 | + mhRead = new DocumentMetadataHandle(); |
| 583 | + // Read meta data for docId[0] to verify that it reset. |
| 584 | + docMgr.readMetadata(docId[0], mhRead); |
| 585 | + collections = mhRead.getCollections(); |
| 586 | + actualCollections = getDocumentCollectionsString(collections); |
| 587 | + System.out.println(actualCollections); |
| 588 | + String expectedCollections3 = "size:0|"; |
| 589 | + assertEquals("Document collections difference", expectedCollections3, actualCollections); |
| 590 | + |
| 591 | + // Call in a writeDefaultMetadata transaction |
| 592 | + String docIdTx[] = {"/foo/test/myFoo4.txt", "/foo/test/myFoo5.txt"}; |
| 593 | + t1 = client.openTransaction(); |
| 594 | + |
| 595 | + DocumentMetadataHandle docTransSpecificMetadata = |
| 596 | + new DocumentMetadataHandle().withCollections("TransSpecificDocCollection"); |
| 597 | + TextDocumentManager docMgrTx = client.newTextDocumentManager(); |
| 598 | + |
| 599 | + DocumentWriteSet writesetTx = docMgrTx.newWriteSet(); |
| 600 | + writesetTx.add(docIdTx[0], docTransSpecificMetadata, new StringHandle().with("This is so foo4")); |
| 601 | + writesetTx.add(docIdTx[1], docTransSpecificMetadata, new StringHandle().with("This is so foo5")); |
| 602 | + docMgrTx.write(writesetTx, t1); |
| 603 | + t1.commit(); |
| 604 | + Thread.sleep(2000); |
| 605 | + t1 = null; |
| 606 | + |
| 607 | + sh = docMgrTx.read(docIdTx[0], new StringHandle()); |
| 608 | + assertEquals("Doc content incorrect", "This is so foo4", sh.get()); |
| 609 | + docMgrTx.readMetadata(docIdTx[0], mhRead); |
| 610 | + |
| 611 | + collections = mhRead.getCollections(); |
| 612 | + actualCollections = getDocumentCollectionsString(collections); |
| 613 | + System.out.println(actualCollections); |
| 614 | + |
| 615 | + String expectedCollections4 = "size:1|TransSpecificDocCollection|"; |
| 616 | + assertEquals("Document collections difference", expectedCollections4, actualCollections); |
| 617 | + |
| 618 | + tReset = client.openTransaction(); |
| 619 | + docMgrTx.writeDefaultMetadata(tReset, docIdTx); |
| 620 | + // Read again without committing |
| 621 | + |
| 622 | + docMgrTx.readMetadata(docIdTx[0], mhRead); |
| 623 | + |
| 624 | + collections = mhRead.getCollections(); |
| 625 | + actualCollections = getDocumentCollectionsString(collections); |
| 626 | + System.out.println(actualCollections); |
| 627 | + |
| 628 | + expectedCollections4 = "size:1|TransSpecificDocCollection|"; |
| 629 | + assertEquals("Document collections difference", expectedCollections4, actualCollections); |
| 630 | + |
| 631 | + // Now commit transaction and verify if collections reset. |
| 632 | + tReset.commit(); |
| 633 | + |
| 634 | + Thread.sleep(2000); |
| 635 | + tReset = null; |
| 636 | + mhRead = new DocumentMetadataHandle(); |
| 637 | + // Read meta data for docId[1] to verify that it reset. |
| 638 | + docMgrTx.readMetadata(docIdTx[1], mhRead); |
| 639 | + collections = mhRead.getCollections(); |
| 640 | + actualCollections = getDocumentCollectionsString(collections); |
| 641 | + System.out.println(actualCollections); |
| 642 | + String expectedCollections5 = "size:0|"; |
| 643 | + assertEquals("Document collections difference", expectedCollections5, actualCollections); |
| 644 | + |
| 645 | + // Negative case - Call with empty args |
| 646 | + StringBuilder negStr = new StringBuilder(); |
| 647 | + try { |
| 648 | + docMgrTx.writeDefaultMetadata(); |
| 649 | + } |
| 650 | + catch(Exception e) { |
| 651 | + System.out.println(e.getMessage()); |
| 652 | + negStr.append(e.getMessage()); |
| 653 | + } |
| 654 | + finally { |
| 655 | + assertTrue("Negative case failure", negStr.toString().contains("Resetting document metadata with empty identifier list")); |
| 656 | + } |
| 657 | + } |
| 658 | + catch (Exception ex) { |
| 659 | + System.out.println("Exceptions thrown" + ex.getStackTrace()); |
| 660 | + Assert.fail("Test failed due to exceptions"); |
| 661 | + } |
| 662 | + finally { |
| 663 | + if (t1 != null) { |
| 664 | + t1.rollback(); |
| 665 | + } |
| 666 | + if (tReset != null) { |
| 667 | + tReset.rollback(); |
| 668 | + } |
| 669 | + } |
| 670 | + } |
| 671 | + } |
0 commit comments