Project

General

Profile

« Previous | Next » 

Revision 5d7aec66

Added by Ayco Hollemann over 9 years ago

Made EntityValidationResultCrudJdbcImplTest extend CdmIntegrationTest

View differences:

cdmlib-persistence/src/test/java/eu/etaxonomy/cdm/persistence/dao/hibernate/validation/EntityValidationResultDaoHibernateImplTest.java
36 36
@DataSet
37 37
public class EntityValidationResultDaoHibernateImplTest extends CdmTransactionalIntegrationTest {
38 38

  
39
	private static final String MEDIA = "eu.etaxonomy.cdm.model.media.Media";
40
	private static final String SYNONYM_RELATIONSHIP = "eu.etaxonomy.cdm.model.taxon.SynonymRelationship";
41
	private static final String GATHERING_EVENT = "eu.etaxonomy.cdm.model.occurrence.GatheringEvent";
42

  
43
	@SpringBeanByType
44
	private IEntityValidationResultDao dao;
45

  
46

  
47
	@Test
48
	public void init(){
49
		assertNotNull("Expecting an instance of IEntityValidationResultDao", dao);
50
	}
51

  
52

  
53
	@Test
54
	public void testSaveValidationResult(){
55

  
56
		HibernateValidatorConfiguration config = Validation.byProvider(HibernateValidator.class).configure();
57
		ValidatorFactory factory = config.buildValidatorFactory();
58

  
59
		// This is the bean that is going to be tested
60
		Employee emp = new Employee();
61
		emp.setId(1);
62
		UUID uuid = emp.getUuid();
63
		// ERROR 1 (should be JOHN)
64
		emp.setFirstName("john");
65
		// This is an error (should be SMITH), but it is a Level-3
66
		// validation error, so the error should be ignored
67
		emp.setLastName("smith");
68

  
69
		// This is an @Valid bean on the Employee class, so Level-2
70
		// validation errors on the Company object should also be
71
		// listed.
72
		Company comp = new Company();
73
		// ERROR 2 (should be GOOGLE)
74
		comp.setName("Google");
75
		emp.setCompany(comp);
76

  
77
		Set<ConstraintViolation<Employee>> errors = factory.getValidator().validate(emp, Level2.class);
78
		dao.saveValidationResult(errors, emp, CRUDEventType.NONE);
79

  
80
		EntityValidationResult result = dao.getValidationResult(Employee.class.getName(), 1);
81
		assertNotNull(result);
82
		assertEquals("Unexpected UUID", result.getValidatedEntityUuid(), uuid);
83
		assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
84
		Set<EntityConstraintViolation> violations = result.getEntityConstraintViolations();
85
		List<EntityConstraintViolation> list = new ArrayList<EntityConstraintViolation>(violations);
86
		Collections.sort(list, new Comparator<EntityConstraintViolation>() {
87
			@Override
88
			public int compare(EntityConstraintViolation o1, EntityConstraintViolation o2)
89
			{
90
				return o1.getPropertyPath().toString().compareTo(o2.getPropertyPath().toString());
91
			}
92
		});
93
		assertEquals("Unexpected propertypath", list.get(0).getPropertyPath().toString(), "company.name");
94
		assertEquals("Unexpected propertypath", list.get(1).getPropertyPath().toString(), "firstName");
95
	}
96

  
97

  
98
	@Test
99
	@ExpectedDataSet
100
	//@Ignore //FIXME unignore entity validation result dao delete test
101
	public void testDeleteValidationResult(){
102
		dao.deleteValidationResult(SYNONYM_RELATIONSHIP, 200);
103

  
104
		commitAndStartNewTransaction(null);
105

  
106
		List<EntityValidationResult> results = dao.getEntityValidationResults(SYNONYM_RELATIONSHIP);
107
		assertEquals("Unexpected number of validation results", 0, results.size());
108
	}
109

  
110

  
111
	@Test
112
	public void testGetEntityValidationResult(){
113
		EntityValidationResult result;
114

  
115
		result = dao.getValidationResult(MEDIA, 100);
116
		assertNotNull(result);
117
		assertEquals("Unexpected entity id", 1, result.getId());
118
		assertEquals("Unexpected number of constraint violations", 1, result.getEntityConstraintViolations().size());
119

  
120
		result = dao.getValidationResult(SYNONYM_RELATIONSHIP, 200);
121
		assertNotNull(result);
122
		assertEquals("Unexpected entity id", 2, result.getId());
123
		assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
124

  
125
		result = dao.getValidationResult(GATHERING_EVENT, 300);
126
		assertNotNull(result);
127
		assertEquals("Unexpected entity id", 3, result.getId());
128
		assertEquals("Unexpected number of constraint violations", 3, result.getEntityConstraintViolations().size());
129

  
130
		result = dao.getValidationResult(GATHERING_EVENT, 301);
131
		assertNotNull(result);
132
		assertEquals("Unexpected entity id", 4, result.getId());
133
		assertEquals("Unexpected number of constraint violations", 1, result.getEntityConstraintViolations().size());
134

  
135
		// Test we get a null back
136
		result = dao.getValidationResult("Foo Bar", 100);
137
		assertNull(result);
138
	}
139

  
140

  
141
	@Test
142
	public void testGetEntityValidationResults_String(){
143
		List<EntityValidationResult> results;
144

  
145
		results = dao.getEntityValidationResults(MEDIA);
146
		assertEquals("Unexpected number of validation results", 1, results.size());
147

  
148
		results = dao.getEntityValidationResults(SYNONYM_RELATIONSHIP);
149
		assertEquals("Unexpected number of validation results", 1, results.size());
150

  
151
		results = dao.getEntityValidationResults(GATHERING_EVENT);
152
		assertEquals("Unexpected number of validation results", 2, results.size());
153

  
154
		results = dao.getEntityValidationResults("foo.bar");
155
		assertEquals("Unexpected number of validation results", 0, results.size());
156
	}
157

  
158

  
159
	@Test
160
	public void testGetEntitiesViolatingConstraint_String(){
161
		List<EntityValidationResult> results;
162

  
163
		results = dao.getEntitiesViolatingConstraint("com.example.NameValidator");
164
		assertEquals("Unexpected number of validation results", 1, results.size());
165

  
166
		results = dao.getEntitiesViolatingConstraint("com.example.DistanceToGroundValidator");
167
		assertEquals("Unexpected number of validation results", 1, results.size());
168

  
169
		results = dao.getEntitiesViolatingConstraint("com.example.CountryValidator");
170
		assertEquals("Unexpected number of validation results", 2, results.size());
171

  
172
		results = dao.getEntitiesViolatingConstraint("foo.bar");
173
		assertEquals("Unexpected number of validation results", 0, results.size());
174
	}
175

  
176

  
177
	@Test
178
	public void testGetEntityValidationResults_String_Severity(){
179
		List<EntityValidationResult> results;
180

  
181
		results = dao.getValidationResults(MEDIA, Severity.NOTICE);
182
		assertEquals("Unexpected number of validation results", 0, results.size());
183
		results = dao.getValidationResults(MEDIA, Severity.WARNING);
184
		assertEquals("Unexpected number of validation results", 0, results.size());
185
		results = dao.getValidationResults(MEDIA, Severity.ERROR);
186
		assertEquals("Unexpected number of validation results", 1, results.size());
187
		assertEquals("Unexpected number of validation results", 1, results.iterator().next().getEntityConstraintViolations().size());
188
		assertEquals("Unexpected severity", Severity.ERROR, results.iterator().next().getEntityConstraintViolations().iterator().next().getSeverity());
189

  
190
		results = dao.getValidationResults(SYNONYM_RELATIONSHIP, Severity.NOTICE);
191
		assertEquals("Unexpected number of validation results", 0, results.size());
192
		results = dao.getValidationResults(SYNONYM_RELATIONSHIP, Severity.WARNING);
193
		assertEquals("Unexpected number of validation results", 1, results.size());
194
		results = dao.getValidationResults(SYNONYM_RELATIONSHIP, Severity.ERROR);
195
		assertEquals("Unexpected number of validation results", 1, results.size());
196

  
197
		results = dao.getValidationResults(GATHERING_EVENT, Severity.NOTICE);
198
		assertEquals("Unexpected number of validation results", 1, results.size());
199
		results = dao.getValidationResults(GATHERING_EVENT, Severity.WARNING);
200
		assertEquals("Unexpected number of validation results", 1, results.size());
201
		results = dao.getValidationResults(GATHERING_EVENT, Severity.ERROR);
202
		assertEquals("Unexpected number of validation results", 2, results.size());
203

  
204
		results = dao.getValidationResults("foo.bar", Severity.ERROR);
205
		assertEquals("Unexpected number of validation results", 0, results.size());
206
	}
207

  
208

  
209
	@Test
210
	public void testGetEntityValidationResults_Severity(){
211
		List<EntityValidationResult> results;
212
		results = dao.getValidationResults(Severity.NOTICE);
213
		assertEquals("Unexpected number of validation results", 1, results.size());
214
		results = dao.getValidationResults(Severity.WARNING);
215
		assertEquals("Unexpected number of validation results", 2, results.size());
216
		results = dao.getValidationResults(Severity.ERROR);
217
		assertEquals("Unexpected number of validation results", 4, results.size());
218
	}
219

  
220

  
221
	@Override
222
	public void createTestDataSet() throws FileNotFoundException {
223
		// TODO Auto-generated method stub
224
	}
39
    private static final String MEDIA = "eu.etaxonomy.cdm.model.media.Media";
40
    private static final String SYNONYM_RELATIONSHIP = "eu.etaxonomy.cdm.model.taxon.SynonymRelationship";
41
    private static final String GATHERING_EVENT = "eu.etaxonomy.cdm.model.occurrence.GatheringEvent";
42

  
43
    @SpringBeanByType
44
    private IEntityValidationResultDao dao;
45

  
46
    @Test
47
    public void init() {
48
        assertNotNull("Expecting an instance of IEntityValidationResultDao", 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.setFirstName("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.setLastName("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.saveValidationResult(errors, emp, CRUDEventType.NONE);
77

  
78
        EntityValidationResult result = dao.getValidationResult(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(), "firstName");
92
    }
93

  
94
    @Test
95
    @ExpectedDataSet
96
    // @Ignore //FIXME unignore entity validation result dao delete test
97
    public void testDeleteValidationResult() {
98
        dao.deleteValidationResult(SYNONYM_RELATIONSHIP, 200);
99

  
100
        commitAndStartNewTransaction(null);
101

  
102
        List<EntityValidationResult> results = dao.getEntityValidationResults(SYNONYM_RELATIONSHIP);
103
        assertEquals("Unexpected number of validation results", 0, results.size());
104
    }
105

  
106
    @Test
107
    public void testGetEntityValidationResult() {
108
        EntityValidationResult result;
109

  
110
        result = dao.getValidationResult(MEDIA, 100);
111
        assertNotNull(result);
112
        assertEquals("Unexpected entity id", 1, result.getId());
113
        assertEquals("Unexpected number of constraint violations", 1, result.getEntityConstraintViolations().size());
114

  
115
        result = dao.getValidationResult(SYNONYM_RELATIONSHIP, 200);
116
        assertNotNull(result);
117
        assertEquals("Unexpected entity id", 2, result.getId());
118
        assertEquals("Unexpected number of constraint violations", 2, result.getEntityConstraintViolations().size());
119

  
120
        result = dao.getValidationResult(GATHERING_EVENT, 300);
121
        assertNotNull(result);
122
        assertEquals("Unexpected entity id", 3, result.getId());
123
        assertEquals("Unexpected number of constraint violations", 3, result.getEntityConstraintViolations().size());
124

  
125
        result = dao.getValidationResult(GATHERING_EVENT, 301);
126
        assertNotNull(result);
127
        assertEquals("Unexpected entity id", 4, result.getId());
128
        assertEquals("Unexpected number of constraint violations", 1, result.getEntityConstraintViolations().size());
129

  
130
        // Test we get a null back
131
        result = dao.getValidationResult("Foo Bar", 100);
132
        assertNull(result);
133
    }
134

  
135
    @Test
136
    public void testGetEntityValidationResults_String() {
137
        List<EntityValidationResult> results;
138

  
139
        results = dao.getEntityValidationResults(MEDIA);
140
        assertEquals("Unexpected number of validation results", 1, results.size());
141

  
142
        results = dao.getEntityValidationResults(SYNONYM_RELATIONSHIP);
143
        assertEquals("Unexpected number of validation results", 1, results.size());
144

  
145
        results = dao.getEntityValidationResults(GATHERING_EVENT);
146
        assertEquals("Unexpected number of validation results", 2, results.size());
147

  
148
        results = dao.getEntityValidationResults("foo.bar");
149
        assertEquals("Unexpected number of validation results", 0, results.size());
150
    }
151

  
152
    @Test
153
    public void testGetEntitiesViolatingConstraint_String() {
154
        List<EntityValidationResult> results;
155

  
156
        results = dao.getEntitiesViolatingConstraint("com.example.NameValidator");
157
        assertEquals("Unexpected number of validation results", 1, results.size());
158

  
159
        results = dao.getEntitiesViolatingConstraint("com.example.DistanceToGroundValidator");
160
        assertEquals("Unexpected number of validation results", 1, results.size());
161

  
162
        results = dao.getEntitiesViolatingConstraint("com.example.CountryValidator");
163
        assertEquals("Unexpected number of validation results", 2, results.size());
164

  
165
        results = dao.getEntitiesViolatingConstraint("foo.bar");
166
        assertEquals("Unexpected number of validation results", 0, results.size());
167
    }
168

  
169
    @Test
170
    public void testGetEntityValidationResults_String_Severity() {
171
        List<EntityValidationResult> results;
172

  
173
        results = dao.getValidationResults(MEDIA, Severity.NOTICE);
174
        assertEquals("Unexpected number of validation results", 0, results.size());
175
        results = dao.getValidationResults(MEDIA, Severity.WARNING);
176
        assertEquals("Unexpected number of validation results", 0, results.size());
177
        results = dao.getValidationResults(MEDIA, Severity.ERROR);
178
        assertEquals("Unexpected number of validation results", 1, results.size());
179
        assertEquals("Unexpected number of validation results", 1, results.iterator().next()
180
                .getEntityConstraintViolations().size());
181
        assertEquals("Unexpected severity", Severity.ERROR, results.iterator().next().getEntityConstraintViolations()
182
                .iterator().next().getSeverity());
183

  
184
        results = dao.getValidationResults(SYNONYM_RELATIONSHIP, Severity.NOTICE);
185
        assertEquals("Unexpected number of validation results", 0, results.size());
186
        results = dao.getValidationResults(SYNONYM_RELATIONSHIP, Severity.WARNING);
187
        assertEquals("Unexpected number of validation results", 1, results.size());
188
        results = dao.getValidationResults(SYNONYM_RELATIONSHIP, Severity.ERROR);
189
        assertEquals("Unexpected number of validation results", 1, results.size());
190

  
191
        results = dao.getValidationResults(GATHERING_EVENT, Severity.NOTICE);
192
        assertEquals("Unexpected number of validation results", 1, results.size());
193
        results = dao.getValidationResults(GATHERING_EVENT, Severity.WARNING);
194
        assertEquals("Unexpected number of validation results", 1, results.size());
195
        results = dao.getValidationResults(GATHERING_EVENT, Severity.ERROR);
196
        assertEquals("Unexpected number of validation results", 2, results.size());
197

  
198
        results = dao.getValidationResults("foo.bar", Severity.ERROR);
199
        assertEquals("Unexpected number of validation results", 0, results.size());
200
    }
201

  
202
    @Test
203
    public void testGetEntityValidationResults_Severity() {
204
        List<EntityValidationResult> results;
205
        results = dao.getValidationResults(Severity.NOTICE);
206
        assertEquals("Unexpected number of validation results", 1, results.size());
207
        results = dao.getValidationResults(Severity.WARNING);
208
        assertEquals("Unexpected number of validation results", 2, results.size());
209
        results = dao.getValidationResults(Severity.ERROR);
210
        assertEquals("Unexpected number of validation results", 4, results.size());
211
    }
212

  
213
    @Override
214
    public void createTestDataSet() throws FileNotFoundException {
215
        // TODO Auto-generated method stub
216
    }
225 217

  
226 218
}

Also available in: Unified diff