### Question:
HttpContentSizeValidator implements ConstraintValidator<ValidHttpContentSize, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { LOGGER.info("content size validation was called. Value: {}", value); if (value == null) { return true; } else if (!value.startsWith("http")) { context.buildConstraintViolationWithTemplate(INVALID_URL_MSG).addConstraintViolation(); return false; } try { Pair<StatusType, Integer> contentLength = httpHelper.getContentLength(value); if (!contentLength.getKey().getFamily().equals(Family.SUCCESSFUL)) { String msg = String.format(FAILED_TO_GET_BY_FAMILY_TYPE, value, contentLength.getKey().getReasonPhrase()); context.buildConstraintViolationWithTemplate(msg).addConstraintViolation(); return false; } return contentLength.getValue() > 0 && contentLength.getValue() <= MAX_IN_BYTES; } catch (Throwable throwable) { LOGGER.info("content size validation failed.", throwable); context.buildConstraintViolationWithTemplate(FAILED_TO_GET_WITH_EXCEPTION).addConstraintViolation(); } return false; }  @Override void initialize(ValidHttpContentSize constraintAnnotation); @Override boolean isValid(String value, ConstraintValidatorContext context); static final int MAX_SIZE; static final int MAX_IN_BYTES; static final String FAILED_TO_GET_BY_FAMILY_TYPE; static final String INVALID_URL_MSG; static final String FAILED_TO_GET_WITH_EXCEPTION; }### Answer:
@Test public void testUrlWrongProtocol() { assertFalse(underTest.isValid("ftp: verify(constraintValidatorContext, times(1)).buildConstraintViolationWithTemplate(eq(INVALID_MESSAGE)); verify(constraintViolationBuilder, times(1)).addConstraintViolation(); }
@Test public void testUrlWithoutProtocol() { assertFalse(underTest.isValid("without.protocol.com", constraintValidatorContext)); verify(constraintValidatorContext, times(1)).buildConstraintViolationWithTemplate(eq(INVALID_MESSAGE)); verify(constraintViolationBuilder, times(1)).addConstraintViolation(); }
@Test public void testUrlNotSuccessful() { when(statusType.getFamily()).thenReturn(Family.OTHER); when(statusType.getReasonPhrase()).thenReturn("not available"); assertFalse(underTest.isValid("http: verify(constraintValidatorContext, times(1)) .buildConstraintViolationWithTemplate(eq("Failed to get response by the specified URL 'http: verify(constraintViolationBuilder, times(1)).addConstraintViolation(); }
@Test public void testUrlFailsWithException() { when(httpHelper.getContentLength(anyString())).thenThrow(RuntimeException.class); assertFalse(underTest.isValid("http: verify(constraintValidatorContext, times(1)) .buildConstraintViolationWithTemplate(eq("Failed to get response by the specified URL!")); verify(constraintViolationBuilder, times(1)).addConstraintViolation(); }
@Test public void testUrlFailsWithMinusOne() { when(httpHelper.getContentLength(anyString())).thenReturn(new ImmutablePair<>(statusType, -1)); assertFalse(underTest.isValid("http: verify(constraintValidatorContext, times(0)).buildConstraintViolationWithTemplate(anyString()); }
@Test public void testUrlFailsWithZero() { when(httpHelper.getContentLength(anyString())).thenReturn(new ImmutablePair<>(statusType, 0)); assertFalse(underTest.isValid("http: verify(constraintValidatorContext, times(0)).buildConstraintViolationWithTemplate(anyString()); }
@Test public void testUrlFailsWithMoreThanMax() { when(httpHelper.getContentLength(anyString())).thenReturn(new ImmutablePair<>(statusType, MAX_IN_BYTES + 1)); assertFalse(underTest.isValid("http: verify(constraintValidatorContext, times(0)).buildConstraintViolationWithTemplate(anyString()); }
@Test public void testUrlNull() { assertTrue(underTest.isValid(null, constraintValidatorContext)); verify(constraintValidatorContext, times(0)).buildConstraintViolationWithTemplate(anyString()); }
@Test public void testUrlOk() { assertTrue(underTest.isValid("http: verify(constraintValidatorContext, times(0)).buildConstraintViolationWithTemplate(anyString()); }