Search in sources :

Example 56 with ConstraintViolation

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 57 with ConstraintViolation

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 58 with ConstraintViolation

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));
}
Also used : ExecutableValidator(javax.validation.executable.ExecutableValidator) ConstraintViolation(javax.validation.ConstraintViolation) Test(org.junit.Test)

Example 59 with ConstraintViolation

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);
    }
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) BeanDescriptor(javax.validation.metadata.BeanDescriptor) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Method(java.lang.reflect.Method) MethodDescriptor(javax.validation.metadata.MethodDescriptor) HashSet(java.util.HashSet)

Example 60 with ConstraintViolation

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());
}
Also used : SMSMessage(org.apache.camel.component.cm.client.SMSMessage) ConstraintViolation(javax.validation.ConstraintViolation) InvalidPayloadRuntimeException(org.apache.camel.InvalidPayloadRuntimeException)

Aggregations

ConstraintViolation (javax.validation.ConstraintViolation)95 Test (org.junit.Test)78 Validator (javax.validation.Validator)19 ConstraintViolationException (javax.validation.ConstraintViolationException)12 SMSMessage (org.apache.camel.component.cm.client.SMSMessage)12 ChangePasswordForm (org.orcid.frontend.web.forms.ChangePasswordForm)11 CmsDocumentBlobSegment (gov.ca.cwds.data.persistence.cms.CmsDocumentBlobSegment)10 ValidatorFactory (javax.validation.ValidatorFactory)9 ManagePasswordOptionsForm (org.orcid.frontend.web.forms.ManagePasswordOptionsForm)8 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 CMConfiguration (org.apache.camel.component.cm.CMConfiguration)6 Set (java.util.Set)5 InitialContext (javax.naming.InitialContext)5 Session (org.hibernate.Session)5 Transaction (org.hibernate.Transaction)5 Method (java.lang.reflect.Method)4 ExecutableValidator (javax.validation.executable.ExecutableValidator)4 CamelExecutionException (org.apache.camel.CamelExecutionException)4 Exchange (org.apache.camel.Exchange)4