Search in sources :

Example 1 with Employee

use of eu.etaxonomy.cdm.persistence.validation.Employee in project cdmlib by cybertaxonomy.

the class EntityValidationDaoHibernateImplTest method testSaveValidationResult.

@Test
public void testSaveValidationResult() {
    HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
    ValidatorFactory factory = config.buildValidatorFactory();
    // This is the bean that is going to be tested
    Employee emp = new Employee();
    emp.setId(1);
    UUID uuid = emp.getUuid();
    // ERROR 1 (should be JOHN)
    emp.setGivenName("john");
    // This is an error (should be SMITH), but it is a Level-3
    // validation error, so the error should be ignored
    emp.setFamilyName("smith");
    // This is an @Valid bean on the Employee class, so Level-2
    // validation errors on the Company object should also be
    // listed.
    Company comp = new Company();
    // ERROR 2 (should be GOOGLE)
    comp.setName("Google");
    emp.setCompany(comp);
    Set<ConstraintViolation<Employee>> errors = factory.getValidator().validate(emp, Level2.class);
    dao.saveEntityValidation(emp, errors, CRUDEventType.NONE, null);
    EntityValidation result = dao.getEntityValidation(Employee.class.getName(), 1);
    assertNotNull(result);
    assertEquals("Unexpected UUID", result.getValidatedEntityUuid(), uuid);
    assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
    Set<EntityConstraintViolation> violations = result.getEntityConstraintViolations();
    List<EntityConstraintViolation> list = new ArrayList<EntityConstraintViolation>(violations);
    Collections.sort(list, new Comparator<EntityConstraintViolation>() {

        @Override
        public int compare(EntityConstraintViolation o1, EntityConstraintViolation o2) {
            return o1.getPropertyPath().toString().compareTo(o2.getPropertyPath().toString());
        }
    });
    assertEquals("Unexpected propertypath", list.get(0).getPropertyPath().toString(), "company.name");
    assertEquals("Unexpected propertypath", list.get(1).getPropertyPath().toString(), "givenName");
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) Company(eu.etaxonomy.cdm.persistence.validation.Company) ValidatorFactory(javax.validation.ValidatorFactory) ArrayList(java.util.ArrayList) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Employee(eu.etaxonomy.cdm.persistence.validation.Employee) EntityConstraintViolation(eu.etaxonomy.cdm.model.validation.EntityConstraintViolation) ConstraintViolation(javax.validation.ConstraintViolation) EntityConstraintViolation(eu.etaxonomy.cdm.model.validation.EntityConstraintViolation) EntityValidation(eu.etaxonomy.cdm.model.validation.EntityValidation) UUID(java.util.UUID) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test)

Example 2 with Employee

use of eu.etaxonomy.cdm.persistence.validation.Employee in project cdmlib by cybertaxonomy.

the class EntityValidationCrudJdbcImplTest method test_SaveValidationResult_Set_T_CRUDEventType.

/**
 * Test method for
 * {@link eu.etaxonomy.cdm.persistence.dao.jdbc.validation.EntityValidationCrudJdbcImpl#saveEntityValidation (eu.etaxonomy.cdm.model.common.CdmBase, java.util.Set, eu.etaxonomy.cdm.model.validation.CRUDEventType, Class)}
 * .
 */
@Test
@DataSet
public void test_SaveValidationResult_Set_T_CRUDEventType() {
    HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
    ValidatorFactory factory = config.buildValidatorFactory();
    // This is the bean that is going to be tested
    Employee emp = new Employee();
    emp.setId(1);
    UUID uuid = emp.getUuid();
    // ERROR 1 (should be JOHN)
    emp.setGivenName("john");
    // This is an error (should be SMITH), but it is a Level-3
    // validation error, so the error should be ignored
    emp.setFamilyName("smith");
    // This is an @Valid bean on the Employee class, so Level-2
    // validation errors on the Company object should also be
    // listed.
    Company comp = new Company();
    // ERROR 2 (should be GOOGLE)
    comp.setName("Google");
    emp.setCompany(comp);
    Set<ConstraintViolation<Employee>> errors = factory.getValidator().validate(emp, Level2.class);
    EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
    dao.saveEntityValidation(emp, errors, CRUDEventType.NONE, null);
    EntityValidation result = dao.getEntityValidation(emp.getClass().getName(), emp.getId());
    assertNotNull(result);
    assertEquals("Unexpected UUID", result.getValidatedEntityUuid(), uuid);
    assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
    Set<EntityConstraintViolation> violations = result.getEntityConstraintViolations();
    List<EntityConstraintViolation> list = new ArrayList<EntityConstraintViolation>(violations);
    Collections.sort(list, new Comparator<EntityConstraintViolation>() {

        @Override
        public int compare(EntityConstraintViolation o1, EntityConstraintViolation o2) {
            return o1.getPropertyPath().toString().compareTo(o2.getPropertyPath().toString());
        }
    });
    assertEquals("Unexpected propertypath", list.get(0).getPropertyPath().toString(), "company.name");
    assertEquals("Unexpected propertypath", list.get(1).getPropertyPath().toString(), "givenName");
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) Company(eu.etaxonomy.cdm.persistence.validation.Company) ValidatorFactory(javax.validation.ValidatorFactory) ArrayList(java.util.ArrayList) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) Employee(eu.etaxonomy.cdm.persistence.validation.Employee) EntityConstraintViolation(eu.etaxonomy.cdm.model.validation.EntityConstraintViolation) ConstraintViolation(javax.validation.ConstraintViolation) EntityConstraintViolation(eu.etaxonomy.cdm.model.validation.EntityConstraintViolation) EntityValidation(eu.etaxonomy.cdm.model.validation.EntityValidation) UUID(java.util.UUID) CdmIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmIntegrationTest) Test(org.junit.Test) ExpectedDataSet(org.unitils.dbunit.annotation.ExpectedDataSet) DataSet(org.unitils.dbunit.annotation.DataSet)

Example 3 with Employee

use of eu.etaxonomy.cdm.persistence.validation.Employee in project cdmlib by cybertaxonomy.

the class EntityValidationCrudJdbcImplTest method testOneOldOneNewError.

@Test
@DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
@ExpectedDataSet("EntityValidationCrudJdbcImplTest.testOneOldOneNewError-result.xml")
public // having 1 entityconstraintviolation)
void testOneOldOneNewError() {
    DateTime created = new DateTime(2014, 1, 1, 0, 0);
    // Same entity as in @DataSet
    Employee emp = new Employee();
    emp.setId(100);
    emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218c"));
    // Other properties not relevant for this test
    EntityValidation entityValidation = EntityValidation.newInstance();
    entityValidation.setValidatedEntity(emp);
    entityValidation.setId(1);
    entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c18"));
    entityValidation.setCreated(created);
    entityValidation.setCrudEventType(CRUDEventType.INSERT);
    // Old error (in @DataSet)
    EntityConstraintViolation error = EntityConstraintViolation.newInstance();
    error.setId(Integer.MIN_VALUE);
    error.setCreated(created);
    error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425ba"));
    error.setSeverity(Severity.ERROR);
    error.setPropertyPath("givenName");
    error.setInvalidValue("Foo");
    error.setMessage("Garbage In Garbage Out");
    error.setValidationGroup("eu.etaxonomy.cdm.validation.Level2");
    error.setValidator("eu.etaxonomy.cdm.persistence.validation.GarbageValidator");
    entityValidation.addEntityConstraintViolation(error);
    // New error (not in @DataSet)
    error = EntityConstraintViolation.newInstance();
    // Don't leave ID generation to chance; we want it to be same as in
    // @ExpectedDataSet
    error.setId(2);
    error.setCreated(created);
    error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425bb"));
    error.setSeverity(Severity.ERROR);
    error.setPropertyPath("familyName");
    error.setInvalidValue("Bar");
    error.setMessage("Garbage In Garbage Out");
    error.setValidationGroup("eu.etaxonomy.cdm.validation.Level2");
    error.setValidator("eu.etaxonomy.cdm.persistence.validation.FamilyNameValidator");
    entityValidation.addEntityConstraintViolation(error);
    // EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
    validationCrudJdbcDao.saveEntityValidation(entityValidation, new Class[] { Level2.class });
}
Also used : Employee(eu.etaxonomy.cdm.persistence.validation.Employee) EntityConstraintViolation(eu.etaxonomy.cdm.model.validation.EntityConstraintViolation) EntityValidation(eu.etaxonomy.cdm.model.validation.EntityValidation) DateTime(org.joda.time.DateTime) CdmIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmIntegrationTest) Test(org.junit.Test) ExpectedDataSet(org.unitils.dbunit.annotation.ExpectedDataSet) DataSet(org.unitils.dbunit.annotation.DataSet) ExpectedDataSet(org.unitils.dbunit.annotation.ExpectedDataSet)

Example 4 with Employee

use of eu.etaxonomy.cdm.persistence.validation.Employee in project cdmlib by cybertaxonomy.

the class EntityValidationCrudJdbcImplTest method testAllErrorsSolved.

@Test
@DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
@ExpectedDataSet("EntityValidationCrudJdbcImplTest.testAllErrorsSolved-result.xml")
public // increased.
void testAllErrorsSolved() {
    DateTime created = new DateTime(2014, 1, 1, 0, 0);
    Employee emp = new Employee();
    emp.setId(100);
    emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218c"));
    EntityValidation entityValidation = EntityValidation.newInstance();
    entityValidation.setValidatedEntity(emp);
    entityValidation.setId(1);
    entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c18"));
    entityValidation.setCreated(created);
    entityValidation.setCrudEventType(CRUDEventType.INSERT);
    entityValidation.setValidationCount(5);
    EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
    dao.saveEntityValidation(entityValidation, new Class[] { Level2.class });
}
Also used : Employee(eu.etaxonomy.cdm.persistence.validation.Employee) EntityValidation(eu.etaxonomy.cdm.model.validation.EntityValidation) DateTime(org.joda.time.DateTime) CdmIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmIntegrationTest) Test(org.junit.Test) ExpectedDataSet(org.unitils.dbunit.annotation.ExpectedDataSet) DataSet(org.unitils.dbunit.annotation.DataSet) ExpectedDataSet(org.unitils.dbunit.annotation.ExpectedDataSet)

Example 5 with Employee

use of eu.etaxonomy.cdm.persistence.validation.Employee in project cdmlib by cybertaxonomy.

the class EntityValidationCrudJdbcImplTest method testReplaceError.

@Test
@DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
@ExpectedDataSet("EntityValidationCrudJdbcImplTest.testReplaceError-result.xml")
public // removed and the new error is inserted.
void testReplaceError() {
    // All identical to @DataSet:
    DateTime created = new DateTime(2014, 1, 1, 0, 0);
    Employee emp = new Employee();
    emp.setId(100);
    emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218c"));
    EntityValidation entityValidation = EntityValidation.newInstance();
    entityValidation.setValidatedEntity(emp);
    entityValidation.setId(1);
    entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c18"));
    entityValidation.setCreated(created);
    entityValidation.setCrudEventType(CRUDEventType.INSERT);
    entityValidation.setValidationCount(5);
    EntityConstraintViolation error = EntityConstraintViolation.newInstance();
    error.setId(38);
    error.setCreated(created);
    error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425ba"));
    error.setSeverity(Severity.ERROR);
    error.setPropertyPath("givenName");
    // Except for:
    error.setInvalidValue("Bar");
    error.setMessage("Garbage In Garbage Out");
    error.setValidationGroup("eu.etaxonomy.cdm.validation.Level2");
    error.setValidator("eu.etaxonomy.cdm.persistence.validation.GarbageValidator");
    Set<EntityConstraintViolation> errors = new HashSet<EntityConstraintViolation>(1);
    errors.add(error);
    entityValidation.addEntityConstraintViolation(error);
    EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
    dao.saveEntityValidation(entityValidation, new Class[] { Level2.class });
}
Also used : Employee(eu.etaxonomy.cdm.persistence.validation.Employee) EntityConstraintViolation(eu.etaxonomy.cdm.model.validation.EntityConstraintViolation) EntityValidation(eu.etaxonomy.cdm.model.validation.EntityValidation) DateTime(org.joda.time.DateTime) HashSet(java.util.HashSet) CdmIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmIntegrationTest) Test(org.junit.Test) ExpectedDataSet(org.unitils.dbunit.annotation.ExpectedDataSet) DataSet(org.unitils.dbunit.annotation.DataSet) ExpectedDataSet(org.unitils.dbunit.annotation.ExpectedDataSet)

Aggregations

EntityValidation (eu.etaxonomy.cdm.model.validation.EntityValidation)7 Employee (eu.etaxonomy.cdm.persistence.validation.Employee)7 Test (org.junit.Test)7 EntityConstraintViolation (eu.etaxonomy.cdm.model.validation.EntityConstraintViolation)6 CdmIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmIntegrationTest)6 DataSet (org.unitils.dbunit.annotation.DataSet)6 ExpectedDataSet (org.unitils.dbunit.annotation.ExpectedDataSet)6 DateTime (org.joda.time.DateTime)5 HashSet (java.util.HashSet)3 Company (eu.etaxonomy.cdm.persistence.validation.Company)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 ConstraintViolation (javax.validation.ConstraintViolation)2 ValidatorFactory (javax.validation.ValidatorFactory)2 HibernateValidator (org.hibernate.validator.HibernateValidator)2 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)2 CdmTransactionalIntegrationTest (eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest)1