Search in sources :

Example 21 with Document

use of com.ibm.cloud.cloudant.v1.model.Document in project cloudant-java-sdk by IBM.

the class DocumentTest method testDocument.

@Test
public void testDocument() throws Throwable {
    Attachment attachmentModel = new Attachment.Builder().contentType("testString").data(TestUtilities.createMockByteArray("This is a mock byte array value.")).digest("testString").encodedLength(Long.valueOf("0")).encoding("testString").follows(true).length(Long.valueOf("0")).revpos(Long.valueOf("1")).stub(true).build();
    assertEquals(attachmentModel.contentType(), "testString");
    assertEquals(attachmentModel.data(), TestUtilities.createMockByteArray("This is a mock byte array value."));
    assertEquals(attachmentModel.digest(), "testString");
    assertEquals(attachmentModel.encodedLength(), Long.valueOf("0"));
    assertEquals(attachmentModel.encoding(), "testString");
    assertEquals(attachmentModel.follows(), Boolean.valueOf(true));
    assertEquals(attachmentModel.length(), Long.valueOf("0"));
    assertEquals(attachmentModel.revpos(), Long.valueOf("1"));
    assertEquals(attachmentModel.stub(), Boolean.valueOf(true));
    Revisions revisionsModel = new Revisions.Builder().ids(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).start(Long.valueOf("1")).build();
    assertEquals(revisionsModel.ids(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(revisionsModel.start(), Long.valueOf("1"));
    DocumentRevisionStatus documentRevisionStatusModel = new DocumentRevisionStatus.Builder().rev("testString").status("available").build();
    assertEquals(documentRevisionStatusModel.rev(), "testString");
    assertEquals(documentRevisionStatusModel.status(), "available");
    Document documentModel = new Document.Builder().attachments(new java.util.HashMap<String, Attachment>() {

        {
            put("foo", attachmentModel);
        }
    }).conflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).deleted(true).deletedConflicts(new java.util.ArrayList<String>(java.util.Arrays.asList("testString"))).id("testString").localSeq("testString").rev("testString").revisions(revisionsModel).revsInfo(new java.util.ArrayList<DocumentRevisionStatus>(java.util.Arrays.asList(documentRevisionStatusModel))).add("foo", "testString").build();
    assertEquals(documentModel.getAttachments(), new java.util.HashMap<String, Attachment>() {

        {
            put("foo", attachmentModel);
        }
    });
    assertEquals(documentModel.getConflicts(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(documentModel.isDeleted(), Boolean.valueOf(true));
    assertEquals(documentModel.getDeletedConflicts(), new java.util.ArrayList<String>(java.util.Arrays.asList("testString")));
    assertEquals(documentModel.getId(), "testString");
    assertEquals(documentModel.getLocalSeq(), "testString");
    assertEquals(documentModel.getRev(), "testString");
    assertEquals(documentModel.getRevisions(), revisionsModel);
    assertEquals(documentModel.getRevsInfo(), new java.util.ArrayList<DocumentRevisionStatus>(java.util.Arrays.asList(documentRevisionStatusModel)));
    assertEquals(documentModel.get("foo"), "testString");
    String json = TestUtilities.serialize(documentModel);
    Document documentModelNew = TestUtilities.deserialize(json, Document.class);
    assertTrue(documentModelNew instanceof Document);
    assertEquals(documentModelNew.isDeleted(), Boolean.valueOf(true));
    assertEquals(documentModelNew.getId(), "testString");
    assertEquals(documentModelNew.getLocalSeq(), "testString");
    assertEquals(documentModelNew.getRev(), "testString");
    assertEquals(documentModelNew.getRevisions().toString(), revisionsModel.toString());
    assertEquals(documentModelNew.get("foo"), "testString");
}
Also used : ArrayList(java.util.ArrayList) Attachment(com.ibm.cloud.cloudant.v1.model.Attachment) Revisions(com.ibm.cloud.cloudant.v1.model.Revisions) Document(com.ibm.cloud.cloudant.v1.model.Document) DocumentRevisionStatus(com.ibm.cloud.cloudant.v1.model.DocumentRevisionStatus) Test(org.testng.annotations.Test)

Example 22 with Document

use of com.ibm.cloud.cloudant.v1.model.Document in project dna by leifeld.

the class Sql method getDocuments.

/**
 * Get documents for a batch of document IDs. The data can be displayed and
 * edited in a {@link gui.DocumentEditor DocumentEditor} dialog. The
 * documents do not contain any statements.
 *
 * @param documentIds    An array of document IDs for which the data should
 *   be queried.
 * @return             An {@link java.util.ArrayList ArrayList} of
 *   {@link model.Document Document} objects, containing the documents and
 *   their meta-data.
 *
 * @category document
 */
public ArrayList<Document> getDocuments(int[] documentIds) {
    ArrayList<Document> documents = new ArrayList<Document>();
    String sql = "SELECT * FROM DOCUMENTS WHERE ID IN (";
    for (int i = 0; i < documentIds.length; i++) {
        sql = sql + documentIds[i];
        if (i < documentIds.length - 1) {
            sql = sql + ", ";
        }
    }
    sql = sql + ");";
    try (Connection conn = getDataSource().getConnection();
        PreparedStatement s = conn.prepareStatement(sql)) {
        ResultSet rs = s.executeQuery();
        while (rs.next()) {
            Document d = new Document(rs.getInt("ID"), rs.getInt("Coder"), rs.getString("Title"), rs.getString("Text"), rs.getString("Author"), rs.getString("Source"), rs.getString("Section"), rs.getString("Type"), rs.getString("Notes"), LocalDateTime.ofEpochSecond(rs.getLong("Date"), 0, ZoneOffset.UTC), new ArrayList<Statement>());
            documents.add(d);
        }
    } catch (SQLException e) {
        LogEvent l = new LogEvent(Logger.WARNING, "[SQL] Failed to retrieve document meta-data from the database.", "Attempted to retrieve document data (other than the document text) for " + documentIds.length + " documents, but this failed. Check if all documents are being displayed in the user interface.", e);
        Dna.logger.log(l);
    }
    return documents;
}
Also used : SQLException(java.sql.SQLException) LogEvent(logger.LogEvent) PreparedStatement(java.sql.PreparedStatement) Statement(model.Statement) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) TableDocument(model.TableDocument) Document(model.Document)

Example 23 with Document

use of com.ibm.cloud.cloudant.v1.model.Document in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method getEvents.

@Override
public List<CloudEvent<?, ?>> getEvents() {
    try {
        List<CloudEvent<?, ?>> events = new ArrayList<>();
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).includeDocs(true).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        for (DocsResultRow docResult : allDocResults.getRows()) {
            Document document = docResult.getDoc();
            @SuppressWarnings("rawtypes") CloudEventImpl evt = this.gson.fromJson(document.toString(), CloudEventImpl.class);
            events.add(evt);
        }
        return events;
    } catch (NotFoundException e) {
        logger.warn("Unable to retrieve all documents from Cloudant", e);
        return Collections.emptyList();
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) ArrayList(java.util.ArrayList) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Document(com.ibm.cloud.cloudant.v1.model.Document) CloudEvent(io.cloudevents.CloudEvent) CloudEventImpl(io.cloudevents.v02.CloudEventImpl)

Example 24 with Document

use of com.ibm.cloud.cloudant.v1.model.Document in project knative-eventing-java-app by IBM.

the class CloudEventStoreCloudant method removeAllEvents.

@Override
public void removeAllEvents() throws Exception {
    try {
        PostAllDocsOptions docsOptions = new PostAllDocsOptions.Builder().db(this.dbName).includeDocs(true).build();
        AllDocsResult allDocResults = this.client.postAllDocs(docsOptions).execute().getResult();
        for (DocsResultRow docResult : allDocResults.getRows()) {
            Document document = docResult.getDoc();
            DeleteDocumentOptions deleteDocumentOptions = new DeleteDocumentOptions.Builder().db(this.dbName).docId(document.getId()).rev(document.getRev()).build();
            DocumentResult deleteDocumentResponse = client.deleteDocument(deleteDocumentOptions).execute().getResult();
            if (!deleteDocumentResponse.isOk()) {
                logger.info("Could not delete a document.");
            }
        }
    } catch (NotFoundException e) {
        String errMsg = "Unable to retrieve all documents from Cloudant";
        logger.error(errMsg, e);
        throw new Exception(errMsg, e);
    }
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) DocumentResult(com.ibm.cloud.cloudant.v1.model.DocumentResult) GsonBuilder(com.google.gson.GsonBuilder) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) PostAllDocsOptions(com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions) Document(com.ibm.cloud.cloudant.v1.model.Document) DeleteDocumentOptions(com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions) NotFoundException(com.ibm.cloud.sdk.core.service.exception.NotFoundException)

Example 25 with Document

use of com.ibm.cloud.cloudant.v1.model.Document in project knative-eventing-java-app by IBM.

the class EventControllerEndpointTest method mockGetEvents.

public void mockGetEvents(@SuppressWarnings("rawtypes") final List<CloudEventImpl> docs) throws Exception {
    Gson customGson = SBApplicationConfig.getCustomGsonBuilder().create();
    List<DocsResultRow> docsResultRows = new ArrayList<>();
    for (CloudEvent<?, ?> cloudEvent : docs) {
        Document document = new Document();
        document.setProperties(customGson.fromJson(customGson.toJson(cloudEvent), Map.class));
        DocsResultRow docsResultRowMock = Mockito.mock(DocsResultRow.class);
        Mockito.when(docsResultRowMock.getDoc()).thenReturn(document);
        docsResultRows.add(docsResultRowMock);
    }
    long expectedNumEvents = docs.size();
    @SuppressWarnings("unchecked") Response<AllDocsResult> responseMock = Mockito.mock(Response.class);
    AllDocsResult allDocsResultMock = Mockito.mock(AllDocsResult.class);
    Mockito.when(this.eventService.getNumEvents()).thenReturn(expectedNumEvents);
    Mockito.when(responseMock.getResult()).thenReturn(allDocsResultMock);
    Mockito.when(allDocsResultMock.getRows()).thenReturn(docsResultRows);
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) AllDocsResult(com.ibm.cloud.cloudant.v1.model.AllDocsResult) Document(com.ibm.cloud.cloudant.v1.model.Document) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)32 RequestBuilder (com.ibm.cloud.sdk.core.http.RequestBuilder)30 Document (com.ibm.cloud.cloudant.v1.model.Document)22 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)20 ArrayList (java.util.ArrayList)16 Test (org.testng.annotations.Test)12 Attachment (com.ibm.cloud.cloudant.v1.model.Attachment)10 DocumentRevisionStatus (com.ibm.cloud.cloudant.v1.model.DocumentRevisionStatus)10 Revisions (com.ibm.cloud.cloudant.v1.model.Revisions)10 DesignDocument (com.ibm.cloud.cloudant.v1.model.DesignDocument)9 ReplicationDocument (com.ibm.cloud.cloudant.v1.model.ReplicationDocument)9 SchedulerDocument (com.ibm.cloud.cloudant.v1.model.SchedulerDocument)9 JsonObject (com.google.gson.JsonObject)8 AllDocsResult (com.ibm.cloud.cloudant.v1.model.AllDocsResult)6 BulkGetQueryDocument (com.ibm.cloud.cloudant.v1.model.BulkGetQueryDocument)6 BulkGetResultDocument (com.ibm.cloud.cloudant.v1.model.BulkGetResultDocument)6 MockResponse (okhttp3.mockwebserver.MockResponse)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 PostDocumentOptions (com.ibm.cloud.cloudant.v1.model.PostDocumentOptions)5