FilterDBObjectBuilder extends DBObjectBuilder { public DBObject build() { DBObject container; if (useAggregationPipeline()) { container = new BasicDBObject(); container.put("$match", filterQuery); } else { container = filterQuery; } return container; } FilterDBObjectBuilder(boolean useAggregation, Collection<Filter> filterCollection); DBObject build();  }
@Test public void buildTest() throws Exception { @SuppressWarnings("unchecked") FilterDBObjectBuilder filterDBObjectBuilder = new FilterDBObjectBuilder(false, Collections.EMPTY_LIST); DBObject dbObject = QueryBuilder.start(COLUMN_1).greaterThanEquals(5).get(); Whitebox.setInternalState(filterDBObjectBuilder, "filterQuery", dbObject); Whitebox.setInternalState(filterDBObjectBuilder, "useAggregation", false); DBObject filterQuery = filterDBObjectBuilder.build(); assertNotNull("The filter query is null", filterQuery); assertEquals("There should be a query only over 1 field", 1, filterQuery.keySet().size()); assertTrue("The query should contain a clause about" + COLUMN_1, filterQuery.containsField(COLUMN_1)); DBObject condition = (DBObject) filterQuery.get(COLUMN_1); assertTrue("The query does not contain '$gte'", condition.containsField("$gte")); assertEquals("The value of '$gte' key should be 5", 5, condition.get("$gte")); }
@SuppressWarnings("unchecked") @Test public void buildAggregationTest() throws Exception { FilterDBObjectBuilder filterDBObjectBuilder = new FilterDBObjectBuilder(true, Collections.EMPTY_LIST); DBObject dbObject = QueryBuilder.start(COLUMN_1).greaterThanEquals(5).get(); Whitebox.setInternalState(filterDBObjectBuilder, "filterQuery", dbObject); Whitebox.setInternalState(filterDBObjectBuilder, "useAggregation", true); DBObject filterQuery = filterDBObjectBuilder.build(); assertNotNull("The filter query is null", filterQuery); assertEquals("There should be a query only over 1 field", 1, filterQuery.keySet().size()); assertTrue("The query should contain '$match'", filterQuery.containsField("$match")); DBObject condition = (DBObject) ((DBObject) filterQuery.get("$match")).get(COLUMN_1); assertTrue("The query does not contain '$gte'", condition.containsField("$gte")); assertEquals("The value of '$gte' key should be 5", 5, condition.get("$gte")); }