marklogic / java-client-api

Java client for the MarkLogic enterprise NoSQL database
https://docs.marklogic.com/guide/java
Apache License 2.0
58 stars 72 forks source link

Add shortcut addAs methods to DocumentWriteSet #392

Closed sammefford closed 7 years ago

sammefford commented 8 years ago

To align with the rest of the Java Client API, DocumentWriteSet should have addAs methods that accept a content object then uses the handle registry to lookup the appropriate handle for the object.

sammefford commented 8 years ago

This is implemented both on develop and 3.0-develop

georgeajit commented 8 years ago

Added the following method to TestDatabaseClientConnection.java

// Test to validate that addAs with a java.io.object in DocumentWriteSet writes the document.
    @Test
    public void testAddAs() throws Exception {

        System.out.println("Running testAddAs");

        String[] docId = {"aggr1.xml", "aggr2.xml", "aggr3.xml"};
        DatabaseClient client = DatabaseClientFactory.newClient("localhost", Uberport, UberdbName, "eval-user", "x", Authentication.DIGEST);
        Transaction transaction = client.openTransaction();

        try {
            TextDocumentManager docMgr = client.newTextDocumentManager();
            docMgr.setMetadataCategories(Metadata.ALL);
            DocumentWriteSet writeset = docMgr.newWriteSet();

            DocumentMetadataHandle mhRead = new DocumentMetadataHandle();       
            InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/" + docId[0]);
            InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/" + docId[1]);
            InputStream inputStream3 = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/" + docId[2]);
            writeset.addAs(docId[0], inputStream1);
            writeset.addAs(docId[1], inputStream2);
            writeset.addAs(docId[2], inputStream3);

            docMgr.write(writeset, transaction);
            StringHandle wrteTransHandle = new StringHandle();
            transaction.readStatus(wrteTransHandle);
            assertTrue("Transaction readStatus during write does not contain Database name. ", (wrteTransHandle.get()).contains(UberrestServerName));
            assertTrue("Transaction readStatus during write does not contain Database name. ", (wrteTransHandle.get()).contains("App-Services"));
            transaction.commit();

            transaction = client.openTransaction();
            String txId = transaction.getTransactionId();

            DocumentPage page = docMgr.read(transaction, docId[0], docId[1], docId[2]);
            assertTrue("DocumentPage Size did not return expected value returned==  "+page.size(), page.size() == 3 );
            // Read back the doc contents to make sure that write succeeded.
            String strDocContent1 = docMgr.read(docId[0], new StringHandle()).get();
            assertTrue("Text document write difference", strDocContent1.contains("Vannevar Bush wrote an article for The Atlantic Monthly"));
            String strDocContent2 = docMgr.read(docId[1], new StringHandle()).get();
            assertTrue("Text document write difference", strDocContent2.contains("The Bush article described a device called a Memex."));
            String strDocContent3 = docMgr.read(docId[2], new StringHandle()).get();
            assertTrue("Text document write difference", strDocContent3.contains("For 1945, the thoughts expressed in The Atlantic Monthly were groundbreaking."));

            StringHandle readTransHandle = new StringHandle();
            transaction.readStatus(readTransHandle);
            assertTrue("Transaction readStatus during read does not contain App Server name. ", (readTransHandle.get()).contains(UberrestServerName));
            assertTrue("Transaction readStatus during read does not contain Database name. ", (readTransHandle.get()).contains(dbName));
            assertTrue("Transaction readStatus during read does not contain Transaction Id name. ", (readTransHandle.get()).contains(txId));
        }
        catch(Exception exp) {
            System.out.println(exp.getMessage());
            throw exp;
        }
        finally {
            transaction.rollback();
        }
    }