1
package org.hisp.dhis.user;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.util.Collection;
31
import java.util.Date;
32
import java.util.HashSet;
34
import org.hisp.dhis.DhisSpringTest;
35
import org.hisp.dhis.organisationunit.OrganisationUnit;
36
import org.hisp.dhis.organisationunit.OrganisationUnitService;
37
import org.hisp.dhis.transaction.TransactionManager;
40
* @author Nguyen Hong Duc
41
* @version $Id: UserStoreTest.java 5724 2008-09-18 14:37:01Z larshelg $
43
public class UserStoreTest
44
extends DhisSpringTest
46
private UserStore userStore;
48
private TransactionManager transactionManager;
50
private OrganisationUnitService organisationUnitService;
52
public void setUpTest()
55
userStore = (UserStore) getBean( UserStore.ID );
57
transactionManager = (TransactionManager) getBean( TransactionManager.ID );
59
organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
62
public void testBasicUser()
65
OrganisationUnit unit1 = new OrganisationUnit( "name1", "shortName1", "organisationUnitCode1", new Date(),
66
new Date(), true, "comment" );
67
OrganisationUnit unit2 = new OrganisationUnit( "name2", "shortName2", "organisationUnitCode2", new Date(),
68
new Date(), true, "comment" );
70
Collection<OrganisationUnit> units1 = new HashSet<OrganisationUnit>();
75
organisationUnitService.addOrganisationUnit( unit1 );
76
organisationUnitService.addOrganisationUnit( unit2 );
78
String userName = "User";
79
User user = new User();
80
user.setSurname( userName );
81
user.setFirstName( userName );
84
int id = userStore.addUser( user );
85
assertEquals( userStore.getUser( id ).getSurname(), userName );
86
assertEquals( userStore.getUser( id ).getFirstName(), userName );
87
assertEquals( 1, userStore.getAllUsers().size(), 1 );
88
assertEquals( 0, userStore.getUsersByOrganisationUnit( unit1 ).size() );
89
assertEquals( 0, userStore.getUsersByOrganisationUnit( unit2 ).size() );
90
assertEquals( 1, userStore.getUsersWithoutOrganisationUnit().size() );
93
user.setSurname( "User1" );
94
user.setOrganisationUnits( units1 );
95
userStore.updateUser( user );
97
assertEquals( userStore.getUser( id ).getSurname(), "User1" );
98
assertEquals( 1, userStore.getUsersByOrganisationUnit( unit1 ).size() );
99
assertEquals( 1, userStore.getUsersByOrganisationUnit( unit2 ).size() );
100
assertEquals( 0, userStore.getUsersWithoutOrganisationUnit().size() );
103
assertEquals( userStore.getUser( user.getId() ).getSurname(), "User1" );
104
assertEquals( userStore.getUser( user.getId() ).getFirstName(), userName );
105
transactionManager.enter();
106
assertEquals( 2, userStore.getUser( user.getId() ).getOrganisationUnits().size() );
107
transactionManager.leave();
108
assertEquals( userStore.getUser( user.getId() ).getId(), 1 );
111
User user2 = new User();
112
Collection<OrganisationUnit> units2 = new HashSet<OrganisationUnit>();
115
user2.setSurname( "User2" );
116
user2.setFirstName( "User2" );
117
user2.setOrganisationUnits( units2 );
118
userStore.addUser( user2 );
120
assertEquals( userStore.getAllUsers().size(), 2 );
121
for ( int i = 1; i <= userStore.getAllUsers().size(); i++ )
123
// System.out.println( "User" + i );
124
assertEquals( userStore.getUser( i ).getSurname(), "User" + i );
126
assertEquals( 1, userStore.getUsersByOrganisationUnit( unit1 ).size() );
127
assertEquals( 2, userStore.getUsersByOrganisationUnit( unit2 ).size() );
128
assertEquals( 0, userStore.getUsersWithoutOrganisationUnit().size() );
131
User user3 = new User();
132
user3.setSurname( "User3" );
133
user3.setFirstName( "User3" );
134
OrganisationUnit unit3 = new OrganisationUnit( "name3", "shortName3", "organisationUnitCode3", new Date(),
135
new Date(), true, "comment" );
136
organisationUnitService.addOrganisationUnit( unit3 );
137
Collection<OrganisationUnit> units3 = new HashSet<OrganisationUnit>();
140
user.setOrganisationUnits( units3 );
141
userStore.addUser( user3 );
143
assertEquals( userStore.getAllUsers().size(), 3 );
145
assertEquals( userStore.getUser( user3.getId() ).getSurname(), "User3" );
146
userStore.deleteUser( user3 );
147
assertEquals( userStore.getAllUsers().size(), 2 );
150
public void testBasicUserCredentials()
153
// Test addUserCredentials
154
String username = "user";
155
String password = "password";
156
String someone = "someone";
157
String iloveyou = "iloveyou";
159
User user = new User();
160
user.setSurname( username );
161
user.setFirstName( username );
162
userStore.addUser( user );
164
UserCredentials userCredentials = new UserCredentials();
165
userCredentials.setUser( user );
166
userCredentials.setUsername( username );
167
userCredentials.setPassword( password );
169
userStore.addUserCredentials( userCredentials );
171
transactionManager.enter();
172
assertEquals( userStore.getUserCredentials( user ).getUser().getId(), user.getId() );
173
transactionManager.leave();
174
assertEquals( userStore.getUserCredentials( user ).getUsername(), username );
175
assertEquals( userStore.getUserCredentials( user ).getPassword(), password );
177
// Test updateUserCredentials
178
userCredentials.setUser( user );
179
userCredentials.setUsername( someone );
180
userCredentials.setPassword( iloveyou );
182
userStore.updateUserCredentials( userCredentials );
183
assertEquals( userStore.getUserCredentials( user ).getUsername(), someone );
184
assertEquals( userStore.getUserCredentials( user ).getPassword(), iloveyou );
186
// Test getUserCredentials
187
assertEquals( userStore.getUserCredentials( user ).getUsername(), someone );
188
assertEquals( userStore.getUserCredentials( user ).getPassword(), iloveyou );
190
// Test getUserCredentialsByUsername
191
// System.out.println( userStore.getUserCredentialsByUsername( someone
192
// ).getPassword() );
193
assertEquals( userStore.getUserCredentialsByUsername( someone ).getPassword(), userCredentials.getPassword() );
194
assertEquals( userStore.getUserCredentialsByUsername( someone ).getClass(), userCredentials.getClass() );
196
// Test deleteUserCredentials
198
assertNotNull( userStore.getUserCredentials( user ) );
199
userStore.deleteUserCredentials( userStore.getUserCredentials( user ) );
201
assertNull( userStore.getUserCredentials( user ) );
204
public void testBasicUserAuthorityGroup()
207
String name = "UserAuthorityGroup";
208
String name1 = "UserAuthorityGroup1";
209
String name2 = "UserAuthorityGroup2";
211
// Test addUserAuthorityGroup
212
UserAuthorityGroup userAuthorityGroup = new UserAuthorityGroup();
213
userAuthorityGroup.setName( name );
214
userStore.addUserAuthorityGroup( userAuthorityGroup );
215
transactionManager.enter();
216
assertEquals( userStore.getUserAuthorityGroup( userAuthorityGroup.getId() ).getName(), name );
217
transactionManager.leave();
219
// Test updateUserAuthorityGroup
220
userAuthorityGroup.setName( name1 );
221
userStore.updateUserAuthorityGroup( userAuthorityGroup );
222
assertEquals( userAuthorityGroup.getName(), name1 );
224
// Test getUserAuthorityGroup
225
assertEquals( userStore.getUserAuthorityGroup( userAuthorityGroup.getId() ).getName(), name1 );
226
assertEquals( userStore.getUserAuthorityGroup( userAuthorityGroup.getId() ).getClass(), userAuthorityGroup
229
// Test getAllUserAuthorityGroups
230
UserAuthorityGroup userAuthorityGroup2 = new UserAuthorityGroup();
231
userAuthorityGroup2.setName( name2 );
232
userStore.addUserAuthorityGroup( userAuthorityGroup2 );
234
assertEquals( userStore.getAllUserAuthorityGroups().size(), 2 );
235
for ( int i = 1; i <= userStore.getAllUserAuthorityGroups().size(); i++ )
237
// System.out.println( "UserAuthorityGroup" + i );
238
assertEquals( userStore.getUserAuthorityGroup( i ).getName(), "UserAuthorityGroup" + i );
241
// Test deleteUserAuthorityGroup
242
assertEquals( userStore.getAllUserAuthorityGroups().size(), 2 );
243
userStore.deleteUserAuthorityGroup( userAuthorityGroup2 );
244
assertEquals( userStore.getAllUserAuthorityGroups().size(), 1 );
247
public void testBasicUserSettings()
250
String name = "name";
251
String value = "value";
252
String value1 = "value1";
254
// Test addUserSetting
255
String userName = "User";
256
User user = new User();
257
user.setSurname( userName );
258
user.setFirstName( userName );
259
userStore.addUser( user );
261
UserSetting userSetting = new UserSetting();
262
userSetting.setUser( user );
263
userSetting.setName( name );
264
userSetting.setValue( value );
266
userStore.addUserSetting( userSetting );
267
assertEquals( userStore.getUserSetting( user, name ).getName(), userSetting.getName() );
269
// Test updateUserSetting
270
userSetting.setValue( value1 );
271
// System.out.println( userSetting.getName() );
272
userStore.updateUserSetting( userSetting );
273
// System.out.println( userSetting.getName() );
274
assertEquals( value1, userStore.getUserSetting( user, name ).getValue() );
276
// Test getUserSetting
277
assertEquals( userStore.getUserSetting( userSetting.getUser(), name ).getName(), name );
278
transactionManager.enter();
279
assertEquals( userStore.getUserSetting( userSetting.getUser(), name ).getUser().getId(), user.getId() );
280
transactionManager.leave();
281
assertEquals( userStore.getUserSetting( userSetting.getUser(), name ).getValue(), value1 );
283
// Test getAllUserSettings
284
assertEquals( userStore.getAllUserSettings( user ).size(), 1 );
285
for ( int i = 1; i <= userStore.getAllUserSettings( user ).size(); i++ )
287
// System.out.println( "UserSettings" + i );
288
assertEquals( userStore.getUserSetting( user, name ).getValue(), "value" + i );
291
// Test deleteUserSetting
292
assertEquals( userStore.getAllUserSettings( user ).size(), 1 );
293
userStore.deleteUserSetting( userStore.getUserSetting( user, name ) );
294
assertEquals( userStore.getAllUserSettings( user ).size(), 0 );