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 junit.framework.Assert;
|
22
|
|
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.database.EvaluationFailedException;
|
35
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
36
|
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
|
37
|
import eu.etaxonomy.cdm.model.common.User;
|
38
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
39
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
40
|
import eu.etaxonomy.cdm.persistence.dao.common.IUserDao;
|
41
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
42
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
43
|
import eu.etaxonomy.cdm.persistence.query.RandomOrder;
|
44
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
45
|
|
46
|
/**
|
47
|
* @author a.mueller
|
48
|
*
|
49
|
*/
|
50
|
public class CdmEntityDaoBaseTest extends CdmTransactionalIntegrationTest {
|
51
|
|
52
|
private UUID uuid;
|
53
|
private TaxonBase cdmBase;
|
54
|
|
55
|
@SpringBeanByType
|
56
|
private ITaxonDao cdmEntityDaoBase;
|
57
|
|
58
|
@SpringBeanByType
|
59
|
private AuthenticationManager authenticationManager;
|
60
|
|
61
|
@SpringBeanByType
|
62
|
private IUserDao userDao;
|
63
|
|
64
|
/**
|
65
|
* @throws java.lang.Exception
|
66
|
*/
|
67
|
@Before
|
68
|
public void setUp() throws Exception {
|
69
|
uuid = UUID.fromString("8d77c380-c76a-11dd-ad8b-0800200c9a66");
|
70
|
cdmBase = Taxon.NewInstance(null, null);
|
71
|
cdmBase.setUuid(UUID.fromString("e463b270-c76b-11dd-ad8b-0800200c9a66"));
|
72
|
|
73
|
// Clear the context prior to each test
|
74
|
SecurityContextHolder.clearContext();
|
75
|
}
|
76
|
|
77
|
private void setAuthentication(User user) {
|
78
|
TestingAuthenticationToken token = new TestingAuthenticationToken(user, "password", new GrantedAuthorityImpl[0]);
|
79
|
Authentication authentication = authenticationManager.authenticate(token);
|
80
|
|
81
|
SecurityContextImpl secureContext = new SecurityContextImpl();
|
82
|
secureContext.setAuthentication(authentication);
|
83
|
SecurityContextHolder.setContext(secureContext);
|
84
|
}
|
85
|
|
86
|
/************ TESTS ********************************/
|
87
|
|
88
|
|
89
|
/**
|
90
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#CdmEntityDaoBase(java.lang.Class)}.
|
91
|
* @throws Exception
|
92
|
*/
|
93
|
@Test
|
94
|
public void testCdmEntityDaoBase() throws Exception {
|
95
|
assertNotNull("cdmEntityDaoBase should exist",cdmEntityDaoBase);
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
|
100
|
*/
|
101
|
@Test
|
102
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
103
|
@ExpectedDataSet
|
104
|
public void testSaveOrUpdate() {
|
105
|
TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
106
|
cdmBase.setDoubtful(true);
|
107
|
cdmEntityDaoBase.saveOrUpdate(cdmBase);
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
|
112
|
*/
|
113
|
@Test
|
114
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
115
|
@ExpectedDataSet
|
116
|
public void testSaveOrUpdateWithAuthentication() {
|
117
|
User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
|
118
|
assert user != null : "User cannot be null";
|
119
|
setAuthentication(user);
|
120
|
TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
121
|
cdmBase.setDoubtful(true);
|
122
|
cdmEntityDaoBase.saveOrUpdate(cdmBase);
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
|
127
|
*/
|
128
|
@Test
|
129
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
130
|
@ExpectedDataSet
|
131
|
public void testSaveOrUpdateNewObjectWithAuthentication() {
|
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
|
public void testSaveWithAuthenticationFailedPermissionEvaluation() throws Exception {
|
153
|
User user = userDao.findByUuid(UUID.fromString("04f43bec-ff0e-4263-b4f8-24d763e590eb"));
|
154
|
assert user != null : "User cannot be null";
|
155
|
setAuthentication(user);
|
156
|
|
157
|
|
158
|
try{
|
159
|
cdmEntityDaoBase.save(cdmBase);
|
160
|
Assert.fail();
|
161
|
}catch(EvaluationFailedException e){
|
162
|
|
163
|
}
|
164
|
}
|
165
|
|
166
|
@Test
|
167
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
168
|
|
169
|
public void testSaveWithAuthentication() throws Exception {
|
170
|
User user = userDao.findByUuid(UUID.fromString("c026b289-1a36-4afc-8673-92ffe8ed05b6"));
|
171
|
assert user != null : "User cannot be null";
|
172
|
setAuthentication(user);
|
173
|
cdmEntityDaoBase.save(cdmBase);
|
174
|
}
|
175
|
/**
|
176
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#update(eu.etaxonomy.cdm.model.common.CdmBase)}.
|
177
|
*/
|
178
|
@Test
|
179
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
180
|
@ExpectedDataSet
|
181
|
public void testUpdate() {
|
182
|
TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
183
|
cdmBase.setDoubtful(true);
|
184
|
cdmEntityDaoBase.update(cdmBase);
|
185
|
}
|
186
|
|
187
|
@Test
|
188
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
189
|
@ExpectedDataSet
|
190
|
public void testUpdateWithAuthentication() {
|
191
|
User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
|
192
|
assert user != null : "User cannot be null";
|
193
|
|
194
|
setAuthentication(user);
|
195
|
TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
196
|
cdmBase.setDoubtful(true);
|
197
|
cdmEntityDaoBase.update(cdmBase);
|
198
|
}
|
199
|
|
200
|
/**
|
201
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findById(int)}.
|
202
|
*/
|
203
|
@Test
|
204
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
205
|
public void testFindById() {
|
206
|
CdmBase cdmBase = cdmEntityDaoBase.findById(1);
|
207
|
assertNotNull("There should be an entity with an id of 1",cdmBase);
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findByUuid(java.util.UUID)}.
|
212
|
*/
|
213
|
@Test
|
214
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
215
|
public void testFindByUuid() {
|
216
|
CdmBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
217
|
assertNotNull("testFindByUuid() an entity with a uuid of " + uuid.toString(),cdmBase);
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#exists(java.util.UUID)}.
|
222
|
*/
|
223
|
@Test
|
224
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
225
|
public void testExists() {
|
226
|
boolean exists = cdmEntityDaoBase.exists(uuid);
|
227
|
assertTrue("exists() should return true for uuid " + uuid.toString(), exists);
|
228
|
boolean existsRandom = cdmEntityDaoBase.exists(UUID.randomUUID());
|
229
|
assertFalse("exists() should return false for any other uuid", existsRandom);
|
230
|
}
|
231
|
|
232
|
/**
|
233
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
|
234
|
*/
|
235
|
@Test
|
236
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
237
|
public void testList() {
|
238
|
List<TaxonBase> list = cdmEntityDaoBase.list(1000, 0);
|
239
|
assertNotNull("list() should not return null",list);
|
240
|
assertEquals("list() should return a list with two entities in it",list.size(),2);
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
|
245
|
*/
|
246
|
@Test
|
247
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
248
|
public void testRandomOrder() {
|
249
|
List<OrderHint> orderHints = new ArrayList<OrderHint>();
|
250
|
orderHints.add(new RandomOrder());
|
251
|
List<TaxonBase> list = cdmEntityDaoBase.list(null,1000, 0,orderHints,null);
|
252
|
assertNotNull("list() should not return null",list);
|
253
|
assertEquals("list() should return a list with two entities in it",list.size(),2);
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)}.
|
258
|
*/
|
259
|
@Test
|
260
|
@DataSet("CdmEntityDaoBaseTest.xml")
|
261
|
@ExpectedDataSet
|
262
|
public void testDelete() {
|
263
|
TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
|
264
|
assertNotNull(cdmBase);
|
265
|
cdmEntityDaoBase.delete(cdmBase);
|
266
|
}
|
267
|
}
|