1
|
/**
|
2
|
* Copyright (C) 2007 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
|
|
10
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.common;
|
11
|
|
12
|
import static org.junit.Assert.assertEquals;
|
13
|
import static org.junit.Assert.assertFalse;
|
14
|
import static org.junit.Assert.assertNotNull;
|
15
|
import static org.junit.Assert.assertTrue;
|
16
|
|
17
|
import java.io.FileNotFoundException;
|
18
|
import java.util.ArrayList;
|
19
|
import java.util.Arrays;
|
20
|
import java.util.List;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import org.junit.Assert;
|
24
|
import org.junit.Before;
|
25
|
import org.junit.Test;
|
26
|
import org.springframework.security.authentication.AuthenticationManager;
|
27
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
28
|
import org.springframework.security.core.Authentication;
|
29
|
import org.springframework.security.core.context.SecurityContextHolder;
|
30
|
import org.springframework.security.core.context.SecurityContextImpl;
|
31
|
import org.unitils.dbunit.annotation.DataSet;
|
32
|
import org.unitils.dbunit.annotation.ExpectedDataSet;
|
33
|
import org.unitils.spring.annotation.SpringBeanByType;
|
34
|
|
35
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
36
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
38
|
import eu.etaxonomy.cdm.persistence.dao.permission.IUserDao;
|
39
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
40
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
41
|
import eu.etaxonomy.cdm.persistence.query.RandomOrder;
|
42
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTestWithSecurity;
|
43
|
|
44
|
/**
|
45
|
* @author a.mueller
|
46
|
*/
|
47
|
public class CdmEntityDaoBaseTest extends CdmTransactionalIntegrationTestWithSecurity {
|
48
|
|
49
|
private UUID uuid;
|
50
|
private TaxonBase<?> cdmBase;
|
51
|
|
52
|
@SpringBeanByType
|
53
|
private ITaxonDao cdmEntityDaoBase;
|
54
|
|
55
|
@SpringBeanByType
|
56
|
private AuthenticationManager authenticationManager;
|
57
|
|
58
|
@SpringBeanByType
|
59
|
private IUserDao userDao;
|
60
|
|
61
|
private TestingAuthenticationToken taxonEditorToken;
|
62
|
private TestingAuthenticationToken adminToken;
|
63
|
private TestingAuthenticationToken testerToken;
|
64
|
|
65
|
|
66
|
@Before
|
67
|
public void setUp() throws Exception {
|
68
|
uuid = UUID.fromString("8d77c380-c76a-11dd-ad8b-0800200c9a66");
|
69
|
cdmBase = Taxon.NewInstance(null, null);
|
70
|
cdmBase.setUuid(UUID.fromString("e463b270-c76b-11dd-ad8b-0800200c9a66"));
|
71
|
|
72
|
taxonEditorToken = new TestingAuthenticationToken("taxonEditor", "password", "TAXONBASE.[CREATE]", "TAXONBASE.[UPDATE]");
|
73
|
adminToken = new TestingAuthenticationToken("admin", "password", "ALL.ADMIN");
|
74
|
testerToken = new TestingAuthenticationToken("tester", "password");
|
75
|
|
76
|
|
77
|
// Clear the context prior to each test
|
78
|
SecurityContextHolder.clearContext();
|
79
|
}
|
80
|
|
81
|
private void setAuthentication(TestingAuthenticationToken token) {
|
82
|
Authentication authentication = authenticationManager.authenticate(token);
|
83
|
|
84
|
SecurityContextImpl secureContext = new SecurityContextImpl();
|
85
|
SecurityContextHolder.setContext(secureContext);
|
86
|
secureContext.setAuthentication(authentication);
|
87
|
}
|
88
|
|
89
|
/************ TESTS ********************************/
|
90
|
|
91
|
@Test
|
92
|
public void testCdmEntityDaoBase() throws Exception {
|
93
|
assertNotNull("cdmEntityDaoBase should exist",cdmEntityDaoBase);
|
94
|
}
|
95
|
|
96
|
@Test
|
97
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
98
|
@ExpectedDataSet
|
99
|
public void testSaveOrUpdate() {
|
100
|
TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
101
|
cdmBase.setDoubtful(true);
|
102
|
cdmEntityDaoBase.saveOrUpdate(cdmBase);
|
103
|
commit();
|
104
|
}
|
105
|
|
106
|
@Test
|
107
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
108
|
@ExpectedDataSet
|
109
|
public void testSaveOrUpdateWithAuthentication() {
|
110
|
|
111
|
setAuthentication(taxonEditorToken);
|
112
|
TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
113
|
cdmBase.setDoubtful(true);
|
114
|
cdmEntityDaoBase.saveOrUpdate(cdmBase);
|
115
|
commit();
|
116
|
}
|
117
|
|
118
|
@Test
|
119
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
120
|
@ExpectedDataSet
|
121
|
public void testSaveOrUpdateNewObjectWithAuthentication() {
|
122
|
// printDataSet(System.err, new String[]{"TAXONBASE", "HIBERNATE_SEQUENCES"});
|
123
|
setAuthentication(taxonEditorToken);
|
124
|
RuntimeException securityException = null;
|
125
|
|
126
|
// 1) test create
|
127
|
try{
|
128
|
cdmEntityDaoBase.saveOrUpdate(cdmBase);
|
129
|
commitAndStartNewTransaction(null);
|
130
|
} catch (RuntimeException e){
|
131
|
securityException = findSecurityRuntimeException(e);
|
132
|
logger.error("Unexpected failure of evaluation.", e);
|
133
|
Assert.fail();
|
134
|
} finally {
|
135
|
// needed in case saveOrUpdate was interrupted by the RuntimeException
|
136
|
// commitAndStartNewTransaction() would raise an UnexpectedRollbackException
|
137
|
endTransaction();
|
138
|
startNewTransaction();
|
139
|
}
|
140
|
|
141
|
// 1) test update
|
142
|
cdmBase = cdmEntityDaoBase.findByUuid(cdmBase.getUuid());
|
143
|
cdmBase.setDoubtful(true);
|
144
|
try{
|
145
|
cdmEntityDaoBase.saveOrUpdate(cdmBase);
|
146
|
commitAndStartNewTransaction(null);
|
147
|
} catch (RuntimeException e){
|
148
|
securityException = findSecurityRuntimeException(e);
|
149
|
logger.error("Unexpected failure of evaluation.", e);
|
150
|
Assert.fail();
|
151
|
} finally {
|
152
|
// needed in case saveOrUpdate was interrupted by the RuntimeException
|
153
|
// commitAndStartNewTransaction() would raise an UnexpectedRollbackException
|
154
|
endTransaction();
|
155
|
startNewTransaction();
|
156
|
}
|
157
|
}
|
158
|
|
159
|
@Test
|
160
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
161
|
@ExpectedDataSet
|
162
|
public void testSave() throws Exception {
|
163
|
cdmEntityDaoBase.save(cdmBase);
|
164
|
commit();
|
165
|
}
|
166
|
|
167
|
@Test
|
168
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
169
|
public void testSaveWithAuthenticationFailedPermissionEvaluation() throws Exception {
|
170
|
setAuthentication(testerToken);
|
171
|
RuntimeException securityException = null;
|
172
|
try{
|
173
|
cdmEntityDaoBase.save(cdmBase);
|
174
|
commitAndStartNewTransaction(null);
|
175
|
logger.error("Expected failure of evaluation.");
|
176
|
} catch (RuntimeException e){
|
177
|
securityException = findSecurityRuntimeException(e);
|
178
|
} finally {
|
179
|
// needed in case saveOrUpdate was interrupted by the RuntimeException
|
180
|
// commitAndStartNewTransaction() would raise an UnexpectedRollbackException
|
181
|
endTransaction();
|
182
|
startNewTransaction();
|
183
|
}
|
184
|
Assert.assertNotNull("evaluation must fail since the user has no permission", securityException);
|
185
|
}
|
186
|
|
187
|
@Test
|
188
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
189
|
@ExpectedDataSet
|
190
|
public void testSaveWithAuthentication() throws Exception {
|
191
|
setAuthentication(taxonEditorToken);
|
192
|
RuntimeException securityException = null;
|
193
|
try {
|
194
|
cdmEntityDaoBase.save(cdmBase);
|
195
|
commitAndStartNewTransaction(null);
|
196
|
} catch (RuntimeException e){
|
197
|
securityException = findSecurityRuntimeException(e);
|
198
|
logger.error("Unexpected failure of evaluation.", securityException);
|
199
|
} finally {
|
200
|
// needed in case saveOrUpdate was interrupted by the RuntimeException
|
201
|
// commitAndStartNewTransaction() would raise an UnexpectedRollbackException
|
202
|
endTransaction();
|
203
|
startNewTransaction();
|
204
|
}
|
205
|
Assert.assertNull("evaluation must not fail since the user is permitted, CAUSE :" + (securityException != null ? securityException.getMessage() : ""), securityException);
|
206
|
}
|
207
|
|
208
|
@Test
|
209
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
210
|
@ExpectedDataSet
|
211
|
public void testUpdate() {
|
212
|
TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
213
|
cdmBase.setDoubtful(true);
|
214
|
cdmEntityDaoBase.update(cdmBase);
|
215
|
commit();
|
216
|
}
|
217
|
|
218
|
@Test
|
219
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
220
|
@ExpectedDataSet
|
221
|
public void testUpdateWithAuthentication() {
|
222
|
|
223
|
setAuthentication(taxonEditorToken);
|
224
|
TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
225
|
cdmBase.setDoubtful(true);
|
226
|
RuntimeException securityException = null;
|
227
|
try {
|
228
|
cdmEntityDaoBase.update(cdmBase);
|
229
|
commitAndStartNewTransaction(null);
|
230
|
} catch (RuntimeException e){
|
231
|
securityException = findSecurityRuntimeException(e);
|
232
|
logger.error("Unexpected failure of evaluation.", securityException);
|
233
|
} finally {
|
234
|
// needed in case saveOrUpdate was interrupted by the RuntimeException
|
235
|
// commitAndStartNewTransaction() would raise an UnexpectedRollbackException
|
236
|
endTransaction();
|
237
|
startNewTransaction();
|
238
|
}
|
239
|
Assert.assertNull("evaluation must not fail since the user is permitted, CAUSE :" + (securityException != null ? securityException.getMessage() : ""), securityException);
|
240
|
}
|
241
|
|
242
|
@Test
|
243
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
244
|
public void testFindById() {
|
245
|
CdmBase cdmBase = cdmEntityDaoBase.findById(1);
|
246
|
assertNotNull("There should be an entity with an id of 1",cdmBase);
|
247
|
}
|
248
|
|
249
|
@Test
|
250
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
251
|
public void testFindByUuid() {
|
252
|
CdmBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
253
|
assertNotNull("testFindByUuid() an entity with a uuid of " + uuid.toString(),cdmBase);
|
254
|
}
|
255
|
|
256
|
@Test
|
257
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
258
|
public void testExists() {
|
259
|
boolean exists = cdmEntityDaoBase.exists(uuid);
|
260
|
assertTrue("exists() should return true for uuid " + uuid.toString(), exists);
|
261
|
boolean existsRandom = cdmEntityDaoBase.exists(UUID.randomUUID());
|
262
|
assertFalse("exists() should return false for any other uuid", existsRandom);
|
263
|
}
|
264
|
|
265
|
@Test
|
266
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
267
|
public void testList() {
|
268
|
List<TaxonBase> list = cdmEntityDaoBase.list(1000, 0);
|
269
|
assertNotNull("list() should not return null",list);
|
270
|
assertEquals("list() should return a list with two entities in it",list.size(),2);
|
271
|
}
|
272
|
|
273
|
@Test
|
274
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
275
|
public void testRandomOrder() {
|
276
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
277
|
orderHints.add(new RandomOrder());
|
278
|
List<TaxonBase> list = cdmEntityDaoBase.list((Class)null, 1000, 0, orderHints, null);
|
279
|
assertNotNull("list() should not return null", list);
|
280
|
assertEquals("list() should return a list with two entities in it", list.size(), 2);
|
281
|
}
|
282
|
|
283
|
@Test
|
284
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
285
|
public void testListByUuids() {
|
286
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
287
|
orderHints.add(new RandomOrder());
|
288
|
UUID[] uuids = new UUID[]{UUID.fromString("8d77c380-c76a-11dd-ad8b-0800200c9a66"), UUID.fromString("822d98dc-9ef7-44b7-a870-94573a3bcb46")};
|
289
|
List<TaxonBase> list = cdmEntityDaoBase.list(Arrays.asList(uuids), 20, 0, orderHints, null);
|
290
|
assertNotNull("list() should not return null",list);
|
291
|
assertEquals("list() should return a list with two entities in it",list.size(),2);
|
292
|
}
|
293
|
|
294
|
@Test
|
295
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
296
|
public void testListByIds() {
|
297
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
298
|
orderHints.add(new RandomOrder());
|
299
|
Integer[] ids = new Integer[]{1, 2};
|
300
|
List<TaxonBase> list = cdmEntityDaoBase.loadList(Arrays.asList(ids), orderHints, null);
|
301
|
assertNotNull("list() should not return null",list);
|
302
|
assertEquals("list() should return a list with two entities in it",2,list.size());
|
303
|
}
|
304
|
|
305
|
@Test
|
306
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
307
|
@ExpectedDataSet
|
308
|
public void testDelete() {
|
309
|
TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
310
|
assertNotNull(cdmBase);
|
311
|
cdmEntityDaoBase.delete(cdmBase);
|
312
|
}
|
313
|
|
314
|
@Override
|
315
|
public void createTestDataSet() throws FileNotFoundException {}
|
316
|
}
|