private void verifyCollectionNames(final MongoDatabase connection, final Set<String> expectedCollectionNames, final AssertionErrorCollector errorCollector) { final List<String> currentCollections = new ArrayList<>(); connection.listCollectionNames().iterator().forEachRemaining(currentCollections::add); for (final String expectedCollectionName : expectedCollectionNames) { if (!currentCollections.contains(expectedCollectionName)) { errorCollector.collect(expectedCollectionName + " was expected to be present, but not found"); } } }
/** * * @param dbName * @param collName * @return */ List<BsonDocument> getCollectionIndexes(String dbName, String collName) { List<BsonDocument> ret = new ArrayList<>(); ListIndexesIterable<Document> indexes = client .getDatabase(dbName) .getCollection(collName, BsonDocument.class) .listIndexes(); indexes.iterator().forEachRemaining( i -> { BsonDocument bi = BsonDocument.parse(i.toJson()); BsonValue name = bi.remove("name"); bi.put("_id", name); ret.add(bi); }); return ret; }
private Function<Exchange, Object> createDoDistinct() { return exchange -> { Iterable<String> result = new ArrayList<>(); MongoCollection<BasicDBObject> dbCol = calculateCollection(exchange); // get the parameters out of the Exchange Header String distinctFieldName = exchange.getIn().getHeader(MongoDbConstants.DISTINCT_QUERY_FIELD, String.class); BasicDBObject query = exchange.getContext().getTypeConverter().tryConvertTo(BasicDBObject.class, exchange, exchange.getIn().getBody()); DistinctIterable<String> ret; if (query != null) { ret = dbCol.distinct(distinctFieldName, query, String.class); } else { ret = dbCol.distinct(distinctFieldName, String.class); } try { ret.iterator().forEachRemaining(((List<String>) result)::add); exchange.getOut().setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ((List<String>) result).size()); } finally { ret.iterator().close(); } return result; }; }
try { result = new ArrayList<>(); aggregationResult.iterator().forEachRemaining(((List<BasicDBObject>) result)::add); exchange.getOut().setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ((List<BasicDBObject>) result).size()); } finally {
existingIndices.forEachRemaining(existingIndex -> { existingIndex.remove("ns"); existingIndexDocuments.add(existingIndex);
try { result = new ArrayList<>(); ret.iterator().forEachRemaining(((List<BasicDBObject>) result)::add); exchange.getOut().setHeader(MongoDbConstants.RESULT_PAGE_SIZE, ((List<BasicDBObject>) result).size()); } finally {
private void verifyCollectionContent(final MongoDatabase connection, final Document expectedDataSet, final String collectionName, final AssertionErrorCollector errorCollector) { final List<Document> expectedCollectionEntries = getCollectionData(expectedDataSet.get(collectionName)); final List<String> columnsToExclude = toExclude.getColumns(collectionName); final List<Document> foundEntries = new ArrayList<>(); final MongoCollection<Document> currentCollection = connection.getCollection(collectionName); for (final Document expectedEntry : expectedCollectionEntries) { final Document expected = filterRequest(expectedEntry, columnsToExclude); final FindIterable<Document> resultIt = currentCollection.find(expected); if (!resultIt.iterator().hasNext()) { errorCollector.collect(expectedEntry + " was expected in [" + collectionName + "], but is not present"); } resultIt.iterator().forEachRemaining(foundEntries::add); } final FindIterable<Document> allEntries = currentCollection.find(); for (final Document d : allEntries) { if (!foundEntries.contains(d)) { errorCollector.collect(d + " was not expected in [" + collectionName + "], but is present"); } } }