1
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.validation;
|
2
|
|
3
|
import static org.junit.Assert.assertEquals;
|
4
|
import static org.junit.Assert.assertNotNull;
|
5
|
import static org.junit.Assert.assertNull;
|
6
|
|
7
|
import java.io.FileNotFoundException;
|
8
|
import java.util.ArrayList;
|
9
|
import java.util.Collections;
|
10
|
import java.util.Comparator;
|
11
|
import java.util.List;
|
12
|
import java.util.Set;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import javax.validation.ConstraintViolation;
|
16
|
import javax.validation.Validation;
|
17
|
import javax.validation.ValidatorFactory;
|
18
|
|
19
|
import org.hibernate.validator.HibernateValidator;
|
20
|
import org.hibernate.validator.HibernateValidatorConfiguration;
|
21
|
import org.junit.Test;
|
22
|
import org.unitils.dbunit.annotation.DataSet;
|
23
|
import org.unitils.dbunit.annotation.ExpectedDataSet;
|
24
|
import org.unitils.spring.annotation.SpringBeanByType;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.validation.CRUDEventType;
|
27
|
import eu.etaxonomy.cdm.model.validation.EntityConstraintViolation;
|
28
|
import eu.etaxonomy.cdm.model.validation.EntityValidation;
|
29
|
import eu.etaxonomy.cdm.model.validation.Severity;
|
30
|
import eu.etaxonomy.cdm.persistence.dao.validation.IEntityValidationDao;
|
31
|
import eu.etaxonomy.cdm.persistence.validation.Company;
|
32
|
import eu.etaxonomy.cdm.persistence.validation.Employee;
|
33
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
34
|
import eu.etaxonomy.cdm.validation.Level2;
|
35
|
|
36
|
@DataSet
|
37
|
public class EntityValidationDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
|
38
|
|
39
|
private static final String MEDIA = "eu.etaxonomy.cdm.model.media.Media";
|
40
|
private static final String SYNONYM = "eu.etaxonomy.cdm.model.taxon.Synonym";
|
41
|
private static final String GATHERING_EVENT = "eu.etaxonomy.cdm.model.occurrence.GatheringEvent";
|
42
|
|
43
|
@SpringBeanByType
|
44
|
private IEntityValidationDao dao;
|
45
|
|
46
|
@Test
|
47
|
public void init() {
|
48
|
assertNotNull("Expecting an instance of IEntityValidationDao", dao);
|
49
|
}
|
50
|
|
51
|
@Test
|
52
|
public void testSaveValidationResult() {
|
53
|
|
54
|
HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
|
55
|
ValidatorFactory factory = config.buildValidatorFactory();
|
56
|
|
57
|
// This is the bean that is going to be tested
|
58
|
Employee emp = new Employee();
|
59
|
emp.setId(1);
|
60
|
UUID uuid = emp.getUuid();
|
61
|
// ERROR 1 (should be JOHN)
|
62
|
emp.setGivenName("john");
|
63
|
// This is an error (should be SMITH), but it is a Level-3
|
64
|
// validation error, so the error should be ignored
|
65
|
emp.setFamilyName("smith");
|
66
|
|
67
|
// This is an @Valid bean on the Employee class, so Level-2
|
68
|
// validation errors on the Company object should also be
|
69
|
// listed.
|
70
|
Company comp = new Company();
|
71
|
// ERROR 2 (should be GOOGLE)
|
72
|
comp.setName("Google");
|
73
|
emp.setCompany(comp);
|
74
|
|
75
|
Set<ConstraintViolation<Employee>> errors = factory.getValidator().validate(emp, Level2.class);
|
76
|
dao.saveEntityValidation(emp, errors, CRUDEventType.NONE, null);
|
77
|
|
78
|
EntityValidation result = dao.getEntityValidation(Employee.class.getName(), 1);
|
79
|
assertNotNull(result);
|
80
|
assertEquals("Unexpected UUID", result.getValidatedEntityUuid(), uuid);
|
81
|
assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
|
82
|
Set<EntityConstraintViolation> violations = result.getEntityConstraintViolations();
|
83
|
List<EntityConstraintViolation> list = new ArrayList<EntityConstraintViolation>(violations);
|
84
|
Collections.sort(list, new Comparator<EntityConstraintViolation>() {
|
85
|
@Override
|
86
|
public int compare(EntityConstraintViolation o1, EntityConstraintViolation o2) {
|
87
|
return o1.getPropertyPath().toString().compareTo(o2.getPropertyPath().toString());
|
88
|
}
|
89
|
});
|
90
|
assertEquals("Unexpected propertypath", list.get(0).getPropertyPath().toString(), "company.name");
|
91
|
assertEquals("Unexpected propertypath", list.get(1).getPropertyPath().toString(), "givenName");
|
92
|
}
|
93
|
|
94
|
@Test
|
95
|
@ExpectedDataSet
|
96
|
public void testDeleteValidationResult() {
|
97
|
dao.deleteEntityValidation(SYNONYM, 200);
|
98
|
|
99
|
commitAndStartNewTransaction(null);
|
100
|
|
101
|
List<EntityValidation> results = dao.getEntityValidations(SYNONYM);
|
102
|
assertEquals("Unexpected number of validation results", 0, results.size());
|
103
|
}
|
104
|
|
105
|
@Test
|
106
|
public void testGetEntityValidation() {
|
107
|
EntityValidation result;
|
108
|
|
109
|
result = dao.getEntityValidation(MEDIA, 100);
|
110
|
assertNotNull(result);
|
111
|
assertEquals("Unexpected entity id", 1, result.getId());
|
112
|
assertEquals("Unexpected number of constraint violations", 1, result.getEntityConstraintViolations().size());
|
113
|
|
114
|
result = dao.getEntityValidation(SYNONYM, 200);
|
115
|
assertNotNull(result);
|
116
|
assertEquals("Unexpected entity id", 2, result.getId());
|
117
|
assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
|
118
|
|
119
|
result = dao.getEntityValidation(GATHERING_EVENT, 300);
|
120
|
assertNotNull(result);
|
121
|
assertEquals("Unexpected entity id", 3, result.getId());
|
122
|
assertEquals("Unexpected number of constraint violations", 3, result.getEntityConstraintViolations().size());
|
123
|
|
124
|
result = dao.getEntityValidation(GATHERING_EVENT, 301);
|
125
|
assertNotNull(result);
|
126
|
assertEquals("Unexpected entity id", 4, result.getId());
|
127
|
assertEquals("Unexpected number of constraint violations", 1, result.getEntityConstraintViolations().size());
|
128
|
|
129
|
// Test we get a null back
|
130
|
result = dao.getEntityValidation("Foo Bar", 100);
|
131
|
assertNull(result);
|
132
|
}
|
133
|
|
134
|
@Test
|
135
|
public void testGetEntityValidations() {
|
136
|
List<EntityValidation> results = dao.getEntityValidations();
|
137
|
assertNotNull(results);
|
138
|
assertEquals("Unexpected number of entity validations", 4, results.size());
|
139
|
int errors = 0;
|
140
|
for(EntityValidation ev : results) {
|
141
|
errors += ev.getEntityConstraintViolations().size();
|
142
|
}
|
143
|
assertEquals("Unexpected number of errors", 7, errors);
|
144
|
}
|
145
|
|
146
|
@Test
|
147
|
public void testGetEntityValidations_String() {
|
148
|
List<EntityValidation> results;
|
149
|
|
150
|
results = dao.getEntityValidations(MEDIA);
|
151
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
152
|
assertEquals("Unexpected number of error", 1, results.get(0).getEntityConstraintViolations().size());
|
153
|
|
154
|
results = dao.getEntityValidations(SYNONYM);
|
155
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
156
|
assertEquals("Unexpected number of error", 2, results.get(0).getEntityConstraintViolations().size());
|
157
|
|
158
|
results = dao.getEntityValidations(GATHERING_EVENT);
|
159
|
assertEquals("Unexpected number of validation results", 2, results.size());
|
160
|
assertEquals("Unexpected number of error", 3, results.get(0).getEntityConstraintViolations().size());
|
161
|
assertEquals("Unexpected number of error", 1, results.get(1).getEntityConstraintViolations().size());
|
162
|
|
163
|
results = dao.getEntityValidations("foo.bar");
|
164
|
assertEquals("Unexpected number of validation results", 0, results.size());
|
165
|
}
|
166
|
|
167
|
@Test
|
168
|
public void testGetEntitiesViolatingConstraint_String() {
|
169
|
List<EntityValidation> results;
|
170
|
|
171
|
results = dao.getEntitiesViolatingConstraint("com.example.NameValidator");
|
172
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
173
|
|
174
|
results = dao.getEntitiesViolatingConstraint("com.example.DistanceToGroundValidator");
|
175
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
176
|
|
177
|
results = dao.getEntitiesViolatingConstraint("com.example.CountryValidator");
|
178
|
assertEquals("Unexpected number of validation results", 2, results.size());
|
179
|
|
180
|
results = dao.getEntitiesViolatingConstraint("foo.bar");
|
181
|
assertEquals("Unexpected number of validation results", 0, results.size());
|
182
|
}
|
183
|
|
184
|
@Test
|
185
|
public void testGetEntityValidations_String_Severity() {
|
186
|
List<EntityValidation> results;
|
187
|
|
188
|
results = dao.getEntityValidations(MEDIA, Severity.NOTICE);
|
189
|
assertEquals("Unexpected number of validation results", 0, results.size());
|
190
|
results = dao.getEntityValidations(MEDIA, Severity.WARNING);
|
191
|
assertEquals("Unexpected number of validation results", 0, results.size());
|
192
|
results = dao.getEntityValidations(MEDIA, Severity.ERROR);
|
193
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
194
|
assertEquals("Unexpected number of validation results", 1, results.iterator().next()
|
195
|
.getEntityConstraintViolations().size());
|
196
|
assertEquals("Unexpected severity", Severity.ERROR, results.iterator().next().getEntityConstraintViolations()
|
197
|
.iterator().next().getSeverity());
|
198
|
|
199
|
results = dao.getEntityValidations(SYNONYM, Severity.NOTICE);
|
200
|
assertEquals("Unexpected number of validation results", 0, results.size());
|
201
|
results = dao.getEntityValidations(SYNONYM, Severity.WARNING);
|
202
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
203
|
results = dao.getEntityValidations(SYNONYM, Severity.ERROR);
|
204
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
205
|
|
206
|
results = dao.getEntityValidations(GATHERING_EVENT, Severity.NOTICE);
|
207
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
208
|
results = dao.getEntityValidations(GATHERING_EVENT, Severity.WARNING);
|
209
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
210
|
results = dao.getEntityValidations(GATHERING_EVENT, Severity.ERROR);
|
211
|
assertEquals("Unexpected number of validation results", 2, results.size());
|
212
|
|
213
|
results = dao.getEntityValidations("foo.bar", Severity.ERROR);
|
214
|
assertEquals("Unexpected number of validation results", 0, results.size());
|
215
|
}
|
216
|
|
217
|
@Test
|
218
|
public void testGetEntityValidations_Severity() {
|
219
|
List<EntityValidation> results;
|
220
|
results = dao.getEntityValidations(Severity.NOTICE);
|
221
|
assertEquals("Unexpected number of validation results", 1, results.size());
|
222
|
results = dao.getEntityValidations(Severity.WARNING);
|
223
|
assertEquals("Unexpected number of validation results", 2, results.size());
|
224
|
results = dao.getEntityValidations(Severity.ERROR);
|
225
|
assertEquals("Unexpected number of validation results", 4, results.size());
|
226
|
}
|
227
|
|
228
|
@Override
|
229
|
public void createTestDataSet() throws FileNotFoundException {
|
230
|
// TODO Auto-generated method stub
|
231
|
}
|
232
|
|
233
|
}
|