Search in sources :

Example 86 with ConstraintViolationException

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

the class ReferenceConstrainClusterTest method clusterConfigRefInvalid.

@Test
public void clusterConfigRefInvalid() throws TransactionFailure {
    Cluster cluster = habitat.getService(Cluster.class, "clusterA");
    assertNotNull(cluster);
    ConfigBean serverConfig = (ConfigBean) ConfigBean.unwrap(cluster);
    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) HashMap(java.util.HashMap) Cluster(com.sun.enterprise.config.serverbeans.Cluster) 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 87 with ConstraintViolationException

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

the class ContactRESTService method updateContact.

/**
 * Updates a contact with the ID provided in the Contact. 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
 */
@PUT
@Path("/{id:[0-9][0-9]*}")
public Response updateContact(@PathParam("id") long id, Contact contact) {
    if (contact == null) {
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
    log.info("updateContact started. Contact = " + contact.getFirstName() + " " + contact.getLastName() + " " + contact.getEmail() + " " + contact.getPhoneNumber() + " " + contact.getBirthDate() + " " + contact.getId());
    if (contact.getId() != id) {
        // The client attempted to update the read-only Id. This is not permitted.
        Response response = Response.status(Response.Status.CONFLICT).entity("The contact ID cannot be modified").build();
        throw new WebApplicationException(response);
    }
    if (service.findById(contact.getId()) == null) {
        // Verify if the contact exists. Return 404, if not present.
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    Response.ResponseBuilder builder = null;
    try {
        // Apply the changes the Contact.
        service.update(contact);
        // Create an OK Response and pass the contact back in case it is needed.
        builder = Response.ok(contact);
        log.info("updateContact 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");
        responseObj.put("error", "This is where errors are displayed that are not related to a specific field");
        responseObj.put("anotherError", "You can find this error message in /src/main/java/org/jboss/quickstarts/contact/ContactRESTService.java line 242.");
        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) ValidationException(javax.validation.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) ConstraintViolationException(javax.validation.ConstraintViolationException) HashMap(java.util.HashMap) Map(java.util.Map) ConstraintViolationException(javax.validation.ConstraintViolationException) ValidationException(javax.validation.ValidationException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

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