GroupAction extends Action<GroupRequest> { @Override public ActionResponse execute(GroupRequest parameter) throws QueryStoreException { parameter.setTable(ElasticsearchUtils.getValidTableName(parameter.getTable())); if (null == parameter.getFilters()) { parameter.setFilters(Lists.<Filter>newArrayList(new AnyFilter(parameter.getTable()))); } if (parameter.getTable() == null) { throw new QueryStoreException(QueryStoreException.ErrorCode.INVALID_REQUEST, "Invalid Table"); } try { SearchRequestBuilder query = getConnection().getClient().prepareSearch(ElasticsearchUtils.getIndices( parameter.getTable(), parameter)); TermsBuilder rootBuilder = null; TermsBuilder termsBuilder = null; for (String field : parameter.getNesting()) { if (field == null || field.trim().isEmpty()) { throw new QueryStoreException(QueryStoreException.ErrorCode.INVALID_REQUEST, "Illegal Nesting Parameters"); } if (null == termsBuilder) { termsBuilder = AggregationBuilders.terms(Utils.sanitizeFieldForAggregation(field)).field(field); } else { TermsBuilder tempBuilder = AggregationBuilders.terms(Utils.sanitizeFieldForAggregation(field)).field(field); termsBuilder.subAggregation(tempBuilder); termsBuilder = tempBuilder; } termsBuilder.size(0); if (null == rootBuilder) { rootBuilder = termsBuilder; } } query.setQuery(new ElasticSearchQueryGenerator(FilterCombinerType.and) .genFilter(parameter.getFilters())) .setSearchType(SearchType.COUNT) .addAggregation(rootBuilder); SearchResponse response = query.execute().actionGet(); List<String> fields = parameter.getNesting(); Aggregations aggregations = response.getAggregations(); if (aggregations == null) { logger.error("Null response for Group. Request : " + parameter.toString()); return new GroupResponse(Collections.<String, Object>emptyMap()); } return new GroupResponse(getMap(fields, aggregations)); } catch (QueryStoreException ex) { throw ex; } catch (Exception e) { logger.error("Error running grouping: ", e); throw new QueryStoreException(QueryStoreException.ErrorCode.QUERY_EXECUTION_ERROR, "Error running group query.", e); } } GroupAction(GroupRequest parameter,
                       TableMetadataManager tableMetadataManager,
                       DataStore dataStore,
                       QueryStore queryStore,
                       ElasticsearchConnection connection,
                       String cacheToken); @Override ActionResponse execute(GroupRequest parameter);  }
@Test public void testGroupActionSingleQueryException() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os")); when(elasticsearchServer.getClient()).thenReturn(null); try { queryExecutor.execute(groupRequest); fail(); } catch (QueryStoreException ex) { assertEquals(QueryStoreException.ErrorCode.QUERY_EXECUTION_ERROR, ex.getErrorCode()); } }
@Test public void testGroupActionSingleFieldNoFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os")); Map<String, Object> response = Maps.newHashMap(); response.put("android", 7L); response.put("ios", 4L); GroupResponse actualResult = GroupResponse.class.cast(queryExecutor.execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionSingleFieldSpecialCharacterNoFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("header.data")); Map<String, Object> response = Maps.newHashMap(); response.put("ios", 1L); GroupResponse actualResult = GroupResponse.class.cast(queryExecutor.execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionSingleFieldEmptyFieldNoFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("")); try { queryExecutor.execute(groupRequest); fail(); } catch (QueryStoreException ex) { assertEquals(QueryStoreException.ErrorCode.INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testGroupActionSingleFieldSpecialCharactersNoFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("")); try { queryExecutor.execute(groupRequest); fail(); } catch (QueryStoreException ex) { assertEquals(QueryStoreException.ErrorCode.INVALID_REQUEST, ex.getErrorCode()); } }
@Test public void testGroupActionSingleFieldHavingSpecialCharactersWithFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); EqualsFilter equalsFilter = new EqualsFilter(); equalsFilter.setField("device"); equalsFilter.setValue("nexus"); groupRequest.setFilters(Collections.<Filter>singletonList(equalsFilter)); groupRequest.setNesting(Arrays.asList("!@#$%^&*()")); Map<String, Object> response = Maps.newHashMap(); GroupResponse actualResult = GroupResponse.class.cast(queryExecutor.execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionSingleFieldWithFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); EqualsFilter equalsFilter = new EqualsFilter(); equalsFilter.setField("device"); equalsFilter.setValue("nexus"); groupRequest.setFilters(Collections.<Filter>singletonList(equalsFilter)); groupRequest.setNesting(Arrays.asList("os")); Map<String, Object> response = Maps.newHashMap(); response.put("android", 5L); response.put("ios", 1L); GroupResponse actualResult = GroupResponse.class.cast(queryExecutor.execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionTwoFieldsNoFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os", "device")); Map<String, Object> response = Maps.newHashMap(); response.put("android", new HashMap<String, Object>() {{put("nexus", 5L); put("galaxy", 2L); }}); response.put("ios", new HashMap<String, Object>() {{put("nexus", 1L); put("ipad", 2L); put("iphone", 1L); }}); GroupResponse actualResult = GroupResponse.class.cast(queryExecutor.execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionTwoFieldsWithFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os", "device")); GreaterThanFilter greaterThanFilter = new GreaterThanFilter(); greaterThanFilter.setField("battery"); greaterThanFilter.setValue(48); groupRequest.setFilters(Collections.<Filter>singletonList(greaterThanFilter)); Map<String, Object> response = Maps.newHashMap(); response.put("android", new HashMap<String, Object>() {{put("nexus", 3L); put("galaxy", 2L); }}); response.put("ios", new HashMap<String, Object>() {{ put("ipad", 1L); }}); GroupResponse actualResult = GroupResponse.class.cast(queryExecutor.execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionMultipleFieldsNoFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os", "device", "version")); Map<String, Object> response = Maps.newHashMap(); final Map<String, Object> nexusResponse = new HashMap<String, Object>(){{ put("1", 2L); put("2", 2L); put("3", 1L); }}; final Map<String, Object> galaxyResponse = new HashMap<String, Object>(){{ put("2", 1L); put("3", 1L); }}; response.put("android", new HashMap<String, Object>() {{put("nexus", nexusResponse); put("galaxy", galaxyResponse); }}); final Map<String, Object> nexusResponse2 = new HashMap<String, Object>(){{ put("2", 1L);}}; final Map<String, Object> iPadResponse = new HashMap<String, Object>(){{ put("2", 2L); }}; final Map<String, Object> iPhoneResponse = new HashMap<String, Object>(){{ put("1", 1L); }}; response.put("ios", new HashMap<String, Object>() {{put("nexus", nexusResponse2); put("ipad", iPadResponse); put("iphone", iPhoneResponse); }}); GroupResponse actualResult = GroupResponse.class.cast(queryExecutor.execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionMultipleFieldsWithFilter() throws QueryStoreException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os", "device", "version")); GreaterThanFilter greaterThanFilter = new GreaterThanFilter(); greaterThanFilter.setField("battery"); greaterThanFilter.setValue(48); groupRequest.setFilters(Collections.<Filter>singletonList(greaterThanFilter)); Map<String, Object> response = Maps.newHashMap(); final Map<String, Object> nexusResponse = new HashMap<String, Object>(){{ put("2", 2L); put("3", 1L); }}; final Map<String, Object> galaxyResponse = new HashMap<String, Object>(){{ put("2", 1L); put("3", 1L); }}; response.put("android", new HashMap<String, Object>() {{put("nexus", nexusResponse); put("galaxy", galaxyResponse); }}); final Map<String, Object> iPadResponse = new HashMap<String, Object>(){{ put("2", 1L); }}; response.put("ios", new HashMap<String, Object>() {{ put("ipad", iPadResponse); }}); GroupResponse actualResult = GroupResponse.class.cast(queryExecutor.execute(groupRequest)); assertEquals(response, actualResult.getResult()); }