CommentRepositoryImpl implements CommentRepositoryCustom { @Override public LineChartStatisticsResult sumCommentsGroupByCreatedDate(TimeRangedStatisticsRequest request) { String map = "function () { " + " var day = Date.UTC(this.createdDate.getFullYear(), this.createdDate.getMonth(), this.createdDate.getDate()); " + " emit(day.toString(), 1); " + "}"; final String reduce = "function(key,values){ return values.length;}"; Query filter = Query.query(Criteria.where("createdDate").gte(request.getStartDate()).lte(request.getEndDate())); MapReduceResults<KeyValuePair> sumResults = mongoTemplate.mapReduce( filter, COLLECTION_NAME, map, reduce, KeyValuePair.class ); return toLineChartStatisticsResult(sumResults, request); } @Autowired  CommentRepositoryImpl(MongoTemplate mongoTemplate); @Override List<BarChartStatisticsResult> countCommentsGroupByProject(int projectCount); @Override LineChartStatisticsResult sumCommentsGroupByCreatedDate(TimeRangedStatisticsRequest request);  }
@Test public void sumCommentsGroupByCreatedDate_no_data_from_DB_should_return_linechart_instance_with_dates_of_timespan_and_zeros() throws Exception { DateTime startDate = DateTime.now(DateTimeZone.UTC).minusDays(1); DateTime endDate = DateTime.now(DateTimeZone.UTC); LineChartStatisticsResult result = commentRepository.sumCommentsGroupByCreatedDate( new TimeRangedStatisticsRequest(startDate, endDate)); verify(mockIterator).hasNext(); assertThat(result.getName(), is(CHART_NAME_SUM_COMMENTS)); assertThat(result.getData().size(), is(2)); assertThat(result.getData().get(0).getLabel(), is(StatisticsActionUtil.formatDate(startDate))); assertThat(result.getData().get(0).getData(), is(0L)); assertThat(result.getData().get(1).getLabel(), is(StatisticsActionUtil.formatDate(endDate))); assertThat(result.getData().get(1).getData(), is(0L)); }
@Test public void sumCommentsGroupByCreatedDate_should_call_mongo_with_timerange_query() throws Exception { DateTime startDate = DateTime.now(DateTimeZone.UTC).minusDays(1); DateTime endDate = DateTime.now(DateTimeZone.UTC); ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class); when(mongoTemplate.mapReduce(captor.capture(), eq(COLLECTION_NAME), anyString(), anyString(), eq(CommentRepositoryImpl.KeyValuePair.class))) .thenReturn(mockedMapReduceResult); commentRepository.sumCommentsGroupByCreatedDate(new TimeRangedStatisticsRequest(startDate, endDate)); Query capturedTimeFilterParam = captor.getValue(); DBObject capturedQuery = capturedTimeFilterParam.getQueryObject(); assertThat(capturedQuery.get("createdDate"), instanceOf(BasicDBObject.class)); BasicDBObject createdDateFromQuery = (BasicDBObject) capturedQuery.get("createdDate"); assertThat(createdDateFromQuery.get("$gte"), instanceOf(DateTime.class)); assertThat(createdDateFromQuery.get("$lte"), instanceOf(DateTime.class)); assertThat( ((DateTime) createdDateFromQuery.get("$gte")).getMillis(), is(startDate.withTimeAtStartOfDay().getMillis())); assertThat( ((DateTime) createdDateFromQuery.get("$lte")).getMillis(), is(endDate.withTimeAtStartOfDay().plusDays(1).getMillis())); }
@Test public void sumCommentsGroupByCreatedDate_should_map_results_into_linechart_representation_and_fillNonDefinedDays() { DateTime startDate = DateTime.now(DateTimeZone.UTC).minusDays(5); DateTime existingResDate_0 = startDate.plusDays(1); DateTime existingResDate_1 = startDate.plusDays(3); DateTime endDate = DateTime.now(DateTimeZone.UTC); CommentRepositoryImpl.KeyValuePair mockResult1 = new CommentRepositoryImpl.KeyValuePair("" + existingResDate_0.getMillis(), 3L); CommentRepositoryImpl.KeyValuePair mockResult2 = new CommentRepositoryImpl.KeyValuePair("" + existingResDate_1.getMillis(), 1L); when(mockIterator.hasNext()).thenReturn(true, true, false); when(mockIterator.next()).thenReturn(mockResult1, mockResult2); LineChartStatisticsResult result = commentRepository.sumCommentsGroupByCreatedDate(new TimeRangedStatisticsRequest(startDate, endDate)); verify(mockIterator, times(3)).hasNext(); verify(mockIterator, times(2)).next(); assertThat(result.getName(), is(CHART_NAME_SUM_COMMENTS)); assertThat(result.getData().size(), is(6)); assertThat(result.getData().get(0).getLabel(), is(StatisticsActionUtil.formatDate(startDate))); assertThat(result.getData().get(1).getLabel(), is(StatisticsActionUtil.formatDate(existingResDate_0))); assertThat(result.getData().get(2).getLabel(), is(StatisticsActionUtil.formatDate(existingResDate_0.plusDays(1)))); assertThat(result.getData().get(3).getLabel(), is(StatisticsActionUtil.formatDate(existingResDate_1))); assertThat(result.getData().get(4).getLabel(), is(StatisticsActionUtil.formatDate(existingResDate_1.plusDays(1)))); assertThat(result.getData().get(5).getLabel(), is(StatisticsActionUtil.formatDate(endDate))); assertThat(result.getData().get(1).getData(), is(mockResult1.getValue())); assertThat(result.getData().get(3).getData(), is(mockResult2.getValue())); assertThat(result.getData().get(0).getData(), is(0L)); assertThat(result.getData().get(2).getData(), is(0L)); assertThat(result.getData().get(4).getData(), is(0L)); assertThat(result.getData().get(5).getData(), is(0L)); }
@Test(expected = IllegalStateException.class) public void sumCommentsGroupByCreatedDate_should_throw_exception_on_unparseable_data_from_db() { startDate = DateTime.now().minusDays(1); endDate = DateTime.now(); CommentRepositoryImpl.KeyValuePair mockResult = new CommentRepositoryImpl.KeyValuePair("test_abc", 1L); when(mockIterator.hasNext()).thenReturn(true, false); when(mockIterator.next()).thenReturn(mockResult); commentRepository.sumCommentsGroupByCreatedDate(new TimeRangedStatisticsRequest(startDate, endDate)); }