minor
[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.List;
18 import java.util.UUID;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.springframework.security.Authentication;
23 import org.springframework.security.AuthenticationManager;
24 import org.springframework.security.context.SecurityContextHolder;
25 import org.springframework.security.context.SecurityContextImpl;
26 import org.springframework.security.providers.TestingAuthenticationToken;
27 import org.unitils.dbunit.annotation.DataSet;
28 import org.unitils.dbunit.annotation.ExpectedDataSet;
29 import org.unitils.spring.annotation.SpringBeanByType;
30
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
33 import eu.etaxonomy.cdm.model.common.User;
34 import eu.etaxonomy.cdm.model.taxon.Taxon;
35 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36 import eu.etaxonomy.cdm.persistence.dao.common.IUserDao;
37 import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
38 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
39
40 /**
41 * @author a.mueller
42 *
43 */
44 public class CdmEntityDaoBaseTest extends CdmTransactionalIntegrationTest {
45
46 private UUID uuid;
47 private TaxonBase cdmBase;
48
49 @SpringBeanByType
50 private ITaxonDao cdmEntityDaoBase;
51
52 @SpringBeanByType
53 private AuthenticationManager authenticationManager;
54
55 @SpringBeanByType
56 private IUserDao userDao;
57
58 /**
59 * @throws java.lang.Exception
60 */
61 @Before
62 public void setUp() throws Exception {
63 uuid = UUID.fromString("8d77c380-c76a-11dd-ad8b-0800200c9a66");
64 cdmBase = Taxon.NewInstance(null, null);
65 cdmBase.setUuid(UUID.fromString("e463b270-c76b-11dd-ad8b-0800200c9a66"));
66
67 // Clear the context prior to each test
68 SecurityContextHolder.clearContext();
69 }
70
71 private void setAuthentication(User user) {
72 TestingAuthenticationToken token = new TestingAuthenticationToken(user, "password", new GrantedAuthorityImpl[0]);
73 Authentication authentication = authenticationManager.authenticate(token);
74
75 SecurityContextImpl secureContext = new SecurityContextImpl();
76 secureContext.setAuthentication(authentication);
77 SecurityContextHolder.setContext(secureContext);
78 }
79
80 /************ TESTS ********************************/
81
82
83 /**
84 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#CdmEntityDaoBase(java.lang.Class)}.
85 * @throws Exception
86 */
87 @Test
88 public void testCdmEntityDaoBase() throws Exception {
89 assertNotNull("cdmEntityDaoBase should exist",cdmEntityDaoBase);
90 }
91
92 /**
93 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
94 */
95 @Test
96 @DataSet("CdmEntityDaoBaseTest.xml")
97 @ExpectedDataSet
98 public void testSaveOrUpdate() {
99 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
100 cdmBase.setDoubtful(true);
101 cdmEntityDaoBase.saveOrUpdate(cdmBase);
102 }
103
104 /**
105 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
106 */
107 @Test
108 @DataSet("CdmEntityDaoBaseTest.xml")
109 @ExpectedDataSet
110 public void testSaveOrUpdateWithAuthentication() {
111 User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
112 assert user != null : "User cannot be null";
113 setAuthentication(user);
114 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
115 cdmBase.setDoubtful(true);
116 cdmEntityDaoBase.saveOrUpdate(cdmBase);
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 testSaveOrUpdateNewObjectWithAuthentication() {
126 User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
127 assert user != null : "User cannot be null";
128 setAuthentication(user);
129 cdmEntityDaoBase.saveOrUpdate(cdmBase);
130 }
131 /**
132 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
133 */
134 @Test
135 @DataSet("CdmEntityDaoBaseTest.xml")
136 @ExpectedDataSet
137 public void testSave() throws Exception {
138 cdmEntityDaoBase.save(cdmBase);
139 }
140
141 /**
142 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
143 */
144 @Test
145 @DataSet("CdmEntityDaoBaseTest.xml")
146 @ExpectedDataSet
147 public void testSaveWithAuthentication() throws Exception {
148 User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
149 assert user != null : "User cannot be null";
150 setAuthentication(user);
151 cdmEntityDaoBase.save(cdmBase);
152 }
153
154 /**
155 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#update(eu.etaxonomy.cdm.model.common.CdmBase)}.
156 */
157 @Test
158 @DataSet("CdmEntityDaoBaseTest.xml")
159 @ExpectedDataSet
160 public void testUpdate() {
161 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
162 cdmBase.setDoubtful(true);
163 cdmEntityDaoBase.update(cdmBase);
164 }
165
166 @Test
167 @DataSet("CdmEntityDaoBaseTest.xml")
168 @ExpectedDataSet
169 public void testUpdateWithAuthentication() {
170 User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
171 assert user != null : "User cannot be null";
172
173 setAuthentication(user);
174 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
175 cdmBase.setDoubtful(true);
176 cdmEntityDaoBase.update(cdmBase);
177 }
178
179 /**
180 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findById(int)}.
181 */
182 @Test
183 @DataSet("CdmEntityDaoBaseTest.xml")
184 public void testFindById() {
185 CdmBase cdmBase = cdmEntityDaoBase.findById(1);
186 assertNotNull("There should be an entity with an id of 1",cdmBase);
187 }
188
189 /**
190 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findByUuid(java.util.UUID)}.
191 */
192 @Test
193 @DataSet("CdmEntityDaoBaseTest.xml")
194 public void testFindByUuid() {
195 CdmBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
196 assertNotNull("testFindByUuid() an entity with a uuid of " + uuid.toString(),cdmBase);
197 }
198
199 /**
200 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#exists(java.util.UUID)}.
201 */
202 @Test
203 @DataSet("CdmEntityDaoBaseTest.xml")
204 public void testExists() {
205 boolean exists = cdmEntityDaoBase.exists(uuid);
206 assertTrue("exists() should return true for uuid " + uuid.toString(), exists);
207 boolean existsRandom = cdmEntityDaoBase.exists(UUID.randomUUID());
208 assertFalse("exists() should return false for any other uuid", existsRandom);
209 }
210
211 /**
212 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
213 */
214 @Test
215 @DataSet("CdmEntityDaoBaseTest.xml")
216 public void testList() {
217 List<TaxonBase> list = cdmEntityDaoBase.list(1000, 0);
218 assertNotNull("list() should not return null",list);
219 assertEquals("list() should return a list with two entities in it",list.size(),2);
220 }
221
222 /**
223 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)}.
224 */
225 @Test
226 @DataSet("CdmEntityDaoBaseTest.xml")
227 @ExpectedDataSet
228 public void testDelete() {
229 TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
230 assertNotNull(cdmBase);
231 cdmEntityDaoBase.delete(cdmBase);
232 }
233 }