GroupAction extends Action<GroupRequest> { @Override public ActionResponse execute(GroupRequest parameter) { SearchRequestBuilder query = getRequestBuilder(parameter); try { SearchResponse response = query.execute() .actionGet(getGetQueryTimeout()); return getResponse(response, parameter); } catch (ElasticsearchException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } GroupAction(GroupRequest parameter, AnalyticsLoader analyticsLoader); @Override void preprocess(); @Override String getMetricKey(); @Override String getRequestCacheKey(); @Override void validateImpl(GroupRequest parameter); @Override ActionResponse execute(GroupRequest parameter); @Override SearchRequestBuilder getRequestBuilder(GroupRequest parameter); @Override ActionResponse getResponse(org.elasticsearch.action.ActionResponse response, GroupRequest parameter);  }
@Ignore @Test public void testGroupActionSingleQueryException() throws FoxtrotException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Collections.singletonList("os")); doReturn(null).when(getElasticsearchConnection()) .getClient(); try { getQueryExecutor().execute(groupRequest); fail(); } catch (FoxtrotException ex) { ex.printStackTrace(); assertEquals(ErrorCode.ACTION_EXECUTION_ERROR, ex.getCode()); } }
@Test public void testGroupActionSingleFieldNoFilter() throws FoxtrotException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Collections.singletonList("os")); Map<String, Object> response = Maps.newHashMap(); response.put("android", 7L); response.put("ios", 4L); GroupResponse actualResult = GroupResponse.class.cast(getQueryExecutor().execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionSingleFieldEmptyFieldNoFilter() throws FoxtrotException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Collections.singletonList("")); try { getQueryExecutor().execute(groupRequest); fail(); } catch (FoxtrotException ex) { ex.printStackTrace(); assertEquals(ErrorCode.MALFORMED_QUERY, ex.getCode()); } }
@Test public void testGroupActionSingleFieldSpecialCharactersNoFilter() throws FoxtrotException, JsonProcessingException { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("")); try { getQueryExecutor().execute(groupRequest); fail(); } catch (FoxtrotException ex) { ex.printStackTrace(); assertEquals(ErrorCode.MALFORMED_QUERY, ex.getCode()); } }
@Test public void testGroupActionSingleFieldHavingSpecialCharactersWithFilter() throws FoxtrotException, 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(getQueryExecutor().execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionSingleFieldWithFilter() throws FoxtrotException, 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(getQueryExecutor().execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionTwoFieldsNoFilter() throws FoxtrotException, 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(getQueryExecutor().execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionTwoFieldsWithFilter() throws FoxtrotException, 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(getQueryExecutor().execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionMultipleFieldsNoFilter() throws FoxtrotException, 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(getQueryExecutor().execute(groupRequest)); assertEquals(response, actualResult.getResult()); }
@Test public void testGroupActionMultipleFieldsWithFilter() throws FoxtrotException, 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(getQueryExecutor().execute(groupRequest)); assertEquals(response, actualResult.getResult()); }