1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.validation.batch;
|
10
|
|
11
|
import java.io.FileNotFoundException;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.List;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import javax.validation.Validation;
|
17
|
import javax.validation.Validator;
|
18
|
import javax.validation.ValidatorFactory;
|
19
|
|
20
|
import org.hibernate.validator.HibernateValidator;
|
21
|
import org.hibernate.validator.HibernateValidatorConfiguration;
|
22
|
import org.junit.Assert;
|
23
|
import org.junit.Ignore;
|
24
|
import org.junit.Test;
|
25
|
import org.springframework.context.ApplicationContext;
|
26
|
import org.unitils.spring.annotation.SpringApplicationContext;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
29
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
30
|
import eu.etaxonomy.cdm.api.service.IEntityValidationService;
|
31
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
32
|
import eu.etaxonomy.cdm.database.CdmDataSource;
|
33
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
34
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
35
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
36
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
37
|
import eu.etaxonomy.cdm.model.validation.EntityValidation;
|
38
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
39
|
|
40
|
/**
|
41
|
* @author admin.ayco.holleman
|
42
|
\* @since 11 feb. 2015
|
43
|
*
|
44
|
*/
|
45
|
public class BatchValidatorTest extends CdmTransactionalIntegrationTest {
|
46
|
|
47
|
// @SpringBeanByType
|
48
|
// private BatchValidator batchValidator;
|
49
|
|
50
|
// @SpringBeanByName //does not exist as such
|
51
|
private ICdmRepository app;
|
52
|
|
53
|
@SpringApplicationContext
|
54
|
private ApplicationContext applicationContext;
|
55
|
|
56
|
/**
|
57
|
* Test method for
|
58
|
* {@link eu.etaxonomy.cdm.api.validation.batch.BatchValidator#run()}.
|
59
|
*
|
60
|
* @throws SQLException
|
61
|
*/
|
62
|
@Test
|
63
|
@Ignore
|
64
|
public void testRun() throws SQLException {
|
65
|
|
66
|
ICdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("PUBLIC", "sa", "");
|
67
|
app = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.CREATE);
|
68
|
|
69
|
HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
|
70
|
ValidatorFactory validatorFactory = config.buildValidatorFactory();
|
71
|
Validator validator = validatorFactory.getValidator();
|
72
|
|
73
|
BatchValidator batchValidator = new BatchValidator();
|
74
|
batchValidator.setAppController(app);
|
75
|
batchValidator.setValidator(validator);
|
76
|
batchValidator.setValidationGroups(BatchValidator.DEFAULT_VALIDATION_GROUPS);
|
77
|
|
78
|
// Create 10 References of type book and 10 of type Journal
|
79
|
|
80
|
IReferenceService refService = app.getReferenceService();
|
81
|
for (int i = 0; i < 10; ++i) {
|
82
|
Reference ref0 = ReferenceFactory.newBook();
|
83
|
ref0.setIsbn("bla bla");
|
84
|
ref0.setIssn("foo foo");
|
85
|
// Each book should violate 3 constraints
|
86
|
if (i == 0) {
|
87
|
Set<?> errors = validator.validate(ref0, BatchValidator.DEFAULT_VALIDATION_GROUPS);
|
88
|
// We 're really not testing the BatchValidator here, but our
|
89
|
// knowledge of the constraints on the Reference class, so we
|
90
|
// do a plain java language assert here.
|
91
|
assert (errors.size() == 3);
|
92
|
}
|
93
|
refService.save(ref0);
|
94
|
Reference ref1 = ReferenceFactory.newJournal();
|
95
|
ref1.setIsbn("bar bar");
|
96
|
ref1.setIssn("baz baz");
|
97
|
// Each journal should violate 4 constraints
|
98
|
if (i == 0) {
|
99
|
Set<?> errors = validator.validate(ref1, BatchValidator.DEFAULT_VALIDATION_GROUPS);
|
100
|
assert (errors.size() == 4);
|
101
|
}
|
102
|
refService.save(ref1);
|
103
|
}
|
104
|
|
105
|
batchValidator.run();
|
106
|
|
107
|
// So we should have 20 validation results (10 for books, 10 for
|
108
|
// journals);
|
109
|
IEntityValidationService validationResultService = app.getEntityValidationService();
|
110
|
List<EntityValidation> results = validationResultService.getValidationResults();
|
111
|
Assert.assertEquals("Expected 20 validation results", 20, results.size());
|
112
|
|
113
|
// And we should have a total of 70 (10 * (3+4)) constraint violations
|
114
|
int errors = 0;
|
115
|
for(EntityValidation result: results) {
|
116
|
errors += result.getEntityConstraintViolations().size();
|
117
|
}
|
118
|
Assert.assertEquals("Expected 70 errors", 70, errors);
|
119
|
|
120
|
}
|
121
|
|
122
|
@Override
|
123
|
public void createTestDataSet() throws FileNotFoundException {
|
124
|
// TODO Auto-generated method stub
|
125
|
}
|
126
|
|
127
|
}
|