### Question:
DuplicateAuthnRequestValidator { public boolean valid(String requestId) { AuthnRequestIdKey key = new AuthnRequestIdKey(requestId); if (previousRequests.contains(key) && previousRequests.getExpiration(key).isAfterNow()) { return false; } DateTime expire = DateTime.now().plus(expirationDuration); previousRequests.setExpiration(key, expire); return true; } @Inject  DuplicateAuthnRequestValidator(IdExpirationCache<AuthnRequestIdKey> previousRequests, SamlDuplicateRequestValidationConfiguration samlDuplicateRequestValidationConfiguration); boolean valid(String requestId);  }### Answer:
@Test public void valid_shouldThrowAnExceptionIfTheAuthnRequestIsADuplicateOfAPreviousOne() throws Exception { final String duplicateRequestId = "duplicate-id"; duplicateAuthnRequestValidator.valid(duplicateRequestId); boolean isValid = duplicateAuthnRequestValidator.valid(duplicateRequestId); assertThat(isValid).isEqualTo(false); }
@Test public void valid_shouldPassIfTheAuthnRequestIsNotADuplicateOfAPreviousOne() throws Exception { duplicateAuthnRequestValidator.valid("some-request-id"); boolean isValid = duplicateAuthnRequestValidator.valid("another-request-id"); assertThat(isValid).isEqualTo(true); }
@Test public void valid_shouldPassIfTwoAuthnRequestsHaveTheSameIdButTheFirstAssertionHasExpired() throws Exception { final String duplicateRequestId = "duplicate-id"; duplicateAuthnRequestValidator.valid(duplicateRequestId); DateTimeFreezer.freezeTime(DateTime.now().plusHours(EXPIRATION_HOURS).plusMinutes(1)); boolean isValid = duplicateAuthnRequestValidator.valid(duplicateRequestId); assertThat(isValid).isEqualTo(true); }
@Test public void valid_shouldFailIfAuthnRequestsReceivedWithSameIdAndFirstIdHasNotExpired() throws Exception { final String duplicateRequestId = "duplicate-id"; duplicateAuthnRequestValidator.valid(duplicateRequestId); DateTimeFreezer.freezeTime(DateTime.now().plusHours(EXPIRATION_HOURS).minusMinutes(1)); boolean isValid = duplicateAuthnRequestValidator.valid(duplicateRequestId); assertThat(isValid).isEqualTo(false); }