logger.debug("*** testImportAttachmentInitialImport ***");
try {
createDatabase();
byte[] content = copyToBytesFromClasspath(RiverMongoWithGridFSTest.TEST_ATTACHMENT_HTML);
logger.debug("Content in bytes: {}", content.length);
GridFS gridFS = new GridFS(mongoDB);
GridFSInputFile in = gridFS.createFile(content);
in.setFilename("test-attachment.html");
in.setContentType("text/html");
in.save();
in.validate();
String id = in.getId().toString();
logger.debug("GridFS in: {}", in);
logger.debug("Document created with id: {}", id);
GridFSDBFile out = gridFS.findOne(in.getFilename());
logger.debug("GridFS from findOne: {}", out);
out = gridFS.findOne(new ObjectId(id));
logger.debug("GridFS from findOne: {}", out);
Assert.assertEquals(out.getId(), in.getId());
createRiver();
Thread.sleep(wait);
refreshIndex();
CountResponse countResponse = getNode().client().count(countRequest(getIndex())).actionGet();
logger.debug("Index total count: {}", countResponse.getCount());
assertThat(countResponse.getCount(), equalTo(1l));
GetResponse getResponse = getNode().client().get(getRequest(getIndex()).id(id)).get();
logger.debug("Get request for id {}: {}", id, getResponse.isExists());
assertThat(getResponse.isExists(), equalTo(true));
SearchResponse response = getNode().client().prepareSearch(getIndex()).setQuery(QueryBuilders.queryString("Aliquam")).execute()
.actionGet();
logger.debug("SearchResponse {}", response.toString());
long totalHits = response.getHits().getTotalHits();
logger.debug("TotalHits: {}", totalHits);
assertThat(totalHits, equalTo(1l));
in = gridFS.createFile(content);
in.setFilename("test-attachment-2.html");
in.setContentType("text/html");
in.save();
in.validate();
id = in.getId().toString();
out = gridFS.findOne(in.getFilename());
logger.debug("GridFS from findOne: {}", out);
out = gridFS.findOne(new ObjectId(id));
logger.debug("GridFS from findOne: {}", out);
Assert.assertEquals(out.getId(), in.getId());
Thread.sleep(wait);
refreshIndex();
countResponse = getNode().client().count(countRequest(getIndex())).actionGet();
logger.debug("Index total count: {}", countResponse.getCount());
assertThat(countResponse.getCount(), equalTo(2l));
getResponse = getNode().client().get(getRequest(getIndex()).id(id)).get();
logger.debug("Get request for id {}: {}", id, getResponse.isExists());
assertThat(getResponse.isExists(), equalTo(true));
// countResponse = getNode().client().count(countRequest(getIndex()).query(fieldQuery("_id", id))).actionGet();
// logger.debug("Index count for id {}: {}", id, countResponse.getCount());
// assertThat(countResponse.getCount(), equalTo(1l));
response = getNode().client().prepareSearch(getIndex()).setQuery(QueryBuilders.queryString("Aliquam")).execute().actionGet();
logger.debug("SearchResponse {}", response.toString());
totalHits = response.getHits().getTotalHits();
logger.debug("TotalHits: {}", totalHits);
assertThat(totalHits, equalTo(2l));
DBCursor cursor = gridFS.getFileList();
try {
while (cursor.hasNext()) {
DBObject object = cursor.next();
gridFS.remove(new ObjectId(object.get("_id").toString()));
}
} finally {
cursor.close();
}