RegisteredUserSumAction { public Future<LineChartStatisticsResult> getCountOfRegisteredUsersByTimeRange(TimeRangedStatisticsRequest request) { final List<UserEntity> userEntityList = userRepository.findByCreatedDateBetween(request.getStartDate(), request.getEndDate()); final Map<String, Long> map = userEntityList.stream().collect(Collectors.groupingBy( p -> formatDate(p.getCreatedDate()), Collectors.reducing( 0L, t -> 1L, Long::sum ) )); final Map<String, Long> resultMap = fillMap( getDefaultMap(request), map ); final LineChartStatisticsResult result = new LineChartStatisticsResult(SUM_REGISTERED_USER.getDisplayName(), resultMap); return new AsyncResult<>(result); } @Autowired  RegisteredUserSumAction(UserRepository userRepository); Future<LineChartStatisticsResult> getCountOfRegisteredUsersByTimeRange(TimeRangedStatisticsRequest request);  }
@Test public void getCountOfRegisteredUsersByTimeRange_provide_correct_name_of_statistic() throws Exception { final DateTime startDate = DateTime.now().minusDays(10); final DateTime endDate = DateTime.now(); Future<LineChartStatisticsResult> result = instance.getCountOfRegisteredUsersByTimeRange(new TimeRangedStatisticsRequest(startDate, endDate)); verify(userRepository).findByCreatedDateBetween(startDate.withTimeAtStartOfDay(), endDate.plusDays(1).withTimeAtStartOfDay()); assertThat(result.get().getName(), is(SUM_REGISTERED_USER.getDisplayName())); }
@Test public void getCountOfRegisteredUsersByTimeRange_provide_correct_sum_all() throws Exception { final DateTime startDate = DateTime.now().minusDays(9); final DateTime endDate = DateTime.now(); Future<LineChartStatisticsResult> result = instance.getCountOfRegisteredUsersByTimeRange(new TimeRangedStatisticsRequest(startDate, endDate)); verify(userRepository).findByCreatedDateBetween(startDate.withTimeAtStartOfDay(), endDate.plusDays(1).withTimeAtStartOfDay()); assertThat(result.get().getData().size(), is(10)); assertThat(result.get().getData().stream().map(e -> e.getData()).reduce(0L, Long::sum), is(10L)); }
@Test public void getCountOfRegisteredUsersByTimeRange_provide_correct_sum_empty() throws Exception { final DateTime startDate = DateTime.now().minusDays(20); final DateTime endDate = DateTime.now().minusDays(11); when(userRepository.findByCreatedDateBetween(eq(startDate.withTimeAtStartOfDay()), eq(endDate.plusDays(1).withTimeAtStartOfDay()))).thenReturn(Collections.emptyList()); Future<LineChartStatisticsResult> result = instance.getCountOfRegisteredUsersByTimeRange(new TimeRangedStatisticsRequest(startDate, endDate)); verify(userRepository).findByCreatedDateBetween(startDate.withTimeAtStartOfDay(), endDate.plusDays(1).withTimeAtStartOfDay()); assertThat(result.get().getData().stream().map(e -> e.getData()).reduce(0L, Long::sum), is(0L)); }
@Test public void getCountOfRegisteredUsersByTimeRange_provide_correct_sum_on_same_day() throws Exception { final DateTime startDate = DateTime.now().minusDays(1); UserEntity entity = new UserEntity(); entity.setCreatedDate(DateTime.now().minusDays(1)); when(userRepository.findByCreatedDateBetween(eq(startDate.withTimeAtStartOfDay()), any(DateTime.class))).thenReturn(Arrays.asList(entity, entity, entity)); Future<LineChartStatisticsResult> result = instance.getCountOfRegisteredUsersByTimeRange(new TimeRangedStatisticsRequest(startDate, startDate)); verify(userRepository).findByCreatedDateBetween(startDate.withTimeAtStartOfDay(), startDate.plusDays(1).withTimeAtStartOfDay()); assertThat(result.get().getData().size(), is(1)); assertThat(result.get().getData().stream().map(e -> e.getData()).findFirst().get(), is(3L)); }