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);  }
@Test public void removeAccessFromUserNoActiveAccessIntervalPresent() { var inactiveStartTime = LocalDateTime.now(clock).minus(10L, ChronoUnit.DAYS); var inactiveEndTime = LocalDateTime.now(clock).minus(1L, ChronoUnit.DAYS); given(userMock.getLatestAccessInterval()).willReturn(Optional.of(accessIntervalMock)); given(accessIntervalMock.getStartTime()).willReturn(inactiveStartTime); given(accessIntervalMock.getEndTime()).willReturn(inactiveEndTime); var actualUser = accessService.removeAccessFromUser(userMock); assertThat(actualUser).isEqualTo(userMock); verify(accessIntervalMock, never()).setEndTime(any()); verify(userAccessEventHandlerMock, never()).onAccessCreated(any(), any()); }
@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); }