Project

General

Profile

Download (14.5 KB) Statistics
| Branch: | Tag: | Revision:
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.common.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
 */
48
public class CdmEntityDaoBaseTest extends CdmTransactionalIntegrationTestWithSecurity {
49

    
50
    private static final UUID UUID_USER_BEN = UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66");
51
    private UUID uuid;
52
    private TaxonBase<?> cdmBase;
53

    
54
    @SpringBeanByType
55
    private ITaxonDao cdmEntityDaoBase;
56

    
57
    @SpringBeanByType
58
    private AuthenticationManager authenticationManager;
59

    
60
    @SpringBeanByType
61
    private IUserDao userDao;
62

    
63
    private TestingAuthenticationToken taxonEditorToken;
64
    private TestingAuthenticationToken adminToken;
65
    private TestingAuthenticationToken testerToken;
66

    
67

    
68

    
69
    /**
70
     * @throws java.lang.Exception
71
     */
72
    @Before
73
    public void setUp() throws Exception {
74
        uuid = UUID.fromString("8d77c380-c76a-11dd-ad8b-0800200c9a66");
75
        cdmBase = Taxon.NewInstance(null, null);
76
        cdmBase.setUuid(UUID.fromString("e463b270-c76b-11dd-ad8b-0800200c9a66"));
77

    
78
        taxonEditorToken = new TestingAuthenticationToken("taxonEditor", "password",  "TAXONBASE.[CREATE]", "TAXONBASE.[UPDATE]");
79
        adminToken = new TestingAuthenticationToken("admin", "password",  "ALL.ADMIN");
80
        testerToken = new TestingAuthenticationToken("tester", "password");
81

    
82

    
83
        // Clear the context prior to each test
84
        SecurityContextHolder.clearContext();
85
    }
86

    
87
    private void setAuthentication(TestingAuthenticationToken token) {
88
         Authentication authentication = authenticationManager.authenticate(token);
89

    
90
        SecurityContextImpl secureContext = new SecurityContextImpl();
91
        SecurityContextHolder.setContext(secureContext);
92
        secureContext.setAuthentication(authentication);
93
    }
94

    
95
/************ TESTS ********************************/
96

    
97

    
98
    /**
99
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#CdmEntityDaoBase(java.lang.Class)}.
100
     * @throws Exception
101
     */
102
    @Test
103
    public void testCdmEntityDaoBase() throws Exception {
104
        assertNotNull("cdmEntityDaoBase should exist",cdmEntityDaoBase);
105
    }
106

    
107
    /**
108
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
109
     */
110
    @Test
111
    @DataSet("CdmEntityDaoBaseTest.xml")
112
    @ExpectedDataSet
113
    public void testSaveOrUpdate() {
114
        TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
115
        cdmBase.setDoubtful(true);
116
        cdmEntityDaoBase.saveOrUpdate(cdmBase);
117
        commit();
118
    }
119

    
120
    /**
121
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
122
     */
123
    @Test
124
    @DataSet("CdmEntityDaoBaseTest.xml")
125
    @ExpectedDataSet
126
    public void testSaveOrUpdateWithAuthentication() {
127

    
128
        setAuthentication(taxonEditorToken);
129
        TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
130
        cdmBase.setDoubtful(true);
131
        cdmEntityDaoBase.saveOrUpdate(cdmBase);
132
        commit();
133
    }
134

    
135
    /**
136
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
137
     */
138
    @Test
139
    @DataSet("CdmEntityDaoBaseTest.xml")
140
    @ExpectedDataSet
141
    public void testSaveOrUpdateNewObjectWithAuthentication() {
142
//		printDataSet(System.err, new String[]{"TAXONBASE", "HIBERNATE_SEQUENCES"});
143
        setAuthentication(taxonEditorToken);
144
        RuntimeException securityException = null;
145

    
146
        // 1) test create
147
        try{
148
            cdmEntityDaoBase.saveOrUpdate(cdmBase);
149
            commitAndStartNewTransaction(null);
150
        } catch (RuntimeException e){
151
            securityException = findSecurityRuntimeException(e);
152
            logger.error("Unexpected failure of evaluation.", e);
153
            Assert.fail();
154
        } finally {
155
            // needed in case saveOrUpdate was interrupted by the RuntimeException
156
            // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
157
            endTransaction();
158
            startNewTransaction();
159
        }
160

    
161
        // 1) test update
162
        cdmBase = cdmEntityDaoBase.findByUuid(cdmBase.getUuid());
163
        cdmBase.setDoubtful(true);
164
        try{
165
            cdmEntityDaoBase.saveOrUpdate(cdmBase);
166
            commitAndStartNewTransaction(null);
167
        } catch (RuntimeException e){
168
            securityException = findSecurityRuntimeException(e);
169
            logger.error("Unexpected failure of evaluation.", e);
170
            Assert.fail();
171
        } finally {
172
            // needed in case saveOrUpdate was interrupted by the RuntimeException
173
            // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
174
            endTransaction();
175
            startNewTransaction();
176
        }
177

    
178
    }
179
    /**
180
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
181
     */
182
    @Test
183
    @DataSet("CdmEntityDaoBaseTest.xml")
184
    @ExpectedDataSet
185
    public void testSave() throws Exception {
186
        cdmEntityDaoBase.save(cdmBase);
187
        commit();
188
    }
189

    
190

    
191
    /**
192
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
193
     */
194
    @Test
195
    @DataSet("CdmEntityDaoBaseTest.xml")
196
    public void testSaveWithAuthenticationFailedPermissionEvaluation() throws Exception {
197
        setAuthentication(testerToken);
198
        RuntimeException securityException = null;
199
        try{
200
            cdmEntityDaoBase.save(cdmBase);
201
            commitAndStartNewTransaction(null);
202
            logger.error("Expected failure of evaluation.");
203
        } catch (RuntimeException e){
204
            securityException = findSecurityRuntimeException(e);
205
        } finally {
206
            // needed in case saveOrUpdate was interrupted by the RuntimeException
207
            // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
208
            endTransaction();
209
            startNewTransaction();
210
        }
211
        Assert.assertNotNull("evaluation must fail since the user has no permission", securityException);
212

    
213
    }
214

    
215
    /**
216
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
217
     */
218
    @Test
219
    @DataSet("CdmEntityDaoBaseTest.xml")
220
    @ExpectedDataSet
221
    public void testSaveWithAuthentication() throws Exception {
222
        setAuthentication(taxonEditorToken);
223
        RuntimeException securityException = null;
224
        try {
225
            cdmEntityDaoBase.save(cdmBase);
226
            commitAndStartNewTransaction(null);
227
        } catch (RuntimeException e){
228
            securityException   = findSecurityRuntimeException(e);
229
            logger.error("Unexpected failure of evaluation.", securityException);
230
        } finally {
231
            // needed in case saveOrUpdate was interrupted by the RuntimeException
232
            // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
233
            endTransaction();
234
            startNewTransaction();
235
        }
236
        Assert.assertNull("evaluation must not fail since the user is permitted, CAUSE :" + (securityException != null ? securityException.getMessage() : ""), securityException);
237
    }
238

    
239
    /**
240
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#update(eu.etaxonomy.cdm.model.common.CdmBase)}.
241
     */
242
    @Test
243
    @DataSet("CdmEntityDaoBaseTest.xml")
244
    @ExpectedDataSet
245
    public void testUpdate() {
246
        TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
247
        cdmBase.setDoubtful(true);
248
        cdmEntityDaoBase.update(cdmBase);
249
        commit();
250
    }
251

    
252
    @Test
253
    @DataSet("CdmEntityDaoBaseTest.xml")
254
    @ExpectedDataSet
255
    public void testUpdateWithAuthentication() {
256

    
257
        setAuthentication(taxonEditorToken);
258
        TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
259
        cdmBase.setDoubtful(true);
260
        RuntimeException securityException = null;
261
        try {
262
        cdmEntityDaoBase.update(cdmBase);
263
            commitAndStartNewTransaction(null);
264
        } catch (RuntimeException e){
265
            securityException    = findSecurityRuntimeException(e);
266
            logger.error("Unexpected failure of evaluation.", securityException);
267
        } finally {
268
            // needed in case saveOrUpdate was interrupted by the RuntimeException
269
            // commitAndStartNewTransaction() would raise an UnexpectedRollbackException
270
            endTransaction();
271
            startNewTransaction();
272
        }
273
        Assert.assertNull("evaluation must not fail since the user is permitted, CAUSE :" + (securityException != null ? securityException.getMessage() : ""), securityException);
274

    
275
    }
276

    
277
    /**
278
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findById(int)}.
279
     */
280
    @Test
281
    @DataSet("CdmEntityDaoBaseTest.xml")
282
    public void testFindById() {
283
        CdmBase cdmBase = cdmEntityDaoBase.findById(1);
284
        assertNotNull("There should be an entity with an id of 1",cdmBase);
285
    }
286

    
287
    /**
288
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findByUuid(java.util.UUID)}.
289
     */
290
    @Test
291
    @DataSet("CdmEntityDaoBaseTest.xml")
292
    public void testFindByUuid() {
293
        CdmBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
294
        assertNotNull("testFindByUuid() an entity with a uuid of " + uuid.toString(),cdmBase);
295
    }
296

    
297
    /**
298
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#exists(java.util.UUID)}.
299
     */
300
    @Test
301
    @DataSet("CdmEntityDaoBaseTest.xml")
302
    public void testExists() {
303
        boolean exists = cdmEntityDaoBase.exists(uuid);
304
        assertTrue("exists() should return true for uuid " + uuid.toString(), exists);
305
        boolean existsRandom = cdmEntityDaoBase.exists(UUID.randomUUID());
306
        assertFalse("exists() should return false for any other uuid", existsRandom);
307
    }
308

    
309
    /**
310
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
311
     */
312
    @Test
313
    @DataSet("CdmEntityDaoBaseTest.xml")
314
    public void testList() {
315
        List<TaxonBase> list = cdmEntityDaoBase.list(1000, 0);
316
        assertNotNull("list() should not return null",list);
317
        assertEquals("list() should return a list with two entities in it",list.size(),2);
318
    }
319

    
320
    /**
321
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(Class, Integer, Integer, List, List)}.
322
     */
323
    @Test
324
    @DataSet("CdmEntityDaoBaseTest.xml")
325
    public void testRandomOrder() {
326
        List<OrderHint> orderHints = new ArrayList<OrderHint>();
327
        orderHints.add(new RandomOrder());
328
        List<TaxonBase> list = cdmEntityDaoBase.list((Class)null, 1000, 0, orderHints, null);
329
        assertNotNull("list() should not return null", list);
330
        assertEquals("list() should return a list with two entities in it", list.size(), 2);
331
    }
332

    
333
    /**
334
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(java.util.Collection, Integer, Integer, List, List)}.
335
     */
336
    @Test
337
    @DataSet("CdmEntityDaoBaseTest.xml")
338
    public void testListByUuids() {
339
        List<OrderHint> orderHints = new ArrayList<OrderHint>();
340
        orderHints.add(new RandomOrder());
341
        UUID[] uuids = new UUID[]{UUID.fromString("8d77c380-c76a-11dd-ad8b-0800200c9a66"), UUID.fromString("822d98dc-9ef7-44b7-a870-94573a3bcb46")};
342
        List<TaxonBase> list = cdmEntityDaoBase.list(Arrays.asList(uuids), 20, 0, orderHints, null);
343
        assertNotNull("list() should not return null",list);
344
        assertEquals("list() should return a list with two entities in it",list.size(),2);
345
    }
346

    
347
    /**
348
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#loadList(java.util.Collection, List)}.
349
     */
350
    @Test
351
    @DataSet("CdmEntityDaoBaseTest.xml")
352
    public void testListByIds() {
353
        List<OrderHint> orderHints = new ArrayList<OrderHint>();
354
        orderHints.add(new RandomOrder());
355
        Integer[] ids = new Integer[]{1, 2};
356
        List<TaxonBase> list = cdmEntityDaoBase.loadList(Arrays.asList(ids), null);
357
        assertNotNull("list() should not return null",list);
358
        assertEquals("list() should return a list with two entities in it",list.size(),2);
359
    }
360

    
361
    /**
362
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)}.
363
     */
364
    @Test
365
    @DataSet("CdmEntityDaoBaseTest.xml")
366
    @ExpectedDataSet
367
    public void testDelete() {
368
        TaxonBase<?> cdmBase = cdmEntityDaoBase.findByUuid(uuid);
369
        assertNotNull(cdmBase);
370
        cdmEntityDaoBase.delete(cdmBase);
371
    }
372

    
373
    /* (non-Javadoc)
374
     * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
375
     */
376
    @Override
377
    public void createTestDataSet() throws FileNotFoundException {
378
        // TODO Auto-generated method stub
379

    
380
    }
381
}
(2-2/11)