@Test public void removeAccessFromUserActiveAccessIntervalPresent() { var activeStartTime = LocalDateTime.now(clock).minus(10L, ChronoUnit.DAYS); var activeEndTime = LocalDateTime.now(clock).plus(1L, ChronoUnit.WEEKS); var expectedEndTime = LocalDateTime.now(clock); given(userMock.getLatestAccessInterval()).willReturn(Optional.of(accessIntervalMock)); given(accessIntervalMock.getStartTime()).willReturn(activeStartTime); given(accessIntervalMock.getEndTime()).willReturn(activeEndTime); var actualUser = accessService.removeAccessFromUser(userMock); assertThat(actualUser).isEqualTo(userMock); verify(accessIntervalMock).setEndTime(expectedEndTime); verify(intervalRepo).save(accessIntervalMock); verify(userAccessEventHandlerMock).onAccessRevoked(userMock, activeEndTime); }
@Override public User removeAccessFromUser(User user) { if(userHasActiveAccessInterval(user)) { var latestInterval = user.getLatestAccessInterval() .orElseThrow(() -> new IllegalStateException("No interval instance present!")); var previousEndTime = latestInterval.getEndTime(); latestInterval.setEndTime(LocalDateTime.now(clock)); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessRevoked(user, previousEndTime); } return user; }
RepositoryUserAccessService implements UserAccessService { @Override public User removeAccessFromUser(User user) { if(userHasActiveAccessInterval(user)) { var latestInterval = user.getLatestAccessInterval() .orElseThrow(() -> new IllegalStateException("No interval instance present!")); var previousEndTime = latestInterval.getEndTime(); latestInterval.setEndTime(LocalDateTime.now(clock)); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessRevoked(user, previousEndTime); } return user; } }
RepositoryUserAccessService implements UserAccessService { @Override public User removeAccessFromUser(User user) { if(userHasActiveAccessInterval(user)) { var latestInterval = user.getLatestAccessInterval() .orElseThrow(() -> new IllegalStateException("No interval instance present!")); var previousEndTime = latestInterval.getEndTime(); latestInterval.setEndTime(LocalDateTime.now(clock)); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessRevoked(user, previousEndTime); } return user; } @Autowired  RepositoryUserAccessService(RepositoryUserService userService,
                                       AccessIntervalRepository intervalRepo,
                                       UserAccessEventHandler userAccessEventHandler,
                                       Clock clock); }
RepositoryUserAccessService implements UserAccessService { @Override public User removeAccessFromUser(User user) { if(userHasActiveAccessInterval(user)) { var latestInterval = user.getLatestAccessInterval() .orElseThrow(() -> new IllegalStateException("No interval instance present!")); var previousEndTime = latestInterval.getEndTime(); latestInterval.setEndTime(LocalDateTime.now(clock)); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessRevoked(user, previousEndTime); } return user; } @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); }
RepositoryUserAccessService implements UserAccessService { @Override public User removeAccessFromUser(User user) { if(userHasActiveAccessInterval(user)) { var latestInterval = user.getLatestAccessInterval() .orElseThrow(() -> new IllegalStateException("No interval instance present!")); var previousEndTime = latestInterval.getEndTime(); latestInterval.setEndTime(LocalDateTime.now(clock)); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessRevoked(user, previousEndTime); } return user; } @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);  }