re-integrating branch cdmlib/3.x-unitils-upgrade
[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.List;
19 import java.util.UUID;
20
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.springframework.security.authentication.AuthenticationManager;
24 import org.springframework.security.authentication.TestingAuthenticationToken;
25 import org.springframework.security.core.Authentication;
26 import org.springframework.security.core.context.SecurityContextHolder;
27 import org.springframework.security.core.context.SecurityContextImpl;
28 import org.unitils.database.annotations.Transactional;
29 import org.unitils.database.util.TransactionMode;
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.common.GrantedAuthorityImpl;
36 import eu.etaxonomy.cdm.model.common.User;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39 import eu.etaxonomy.cdm.persistence.dao.common.IUserDao;
40 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
41 import eu.etaxonomy.cdm.persistence.query.OrderHint;
42 import eu.etaxonomy.cdm.persistence.query.RandomOrder;
43 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
44
45 /**
46 * @author a.mueller
47 *
48 */
49 public class CdmEntityDaoBaseTest extends CdmTransactionalIntegrationTest {
50
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 /**
64 * @throws java.lang.Exception
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 // Clear the context prior to each test
73 SecurityContextHolder.clearContext();
74 }
75
76 private void setAuthentication(User user) {
77 TestingAuthenticationToken token = new TestingAuthenticationToken(user, "password", new GrantedAuthorityImpl[0]);
78 Authentication authentication = authenticationManager.authenticate(token);
79
80 SecurityContextImpl secureContext = new SecurityContextImpl();
81 secureContext.setAuthentication(authentication);
82 SecurityContextHolder.setContext(secureContext);
83 }
84
85 /************ TESTS ********************************/
86
87
88 /**
89 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#CdmEntityDaoBase(java.lang.Class)}.
90 * @throws Exception
91 */
92 @Test
93 public void testCdmEntityDaoBase() throws Exception {
94 assertNotNull("cdmEntityDaoBase should exist",cdmEntityDaoBase);
95 }
96
97 /**
98 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
99 */
100 @Test
101 @DataSet("CdmEntityDaoBaseTest.xml")
102 @ExpectedDataSet
103 public void testSaveOrUpdate() {
104 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
105 cdmBase.setDoubtful(true);
106 cdmEntityDaoBase.saveOrUpdate(cdmBase);
107 }
108
109 /**
110 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
111 */
112 @Test
113 @DataSet("CdmEntityDaoBaseTest.xml")
114 @ExpectedDataSet
115 public void testSaveOrUpdateWithAuthentication() {
116 User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
117 assert user != null : "User cannot be null";
118 setAuthentication(user);
119 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
120 cdmBase.setDoubtful(true);
121 cdmEntityDaoBase.saveOrUpdate(cdmBase);
122 }
123
124 /**
125 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
126 */
127 @Test
128 @DataSet("CdmEntityDaoBaseTest.xml")
129 @ExpectedDataSet
130 public void testSaveOrUpdateNewObjectWithAuthentication() {
131 // printDataSet(System.err, new String[]{"TAXONBASE", "HIBERNATE_SEQUENCES"});
132 User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
133 assert user != null : "User cannot be null";
134 setAuthentication(user);
135 cdmEntityDaoBase.saveOrUpdate(cdmBase);
136 }
137 /**
138 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
139 */
140 @Test
141 @DataSet("CdmEntityDaoBaseTest.xml")
142 @ExpectedDataSet
143 public void testSave() throws Exception {
144 cdmEntityDaoBase.save(cdmBase);
145 }
146
147 /**
148 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
149 */
150 @Test
151 @DataSet("CdmEntityDaoBaseTest.xml")
152 @ExpectedDataSet
153 public void testSaveWithAuthentication() throws Exception {
154 User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
155 assert user != null : "User cannot be null";
156 setAuthentication(user);
157 cdmEntityDaoBase.save(cdmBase);
158 }
159
160 /**
161 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#update(eu.etaxonomy.cdm.model.common.CdmBase)}.
162 */
163 @Test
164 @DataSet("CdmEntityDaoBaseTest.xml")
165 @ExpectedDataSet
166 public void testUpdate() {
167 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
168 cdmBase.setDoubtful(true);
169 cdmEntityDaoBase.update(cdmBase);
170 }
171
172 @Test
173 @DataSet("CdmEntityDaoBaseTest.xml")
174 @ExpectedDataSet
175 public void testUpdateWithAuthentication() {
176 User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
177 assert user != null : "User cannot be null";
178
179 setAuthentication(user);
180 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
181 cdmBase.setDoubtful(true);
182 cdmEntityDaoBase.update(cdmBase);
183 }
184
185 /**
186 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findById(int)}.
187 */
188 @Test
189 @DataSet("CdmEntityDaoBaseTest.xml")
190 public void testFindById() {
191 CdmBase cdmBase = cdmEntityDaoBase.findById(1);
192 assertNotNull("There should be an entity with an id of 1",cdmBase);
193 }
194
195 /**
196 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findByUuid(java.util.UUID)}.
197 */
198 @Test
199 @DataSet("CdmEntityDaoBaseTest.xml")
200 public void testFindByUuid() {
201 CdmBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
202 assertNotNull("testFindByUuid() an entity with a uuid of " + uuid.toString(),cdmBase);
203 }
204
205 /**
206 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#exists(java.util.UUID)}.
207 */
208 @Test
209 @DataSet("CdmEntityDaoBaseTest.xml")
210 public void testExists() {
211 boolean exists = cdmEntityDaoBase.exists(uuid);
212 assertTrue("exists() should return true for uuid " + uuid.toString(), exists);
213 boolean existsRandom = cdmEntityDaoBase.exists(UUID.randomUUID());
214 assertFalse("exists() should return false for any other uuid", existsRandom);
215 }
216
217 /**
218 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
219 */
220 @Test
221 @DataSet("CdmEntityDaoBaseTest.xml")
222 public void testList() {
223 List<TaxonBase> list = cdmEntityDaoBase.list(1000, 0);
224 assertNotNull("list() should not return null",list);
225 assertEquals("list() should return a list with two entities in it",list.size(),2);
226 }
227
228 /**
229 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
230 */
231 @Test
232 @DataSet("CdmEntityDaoBaseTest.xml")
233 public void testRandomOrder() {
234 List<OrderHint> orderHints = new ArrayList<OrderHint>();
235 orderHints.add(new RandomOrder());
236 List<TaxonBase> list = cdmEntityDaoBase.list(null,1000, 0,orderHints,null);
237 assertNotNull("list() should not return null",list);
238 assertEquals("list() should return a list with two entities in it",list.size(),2);
239 }
240
241 /**
242 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)}.
243 */
244 @Test
245 @DataSet("CdmEntityDaoBaseTest.xml")
246 @ExpectedDataSet
247 public void testDelete() {
248 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
249 assertNotNull(cdmBase);
250 cdmEntityDaoBase.delete(cdmBase);
251 }
252 }