~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-user-hibernate/src/test/java/org/hisp/dhis/user/UserStoreTest.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.user;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import java.util.Collection;
 
31
import java.util.Date;
 
32
import java.util.HashSet;
 
33
 
 
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;
 
38
 
 
39
/**
 
40
 * @author Nguyen Hong Duc
 
41
 * @version $Id: UserStoreTest.java 5724 2008-09-18 14:37:01Z larshelg $
 
42
 */
 
43
public class UserStoreTest
 
44
    extends DhisSpringTest
 
45
{
 
46
    private UserStore userStore;
 
47
 
 
48
    private TransactionManager transactionManager;
 
49
 
 
50
    private OrganisationUnitService organisationUnitService;
 
51
 
 
52
    public void setUpTest()
 
53
        throws Exception
 
54
    {
 
55
        userStore = (UserStore) getBean( UserStore.ID );
 
56
 
 
57
        transactionManager = (TransactionManager) getBean( TransactionManager.ID );
 
58
 
 
59
        organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
 
60
    }
 
61
 
 
62
    public void testBasicUser()
 
63
        throws Exception
 
64
    {
 
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" );
 
69
        
 
70
        Collection<OrganisationUnit> units1 = new HashSet<OrganisationUnit>();       
 
71
        
 
72
        units1.add(unit1);
 
73
        units1.add(unit2);
 
74
 
 
75
        organisationUnitService.addOrganisationUnit( unit1 );
 
76
        organisationUnitService.addOrganisationUnit( unit2 );
 
77
        
 
78
        String userName = "User";
 
79
        User user = new User();
 
80
        user.setSurname( userName );
 
81
        user.setFirstName( userName );
 
82
 
 
83
        // Test addUser
 
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() );
 
91
 
 
92
        // Test updateUser
 
93
        user.setSurname( "User1" );
 
94
        user.setOrganisationUnits( units1 );
 
95
        userStore.updateUser( user );
 
96
        
 
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() );
 
101
 
 
102
        // Test getUser
 
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 );
 
109
 
 
110
        // Test getAllUsers
 
111
        User user2 = new User();
 
112
        Collection<OrganisationUnit> units2 = new HashSet<OrganisationUnit>();        
 
113
        units2.add(unit2);
 
114
        
 
115
        user2.setSurname( "User2" );
 
116
        user2.setFirstName( "User2" );
 
117
        user2.setOrganisationUnits( units2 );
 
118
        userStore.addUser( user2 );
 
119
 
 
120
        assertEquals( userStore.getAllUsers().size(), 2 );
 
121
        for ( int i = 1; i <= userStore.getAllUsers().size(); i++ )
 
122
        {
 
123
            // System.out.println( "User" + i );
 
124
            assertEquals( userStore.getUser( i ).getSurname(), "User" + i );
 
125
        }
 
126
        assertEquals( 1, userStore.getUsersByOrganisationUnit( unit1 ).size() );
 
127
        assertEquals( 2, userStore.getUsersByOrganisationUnit( unit2 ).size() );
 
128
        assertEquals( 0, userStore.getUsersWithoutOrganisationUnit().size() );
 
129
 
 
130
        // Test deleteUser
 
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>();        
 
138
        units3.add(unit3);
 
139
        
 
140
        user.setOrganisationUnits( units3 );
 
141
        userStore.addUser( user3 );
 
142
 
 
143
        assertEquals( userStore.getAllUsers().size(), 3 );
 
144
        // delete User3
 
145
        assertEquals( userStore.getUser( user3.getId() ).getSurname(), "User3" );
 
146
        userStore.deleteUser( user3 );
 
147
        assertEquals( userStore.getAllUsers().size(), 2 );
 
148
    }
 
149
 
 
150
    public void testBasicUserCredentials()
 
151
        throws Exception
 
152
    {
 
153
        // Test addUserCredentials
 
154
        String username = "user";
 
155
        String password = "password";
 
156
        String someone = "someone";
 
157
        String iloveyou = "iloveyou";
 
158
 
 
159
        User user = new User();
 
160
        user.setSurname( username );
 
161
        user.setFirstName( username );
 
162
        userStore.addUser( user );
 
163
 
 
164
        UserCredentials userCredentials = new UserCredentials();
 
165
        userCredentials.setUser( user );
 
166
        userCredentials.setUsername( username );
 
167
        userCredentials.setPassword( password );
 
168
 
 
169
        userStore.addUserCredentials( userCredentials );
 
170
 
 
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 );
 
176
 
 
177
        // Test updateUserCredentials
 
178
        userCredentials.setUser( user );
 
179
        userCredentials.setUsername( someone );
 
180
        userCredentials.setPassword( iloveyou );
 
181
 
 
182
        userStore.updateUserCredentials( userCredentials );
 
183
        assertEquals( userStore.getUserCredentials( user ).getUsername(), someone );
 
184
        assertEquals( userStore.getUserCredentials( user ).getPassword(), iloveyou );
 
185
 
 
186
        // Test getUserCredentials
 
187
        assertEquals( userStore.getUserCredentials( user ).getUsername(), someone );
 
188
        assertEquals( userStore.getUserCredentials( user ).getPassword(), iloveyou );
 
189
 
 
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() );
 
195
 
 
196
        // Test deleteUserCredentials
 
197
        // Before delete
 
198
        assertNotNull( userStore.getUserCredentials( user ) );
 
199
        userStore.deleteUserCredentials( userStore.getUserCredentials( user ) );
 
200
        // After delete
 
201
        assertNull( userStore.getUserCredentials( user ) );
 
202
    }
 
203
 
 
204
    public void testBasicUserAuthorityGroup()
 
205
        throws Exception
 
206
    {
 
207
        String name = "UserAuthorityGroup";
 
208
        String name1 = "UserAuthorityGroup1";
 
209
        String name2 = "UserAuthorityGroup2";
 
210
 
 
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();
 
218
 
 
219
        // Test updateUserAuthorityGroup
 
220
        userAuthorityGroup.setName( name1 );
 
221
        userStore.updateUserAuthorityGroup( userAuthorityGroup );
 
222
        assertEquals( userAuthorityGroup.getName(), name1 );
 
223
 
 
224
        // Test getUserAuthorityGroup
 
225
        assertEquals( userStore.getUserAuthorityGroup( userAuthorityGroup.getId() ).getName(), name1 );
 
226
        assertEquals( userStore.getUserAuthorityGroup( userAuthorityGroup.getId() ).getClass(), userAuthorityGroup
 
227
            .getClass() );
 
228
 
 
229
        // Test getAllUserAuthorityGroups
 
230
        UserAuthorityGroup userAuthorityGroup2 = new UserAuthorityGroup();
 
231
        userAuthorityGroup2.setName( name2 );
 
232
        userStore.addUserAuthorityGroup( userAuthorityGroup2 );
 
233
 
 
234
        assertEquals( userStore.getAllUserAuthorityGroups().size(), 2 );
 
235
        for ( int i = 1; i <= userStore.getAllUserAuthorityGroups().size(); i++ )
 
236
        {
 
237
            // System.out.println( "UserAuthorityGroup" + i );
 
238
            assertEquals( userStore.getUserAuthorityGroup( i ).getName(), "UserAuthorityGroup" + i );
 
239
        }
 
240
 
 
241
        // Test deleteUserAuthorityGroup
 
242
        assertEquals( userStore.getAllUserAuthorityGroups().size(), 2 );
 
243
        userStore.deleteUserAuthorityGroup( userAuthorityGroup2 );
 
244
        assertEquals( userStore.getAllUserAuthorityGroups().size(), 1 );
 
245
    }
 
246
 
 
247
    public void testBasicUserSettings()
 
248
        throws Exception
 
249
    {
 
250
        String name = "name";
 
251
        String value = "value";
 
252
        String value1 = "value1";
 
253
 
 
254
        // Test addUserSetting
 
255
        String userName = "User";
 
256
        User user = new User();
 
257
        user.setSurname( userName );
 
258
        user.setFirstName( userName );
 
259
        userStore.addUser( user );
 
260
 
 
261
        UserSetting userSetting = new UserSetting();
 
262
        userSetting.setUser( user );
 
263
        userSetting.setName( name );
 
264
        userSetting.setValue( value );
 
265
 
 
266
        userStore.addUserSetting( userSetting );
 
267
        assertEquals( userStore.getUserSetting( user, name ).getName(), userSetting.getName() );
 
268
 
 
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() );
 
275
 
 
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 );
 
282
 
 
283
        // Test getAllUserSettings
 
284
        assertEquals( userStore.getAllUserSettings( user ).size(), 1 );
 
285
        for ( int i = 1; i <= userStore.getAllUserSettings( user ).size(); i++ )
 
286
        {
 
287
            // System.out.println( "UserSettings" + i );
 
288
            assertEquals( userStore.getUserSetting( user, name ).getValue(), "value" + i );
 
289
        }
 
290
 
 
291
        // Test deleteUserSetting
 
292
        assertEquals( userStore.getAllUserSettings( user ).size(), 1 );
 
293
        userStore.deleteUserSetting( userStore.getUserSetting( user, name ) );
 
294
        assertEquals( userStore.getAllUserSettings( user ).size(), 0 );
 
295
    }
 
296
}