OrderByDBObjectBuilder extends DBObjectBuilder { public DBObject build() { DBObject container; if (useAggregationPipeline()) { container = new BasicDBObject(); container.put("$sort", orderByQuery); } else { container = orderByQuery; } return container; } OrderByDBObjectBuilder(boolean useAggregation, OrderBy orderBy); DBObject build();  }
@Test public void buildTest() throws Exception { OrderByDBObjectBuilder orderByDBObjectBuilder = new OrderByDBObjectBuilder(false, getOrderBy("mock")); DBObject fakeOrderBy = new BasicDBObject("fake", 5); Whitebox.setInternalState(orderByDBObjectBuilder, "orderByQuery", fakeOrderBy); Whitebox.setInternalState(orderByDBObjectBuilder, "useAggregation", true); DBObject orderByCommand = orderByDBObjectBuilder.build(); assertTrue("The orderBy command should have a root $sort", orderByCommand.keySet().contains("$sort")); assertEquals("The value is not the expected", fakeOrderBy, orderByCommand.get("$sort")); Whitebox.setInternalState(orderByDBObjectBuilder, "orderByQuery", fakeOrderBy); Whitebox.setInternalState(orderByDBObjectBuilder, "useAggregation", false); orderByCommand = orderByDBObjectBuilder.build(); assertFalse("The orderBy command should not have a root $sort", orderByCommand.keySet().contains("$sort")); assertEquals("The value is not the expected", fakeOrderBy, orderByCommand); }