View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.portal.business.user;
35  
36  import fr.paris.lutece.test.LuteceTestCase;
37  
38  import java.util.Collection;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  public class AdminUserTest extends LuteceTestCase
43  {
44      private final static String ACCESSCODE1 = "AccessCode 1";
45      private final static String ACCESSCODE2 = "AccessCode 2";
46      private final static String LASTNAME1 = "LastName 1";
47      private final static String LASTNAME2 = "LastName 2";
48      private final static String FIRSTNAME1 = "FirstName 1";
49      private final static String FIRSTNAME2 = "FirstName 2";
50      private final static String EMAIL1 = "Email 1";
51      private final static String EMAIL2 = "Email 2";
52      private final static int STATUS1 = 1;
53      private final static int STATUS2 = 2;
54      private static final String RIGHT1 = "Right 1";
55      private static final String ROLE1 = "Role 1";
56      private final static int LEVEL = 0;
57  
58      public void testBusinessUser( )
59      {
60          // Initialize an object
61          AdminUser user = new AdminUser( );
62          user.setAccessCode( ACCESSCODE1 );
63          user.setLastName( LASTNAME1 );
64          user.setFirstName( FIRSTNAME1 );
65          user.setEmail( EMAIL1 );
66          user.setStatus( STATUS1 );
67          user.setLocale( Locale.ENGLISH );
68          user.setUserLevel( LEVEL );
69  
70          // Create test
71          AdminUserHome.create( user );
72  
73          AdminUser userStored = AdminUserHome.findByPrimaryKey( user.getUserId( ) );
74          assertEquals( userStored.getAccessCode( ), user.getAccessCode( ) );
75          assertEquals( userStored.getLastName( ), user.getLastName( ) );
76          assertEquals( userStored.getFirstName( ), user.getFirstName( ) );
77          assertEquals( userStored.getEmail( ), user.getEmail( ) );
78          assertEquals( userStored.getStatus( ), user.getStatus( ) );
79          assertEquals( userStored.getLocale( ), user.getLocale( ) );
80          assertEquals( userStored.getUserLevel( ), user.getUserLevel( ) );
81  
82          AdminUserHome.createRightForUser( user.getUserId( ), RIGHT1 );
83          AdminUserHome.createRoleForUser( user.getUserId( ), ROLE1 );
84  
85          // List Test
86          Collection listUsers = AdminUserHome.findUserList( );
87          assertTrue( listUsers.size( ) > 0 );
88  
89          Map listRights = AdminUserHome.getRightsListForUser( user.getUserId( ) );
90  
91          // assertTrue( listRights.size() > 0 );
92          Map listRoles = AdminUserHome.getRolesListForUser( user.getUserId( ) );
93          // assertTrue( listRoles.size() > 0 );
94  
95          // Update test
96          user.setAccessCode( ACCESSCODE2 );
97          user.setLastName( LASTNAME2 );
98          user.setFirstName( FIRSTNAME2 );
99          user.setEmail( EMAIL2 );
100         user.setStatus( STATUS2 );
101         user.setLocale( Locale.FRANCE );
102 
103         AdminUserHome.update( user );
104         userStored = AdminUserHome.findByPrimaryKey( user.getUserId( ) );
105         assertEquals( userStored.getAccessCode( ), user.getAccessCode( ) );
106         assertEquals( userStored.getLastName( ), user.getLastName( ) );
107         assertEquals( userStored.getFirstName( ), user.getFirstName( ) );
108         assertEquals( userStored.getEmail( ), user.getEmail( ) );
109         assertEquals( userStored.getStatus( ), user.getStatus( ) );
110         // assertEquals( userStored.getLocale( ), user.getLocale( ) );
111 
112         // Delete test
113         AdminUserHome.removeAllRightsForUser( user.getUserId( ) );
114         AdminUserHome.removeAllRolesForUser( user.getUserId( ) );
115         AdminUserHome.remove( user.getUserId( ) );
116         userStored = AdminUserHome.findByPrimaryKey( user.getUserId( ) );
117         assertNull( userStored );
118     }
119 
120     public void testSetUserInfo( )
121     {
122         AdminUser user = new AdminUser( );
123         final Object info = new Object( );
124 
125         final String strKey = "key";
126         assertNull( user.setUserInfo( strKey, info ) );
127         assertEquals( info, user.getUserInfo( strKey ) );
128     }
129 
130     public void testSetUserInfoReturnPrevious( )
131     {
132         AdminUser user = new AdminUser( );
133         final Object previous = new Object( );
134 
135         final String strKey = "key";
136         assertNull( user.setUserInfo( strKey, previous ) );
137         assertEquals( previous, user.getUserInfo( strKey ) );
138         final Object info = new Object( );
139         assertSame( previous, user.setUserInfo( strKey, info ) );
140         assertEquals( info, user.getUserInfo( strKey ) );
141     }
142 }