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