SingleOutputValidator implements ConstraintValidator<ValidSingleOutput, SingleOutputTaskParameters> { @Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value)) { if (isNull(value.getOutput())) { return false; } if (isNull(value.getOutput().getDestination()) || (value.getOutput().getDestination().exists() && (value.getExistingOutputPolicy() == ExistingOutputPolicy.FAIL || value.getExistingOutputPolicy() == ExistingOutputPolicy.SKIP))) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("File destination already exists: %s.", value.getOutput().getDestination())) .addConstraintViolation(); return false; } } return true; }  @Override void initialize(ValidSingleOutput constraintAnnotation); @Override boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context);  }
@Test public void testNull() { assertTrue(victim.isValid(null, null)); }
@Test public void testNullOutput() { assertFalse(victim.isValid(new SetMetadataParameters(), null)); }
@Test public void destinationExistsPolicyOverwrite() throws IOException { SetMetadataParameters params = new SetMetadataParameters(); params.setOutput(new FileTaskOutput(folder.newFile())); params.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); assertTrue(victim.isValid(params, null)); }
@Test public void destinationExistsPolicyRename() throws IOException { SetMetadataParameters params = new SetMetadataParameters(); params.setOutput(new FileTaskOutput(folder.newFile())); params.setExistingOutputPolicy(ExistingOutputPolicy.RENAME); assertTrue(victim.isValid(params, null)); }
@Test public void destinationExistsPolicyFail() throws IOException { ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); SetMetadataParameters params = new SetMetadataParameters(); params.setOutput(new FileTaskOutput(folder.newFile())); params.setExistingOutputPolicy(ExistingOutputPolicy.FAIL); assertFalse(victim.isValid(params, context)); verify(context).buildConstraintViolationWithTemplate(contains("File destination already exists")); }
@Test public void destinationExistsPolicySkip() throws IOException { ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); SetMetadataParameters params = new SetMetadataParameters(); params.setOutput(new FileTaskOutput(folder.newFile())); params.setExistingOutputPolicy(ExistingOutputPolicy.SKIP); assertFalse(victim.isValid(params, context)); verify(context).buildConstraintViolationWithTemplate(contains("File destination already exists")); }