AccessInterval { @Override public boolean equals(Object obj) { if(this == obj) { return true; } if(obj instanceof AccessInterval) { AccessInterval other = (AccessInterval) obj; boolean userEquals; if(Objects.nonNull(this.user) && Objects.nonNull(other.user)) { userEquals = Objects.equals(this.user.getId(), other.user.getId()); } else { userEquals = Objects.isNull(this.user) && Objects.isNull(other.user); } return userEquals && Objects.equals(this.id, other.id) && Objects.equals(this.startTime, other.startTime) && Objects.equals(this.endTime, other.endTime); } return false; } AccessInterval(User user, LocalDateTime startTime, LocalDateTime endTime); @Override boolean equals(Object obj); @Override int hashCode();  }
@Test public void equalsReturnsTrueForSameInstance() { var accessIntervalId = 1L; var startTime = LocalDateTime.now(clock); var endTime = LocalDateTime.now(clock).plus(1L, ChronoUnit.DAYS); var accessInterval = new AccessInterval(userMock, startTime, endTime); accessInterval.id = accessIntervalId; boolean actualEquals = accessInterval.equals(accessInterval); assertThat(actualEquals).isTrue(); }
@Test public void equalsReturnsFalseForDifferentIds() { var accessIntervalId = 1L; var otherAccessIntervalId = 2L; var userId = "user"; var startTime = LocalDateTime.now(clock); var endTime = LocalDateTime.now(clock).plus(1L, ChronoUnit.DAYS); given(userMock.getId()).willReturn(userId); var accessInterval = new AccessInterval(userMock, startTime, endTime); accessInterval.id = accessIntervalId; var otherAccessInterval = new AccessInterval(userMock, startTime, endTime); otherAccessInterval.id = otherAccessIntervalId; boolean actualEquals = accessInterval.equals(otherAccessInterval); assertThat(actualEquals).isFalse(); }
@Test public void equalsReturnsFalseForDifferentUserIds() { var accessIntervalId = 1L; var userId = "user"; var otherUserId = "other-user"; var startTime = LocalDateTime.now(clock); var endTime = LocalDateTime.now(clock).plus(1L, ChronoUnit.DAYS); given(userMock.getId()).willReturn(userId); given(otherUserMock.getId()).willReturn(otherUserId); var accessInterval = new AccessInterval(userMock, startTime, endTime); accessInterval.id = accessIntervalId; var otherAccessInterval = new AccessInterval(otherUserMock, startTime, endTime); otherAccessInterval.id = accessIntervalId; boolean actualEquals = accessInterval.equals(otherAccessInterval); assertThat(actualEquals).isFalse(); }
@Test public void equalsReturnsFalseForDifferentStartTimes() { var accessIntervalId = 1L; var userId = "user"; var startTime = LocalDateTime.now(clock); var otherStartTime = LocalDateTime.now(clock).plus(10L, ChronoUnit.MINUTES); var endTime = LocalDateTime.now(clock).plus(1L, ChronoUnit.DAYS); given(userMock.getId()).willReturn(userId); var accessInterval = new AccessInterval(userMock, startTime, endTime); accessInterval.id = accessIntervalId; var otherAccessInterval = new AccessInterval(userMock, otherStartTime, endTime); otherAccessInterval.id = accessIntervalId; boolean actualEquals = accessInterval.equals(otherAccessInterval); assertThat(actualEquals).isFalse(); }
@Test public void equalsReturnsFalseForDifferentEndTimes() { var accessIntervalId = 1L; var userId = "user"; var startTime = LocalDateTime.now(clock); var endTime = LocalDateTime.now(clock).plus(1L, ChronoUnit.DAYS); var otherEndTime = LocalDateTime.now(clock).plus(2L, ChronoUnit.DAYS); given(userMock.getId()).willReturn(userId); var accessInterval = new AccessInterval(userMock, startTime, endTime); accessInterval.id = accessIntervalId; var otherAccessInterval = new AccessInterval(userMock, startTime, otherEndTime); otherAccessInterval.id = accessIntervalId; boolean actualEquals = accessInterval.equals(otherAccessInterval); assertThat(actualEquals).isFalse(); }