@Test public void giveUserAccessUntilCreatesAccessWhenUserHasNoAccessInstance() { var expectedStartTime = LocalDateTime.now(clock); var expectedEndTime = LocalDateTime.now(clock).plus(10L , ChronoUnit.DAYS); given(userMock.getLatestAccessInterval()).willReturn(Optional.empty()); given(userService.save(userMock)).willReturn(userMock); var actualUser = accessService.giveUserAccessUntil(userMock, expectedEndTime); assertThat(actualUser).isEqualTo(userMock); var accessInfoArgumentCaptor = ArgumentCaptor.forClass(AccessInterval.class); verify(userMock, times(2)).addAccessInterval(accessInfoArgumentCaptor.capture()); var createdAccessInterval = accessInfoArgumentCaptor.getValue(); var softly = new SoftAssertions(); softly.assertThat(createdAccessInterval.getUser()).isEqualTo(userMock); softly.assertThat(createdAccessInterval.getStartTime()).isEqualTo(expectedStartTime); softly.assertThat(createdAccessInterval.getEndTime()).isEqualTo(expectedEndTime); softly.assertAll(); verify(userService).save(userMock); verify(userAccessEventHandlerMock).onAccessCreated(userMock, createdAccessInterval); }
@Override public User giveUserAccessUntil(User user, LocalDateTime until) throws IllegalArgumentException { if(until.isBefore(LocalDateTime.now(clock))) { throw new IllegalArgumentException("End date must lie in the future!"); } var latestIntervalOptional = user.getLatestAccessInterval(); if(latestIntervalOptional.isEmpty() || !userHasActiveAccessInterval(user)) { var interval = new AccessInterval(user, LocalDateTime.now(clock), until); user.addAccessInterval(interval); userAccessEventHandler.onAccessCreated(user, interval); return userService.save(user); } else { var latestInterval = latestIntervalOptional.get(); var oldEndTime = latestInterval.getEndTime(); if(oldEndTime.isEqual(until)) { return user; } latestInterval.setEndTime(until); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessChanged(user, latestInterval, oldEndTime); } return user; }
RepositoryUserAccessService implements UserAccessService { @Override public User giveUserAccessUntil(User user, LocalDateTime until) throws IllegalArgumentException { if(until.isBefore(LocalDateTime.now(clock))) { throw new IllegalArgumentException("End date must lie in the future!"); } var latestIntervalOptional = user.getLatestAccessInterval(); if(latestIntervalOptional.isEmpty() || !userHasActiveAccessInterval(user)) { var interval = new AccessInterval(user, LocalDateTime.now(clock), until); user.addAccessInterval(interval); userAccessEventHandler.onAccessCreated(user, interval); return userService.save(user); } else { var latestInterval = latestIntervalOptional.get(); var oldEndTime = latestInterval.getEndTime(); if(oldEndTime.isEqual(until)) { return user; } latestInterval.setEndTime(until); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessChanged(user, latestInterval, oldEndTime); } return user; } }
RepositoryUserAccessService implements UserAccessService { @Override public User giveUserAccessUntil(User user, LocalDateTime until) throws IllegalArgumentException { if(until.isBefore(LocalDateTime.now(clock))) { throw new IllegalArgumentException("End date must lie in the future!"); } var latestIntervalOptional = user.getLatestAccessInterval(); if(latestIntervalOptional.isEmpty() || !userHasActiveAccessInterval(user)) { var interval = new AccessInterval(user, LocalDateTime.now(clock), until); user.addAccessInterval(interval); userAccessEventHandler.onAccessCreated(user, interval); return userService.save(user); } else { var latestInterval = latestIntervalOptional.get(); var oldEndTime = latestInterval.getEndTime(); if(oldEndTime.isEqual(until)) { return user; } latestInterval.setEndTime(until); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessChanged(user, latestInterval, oldEndTime); } return user; } @Autowired  RepositoryUserAccessService(RepositoryUserService userService,
                                       AccessIntervalRepository intervalRepo,
                                       UserAccessEventHandler userAccessEventHandler,
                                       Clock clock); }
RepositoryUserAccessService implements UserAccessService { @Override public User giveUserAccessUntil(User user, LocalDateTime until) throws IllegalArgumentException { if(until.isBefore(LocalDateTime.now(clock))) { throw new IllegalArgumentException("End date must lie in the future!"); } var latestIntervalOptional = user.getLatestAccessInterval(); if(latestIntervalOptional.isEmpty() || !userHasActiveAccessInterval(user)) { var interval = new AccessInterval(user, LocalDateTime.now(clock), until); user.addAccessInterval(interval); userAccessEventHandler.onAccessCreated(user, interval); return userService.save(user); } else { var latestInterval = latestIntervalOptional.get(); var oldEndTime = latestInterval.getEndTime(); if(oldEndTime.isEqual(until)) { return user; } latestInterval.setEndTime(until); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessChanged(user, latestInterval, oldEndTime); } 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 giveUserAccessUntil(User user, LocalDateTime until) throws IllegalArgumentException { if(until.isBefore(LocalDateTime.now(clock))) { throw new IllegalArgumentException("End date must lie in the future!"); } var latestIntervalOptional = user.getLatestAccessInterval(); if(latestIntervalOptional.isEmpty() || !userHasActiveAccessInterval(user)) { var interval = new AccessInterval(user, LocalDateTime.now(clock), until); user.addAccessInterval(interval); userAccessEventHandler.onAccessCreated(user, interval); return userService.save(user); } else { var latestInterval = latestIntervalOptional.get(); var oldEndTime = latestInterval.getEndTime(); if(oldEndTime.isEqual(until)) { return user; } latestInterval.setEndTime(until); intervalRepo.save(latestInterval); userAccessEventHandler.onAccessChanged(user, latestInterval, oldEndTime); } 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);  }