use of javax.validation.ConstraintViolation in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testToOneAssocNotValidated.
@Test
public void testToOneAssocNotValidated() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Screen screen = new Screen();
PowerSupply ps = new PowerSupply();
ps.setPosition("1");
ps.setPower(new BigDecimal(350));
screen.setPowerSupply(ps);
try {
s.persist(screen);
s.flush();
fail("Associated objects should not be validated");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
assertEquals(PowerSupply.class, constraintViolation.getRootBeanClass());
}
tx.rollback();
s.close();
}
use of javax.validation.ConstraintViolation in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testAssocInEmbeddedNotValidated.
@Test
public void testAssocInEmbeddedNotValidated() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Screen screen = new Screen();
screen.setStopButton(new Button());
screen.getStopButton().setName("STOOOOOP");
PowerSupply ps = new PowerSupply();
screen.setPowerSupply(ps);
DisplayConnector conn = new DisplayConnector();
conn.setNumber(1);
screen.getConnectors().add(conn);
final Display display = new Display();
display.setBrand("dell");
conn.setDisplay(display);
s.persist(display);
s.flush();
try {
display.setBrand(null);
s.persist(screen);
s.flush();
fail("Collection of embedded objects should be validated");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
assertEquals(Display.class, constraintViolation.getRootBeanClass());
}
tx.rollback();
s.close();
}
use of javax.validation.ConstraintViolation in project javaee7-samples by javaee-samples.
the class ConstructorParametersConstraintsTest method constructorViolationsWhenNotNullParameters.
@Test
public void constructorViolationsWhenNotNullParameters() throws NoSuchMethodException, SecurityException {
final MyParameter parameter = new MyParameter();
parameter.setValue("foo");
ExecutableValidator methodValidator = validator.forExecutables();
Constructor<MyBean2> constructor = MyBean2.class.getConstructor(parameter.getClass());
Set<ConstraintViolation<MyBean2>> constraints = methodValidator.validateConstructorParameters(constructor, new Object[] { parameter });
assertThat(constraints.isEmpty(), equalTo(true));
}
use of javax.validation.ConstraintViolation in project jersey by jersey.
the class DefaultConfiguredValidator method onValidate.
// Invoked as the last validation interceptor method in the chain.
@Override
public void onValidate(final ValidationInterceptorContext ctx) {
final Object resource = ctx.getResource();
final Invocable resourceMethod = ctx.getInvocable();
final Object[] args = ctx.getArgs();
final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();
final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
// Resource validation.
if (beanDescriptor.isBeanConstrained()) {
constraintViolations.addAll(validate(resource));
}
if (resourceMethod != null && configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
final Method handlingMethod = resourceMethod.getHandlingMethod();
// Resource method validation - input parameters.
final MethodDescriptor methodDescriptor = beanDescriptor.getConstraintsForMethod(handlingMethod.getName(), handlingMethod.getParameterTypes());
if (methodDescriptor != null && methodDescriptor.hasConstrainedParameters()) {
constraintViolations.addAll(forExecutables().validateParameters(resource, handlingMethod, args));
}
}
if (!constraintViolations.isEmpty()) {
throw new ConstraintViolationException(constraintViolations);
}
}
use of javax.validation.ConstraintViolation in project camel by apache.
the class CMProducer method process.
/**
* Producer is a exchange processor. This process is built in several steps. 1. Validate message receive from client 2. Send validated message to CM endpoints. 3. Process response from CM
* endpoints.
*/
@Override
public void process(final Exchange exchange) throws Exception {
// Immutable message receive from clients. Throws camel ' s
// InvalidPayloadException
final SMSMessage smsMessage = exchange.getIn().getMandatoryBody(SMSMessage.class);
// Validates Payload - SMSMessage
log.trace("Validating SMSMessage instance provided: {}", smsMessage.toString());
final Set<ConstraintViolation<SMSMessage>> constraintViolations = getValidator().validate(smsMessage);
if (constraintViolations.size() > 0) {
final StringBuffer msg = new StringBuffer();
for (final ConstraintViolation<SMSMessage> cv : constraintViolations) {
msg.append(String.format("- Invalid value for %s: %s", cv.getPropertyPath().toString(), cv.getMessage()));
}
log.debug(msg.toString());
throw new InvalidPayloadRuntimeException(exchange, SMSMessage.class);
}
log.trace("SMSMessage instance is valid: {}", smsMessage.toString());
// We have a valid (immutable) SMSMessage instance, lets extend to
// CMMessage
// This is the instance we will use to build the XML document to be
// sent to CM SMS GW.
final CMMessage cmMessage = new CMMessage(smsMessage.getPhoneNumber(), smsMessage.getMessage());
log.debug("CMMessage instance build from valid SMSMessage instance");
if (smsMessage.getFrom() == null || smsMessage.getFrom().isEmpty()) {
String df = getConfiguration().getDefaultFrom();
cmMessage.setSender(df);
log.debug("Dynamic sender is set to default dynamic sender: {}", df);
}
// Remember, this can be null.
cmMessage.setIdAsString(smsMessage.getId());
// Unicode and multipart
cmMessage.setUnicodeAndMultipart(getConfiguration().getDefaultMaxNumberOfParts());
// 2. Send a validated sms message to CM endpoints
// for abnormal situations.
sender.send(cmMessage);
log.debug("Request accepted by CM Host: {}", cmMessage.toString());
}
Aggregations