Search in sources :

Example 96 with ConstraintViolation

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;
}
Also used : HashMap(java.util.HashMap) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) PasswordReusedException(ca.corefacility.bioinformatics.irida.exceptions.PasswordReusedException) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 97 with ConstraintViolation

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());
    }
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.utils.model.IdentifiableTestEntity) Set(java.util.Set) HashMap(java.util.HashMap) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Test(org.junit.Test)

Example 98 with ConstraintViolation

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);
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ConstraintViolation(javax.validation.ConstraintViolation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 99 with ConstraintViolation

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));
}
Also used : ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ConstraintViolation(javax.validation.ConstraintViolation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 100 with ConstraintViolation

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);
}
Also used : IdentifiableTestEntity(ca.corefacility.bioinformatics.irida.web.controller.test.unit.support.IdentifiableTestEntity) ValidatorFactory(javax.validation.ValidatorFactory) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) PlatformResourceBundleLocator(org.hibernate.validator.resourceloading.PlatformResourceBundleLocator) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) List(java.util.List) Map(java.util.Map) Validator(javax.validation.Validator) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ConstraintViolation (javax.validation.ConstraintViolation)339 Test (org.junit.Test)194 Validator (javax.validation.Validator)76 ConstraintViolationException (javax.validation.ConstraintViolationException)54 ArrayList (java.util.ArrayList)29 ValidatorFactory (javax.validation.ValidatorFactory)28 HashSet (java.util.HashSet)22 Set (java.util.Set)22 Test (org.junit.jupiter.api.Test)20 Method (java.lang.reflect.Method)18 Name (org.kie.workbench.common.stunner.bpmn.definition.property.general.Name)16 Date (java.util.Date)14 EJBException (javax.ejb.EJBException)12 ExecutableValidator (javax.validation.executable.ExecutableValidator)12 PhoneNumberType (gov.ca.cwds.cals.persistence.model.calsns.dictionaries.PhoneNumberType)11 Path (javax.validation.Path)11 Customer (org.baeldung.javaxval.methodvalidation.model.Customer)11 CmsDocumentBlobSegment (gov.ca.cwds.data.persistence.cms.CmsDocumentBlobSegment)10 List (java.util.List)10 XmEntity (com.icthh.xm.ms.entity.domain.XmEntity)9