User { public Optional<AccessInterval> getLatestAccessInterval() { if(accessIntervals.size() > 0) { return Optional.of(accessIntervals.get(accessIntervals.size() - 1)); } return Optional.empty(); } User(String userId); protected  User(); void addBookmark(Project project); void removeBookmark(Project project); void addApplication(ProjectApplication application); void removeApplication(ProjectApplication application); Optional<AccessInterval> getLatestAccessInterval(); void addAccessInterval(AccessInterval interval); void removeAccessInterval(AccessInterval interval);  }
@Test public void getLatestAccessIntervalReturnsElementCollectionContainsSingleElement() { User user = new User("user"); user.accessIntervals.add(accessIntervalMock); AccessInterval actualAccessInterval = user.getLatestAccessInterval() .orElseThrow(() -> new IllegalStateException("No interval instance present!")); assertThat(actualAccessInterval).isEqualTo(accessIntervalMock); }
@Test public void getLatestAccessIntervalReturnsLastAddedWhenCollectionContainsMultipleElements() { AccessInterval expectedAccessIntervalMock = mock(AccessInterval.class); User user = new User("user"); user.accessIntervals.addAll(Arrays.asList(accessIntervalMock, expectedAccessIntervalMock)); AccessInterval actualAccessInterval = user.getLatestAccessInterval() .orElseThrow(() -> new IllegalStateException("No interval instance present!")); assertThat(actualAccessInterval).isEqualTo(expectedAccessIntervalMock); }
@Test public void getLatestAccessIntervalReturnsNullWhenCollectionContainsNoElement() { User user = new User("user"); Optional<AccessInterval> actualAccessIntervalOptional = user.getLatestAccessInterval(); assertThat(actualAccessIntervalOptional).isNotPresent(); }