Project

General

Profile

Download (18 KB) Statistics
| Branch: | Tag: | Revision:
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.persistence.dao.jdbc.validation;
10

    
11
import static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertNotNull;
13
import static org.junit.Assert.assertNull;
14
import static org.junit.Assert.assertTrue;
15

    
16
import java.io.FileNotFoundException;
17
import java.util.ArrayList;
18
import java.util.Collections;
19
import java.util.Comparator;
20
import java.util.HashSet;
21
import java.util.List;
22
import java.util.Set;
23
import java.util.UUID;
24

    
25
import javax.validation.ConstraintViolation;
26
import javax.validation.Validation;
27
import javax.validation.ValidatorFactory;
28

    
29
import org.hibernate.validator.HibernateValidator;
30
import org.hibernate.validator.HibernateValidatorConfiguration;
31
import org.joda.time.DateTime;
32
import org.junit.Before;
33
import org.junit.Ignore;
34
import org.junit.Test;
35
import org.unitils.dbunit.annotation.DataSet;
36
import org.unitils.dbunit.annotation.ExpectedDataSet;
37
import org.unitils.spring.annotation.SpringBeanByType;
38

    
39
import eu.etaxonomy.cdm.model.validation.CRUDEventType;
40
import eu.etaxonomy.cdm.model.validation.EntityConstraintViolation;
41
import eu.etaxonomy.cdm.model.validation.EntityValidation;
42
import eu.etaxonomy.cdm.model.validation.Severity;
43
import eu.etaxonomy.cdm.persistence.validation.Company;
44
import eu.etaxonomy.cdm.persistence.validation.Employee;
45
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
46
import eu.etaxonomy.cdm.validation.Level2;
47

    
48
/**
49
 * @author ayco_holleman
50
 \* @since 20 jan. 2015
51
 *
52
 */
53
@Ignore
54
public class EntityValidationCrudJdbcImplTest extends CdmIntegrationTest {
55

    
56
    private static final String MEDIA = "eu.etaxonomy.cdm.model.media.Media";
57
    private static final String SYNONYM = "eu.etaxonomy.cdm.model.taxon.Synonym";
58
    private static final String GATHERING_EVENT = "eu.etaxonomy.cdm.model.occurrence.GatheringEvent";
59

    
60
    @SpringBeanByType
61
    private EntityValidationCrudJdbcImpl validationCrudJdbcDao;
62

    
63
    /**
64
     * @throws java.lang.Exception
65
     */
66
    @Before
67
    public void setUp() throws Exception {
68
    }
69

    
70
    /**
71
     * Test method for
72
     * {@link eu.etaxonomy.cdm.persistence.dao.jdbc.validation.EntityValidationCrudJdbcImpl#EntityValidationCrudJdbcImpl()}
73
     * .
74
     */
75
    @SuppressWarnings("unused")
76
    @Test
77
    public void testEntityValidationCrudJdbcImpl() {
78
        new EntityValidationCrudJdbcImpl();
79
    }
80

    
81
    /**
82
     * Test method for
83
     * {@link eu.etaxonomy.cdm.persistence.dao.jdbc.validation.EntityValidationCrudJdbcImpl#EntityValidationCrudJdbcImpl (eu.etaxonomy.cdm.database.ICdmDataSource)}
84
     * .
85
     */
86
    @SuppressWarnings("unused")
87
    @Test
88
    public void test_EntityValidationCrudJdbcImplI_CdmDataSource() {
89
        new EntityValidationCrudJdbcImpl(dataSource);
90
    }
91

    
92
    /**
93
     * Test method for
94
     * {@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)}
95
     * .
96
     */
97
    @Test
98
    @DataSet
99
    public void test_SaveValidationResult_Set_T_CRUDEventType() {
100
        HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
101
        ValidatorFactory factory = config.buildValidatorFactory();
102

    
103
        // This is the bean that is going to be tested
104
        Employee emp = new Employee();
105
        emp.setId(1);
106
        UUID uuid = emp.getUuid();
107
        // ERROR 1 (should be JOHN)
108
        emp.setFirstName("john");
109
        // This is an error (should be SMITH), but it is a Level-3
110
        // validation error, so the error should be ignored
111
        emp.setLastName("smith");
112

    
113
        // This is an @Valid bean on the Employee class, so Level-2
114
        // validation errors on the Company object should also be
115
        // listed.
116
        Company comp = new Company();
117
        // ERROR 2 (should be GOOGLE)
118
        comp.setName("Google");
119
        emp.setCompany(comp);
120

    
121
        Set<ConstraintViolation<Employee>> errors = factory.getValidator().validate(emp, Level2.class);
122
        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
123
        dao.saveEntityValidation(emp, errors, CRUDEventType.NONE, null);
124

    
125
        EntityValidation result = dao.getEntityValidation(emp.getClass().getName(), emp.getId());
126
        assertNotNull(result);
127
        assertEquals("Unexpected UUID", result.getValidatedEntityUuid(), uuid);
128
        assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
129
        Set<EntityConstraintViolation> violations = result.getEntityConstraintViolations();
130
        List<EntityConstraintViolation> list = new ArrayList<EntityConstraintViolation>(violations);
131
        Collections.sort(list, new Comparator<EntityConstraintViolation>() {
132
            @Override
133
            public int compare(EntityConstraintViolation o1, EntityConstraintViolation o2) {
134
                return o1.getPropertyPath().toString().compareTo(o2.getPropertyPath().toString());
135
            }
136
        });
137
        assertEquals("Unexpected propertypath", list.get(0).getPropertyPath().toString(), "company.name");
138
        assertEquals("Unexpected propertypath", list.get(1).getPropertyPath().toString(), "firstName");
139

    
140
    }
141

    
142
    @Test
143
    @DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
144
    @ExpectedDataSet("EntityValidationCrudJdbcImplTest.testSaveAlreadyExistingError-result.xml")
145
    // Test proving that if an exactly identical
146
    // EntityConstraintViolation (as per equals() method)
147
    // is already in database, the only thing that happens
148
    // is an increment of the validation counter.
149
    public void testSaveAlreadyExistingError() {
150

    
151
        // All same as in @DataSet:
152

    
153
        DateTime created = new DateTime(2014, 1, 1, 0, 0);
154

    
155
        Employee emp = new Employee();
156
        emp.setId(100);
157
        emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218c"));
158
        // Other properties not relevant for this test
159

    
160
        EntityValidation entityValidation = EntityValidation.newInstance();
161
        entityValidation.setValidatedEntity(emp);
162
        entityValidation.setId(1);
163
        entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c18"));
164
        entityValidation.setCreated(created);
165
        entityValidation.setCrudEventType(CRUDEventType.INSERT);
166
        entityValidation.setValidationCount(5);
167

    
168
        EntityConstraintViolation error = EntityConstraintViolation.newInstance();
169

    
170
        // Actually not same as in @DataSet to force
171
        // EntityConstraintViolation.equals() method to take
172
        // other properties into account (e.g. propertyPath,
173
        // invalidValue, etc.)
174
        error.setId(Integer.MIN_VALUE);
175

    
176
        error.setCreated(created);
177
        error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425ba"));
178
        error.setSeverity(Severity.ERROR);
179
        error.setPropertyPath("firstName");
180
        error.setInvalidValue("Foo");
181
        error.setMessage("Garbage In Garbage Out");
182
        error.setValidationGroup("eu.etaxonomy.cdm.validation.Level2");
183
        error.setValidator("eu.etaxonomy.cdm.persistence.validation.GarbageValidator");
184
        Set<EntityConstraintViolation> errors = new HashSet<EntityConstraintViolation>(1);
185
        errors.add(error);
186

    
187
        entityValidation.addEntityConstraintViolation(error);
188

    
189
        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
190
        dao.saveEntityValidation(entityValidation, new Class[] { Level2.class });
191
    }
192

    
193
    @Test
194
    @DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
195
    @ExpectedDataSet("EntityValidationCrudJdbcImplTest.testReplaceError-result.xml")
196
    // Test proving that if an entity has been validated,
197
    // yielding 1 error (as in @DataSet), and a subsequent
198
    // validation also yields 1 error, but a different one,
199
    // then validation count is increased, the old error is
200
    // removed and the new error is inserted.
201
    public void testReplaceError() {
202

    
203
        // All identical to @DataSet:
204

    
205
        DateTime created = new DateTime(2014, 1, 1, 0, 0);
206

    
207
        Employee emp = new Employee();
208
        emp.setId(100);
209
        emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218c"));
210

    
211
        EntityValidation entityValidation = EntityValidation.newInstance();
212
        entityValidation.setValidatedEntity(emp);
213
        entityValidation.setId(1);
214
        entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c18"));
215
        entityValidation.setCreated(created);
216
        entityValidation.setCrudEventType(CRUDEventType.INSERT);
217
        entityValidation.setValidationCount(5);
218

    
219
        EntityConstraintViolation error = EntityConstraintViolation.newInstance();
220
        error.setId(38);
221
        error.setCreated(created);
222
        error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425ba"));
223
        error.setSeverity(Severity.ERROR);
224
        error.setPropertyPath("firstName");
225

    
226
        // Except for:
227
        error.setInvalidValue("Bar");
228

    
229
        error.setMessage("Garbage In Garbage Out");
230
        error.setValidationGroup("eu.etaxonomy.cdm.validation.Level2");
231
        error.setValidator("eu.etaxonomy.cdm.persistence.validation.GarbageValidator");
232
        Set<EntityConstraintViolation> errors = new HashSet<EntityConstraintViolation>(1);
233
        errors.add(error);
234

    
235
        entityValidation.addEntityConstraintViolation(error);
236

    
237
        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
238
        dao.saveEntityValidation(entityValidation, new Class[] { Level2.class });
239
    }
240

    
241
    @Test
242
    @DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
243
    @ExpectedDataSet("EntityValidationCrudJdbcImplTest.testSameErrorOtherEntity-result.xml")
244
    // Test proving that if an entity has been validated,
245
    // yielding 1 error (as in @DataSet), and _another_
246
    // entity is now validated yielding an equals() error,
247
    // things behave as expected (2 entityvalidations, each
248
    // having 1 entityconstraintviolation)
249
    public void testSameErrorOtherEntity() {
250

    
251
        DateTime created = new DateTime(2014, 1, 1, 0, 0);
252

    
253
        // Not in @DataSet
254
        Employee emp = new Employee();
255
        emp.setId(200);
256
        emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218d"));
257

    
258
        EntityValidation entityValidation = EntityValidation.newInstance();
259
        entityValidation.setValidatedEntity(emp);
260
        entityValidation.setId(2);
261
        entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c19"));
262
        entityValidation.setCreated(created);
263
        entityValidation.setCrudEventType(CRUDEventType.INSERT);
264
        entityValidation.setValidationCount(1);
265

    
266
        // equals() error in @DataSet
267
        EntityConstraintViolation error = EntityConstraintViolation.newInstance();
268
        error.setId(2);
269
        error.setCreated(created);
270
        error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425bb"));
271
        error.setSeverity(Severity.ERROR);
272
        error.setPropertyPath("firstName");
273
        error.setInvalidValue("Foo");
274

    
275
        error.setMessage("Garbage In Garbage Out");
276
        error.setValidationGroup("eu.etaxonomy.cdm.validation.Level2");
277
        error.setValidator("eu.etaxonomy.cdm.persistence.validation.GarbageValidator");
278
        Set<EntityConstraintViolation> errors = new HashSet<EntityConstraintViolation>(1);
279
        errors.add(error);
280

    
281
        entityValidation.addEntityConstraintViolation(error);
282

    
283
        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
284
        dao.saveEntityValidation(entityValidation, new Class[] { Level2.class });
285
    }
286
    @Test
287
    @DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
288
    @ExpectedDataSet("EntityValidationCrudJdbcImplTest.testOneOldOneNewError-result.xml")
289
    // Test proving that if an entity has been validated,
290
    // yielding 1 error (as in @DataSet), and _another_
291
    // entity is now validated yielding an equals() error,
292
    // things behave as expected (2 entityvalidations, each
293
    // having 1 entityconstraintviolation)
294
    public void testOneOldOneNewError() {
295

    
296
        DateTime created = new DateTime(2014, 1, 1, 0, 0);
297

    
298
        // Same entity as in @DataSet
299
        Employee emp = new Employee();
300
        emp.setId(100);
301
        emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218c"));
302
        // Other properties not relevant for this test
303

    
304
        EntityValidation entityValidation = EntityValidation.newInstance();
305
        entityValidation.setValidatedEntity(emp);
306
        entityValidation.setId(1);
307
        entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c18"));
308
        entityValidation.setCreated(created);
309
        entityValidation.setCrudEventType(CRUDEventType.INSERT);
310

    
311

    
312
        // Old error (in @DataSet)
313
        EntityConstraintViolation error = EntityConstraintViolation.newInstance();
314
        error.setId(Integer.MIN_VALUE);
315
        error.setCreated(created);
316
        error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425ba"));
317
        error.setSeverity(Severity.ERROR);
318
        error.setPropertyPath("firstName");
319
        error.setInvalidValue("Foo");
320
        error.setMessage("Garbage In Garbage Out");
321
        error.setValidationGroup("eu.etaxonomy.cdm.validation.Level2");
322
        error.setValidator("eu.etaxonomy.cdm.persistence.validation.GarbageValidator");
323
        entityValidation.addEntityConstraintViolation(error);
324

    
325
        // New error (not in @DataSet)
326
        error = EntityConstraintViolation.newInstance();
327
        // Don't leave ID generation to chance; we want it to be same as in
328
        // @ExpectedDataSet
329
        error.setId(2);
330
        error.setCreated(created);
331
        error.setUuid(UUID.fromString("358da71f-b646-4b79-b00e-dcb68b6425bb"));
332
        error.setSeverity(Severity.ERROR);
333
        error.setPropertyPath("lastName");
334
        error.setInvalidValue("Bar");
335
        error.setMessage("Garbage In Garbage Out");
336
        error.setValidationGroup("eu.etaxonomy.cdm.validation.Level2");
337
        error.setValidator("eu.etaxonomy.cdm.persistence.validation.LastNameValidator");
338
        entityValidation.addEntityConstraintViolation(error);
339

    
340
//        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
341
        validationCrudJdbcDao.saveEntityValidation(entityValidation, new Class[] { Level2.class });
342
    }
343

    
344

    
345

    
346
    @Test
347
    @DataSet("EntityValidationCrudJdbcImplTest.testSave.xml")
348
    @ExpectedDataSet("EntityValidationCrudJdbcImplTest.testAllErrorsSolved-result.xml")
349
    // Test proving that if an entity has been validated,
350
    // yielding 1 error (as in @DataSet), and a subsequent
351
    // validation yields 0 errors, all that remains is an
352
    // EntityValidation record with its validation counter
353
    // increased.
354
    public void testAllErrorsSolved() {
355

    
356
        DateTime created = new DateTime(2014, 1, 1, 0, 0);
357

    
358
        Employee emp = new Employee();
359
        emp.setId(100);
360
        emp.setUuid(UUID.fromString("f8de74c6-aa56-4de3-931e-87b61da0218c"));
361

    
362
        EntityValidation entityValidation = EntityValidation.newInstance();
363
        entityValidation.setValidatedEntity(emp);
364
        entityValidation.setId(1);
365
        entityValidation.setUuid(UUID.fromString("dae5b090-30e8-45bc-9460-2eb2028d3c18"));
366
        entityValidation.setCreated(created);
367
        entityValidation.setCrudEventType(CRUDEventType.INSERT);
368
        entityValidation.setValidationCount(5);
369

    
370
        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
371
        dao.saveEntityValidation(entityValidation, new Class[] { Level2.class });
372
    }
373

    
374
    /**
375
     * Test method for
376
     * {@link eu.etaxonomy.cdm.persistence.dao.jdbc.validation.EntityValidationCrudJdbcImpl#deleteEntityValidation (java.lang.String, int)}
377
     * .
378
     */
379
    @Test
380
    @DataSet
381
    @ExpectedDataSet
382
    public void test_DeleteValidationResult() {
383
        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
384
        dao.deleteEntityValidation(SYNONYM, 200);
385
        EntityValidation result = dao.getEntityValidation(SYNONYM, 200);
386
        assertTrue(result == null);
387
    }
388

    
389
    @Test
390
    @DataSet
391
    public void testGetEntityValidation() {
392
        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl(dataSource);
393
        EntityValidation result;
394

    
395
        result = dao.getEntityValidation(MEDIA, 100);
396
        assertNotNull("A validation result for media id=100 should exist", result);
397
        assertEquals("Unexpected entity id", 1, result.getId());
398
        assertEquals("Unexpected number of constraint violations", 1, result.getEntityConstraintViolations().size());
399

    
400
        result = dao.getEntityValidation(SYNONYM, 200);
401
        assertNotNull(result);
402
        assertEquals("Unexpected entity id", 2, result.getId());
403
        assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
404

    
405
        result = dao.getEntityValidation(GATHERING_EVENT, 300);
406
        assertNotNull(result);
407
        assertEquals("Unexpected entity id", 3, result.getId());
408
        assertEquals("Unexpected number of constraint violations", 3, result.getEntityConstraintViolations().size());
409

    
410
        result = dao.getEntityValidation(GATHERING_EVENT, 301);
411
        assertNotNull(result);
412
        assertEquals("Unexpected entity id", 4, result.getId());
413
        assertEquals("Unexpected number of constraint violations", 1, result.getEntityConstraintViolations().size());
414

    
415
        // Test we get a null back
416
        result = dao.getEntityValidation("Foo Bar", 100);
417
        assertNull(result);
418
    }
419

    
420
    /**
421
     * Test method for
422
     * {@link eu.etaxonomy.cdm.persistence.dao.jdbc.validation.EntityValidationCrudJdbcImpl#setDatasource (eu.etaxonomy.cdm.database.ICdmDataSource)}
423
     * .
424
     */
425
    @Test
426
    public void testSetDatasource() {
427
        EntityValidationCrudJdbcImpl dao = new EntityValidationCrudJdbcImpl();
428
        dao.setDatasource(dataSource);
429
    }
430

    
431
    @Override
432
    public void createTestDataSet() throws FileNotFoundException {
433
    }
434

    
435
}
    (1-1/1)