RepositoryUserAccessService implements UserAccessService { @Override public boolean userHasActiveAccessInterval(User user) { var latestIntervalOptional = user.getLatestAccessInterval(); if(latestIntervalOptional.isPresent()) { var latestInterval = latestIntervalOptional.get(); var startTime = latestInterval.getStartTime(); var endTime = latestInterval.getEndTime(); var now = LocalDateTime.now(clock); return (startTime.isEqual(now) || startTime.isBefore(now)) && (endTime.isEqual(now) || endTime.isAfter(now)); } return false; } @Autowired  RepositoryUserAccessService(RepositoryUserService userService,
                                       AccessIntervalRepository intervalRepo,
                                       UserAccessEventHandler userAccessEventHandler,
                                       Clock clock); @Override User giveUserAccessUntil(User user, LocalDateTime until); @Override User removeAccessFromUser(User user); @Override boolean userHasActiveAccessInterval(User user);  }
@Test public void userHasActiveAccessIntervalReturnsTrueWhenStartDateBeforeAndEndDateAfterToday() { var startDateTime = LocalDateTime.now(clock).minus(1L, ChronoUnit.DAYS); var endDateTime = LocalDateTime.now(clock).plus(1L, ChronoUnit.DAYS); given(userMock.getLatestAccessInterval()).willReturn(Optional.of(accessIntervalMock)); given(accessIntervalMock.getStartTime()).willReturn(startDateTime); given(accessIntervalMock.getEndTime()).willReturn(endDateTime); boolean actualHasAccess = accessService.userHasActiveAccessInterval(userMock); assertThat(actualHasAccess).isTrue(); }
@Test public void userHasActiveAccessIntervalReturnsTrueWhenStartDateBeforeEndDateEqualToToday() { var startDateTime = LocalDateTime.now(clock).minus(1L, ChronoUnit.DAYS); var endDateTime = LocalDateTime.now(clock); given(userMock.getLatestAccessInterval()).willReturn(Optional.of(accessIntervalMock)); given(accessIntervalMock.getStartTime()).willReturn(startDateTime); given(accessIntervalMock.getEndTime()).willReturn(endDateTime); boolean actualHasAccess = accessService.userHasActiveAccessInterval(userMock); assertThat(actualHasAccess).isTrue(); }
@Test public void userHasActiveAccessIntervalReturnsTrueWhenStartDateEqualToAndEndDateAfterToday() { var startDateTime = LocalDateTime.now(clock).minus(1L, ChronoUnit.MINUTES); var endDateTime = LocalDateTime.now(clock).plus(10L, ChronoUnit.DAYS); given(userMock.getLatestAccessInterval()).willReturn(Optional.of(accessIntervalMock)); given(accessIntervalMock.getStartTime()).willReturn(startDateTime); given(accessIntervalMock.getEndTime()).willReturn(endDateTime); boolean actualHasAccess = accessService.userHasActiveAccessInterval(userMock); assertThat(actualHasAccess).isTrue(); }
@Test public void userHasActiveAccessIntervalReturnsFalseWhenStartDateAndEndDateBeforeToday() { var startDateTime = LocalDateTime.now(clock).minus(3L, ChronoUnit.DAYS); var endDateTime = LocalDateTime.now(clock).minus(2L, ChronoUnit.DAYS); given(userMock.getLatestAccessInterval()).willReturn(Optional.of(accessIntervalMock)); given(accessIntervalMock.getStartTime()).willReturn(startDateTime); given(accessIntervalMock.getEndTime()).willReturn(endDateTime); boolean actualHasAccess = accessService.userHasActiveAccessInterval(userMock); assertThat(actualHasAccess).isFalse(); }
@Test public void userHasActiveAccessReturnsFalseWhenNoAccessIntervalPresent() { given(userMock.getLatestAccessInterval()).willReturn(Optional.empty()); boolean actualHasAccess = accessService.userHasActiveAccessInterval(userMock); assertThat(actualHasAccess).isFalse(); }
@Test public void userHasActiveAccessIntervalReturnsFalseWhenStartDateAfterToday() { var startDateTime = LocalDateTime.now(clock).plus(1L, ChronoUnit.DAYS); var endDateTime = LocalDateTime.now(clock).plus(3L, ChronoUnit.DAYS); given(userMock.getLatestAccessInterval()).willReturn(Optional.of(accessIntervalMock)); given(accessIntervalMock.getStartTime()).willReturn(startDateTime); given(accessIntervalMock.getEndTime()).willReturn(endDateTime); boolean actualHasAccess = accessService.userHasActiveAccessInterval(userMock); assertThat(actualHasAccess).isFalse(); }