Search in sources :

Example 21 with EntityValidation

use of eu.etaxonomy.cdm.model.validation.EntityValidation in project cdmlib by cybertaxonomy.

the class EntityValidationCrudJdbcImplTest method testSaveAlreadyExistingError.

@Test
@DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
@ExpectedDataSet("EntityValidationCrudJdbcImplTest.testSaveAlreadyExistingError-result.xml")
public // is an increment of the validation counter.
void testSaveAlreadyExistingError() {
    // All same as in @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"));
    // 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);
    entityValidation.setValidationCount(5);
    EntityConstraintViolation error = EntityConstraintViolation.newInstance();
    // Actually not same as in @DataSet to force
    // EntityConstraintViolation.equals() method to take
    // other properties into account (e.g. propertyPath,
    // invalidValue, etc.)
    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");
    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)

Example 22 with EntityValidation

use of eu.etaxonomy.cdm.model.validation.EntityValidation in project cdmlib by cybertaxonomy.

the class EntityValidationCrudJdbcImplTest method testSameErrorOtherEntity.

@Test
@DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
@ExpectedDataSet("EntityValidationCrudJdbcImplTest.testSameErrorOtherEntity-result.xml")
public // having 1 entityconstraintviolation)
void testSameErrorOtherEntity() {
    DateTime created = new DateTime(2014, 1, 1, 0, 0);
    // Not in @DataSet
    Employee emp = new Employee();
    emp.setId(200);
    emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218d"));
    EntityValidation entityValidation = EntityValidation.newInstance();
    entityValidation.setValidatedEntity(emp);
    entityValidation.setId(2);
    entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c19"));
    entityValidation.setCreated(created);
    entityValidation.setCrudEventType(CRUDEventType.INSERT);
    entityValidation.setValidationCount(1);
    // equals() error in @DataSet
    EntityConstraintViolation error = EntityConstraintViolation.newInstance();
    error.setId(2);
    error.setCreated(created);
    error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425bb"));
    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");
    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)

Example 23 with EntityValidation

use of eu.etaxonomy.cdm.model.validation.EntityValidation in project cdmlib by cybertaxonomy.

the class EntityValidationCrudJdbcImplTest method test_DeleteValidationResult.

/**
 * Test method for
 * {@link eu.etaxonomy.cdm.persistence.dao.jdbc.validation.EntityValidationCrudJdbcImpl#deleteEntityValidation (java.lang.String, int)}
 * .
 */
@Test
@DataSet
@ExpectedDataSet
public void test_DeleteValidationResult() {
    EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
    dao.deleteEntityValidation(SYNONYM, 200);
    EntityValidation result = dao.getEntityValidation(SYNONYM, 200);
    assertTrue(result == null);
}
Also used : EntityValidation(eu.etaxonomy.cdm.model.validation.EntityValidation) 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 24 with EntityValidation

use of eu.etaxonomy.cdm.model.validation.EntityValidation in project cdmlib by cybertaxonomy.

the class BatchValidatorTest method testRun.

/**
 * Test method for
 * {@link eu.etaxonomy.cdm.api.validation.batch.BatchValidator#run()}.
 *
 * @throws SQLException
 */
@Test
@Ignore
public void testRun() throws SQLException {
    ICdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("PUBLIC", "sa", "");
    app = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.CREATE);
    HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
    ValidatorFactory validatorFactory = config.buildValidatorFactory();
    Validator validator = validatorFactory.getValidator();
    BatchValidator batchValidator = new BatchValidator();
    batchValidator.setAppController(app);
    batchValidator.setValidator(validator);
    batchValidator.setValidationGroups(BatchValidator.DEFAULT_VALIDATION_GROUPS);
    // Create 10 References of type book and 10 of type Journal
    IReferenceService refService = app.getReferenceService();
    for (int i = 0; i < 10; ++i) {
        Reference ref0 = ReferenceFactory.newBook();
        ref0.setIsbn("bla bla");
        ref0.setIssn("foo foo");
        // Each book should violate 3 constraints
        if (i == 0) {
            Set<?> errors = validator.validate(ref0, BatchValidator.DEFAULT_VALIDATION_GROUPS);
            // do a plain java language assert here.
            assert (errors.size() == 3);
        }
        refService.save(ref0);
        Reference ref1 = ReferenceFactory.newJournal();
        ref1.setIsbn("bar bar");
        ref1.setIssn("baz baz");
        // Each journal should violate 4 constraints
        if (i == 0) {
            Set<?> errors = validator.validate(ref1, BatchValidator.DEFAULT_VALIDATION_GROUPS);
            assert (errors.size() == 4);
        }
        refService.save(ref1);
    }
    batchValidator.run();
    // So we should have 20 validation results (10 for books, 10 for
    // journals);
    IEntityValidationService validationResultService = app.getEntityValidationService();
    List<EntityValidation> results = validationResultService.getValidationResults();
    Assert.assertEquals("Expected 20 validation results", 20, results.size());
    // And we should have a total of 70 (10 * (3+4)) constraint violations
    int errors = 0;
    for (EntityValidation result : results) {
        errors += result.getEntityConstraintViolations().size();
    }
    Assert.assertEquals("Expected 70 errors", 70, errors);
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) Reference(eu.etaxonomy.cdm.model.reference.Reference) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) IReferenceService(eu.etaxonomy.cdm.api.service.IReferenceService) ICdmDataSource(eu.etaxonomy.cdm.database.ICdmDataSource) IEntityValidationService(eu.etaxonomy.cdm.api.service.IEntityValidationService) EntityValidation(eu.etaxonomy.cdm.model.validation.EntityValidation) HibernateValidator(org.hibernate.validator.HibernateValidator) Validator(javax.validation.Validator) Ignore(org.junit.Ignore) CdmTransactionalIntegrationTest(eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest) Test(org.junit.Test)

Aggregations

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