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