Search in sources :

Example 1 with DocsResultRow

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

the class DocsResultRowTest method testDocsResultRow.

@Test
public void testDocsResultRow() throws Throwable {
    DocsResultRow docsResultRowModel = new DocsResultRow();
    assertNull(docsResultRowModel.getCausedBy());
    assertNull(docsResultRowModel.getError());
    assertNull(docsResultRowModel.getReason());
    assertNull(docsResultRowModel.getDoc());
    assertNull(docsResultRowModel.getId());
    assertNull(docsResultRowModel.getKey());
    assertNull(docsResultRowModel.getValue());
}
Also used : DocsResultRow(com.ibm.cloud.cloudant.v1.model.DocsResultRow) Test(org.testng.annotations.Test)

Example 2 with DocsResultRow

use of com.ibm.cloud.cloudant.v1.model.DocsResultRow 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 3 with DocsResultRow

use of com.ibm.cloud.cloudant.v1.model.DocsResultRow 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 4 with DocsResultRow

use of com.ibm.cloud.cloudant.v1.model.DocsResultRow 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

DocsResultRow (com.ibm.cloud.cloudant.v1.model.DocsResultRow)4 AllDocsResult (com.ibm.cloud.cloudant.v1.model.AllDocsResult)3 Document (com.ibm.cloud.cloudant.v1.model.Document)3 PostAllDocsOptions (com.ibm.cloud.cloudant.v1.model.PostAllDocsOptions)2 NotFoundException (com.ibm.cloud.sdk.core.service.exception.NotFoundException)2 ArrayList (java.util.ArrayList)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 DeleteDocumentOptions (com.ibm.cloud.cloudant.v1.model.DeleteDocumentOptions)1 DocumentResult (com.ibm.cloud.cloudant.v1.model.DocumentResult)1 CloudEvent (io.cloudevents.CloudEvent)1 CloudEventImpl (io.cloudevents.v02.CloudEventImpl)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1