### Question:
GroupByElement extends SqlQueryBuilderElement { public static Optional<GroupByElement> createGroupByElement(SqlQueryBuilderAccess queryBuilderAccess) { final QueryParameter queryParameter = queryBuilderAccess.getQueryParameter(); if (queryParameter.getGroups().isEmpty()) { return Optional.absent(); } GroupByElement groupByElement = new GroupByElement(queryBuilderAccess); final SchemaDefinition starSchemaConfig = queryBuilderAccess.getStarSchemaConfig(); for (Pair<String, String> group : queryParameter.getGroups()) { if (!StringUtils.isBlank(group.getLeft())) { DimensionTable dim = starSchemaConfig.getDimensionByName(group.getLeft(), queryParameter.getTimezoneName()); if (dim != null) { groupByElement.addDimension(dim, group.getRight()); } else { if (LOG.isWarnEnabled()) { LOG.warn("Dimension named " + group.getLeft() + " not exist."); } } } } if (groupByElement.groupingColumns.isEmpty()) { throw new IllegalArgumentException("No valid groups"); } return Optional.of(groupByElement); } private  GroupByElement(SqlQueryBuilderAccess queryBuilderAccess); @Override Collection<? extends SqlTable> getSqlTables(); StringBuilder getSqlString(); List<Pair<AbstractSqlColumn, List<AbstractSqlColumn>>> getGroupingColumns(); static Optional<GroupByElement> createGroupByElement(SqlQueryBuilderAccess queryBuilderAccess);  }### Answer:
@Test public void testCreateGroupByElementIsAbsent() throws Exception { new Expectations() { { queryBuilderAccess.getQueryParameter(); result = queryParameter; } }; Optional<GroupByElement> groupByElement = GroupByElement.createGroupByElement(queryBuilderAccess); assertFalse(groupByElement.isPresent()); }
@Test public void testCreateGroupByElement() throws Exception { final DimensionTable dimensionTable = new DimensionTable("testDimension", "testDimensionName"); dimensionTable.addTableColumn("testColumn", ColumnDataType.STRING, "TESTINFONAME"); dimensionTable.addTableColumn("testColumn2", ColumnDataType.STRING, "TESTINFONAME2"); final SelectElement selectElement = new SelectElement(queryBuilderAccess); new Expectations(selectElement) { { queryBuilderAccess.getQueryParameter(); result = queryParameter; queryParameter.getGroups(); result = Lists.newArrayList(Pair.of("TESTDIMENSIONNAME", "TESTINFONAME"), Pair.of("TESTDIMENSIONNAME", "TESTINFONAME2")); queryBuilderAccess.getStarSchemaConfig(); result = starSchemaConfig; queryParameter.getGroups(); result = Lists.newArrayList(Pair.of("TESTDIMENSIONNAME", "TESTINFONAME"), Pair.of("TESTDIMENSIONNAME", "TESTINFONAME2")); queryParameter.getTimezoneName(); result = "UTC"; starSchemaConfig.getDimensionByName("TESTDIMENSIONNAME", "UTC"); result = dimensionTable; queryParameter.getTimezoneName(); result = "UTC"; starSchemaConfig.getDimensionByName("TESTDIMENSIONNAME", "UTC"); result = dimensionTable; } }; Optional<GroupByElement> groupByElement = GroupByElement.createGroupByElement(queryBuilderAccess); assertTrue(groupByElement.isPresent()); assertEquals(groupByElement.get().getSqlTables().size(), 1); assertEquals(groupByElement.get().getSqlString().toString(), "testDimension.testColumn, testDimension.testColumn2"); }