CommentRepositoryImpl implements CommentRepositoryCustom { @Override public List<BarChartStatisticsResult> countCommentsGroupByProject(int projectCount) { final String map = "function(){ emit(this.project.$id , 1 ); } "; final String reduce = "function(key,values){ return values.length;}"; MapReduceResults<KeyValuePair> results = mongoTemplate.mapReduce(COLLECTION_NAME, map, reduce, KeyValuePair.class); final List<KeyValuePair> sortedList = new ArrayList<>(); for (KeyValuePair result : results) { sortedList.add(result); } Collections.sort(sortedList); List<BarChartStatisticsResult> res = new ArrayList<>(projectCount); for (KeyValuePair kv : sortedList) { ProjectEntity project = mongoTemplate.findOne(new Query(Criteria.where("id").is(kv.getId())), ProjectEntity.class); if (project != null) { res.add(new BarChartStatisticsResult(kv.getId(), project.getTitle(), kv.getValue())); } if (res.size() >= projectCount) { break; } } return res; } @Autowired  CommentRepositoryImpl(MongoTemplate mongoTemplate); @Override List<BarChartStatisticsResult> countCommentsGroupByProject(int projectCount); @Override LineChartStatisticsResult sumCommentsGroupByCreatedDate(TimeRangedStatisticsRequest request);  }
@Test public void countCommentsGroupByProject_should_sort_result() { CommentRepositoryImpl.KeyValuePair mockResult1 = new CommentRepositoryImpl.KeyValuePair("test_a", 1L); CommentRepositoryImpl.KeyValuePair mockResult2 = new CommentRepositoryImpl.KeyValuePair("test_b", 100L); CommentRepositoryImpl.KeyValuePair mockResult3 = new CommentRepositoryImpl.KeyValuePair("test_c", 10L); when(mockIterator.hasNext()).thenReturn(true, true, true, false); when(mockIterator.next()).thenReturn(mockResult1, mockResult2,mockResult3); when(mongoTemplate.findOne(any(Query.class), eq(ProjectEntity.class))).thenReturn(new ProjectEntity()); List<BarChartStatisticsResult> result = commentRepository.countCommentsGroupByProject(5); assertThat(result.get(0).getCount(), is(100L)); assertThat(result.get(1).getCount(), is(10L)); assertThat(result.get(2).getCount(), is(1L)); }
@Test public void countCommentsGroupByProject_should_limit_result() { CommentRepositoryImpl.KeyValuePair mockResult1 = new CommentRepositoryImpl.KeyValuePair("test_a", 1L); CommentRepositoryImpl.KeyValuePair mockResult2 = new CommentRepositoryImpl.KeyValuePair("test_b", 100L); CommentRepositoryImpl.KeyValuePair mockResult3 = new CommentRepositoryImpl.KeyValuePair("test_c", 10L); CommentRepositoryImpl.KeyValuePair mockResult4 = new CommentRepositoryImpl.KeyValuePair("test_e", 18L); CommentRepositoryImpl.KeyValuePair mockResult5 = new CommentRepositoryImpl.KeyValuePair("test_f", 17L); CommentRepositoryImpl.KeyValuePair mockResult6 = new CommentRepositoryImpl.KeyValuePair("test_g", 21L); when(mockIterator.hasNext()).thenReturn(true, true, true, true, true, true, false); when(mockIterator.next()).thenReturn(mockResult1, mockResult2,mockResult3, mockResult4, mockResult5, mockResult6); when(mongoTemplate.findOne(any(Query.class), eq(ProjectEntity.class))).thenReturn(new ProjectEntity()); List<BarChartStatisticsResult> result = commentRepository.countCommentsGroupByProject(5); assertThat(result.size(), is(5)); assertThat(result.get(0).getCount(), is(100L)); assertThat(result.get(1).getCount(), is(21L)); assertThat(result.get(2).getCount(), is(18L)); assertThat(result.get(3).getCount(), is(17L)); assertThat(result.get(4).getCount(), is(10L)); }