use of javax.validation.ConstraintViolation in project irida by phac-nml.
the class UsersController method handleCreateUpdateException.
/**
* Handle exceptions for the create and update pages
*
* @param ex
* an exception to handle
* @param locale
* The locale to work with
*
* @return A Map<String,String> of errors to render
*/
private Map<String, String> handleCreateUpdateException(Exception ex, Locale locale) {
Map<String, String> errors = new HashMap<>();
if (ex instanceof ConstraintViolationException) {
ConstraintViolationException cvx = (ConstraintViolationException) ex;
logger.debug("User provided data threw ConstrainViolation");
Set<ConstraintViolation<?>> constraintViolations = cvx.getConstraintViolations();
for (ConstraintViolation<?> violation : constraintViolations) {
logger.debug(violation.getMessage());
String errorKey = violation.getPropertyPath().toString();
errors.put(errorKey, violation.getMessage());
}
} else if (ex instanceof DataIntegrityViolationException) {
DataIntegrityViolationException divx = (DataIntegrityViolationException) ex;
logger.debug(divx.getMessage());
if (divx.getMessage().contains(User.USER_EMAIL_CONSTRAINT_NAME)) {
errors.put("email", messageSource.getMessage("user.edit.emailConflict", null, locale));
}
} else if (ex instanceof EntityExistsException) {
EntityExistsException eex = (EntityExistsException) ex;
errors.put(eex.getFieldName(), eex.getMessage());
} else if (ex instanceof PasswordReusedException) {
errors.put("password", messageSource.getMessage("user.edit.passwordReused", null, locale));
}
return errors;
}
use of javax.validation.ConstraintViolation in project irida by phac-nml.
the class CRUDServiceImplTest method testUpdateInvalidEntry.
@Test
public void testUpdateInvalidEntry() {
IdentifiableTestEntity i = new IdentifiableTestEntity();
i.setNonNull("Definitely not null.");
i.setIntegerValue(Integer.MIN_VALUE);
Long id = new Long(1);
i.setId(id);
when(crudRepository.exists(id)).thenReturn(Boolean.TRUE);
when(crudRepository.findOne(id)).thenReturn(i);
Map<String, Object> updatedFields = new HashMap<>();
updatedFields.put("nonNull", null);
try {
crudService.updateFields(id, updatedFields);
fail();
} catch (ConstraintViolationException e) {
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
assertEquals(1, violations.size());
ConstraintViolation<?> v = violations.iterator().next();
assertEquals("nonNull", v.getPropertyPath().toString());
}
}
use of javax.validation.ConstraintViolation in project irida by phac-nml.
the class ProjectServiceImplTest method testAddSampleToProjectNoSamplePersistedInvalidSample.
@Test(expected = ConstraintViolationException.class)
public void testAddSampleToProjectNoSamplePersistedInvalidSample() {
Project p = project();
Sample s = new Sample();
s.setSampleName("name");
Set<ConstraintViolation<Sample>> violations = new HashSet<>();
violations.add(ConstraintViolationImpl.forBeanValidation(null, null, Sample.class, null, null, null, null, null, null));
when(validator.validate(s)).thenReturn(violations);
projectService.addSampleToProject(p, s, true);
verifyZeroInteractions(sampleRepository, psjRepository);
}
use of javax.validation.ConstraintViolation in project irida by phac-nml.
the class ProjectServiceImplTest method testAddSampleToProjectNoSamplePersisted.
@Test
public void testAddSampleToProjectNoSamplePersisted() {
Project p = project();
Sample s = new Sample();
s.setSampleName("name");
Set<ConstraintViolation<Sample>> noViolations = new HashSet<>();
when(validator.validate(s)).thenReturn(noViolations);
when(sampleRepository.save(s)).thenReturn(s);
projectService.addSampleToProject(p, s, true);
verify(sampleRepository).save(s);
verify(psjRepository).save(new ProjectSampleJoin(p, s, true));
}
use of javax.validation.ConstraintViolation in project irida by phac-nml.
the class ControllerExceptionHandlerTest method testHandleConstraintViolations.
@Test
public void testHandleConstraintViolations() {
final String MESSAGES_BASENAME = "ValidationMessages";
Configuration<?> configuration = Validation.byDefaultProvider().configure();
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(MESSAGES_BASENAME);
configuration.messageInterpolator(new ResourceBundleMessageInterpolator(new PlatformResourceBundleLocator(MESSAGES_BASENAME)));
ValidatorFactory factory = configuration.buildValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<?>> constraintViolations = new HashSet<>();
Set<ConstraintViolation<IdentifiableTestEntity>> violations = validator.validate(new IdentifiableTestEntity());
for (ConstraintViolation<IdentifiableTestEntity> v : violations) {
constraintViolations.add(v);
}
ResponseEntity<Map<String, List<String>>> response = controller.handleConstraintViolations(new ConstraintViolationException(constraintViolations));
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
// assertEquals("{\"label\":[\"You must provide a label.\"]}", response.getBody());
Map<String, List<String>> body = response.getBody();
assertTrue("The response must contain an error about a missing label.", body.containsKey("label"));
List<String> labels = body.get("label");
assertEquals("There must only be one error with the label.", 1, labels.size());
String error = labels.get(0);
assertEquals("The error must be 'You must provide a label.'", "You must provide a label.", error);
}
Aggregations