TrendAction extends Action<TrendRequest> { @Override public ActionResponse execute(TrendRequest parameter) { SearchRequestBuilder searchRequestBuilder = getRequestBuilder(parameter); try { SearchResponse searchResponse = searchRequestBuilder.execute() .actionGet(getGetQueryTimeout()); return getResponse(searchResponse, parameter); } catch (ElasticsearchException e) { throw FoxtrotExceptions.createQueryExecutionException(parameter, e); } } TrendAction(TrendRequest parameter, AnalyticsLoader analyticsLoader); @Override void preprocess(); @Override String getMetricKey(); @Override String getRequestCacheKey(); @Override void validateImpl(TrendRequest parameter); @Override ActionResponse execute(TrendRequest parameter); @Override SearchRequestBuilder getRequestBuilder(TrendRequest parameter); @Override ActionResponse getResponse(org.elasticsearch.action.ActionResponse response, TrendRequest parameter);  }
@Test public void testTrendActionNullField() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField(null); try { getQueryExecutor().execute(trendRequest); fail("Should have thrown exception"); } catch (FoxtrotException e) { assertEquals(ErrorCode.MALFORMED_QUERY, e.getCode()); } }
@Test public void testTrendActionFieldAll() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField("all"); trendRequest.setValues(Collections.<String>emptyList()); TrendResponse expectedResponse = new TrendResponse(); expectedResponse.setTrends(new HashMap<>()); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionEmptyField() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField(""); trendRequest.setValues(Collections.<String>emptyList()); try { getQueryExecutor().execute(trendRequest); fail(); } catch (FoxtrotException ex) { assertEquals(ErrorCode.MALFORMED_QUERY, ex.getCode()); } }
@Test public void testTrendActionFieldWithSpecialCharacters() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); trendRequest.setField("!@!41242$"); trendRequest.setValues(Collections.<String>emptyList()); TrendResponse expectedResponse = new TrendResponse(); expectedResponse.setTrends(new HashMap<>()); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionNullTable() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(null); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); try { getQueryExecutor().execute(trendRequest); fail(); } catch (FoxtrotException ex) { assertEquals(ErrorCode.MALFORMED_QUERY, ex.getCode()); } }
@Test public void testTrendActionWithField() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = Maps.newHashMap(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397586600000L, 6)); counts.add(new TrendResponse.Count(1398623400000L, 1)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397673000000L, 1)); counts.add(new TrendResponse.Count(1397932200000L, 1)); counts.add(new TrendResponse.Count(1398623400000L, 2)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); filterNonZeroCounts(actualResponse.getTrends() .get("android")); filterNonZeroCounts(actualResponse.getTrends() .get("ios")); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldZeroTo() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = Maps.newHashMap(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397586600000L, 6)); counts.add(new TrendResponse.Count(1398623400000L, 1)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397673000000L, 1)); counts.add(new TrendResponse.Count(1397932200000L, 1)); counts.add(new TrendResponse.Count(1398623400000L, 2)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); filterNonZeroCounts(actualResponse.getTrends() .get("android")); filterNonZeroCounts(actualResponse.getTrends() .get("ios")); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldZeroFrom() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Collections.<Filter>singletonList(betweenFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = Maps.newHashMap(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397586600000L, 6)); counts.add(new TrendResponse.Count(1398623400000L, 1)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397673000000L, 1)); counts.add(new TrendResponse.Count(1397932200000L, 1)); counts.add(new TrendResponse.Count(1398623400000L, 2)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); filterNonZeroCounts(actualResponse.getTrends() .get("android")); filterNonZeroCounts(actualResponse.getTrends() .get("ios")); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldWithValues() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); BetweenFilter betweenFilter = new BetweenFilter(); betweenFilter.setFrom(1L); betweenFilter.setTo(System.currentTimeMillis()); betweenFilter.setTemporal(true); betweenFilter.setField("_timestamp"); trendRequest.setField("os"); trendRequest.setFilters(Lists.<Filter>newArrayList(betweenFilter)); trendRequest.setValues(Arrays.asList("android")); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = Maps.newHashMap(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397586600000L, 6)); counts.add(new TrendResponse.Count(1398623400000L, 1)); trends.put("android", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); filterNonZeroCounts(actualResponse.getTrends() .get("android")); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldWithFilterWithValues() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); trendRequest.setField("os"); trendRequest.setValues(Arrays.asList("android")); EqualsFilter equalsFilter = new EqualsFilter(); equalsFilter.setField("version"); equalsFilter.setValue(1); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); trendRequest.setFilters(Lists.newArrayList(equalsFilter, lessThanFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = Maps.newHashMap(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397586600000L, 2)); trends.put("android", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldWithFilter() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); trendRequest.setField("os"); EqualsFilter equalsFilter = new EqualsFilter(); equalsFilter.setField("version"); equalsFilter.setValue(1); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); trendRequest.setFilters(Lists.newArrayList(equalsFilter, lessThanFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = Maps.newHashMap(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397586600000L, 2)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397673000000L, 1)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }
@Test public void testTrendActionWithFieldWithFilterWithInterval() throws FoxtrotException, JsonProcessingException { TrendRequest trendRequest = new TrendRequest(); trendRequest.setTable(TestUtils.TEST_TABLE_NAME); trendRequest.setField("os"); trendRequest.setPeriod(Period.days); EqualsFilter equalsFilter = new EqualsFilter(); equalsFilter.setField("version"); equalsFilter.setValue(1); LessThanFilter lessThanFilter = new LessThanFilter(); lessThanFilter.setTemporal(true); lessThanFilter.setField("_timestamp"); lessThanFilter.setValue(System.currentTimeMillis()); trendRequest.setFilters(Lists.newArrayList(equalsFilter, lessThanFilter)); TrendResponse expectedResponse = new TrendResponse(); Map<String, List<TrendResponse.Count>> trends = Maps.newHashMap(); List<TrendResponse.Count> counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397586600000L, 2)); trends.put("android", counts); counts = Lists.newArrayList(); counts.add(new TrendResponse.Count(1397673000000L, 1)); trends.put("ios", counts); expectedResponse.setTrends(trends); TrendResponse actualResponse = TrendResponse.class.cast(getQueryExecutor().execute(trendRequest)); assertEquals(expectedResponse, actualResponse); }