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);
}
}
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();
}
Aggregations