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");
}
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;
}
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();
}
}
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);
}
}
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);
}
Aggregations