Project

General

Profile

Download (7.06 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.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.CdmIntegrationTest;
39

    
40
/**
41
 * @author a.mueller
42
 *
43
 */
44
public class CdmEntityDaoBaseTest extends CdmIntegrationTest {
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.setUuid(UUID.fromString("61410dd0-c774-11dd-ad8b-0800200c9a66"));
101
		cdmEntityDaoBase.saveOrUpdate(cdmBase);
102
	}
103
	
104
	/**
105
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
106
	 */
107
	@Test
108
	@DataSet("CdmEntityDaoBaseTest.xml")
109
	@ExpectedDataSet
110
	public void testSave() throws Exception {
111
		cdmEntityDaoBase.save(cdmBase);
112
	}
113
	
114
	/**
115
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
116
	 */
117
	@Test
118
	@DataSet("CdmEntityDaoBaseTest.xml")
119
	@ExpectedDataSet
120
	public void testSaveWithAuthentication() throws Exception {
121
		User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
122
		assert user != null : "User cannot be null";
123
		setAuthentication(user);
124
		cdmEntityDaoBase.save(cdmBase);
125
		System.out.println(((Taxon)cdmBase).getTitleCache());
126
	}
127

    
128
	/**
129
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#update(eu.etaxonomy.cdm.model.common.CdmBase)}.
130
	 */
131
	@Test
132
	@DataSet("CdmEntityDaoBaseTest.xml")
133
	@ExpectedDataSet
134
	public void testUpdate() {
135
		TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
136
		cdmBase.setUuid(UUID.fromString("65bc7d70-c76c-11dd-ad8b-0800200c9a66"));
137
		cdmEntityDaoBase.update(cdmBase);
138
	}
139
	
140
	@Test
141
	@DataSet("CdmEntityDaoBaseTest.xml")
142
	@ExpectedDataSet
143
	public void testUpdateWithAuthentication() {
144
		User user = userDao.findByUuid(UUID.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
145
		assert user != null : "User cannot be null";
146
		
147
		setAuthentication(user);
148
		TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
149
		cdmBase.setUuid(UUID.fromString("65bc7d70-c76c-11dd-ad8b-0800200c9a66"));
150
		cdmEntityDaoBase.update(cdmBase);
151
	}
152

    
153
	/**
154
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findById(int)}.
155
	 */
156
	@Test
157
	@DataSet("CdmEntityDaoBaseTest.xml")
158
	public void testFindById() {		
159
		CdmBase cdmBase = cdmEntityDaoBase.findById(1);
160
		assertNotNull("There should be an entity with an id of 1",cdmBase);
161
	}
162

    
163
	/**
164
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findByUuid(java.util.UUID)}.
165
	 */
166
	@Test
167
	@DataSet("CdmEntityDaoBaseTest.xml")
168
	public void testFindByUuid() {
169
		CdmBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
170
		assertNotNull("testFindByUuid() an entity with a uuid of " + uuid.toString(),cdmBase);
171
	}
172

    
173
	/**
174
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#exists(java.util.UUID)}.
175
	 */
176
	@Test
177
	@DataSet("CdmEntityDaoBaseTest.xml")
178
	public void testExists() {
179
		boolean exists = cdmEntityDaoBase.exists(uuid);
180
		assertTrue("exists() should return true for uuid " + uuid.toString(), exists);
181
		boolean existsRandom = cdmEntityDaoBase.exists(UUID.randomUUID());
182
		assertFalse("exists() should return false for any other uuid", existsRandom);
183
	}
184

    
185
	/**
186
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
187
	 */
188
	@Test
189
	@DataSet("CdmEntityDaoBaseTest.xml")
190
	public void testList() {
191
		List<TaxonBase> list = cdmEntityDaoBase.list(1000, 0);
192
		assertNotNull("list() should not return null",list);
193
		assertEquals("list() should return a list with two entities in it",list.size(),2);
194
	}
195
	
196
	/**
197
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)}.
198
	 */
199
	@Test
200
	@DataSet("CdmEntityDaoBaseTest.xml")
201
	@ExpectedDataSet
202
	public void testDelete() {
203
		TaxonBase cdmBase = cdmEntityDaoBase.findByUuid(uuid);
204
		assertNotNull(cdmBase);
205
		cdmEntityDaoBase.delete(cdmBase);
206
	}
207
}
(2-2/8)