Search in sources :

Example 56 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project Payara by payara.

the class ReferenceConstrainClusterTest method clusterServerRefInvalid.

@Test
public void clusterServerRefInvalid() throws TransactionFailure {
    Cluster cluster = habitat.getService(Cluster.class, "clusterA");
    assertNotNull(cluster);
    ServerRef sref = cluster.getServerRef().get(0);
    ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(sref);
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    Map<String, String> configChanges = new HashMap<String, String>();
    configChanges.put("ref", "server-nonexist");
    changes.put(serverConfig, configChanges);
    try {
        ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
        cs.apply(changes);
        fail("Can not reach this point");
    } catch (TransactionFailure tf) {
        ConstraintViolationException cv = findConstrViolation(tf);
        assertNotNull(cv);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigSupport(org.jvnet.hk2.config.ConfigSupport) HashMap(java.util.HashMap) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ConstraintViolationException(javax.validation.ConstraintViolationException) ConfigBean(org.jvnet.hk2.config.ConfigBean) ServerRef(com.sun.enterprise.config.serverbeans.ServerRef) HashMap(java.util.HashMap) Map(java.util.Map) ConfigApiTest(com.sun.enterprise.configapi.tests.ConfigApiTest) Test(org.junit.Test)

Example 57 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project Payara by payara.

the class ReferenceConstrainTest method jmxConnectorAuthRealmRefInvalid.

@Test
public void jmxConnectorAuthRealmRefInvalid() throws TransactionFailure {
    JmxConnector jmxConnector = habitat.getService(JmxConnector.class, "system");
    assertNotNull(jmxConnector);
    ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(jmxConnector);
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    Map<String, String> configChanges = new HashMap<String, String>();
    configChanges.put("auth-realm-name", "realm-not-exist");
    changes.put(serverConfig, configChanges);
    try {
        ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
        cs.apply(changes);
        fail("Can not reach this point");
    } catch (TransactionFailure tf) {
        ConstraintViolationException cv = findConstrViolation(tf);
        assertNotNull(cv);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigSupport(org.jvnet.hk2.config.ConfigSupport) HashMap(java.util.HashMap) ConstraintViolationException(javax.validation.ConstraintViolationException) JmxConnector(com.sun.enterprise.config.serverbeans.JmxConnector) ConfigBean(org.jvnet.hk2.config.ConfigBean) HashMap(java.util.HashMap) Map(java.util.Map) ConfigApiTest(com.sun.enterprise.configapi.tests.ConfigApiTest) Test(org.junit.Test)

Example 58 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project Payara by payara.

the class ReferenceConstrainTest method serverConfigRefInvalid.

@Test
public void serverConfigRefInvalid() throws TransactionFailure {
    Server server = habitat.getService(Server.class, "server");
    assertNotNull(server);
    ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(server);
    Map<ConfigBean, Map<String, String>> changes = new HashMap<ConfigBean, Map<String, String>>();
    Map<String, String> configChanges = new HashMap<String, String>();
    configChanges.put("config-ref", "server-config-nonexist");
    changes.put(serverConfig, configChanges);
    try {
        ConfigSupport cs = getHabitat().getService(ConfigSupport.class);
        cs.apply(changes);
        fail("Can not reach this point");
    } catch (TransactionFailure tf) {
        ConstraintViolationException cv = findConstrViolation(tf);
        assertNotNull(cv);
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) ConfigSupport(org.jvnet.hk2.config.ConfigSupport) Server(com.sun.enterprise.config.serverbeans.Server) HashMap(java.util.HashMap) ConstraintViolationException(javax.validation.ConstraintViolationException) ConfigBean(org.jvnet.hk2.config.ConfigBean) HashMap(java.util.HashMap) Map(java.util.Map) ConfigApiTest(com.sun.enterprise.configapi.tests.ConfigApiTest) Test(org.junit.Test)

Example 59 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project quickstart by wildfly.

the class ContactRESTService method createContact.

/**
 * Creates a new contact from the values provided. Performs validation and will return a JAX-RS response with either 200 (ok)
 * or with a map of fields, and related errors.
 *
 * @param Contact
 * @return Response
 */
@SuppressWarnings("unused")
@POST
public Response createContact(Contact contact) {
    log.info("createContact started. Contact = " + contact.getFirstName() + " " + contact.getLastName() + " " + contact.getEmail() + " " + contact.getPhoneNumber() + " " + contact.getBirthDate() + " " + contact.getId());
    if (contact == null) {
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
    Response.ResponseBuilder builder = null;
    try {
        // Go add the new Contact.
        Contact created = service.create(contact);
        // Construct a location of created contact.
        URI location = null;
        try {
            UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
            uriBuilder.path(Long.toString(created.getId()));
            location = uriBuilder.build();
        } catch (LoggableFailure lf) {
        // IGNORED: UriInfo methods throw this if called outside the scope of request,
        // that happens in ContactRegistrationTest.testRegister method.
        }
        // Create an CREATED Response and pass the URI location back in case it is needed.
        builder = Response.created(location);
        log.info("createContact completed. Contact = " + contact.getFirstName() + " " + contact.getLastName() + " " + contact.getEmail() + " " + contact.getPhoneNumber() + " " + contact.getBirthDate() + " " + contact.getId());
    } catch (ConstraintViolationException ce) {
        log.info("ConstraintViolationException - " + ce.toString());
        // Handle bean validation issues
        builder = createViolationResponse(ce.getConstraintViolations());
    } catch (ValidationException e) {
        log.info("ValidationException - " + e.toString());
        // Handle the unique constrain violation
        Map<String, String> responseObj = new HashMap<>();
        responseObj.put("email", "That email is already used, please use a unique email");
        builder = Response.status(Response.Status.CONFLICT).entity(responseObj);
    } catch (Exception e) {
        log.info("Exception - " + e.toString());
        // Handle generic exceptions
        Map<String, String> responseObj = new HashMap<>();
        responseObj.put("error", e.getMessage());
        builder = Response.status(Response.Status.BAD_REQUEST).entity(responseObj);
    }
    return builder.build();
}
Also used : Response(javax.ws.rs.core.Response) LoggableFailure(org.jboss.resteasy.spi.LoggableFailure) ValidationException(javax.validation.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) ConstraintViolationException(javax.validation.ConstraintViolationException) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidationException(javax.validation.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST)

Example 60 with ConstraintViolationException

use of javax.validation.ConstraintViolationException in project tomee by apache.

the class MdbContainer method createActivationSpec.

private ActivationSpec createActivationSpec(final BeanContext beanContext) throws OpenEJBException {
    try {
        // initialize the object recipe
        final ObjectRecipe objectRecipe = new ObjectRecipe(activationSpecClass);
        objectRecipe.allow(Option.IGNORE_MISSING_PROPERTIES);
        objectRecipe.disallow(Option.FIELD_INJECTION);
        final Map<String, String> activationProperties = beanContext.getActivationProperties();
        for (final Map.Entry<String, String> entry : activationProperties.entrySet()) {
            objectRecipe.setMethodProperty(entry.getKey(), entry.getValue());
        }
        objectRecipe.setMethodProperty("beanClass", beanContext.getBeanClass());
        // create the activationSpec
        final ActivationSpec activationSpec = (ActivationSpec) objectRecipe.create(activationSpecClass.getClassLoader());
        // verify all properties except "destination" and "destinationType" were consumed
        final Set<String> unusedProperties = new TreeSet<String>(objectRecipe.getUnsetProperties().keySet());
        unusedProperties.remove("destination");
        unusedProperties.remove("destinationType");
        unusedProperties.remove("destinationLookup");
        unusedProperties.remove("connectionFactoryLookup");
        unusedProperties.remove("beanClass");
        unusedProperties.remove("MdbActiveOnStartup");
        unusedProperties.remove("MdbJMXControl");
        unusedProperties.remove("DeliveryActive");
        if (!unusedProperties.isEmpty()) {
            final String text = "No setter found for the activation spec properties: " + unusedProperties;
            if (failOnUnknownActivationSpec) {
                throw new IllegalArgumentException(text);
            } else {
                logger.warning(text);
            }
        }
        // validate the activation spec
        try {
            activationSpec.validate();
        } catch (final UnsupportedOperationException uoe) {
            logger.info("ActivationSpec does not support validate. Implementation of validate is optional");
        }
        // also try validating using Bean Validation if there is a Validator available in the context.
        try {
            final Validator validator = (Validator) beanContext.getJndiContext().lookup("comp/Validator");
            final Set generalSet = validator.validate(activationSpec);
            if (!generalSet.isEmpty()) {
                throw new ConstraintViolationException("Constraint violation for ActivationSpec " + activationSpecClass.getName(), generalSet);
            }
        } catch (final NamingException e) {
            logger.debug("No Validator bound to JNDI context");
        }
        // set the resource adapter into the activation spec
        activationSpec.setResourceAdapter(resourceAdapter);
        return activationSpec;
    } catch (final Exception e) {
        throw new OpenEJBException("Unable to create activation spec", e);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) Set(java.util.Set) TreeSet(java.util.TreeSet) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ReflectionException(javax.management.ReflectionException) OpenEJBException(org.apache.openejb.OpenEJBException) UnavailableException(javax.resource.spi.UnavailableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(org.apache.openejb.SystemException) AttributeNotFoundException(javax.management.AttributeNotFoundException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ConstraintViolationException(javax.validation.ConstraintViolationException) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) TreeSet(java.util.TreeSet) ActivationSpec(javax.resource.spi.ActivationSpec) ConstraintViolationException(javax.validation.ConstraintViolationException) NamingException(javax.naming.NamingException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Validator(javax.validation.Validator)

Aggregations

ConstraintViolationException (javax.validation.ConstraintViolationException)87 Test (org.junit.Test)67 LocalMode (com.datatorrent.api.LocalMode)35 Configuration (org.apache.hadoop.conf.Configuration)34 File (java.io.File)16 ConstraintViolation (javax.validation.ConstraintViolation)15 Session (org.hibernate.Session)11 Transaction (org.hibernate.Transaction)11 LogicalPlan (com.datatorrent.stram.plan.logical.LogicalPlan)9 Map (java.util.Map)9 BigDecimal (java.math.BigDecimal)7 HashMap (java.util.HashMap)7 Set (java.util.Set)6 StramLocalCluster (com.datatorrent.stram.StramLocalCluster)5 ConfigApiTest (com.sun.enterprise.configapi.tests.ConfigApiTest)5 HashSet (java.util.HashSet)5 ValidationException (javax.validation.ValidationException)5 Validator (javax.validation.Validator)5 Response (javax.ws.rs.core.Response)5 Path (org.apache.hadoop.fs.Path)5