public void createAndDeleteDocuments() {
// Create a view
URL designUrl = getClass().getClassLoader().getResource("design/simple-design.json");
WriteResponse response = database
.put()
.design("simple", designUrl)
.execute();
assertEquals(response.getHttpStatus(), CouchHttpStatus.CREATED);
GetDocumentResponse documentResponse = database
.get()
.document("_design/simple")
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 1);
// Create a simple document
String docId = "707";
response = database
.put()
.document(docId, "{\"direction\" : \"NORTH\",\"city\" : \"Toronto\"}")
.execute();
assertTrue(response.isCreated(), response.getErrorReason());
documentResponse = database
.get()
.document(docId)
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 1);
// Get count using built in _all_docs
documentResponse = database
.get()
.document("_all_docs")
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 2);
for (TextDocument doc : documentResponse.getDocumentList()) {
assertNotNull(doc.getDocumentId());
assertNotNull(doc.getDocumentRevision());
}
// Get count using all view
CouchViewQuery viewQuery = CouchViewQuery.builder("simple", "all").build();
documentResponse = database
.get()
.document(viewQuery)
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 1);
// Delete all non design
response = database
.delete()
.allNonDesigns()
.execute();
assertTrue(response.isOk(), response.getErrorReason());
// Count should be zero using all view
documentResponse = database
.get()
.document(viewQuery)
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 0);
// Count using _all_docs should be one
documentResponse = database
.get()
.document("_all_docs")
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 1);
for (TextDocument doc : documentResponse.getDocumentList()) {
assertNotNull(doc.getDocumentId());
assertNotNull(doc.getDocumentRevision());
}
// Create a simple document again
docId = "909";
response = database
.put()
.document(docId, "{\"direction\" : \"NORTH\",\"city\" : \"Toronto\"}")
.execute();
assertTrue(response.isCreated(), response.getErrorReason());
documentResponse = database
.get()
.document(docId)
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 1);
// Count using _all_docs should be two
documentResponse = database
.get()
.document("_all_docs")
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 2);
for (TextDocument doc : documentResponse.getDocumentList()) {
assertNotNull(doc.getDocumentId());
assertNotNull(doc.getDocumentRevision());
}
// Delete all design
response = database
.delete()
.allDesigns()
.execute();
assertTrue(response.isOk(), response.getErrorReason());
// Count using _all_docs should be one
documentResponse = database
.get()
.document("_all_docs")
.execute();
assertTrue(documentResponse.isOk(), documentResponse.getErrorReason());
assertEquals(documentResponse.getDocumentCount(), 1);
for (TextDocument doc : documentResponse.getDocumentList()) {
assertNotNull(doc.getDocumentId());
assertNotNull(doc.getDocumentRevision());
}
// Delete all docs
response = database
.delete()
.allDocuments()
.execute();
assertTrue(response.isOk(), response.getErrorReason());
// Count using _all_docs should be one
documentResponse = database
.get()
.document("_all_docs")