GroupByDBObjectBuilder extends DBObjectBuilder { public GroupByDBObjectBuilder(GroupBy groupBy, Set<Selector> selectColumns) throws ExecutionException { super(true); groupQuery = new BasicDBObject(); String identifier; List<Selector> ids = groupBy.getIds(); if (ids.size() == 1) { identifier = (String) SelectorHelper.getRestrictedValue(ids.get(0), SelectorType.COLUMN); groupQuery.put("_id", "$" + identifier); } else if (ids.size() > 1) { BasicDBObject groupFields = new BasicDBObject(); for (Selector selector : ids) { identifier = (String) SelectorHelper.getRestrictedValue(selector, SelectorType.COLUMN); groupFields.put(identifier, "$" + identifier); } groupQuery.put("_id", groupFields); } for (Selector colSelector : selectColumns) { String columnName = (String) SelectorHelper.getRestrictedValue(colSelector, SelectorType.COLUMN); groupQuery.put(columnName, new BasicDBObject("$first", "$" + columnName)); } } GroupByDBObjectBuilder(GroupBy groupBy, Set<Selector> selectColumns); DBObject build();  }
@Test public void groupByDBObjectBuilderTest() throws MongoValidationException, ExecutionException { Set<Selector> colSelected = new HashSet<Selector>(Arrays.asList(new ColumnSelector(new ColumnName(CATALOG, TABLE, SELECTED_FIELD)))); GroupBy groupBy = getGroupBy(GROUP_COLUMN); GroupByDBObjectBuilder groupByDBObjectBuilder = new GroupByDBObjectBuilder(groupBy, colSelected); assertTrue("useAggregations should be true", (Boolean) Whitebox.getInternalState(groupByDBObjectBuilder, "useAggregation")); DBObject aggregationOperation = new BasicDBObject("$first", "$" + SELECTED_FIELD); DBObject internalProjectDBObject = (DBObject) Whitebox.getInternalState(groupByDBObjectBuilder, "groupQuery"); Assert.assertEquals("The groupby command is not the expected", "$" + GROUP_COLUMN, internalProjectDBObject.get("_id")); Assert.assertEquals("The groupby operand is not the expected", aggregationOperation, internalProjectDBObject.get(SELECTED_FIELD)); groupBy = getGroupBy(GROUP_COLUMN, GROUP_COLUMN_SECONDARY); groupByDBObjectBuilder = new GroupByDBObjectBuilder(groupBy, colSelected); groupByDBObjectBuilder = new GroupByDBObjectBuilder(groupBy, colSelected); assertTrue("useAggregations should be true", (Boolean) Whitebox.getInternalState(groupByDBObjectBuilder, "useAggregation")); aggregationOperation = new BasicDBObject("$first", "$" + SELECTED_FIELD); DBObject groupColumnsFields = new BasicDBObject(GROUP_COLUMN, "$" + GROUP_COLUMN); groupColumnsFields.put(GROUP_COLUMN_SECONDARY, "$" + GROUP_COLUMN_SECONDARY); internalProjectDBObject = (DBObject) Whitebox.getInternalState(groupByDBObjectBuilder, "groupQuery"); Assert.assertEquals("The groupby command is not the expected", groupColumnsFields, internalProjectDBObject.get("_id")); Assert.assertEquals("The groupby operand is not the expected", aggregationOperation, internalProjectDBObject.get(SELECTED_FIELD)); }