PdfVersionValidator implements ConstraintValidator<ValidPdfVersion, AbstractPdfOutputParameters> { @Override public boolean isValid(AbstractPdfOutputParameters value, ConstraintValidatorContext context) { boolean isValid = value == null || value.getVersion() == null || value.getVersion().compareTo(value.getMinRequiredPdfVersion()) >= 0; if (!isValid) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("Invalid version %s. Minimum version required is %s.", value.getVersion(), value.getMinRequiredPdfVersion())).addNode("parameters").addConstraintViolation(); } return isValid; }  @Override void initialize(ValidPdfVersion constraintAnnotation); @Override boolean isValid(AbstractPdfOutputParameters value, ConstraintValidatorContext context);  }
@Test public void testNull() { assertTrue(victim.isValid(null, null)); }
@Test public void testNullValue() { when(params.getVersion()).thenReturn(null); assertTrue(victim.isValid(params, null)); }
@Test public void testValidValue() { when(params.getVersion()).thenReturn(PdfVersion.VERSION_1_5); when(params.getMinRequiredPdfVersion()).thenReturn(PdfVersion.VERSION_1_2); assertTrue(victim.isValid(params, null)); }
@Test public void testInvalidValue() { ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); NodeBuilderDefinedContext nodeBuilderContext = mock(NodeBuilderDefinedContext.class); when(builder.addNode(anyString())).thenReturn(nodeBuilderContext); when(params.getVersion()).thenReturn(PdfVersion.VERSION_1_2); when(params.getMinRequiredPdfVersion()).thenReturn(PdfVersion.VERSION_1_5); assertFalse(victim.isValid(params, context)); verify(context).buildConstraintViolationWithTemplate(anyString()); }