2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
;
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
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Arrays
;
19 import java
.util
.List
;
20 import java
.util
.UUID
;
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
;
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
;
47 public class CdmEntityDaoBaseTest
extends CdmTransactionalIntegrationTestWithSecurity
{
49 private static final UUID UUID_USER_BEN
= UUID
.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66");
51 private TaxonBase
<?
> cdmBase
;
54 private ITaxonDao cdmEntityDaoBase
;
57 private AuthenticationManager authenticationManager
;
60 private IUserDao userDao
;
62 private TestingAuthenticationToken taxonEditorToken
;
63 private TestingAuthenticationToken adminToken
;
64 private TestingAuthenticationToken testerToken
;
69 * @throws java.lang.Exception
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"));
77 taxonEditorToken
= new TestingAuthenticationToken("taxonEditor", "password", "TAXONBASE.CREATE", "TAXONBASE.UPDATE");
78 adminToken
= new TestingAuthenticationToken("admin", "password", "ALL.ADMIN");
79 testerToken
= new TestingAuthenticationToken("tester", "password");
82 // Clear the context prior to each test
83 SecurityContextHolder
.clearContext();
86 private void setAuthentication(TestingAuthenticationToken token
) {
87 Authentication authentication
= authenticationManager
.authenticate(token
);
89 SecurityContextImpl secureContext
= new SecurityContextImpl();
90 SecurityContextHolder
.setContext(secureContext
);
91 secureContext
.setAuthentication(authentication
);
94 /************ TESTS ********************************/
98 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#CdmEntityDaoBase(java.lang.Class)}.
102 public void testCdmEntityDaoBase() throws Exception
{
103 assertNotNull("cdmEntityDaoBase should exist",cdmEntityDaoBase
);
107 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
110 @DataSet("CdmEntityDaoBaseTest.xml")
112 public void testSaveOrUpdate() {
113 TaxonBase
<?
> cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
114 cdmBase
.setDoubtful(true);
115 cdmEntityDaoBase
.saveOrUpdate(cdmBase
);
120 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
123 @DataSet("CdmEntityDaoBaseTest.xml")
125 public void testSaveOrUpdateWithAuthentication() {
127 setAuthentication(taxonEditorToken
);
128 TaxonBase
<?
> cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
129 cdmBase
.setDoubtful(true);
130 cdmEntityDaoBase
.saveOrUpdate(cdmBase
);
135 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
138 @DataSet("CdmEntityDaoBaseTest.xml")
140 public void testSaveOrUpdateNewObjectWithAuthentication() {
141 // printDataSet(System.err, new String[]{"TAXONBASE", "HIBERNATE_SEQUENCES"});
142 setAuthentication(taxonEditorToken
);
143 RuntimeException securityException
= null;
147 cdmEntityDaoBase
.saveOrUpdate(cdmBase
);
148 commitAndStartNewTransaction(null);
149 } catch (RuntimeException e
){
150 securityException
= findSecurityRuntimeException(e
);
151 logger
.error("Unexpected failure of evaluation.", e
);
154 // needed in case saveOrUpdate was interrupted by the RuntimeException
155 // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
157 startNewTransaction();
161 cdmBase
= cdmEntityDaoBase
.findByUuid(cdmBase
.getUuid());
162 cdmBase
.setDoubtful(true);
164 cdmEntityDaoBase
.saveOrUpdate(cdmBase
);
165 commitAndStartNewTransaction(null);
166 } catch (RuntimeException e
){
167 securityException
= findSecurityRuntimeException(e
);
168 logger
.error("Unexpected failure of evaluation.", e
);
171 // needed in case saveOrUpdate was interrupted by the RuntimeException
172 // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
174 startNewTransaction();
179 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
182 @DataSet("CdmEntityDaoBaseTest.xml")
184 public void testSave() throws Exception
{
185 cdmEntityDaoBase
.save(cdmBase
);
191 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
194 @DataSet("CdmEntityDaoBaseTest.xml")
195 public void testSaveWithAuthenticationFailedPermissionEvaluation() throws Exception
{
196 setAuthentication(testerToken
);
197 RuntimeException securityException
= null;
199 cdmEntityDaoBase
.save(cdmBase
);
200 commitAndStartNewTransaction(null);
201 } catch (RuntimeException e
){
202 securityException
= findSecurityRuntimeException(e
);
203 logger
.error("Unexpected failure of evaluation.", securityException
);
205 // needed in case saveOrUpdate was interrupted by the RuntimeException
206 // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
208 startNewTransaction();
210 Assert
.assertNotNull("evaluation must fail since the user has no permission", securityException
);
215 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
218 @DataSet("CdmEntityDaoBaseTest.xml")
220 public void testSaveWithAuthentication() throws Exception
{
221 setAuthentication(taxonEditorToken
);
222 RuntimeException securityException
= null;
224 cdmEntityDaoBase
.save(cdmBase
);
225 commitAndStartNewTransaction(null);
226 } catch (RuntimeException e
){
227 securityException
= findSecurityRuntimeException(e
);
228 logger
.error("Unexpected failure of evaluation.", securityException
);
230 // needed in case saveOrUpdate was interrupted by the RuntimeException
231 // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
233 startNewTransaction();
235 Assert
.assertNull("evaluation must not fail since the user is permitted, CAUSE :" + (securityException
!= null ? securityException
.getMessage() : ""), securityException
);
239 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#update(eu.etaxonomy.cdm.model.common.CdmBase)}.
242 @DataSet("CdmEntityDaoBaseTest.xml")
244 public void testUpdate() {
245 TaxonBase
<?
> cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
246 cdmBase
.setDoubtful(true);
247 cdmEntityDaoBase
.update(cdmBase
);
252 @DataSet("CdmEntityDaoBaseTest.xml")
254 public void testUpdateWithAuthentication() {
256 setAuthentication(taxonEditorToken
);
257 TaxonBase
<?
> cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
258 cdmBase
.setDoubtful(true);
259 RuntimeException securityException
= null;
261 cdmEntityDaoBase
.update(cdmBase
);
262 commitAndStartNewTransaction(null);
263 } catch (RuntimeException e
){
264 securityException
= findSecurityRuntimeException(e
);
265 logger
.error("Unexpected failure of evaluation.", securityException
);
267 // needed in case saveOrUpdate was interrupted by the RuntimeException
268 // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
270 startNewTransaction();
272 Assert
.assertNull("evaluation must not fail since the user is permitted, CAUSE :" + (securityException
!= null ? securityException
.getMessage() : ""), securityException
);
277 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findById(int)}.
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
);
287 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findByUuid(java.util.UUID)}.
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
);
297 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#exists(java.util.UUID)}.
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
);
309 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
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);
320 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(Class, Integer, Integer, List, List)}.
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);
333 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(java.util.Collection, Integer, Integer, List, List)}.
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);
347 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#listByIds(java.util.Collection, Integer, Integer, List, List)}.
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);
361 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)}.
364 @DataSet("CdmEntityDaoBaseTest.xml")
366 public void testDelete() {
367 TaxonBase
<?
> cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
368 assertNotNull(cdmBase
);
369 cdmEntityDaoBase
.delete(cdmBase
);