@Override public boolean isValid(final HelperBean bean, final ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); // build a custom property path ConstraintViolationBuilder builder = context.buildConstraintViolationWithTemplate(bean.getMessage()); NodeBuilderCustomizableContext nodeBuilder = null; for (String part : bean.getPath().split("\\.")) { if (nodeBuilder == null) { nodeBuilder = builder.addPropertyNode(part); } else { nodeBuilder = nodeBuilder.addPropertyNode(part); } } if (nodeBuilder != null) { nodeBuilder.addConstraintViolation(); } return false; }
@Override public boolean isValid(Order order, ConstraintValidatorContext ctx) { if (order.getProcessInfo() == null) { order.setProcessInfo(new OrderProcessInfo()); } List<ConstraintViolationBuilder> errors = new ArrayList<ConstraintViolationBuilder>(); addIfNotNull(errors, checkNotNullValue(order.getDates(), "order.validation.missingDates", ctx)); addIfNotNull(errors, checkNotNullValue(order.getType(), "order.validation.missingType", ctx)); if (order.getDates() != null) { addIfNotNull(errors, checkNotNullValue(order.getDates().getEffective(), "order.validation.missingEffectiveDate", ctx)); } if (order.getType() != null) { switch (order.getType()) { case ADDITIONAL_PAYMENT: case INITIAL_PAYMENT: case REGULAR_PAYMENT: validatePayment(order, ctx, errors); break; default: break; } } if (errors.isEmpty()) { return true; } else { for (ConstraintViolationBuilder i : errors) { i.addConstraintViolation(); } return false; } }
@Test public void shouldAscertainPropertyUniqueness() { final HibernateConstraintValidatorContext context = mock(HibernateConstraintValidatorContext.class); when(context.unwrap(HibernateConstraintValidatorContext.class)).thenReturn(context); when(context.addExpressionVariable(eq("nonUnique"), anyString())).thenReturn(context); when(context.getDefaultConstraintMessageTemplate()).thenReturn("template"); final ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate("template")).thenReturn(builder); when(builder.addPropertyNode(anyString())).thenReturn(mock(NodeBuilderCustomizableContext.class)); assertThat(validator.isValid(connection, context)).isEqualTo(validity); }
/** * JAVADOC Method Level Comments */ public void testInvalid() { UsernameValidator validator = new UsernameValidator(); ValidUsername vu = mock(ValidUsername.class); when(vu.message()).thenReturn("Oops"); validator.initialize(vu); UsernameValidatingPlugin plugin = mock(UsernameValidatingPlugin.class); when(plugin.isValid("username")).thenReturn(false); // TODO autowire plugin ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); ConstraintViolationBuilder cvb = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate("Oops")).thenReturn(cvb); NodeBuilderCustomizableContext nbdc = mock(NodeBuilderCustomizableContext.class); when(cvb.addPropertyNode("username")).thenReturn(nbdc); when(nbdc.addConstraintViolation()).thenReturn(context); assertFalse("Should be unique", validator.isValid("username", context)); verify(context).buildConstraintViolationWithTemplate("Oops"); verify(nbdc).addConstraintViolation(); verify(context).disableDefaultConstraintViolation(); }
protected ConstraintViolationBuilder checkPositiveValue(BigDecimal value, String message, ConstraintValidatorContext ctx) { ConstraintViolationBuilder result = null; if (value != null && value.compareTo(BigDecimal.ZERO) < 0) { result = ctx.buildConstraintViolationWithTemplate(message); } return result; }
protected ConstraintViolationBuilder checkNullValue(Object entity, String message, ConstraintValidatorContext ctx) { ConstraintViolationBuilder result = null; if (entity != null) { result = ctx.buildConstraintViolationWithTemplate(message); } return result; }
protected ConstraintViolationBuilder checkNotNullValue(Object entity, String message, ConstraintValidatorContext ctx) { ConstraintViolationBuilder result = null; if (entity == null) { result = ctx.buildConstraintViolationWithTemplate(message); } return result; }
@Test public void test() { final ConstraintValidatorContext context = mock(ConstraintValidatorContext.class); final ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); final NodeBuilderCustomizableContext node = mock(NodeBuilderCustomizableContext.class); when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder); when(builder.addPropertyNode(anyString())).thenReturn(node); mockCountToReturn(2l); final ConstraintValidator<Unique, Object> validator = new UniqueValidator(); final Unique annotation = Model2.class.getAnnotation(Unique.class); validator.initialize(annotation); assertFalse(validator.isValid("somevalue", context)); }
ConstraintViolationBuilder getConstraintViolationBuilder() { return new ConstraintValidatorContextImpl( Collections.emptyList(), null, PathImpl.createRootPath(), new DummyConstraintDescriptor() ).buildConstraintViolationWithTemplate("dummytemplate"); }
@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")); }
@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()); }
@Override public boolean isValid(MarketOrder entity, ConstraintValidatorContext ctx) { List<ConstraintViolationBuilder> errors = new ArrayList<ConstraintViolationBuilder>(); if (entity.getSource() == MarketOrderSource.AMOUNT && (entity.getGrossAmount() == null || entity.getGrossAmount().equals(BigDecimal.ZERO))) { errors.add(ctx.buildConstraintViolationWithTemplate("marketOrder.validation.missingAmount")); } else if (entity.getSource() == MarketOrderSource.UNITS && (entity.getUnits() == null || entity.getUnits().equals(BigDecimal.ZERO))) { errors.add(ctx.buildConstraintViolationWithTemplate("marketOrder.validation.missingUnits")); } addIfNotNull(errors, checkPositiveValue(entity.getGrossAmount(), "marketOrder.validation.negativeGrossAmount", ctx)); addIfNotNull(errors, checkPositiveValue(entity.getNetAmount(), "marketOrder.validation.negativeNetAmount", ctx)); addIfNotNull(errors, checkPositiveValue(entity.getUnits(), "marketOrder.validation.negativeUnits", ctx)); addIfNotNull(errors, checkPositiveValue(entity.getNav(), "marketOrder.validation.negativeNav", ctx)); // Comprobamos la integridad de las fechas y los estados if (entity.getCurrentState() != null && entity.getDates() != null) { MarketOrder.States state = MarketOrder.States.valueOf(entity.getCurrentState().getCode()); switch (state) { case INITIAL: addIfNotNull(errors, checkNullValue(entity.getDates().getProcessed(), Validation.StateInitialWithProcessedDate, ctx)); addIfNotNull(errors, checkNullValue(entity.getDates().getValued(), "marketOrder.validation.stateInitialWithValuedDate", ctx)); addIfNotNull(errors, checkNullValue(entity.getDates().getAccounted(), "marketOrder.validation.stateInitialWithAcountedDate", ctx)); break; case PROCESSED: addIfNotNull(errors, checkNullValue(entity.getDates().getProcessed(), "marketOrder.validation.stateInitialWithProcessedDate", ctx)); addIfNotNull(errors, checkNullValue(entity.getDates().getValued(), "marketOrder.validation.stateInitialWithValuedDate", ctx)); addIfNotNull(errors, checkNullValue(entity.getDates().getAccounted(), "marketOrder.validation.stateInitialWithAcountedDate", ctx)); addIfNotNull(errors, checkNotNullValue(entity.getDates().getProcessed(), "marketOrder.validation.stateProcessedWithoutProcessedDate", ctx)); addIfNotNull(errors, checkNotNullValue(entity.getDates().getValueDate(), "marketOrder.validation.stateProcessedWithoutValueDate", ctx)); addIfNotNull(errors, checkNullValue(entity.getDates().getAccounted(), "marketOrder.validation.stateProcessedWithAcountedDate", ctx)); addIfNotNull(errors, checkNullValue(entity.getDates().getValued(), "marketOrder.validation.stateProcessedWithValuedDate", ctx)); break; case VALUED: addIfNotNull(errors, checkNotNullValue(entity.getDates().getProcessed(), "marketOrder.validation.stateValuedWithoutProcessedDate", ctx)); addIfNotNull(errors, checkNotNullValue(entity.getDates().getValueDate(), "marketOrder.validation.stateValuedWithoutValueDate", ctx)); addIfNotNull(errors, checkNotNullValue(entity.getDates().getValued(), "marketOrder.validation.stateValuedWithoutValuedDate", ctx)); addIfNotNull(errors, checkNullValue(entity.getDates().getAccounted(), "marketOrder.validation.stateValuedWithAccountedDate", ctx)); break; case ACCOUNTED: addIfNotNull(errors, checkNotNullValue(entity.getDates().getProcessed(), "marketOrder.validation.stateAccountedWithoutProcessedDate", ctx)); addIfNotNull(errors, checkNotNullValue(entity.getDates().getValueDate(), "marketOrder.validation.stateAccountedWithoutValueDate", ctx)); addIfNotNull(errors, checkNotNullValue(entity.getDates().getValued(), "marketOrder.validation.stateAccountedWithoutValued", ctx)); addIfNotNull(errors, checkNotNullValue(entity.getDates().getAccounted(), "marketOrder.validation.stateAccountedWithoutAccountedDate", ctx)); break; default: break; } } if (errors.isEmpty()) { return true; } else { for (ConstraintViolationBuilder builder : errors) { builder.addConstraintViolation(); } return false; } }
private void validatePayment(Order order, ConstraintValidatorContext ctx, List<ConstraintViolationBuilder> errors) { if (order.getBuyDistribution() == null || order.getBuyDistribution().isEmpty()) { errors.add(ctx.buildConstraintViolationWithTemplate("order.validation.missingBuyDistribution")); } }