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