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.rbac;
35  
36  import fr.paris.lutece.test.LuteceTestCase;
37  
38  import java.util.Collection;
39  
40  public class RBACTest extends LuteceTestCase
41  {
42      private final static String ROLEKEY1 = "RoleKey 1";
43      private final static String ROLEKEY2 = "RoleKey 2";
44      private final static String RESOURCETYPE1 = "ResourceType 1";
45      private final static String RESOURCETYPE2 = "ResourceType 2";
46      private final static String RESOURCEID1 = "ResourceId 1";
47      private final static String RESOURCEID2 = "ResourceId 2";
48      private final static String PERMISSION1 = "Permission 1";
49      private final static String PERMISSION2 = "Permission 2";
50  
51      public void testBusiness( )
52      {
53          // Initialize an object 1
54          RBAC rbac1 = new RBAC( );
55          rbac1.setRoleKey( ROLEKEY1 );
56          rbac1.setResourceTypeKey( RESOURCETYPE1 );
57          rbac1.setResourceId( RESOURCEID1 );
58          rbac1.setPermissionKey( PERMISSION1 );
59  
60          // Initialize an object 2
61          RBAC rbac2 = new RBAC( );
62          rbac2.setRoleKey( ROLEKEY2 );
63          rbac2.setResourceTypeKey( RESOURCETYPE2 );
64          rbac2.setResourceId( RESOURCEID2 );
65          rbac2.setPermissionKey( PERMISSION2 );
66  
67          // Create test
68          RBACHome.create( rbac1 );
69  
70          RBAC rbacStored = RBACHome.findByPrimaryKey( rbac1.getRBACId( ) );
71          assertEquals( rbacStored.getRoleKey( ), rbac1.getRoleKey( ) );
72          assertEquals( rbacStored.getResourceTypeKey( ), rbac1.getResourceTypeKey( ) );
73          assertEquals( rbacStored.getResourceId( ), rbac1.getResourceId( ) );
74          assertEquals( rbacStored.getPermissionKey( ), rbac1.getPermissionKey( ) );
75  
76          // Update test
77          rbac1.setRoleKey( ROLEKEY2 );
78          rbac1.setResourceTypeKey( RESOURCETYPE2 );
79          rbac1.setResourceId( RESOURCEID2 );
80          rbac1.setPermissionKey( PERMISSION2 );
81  
82          RBACHome.update( rbac1 );
83          rbacStored = RBACHome.findByPrimaryKey( rbac1.getRBACId( ) );
84          assertEquals( rbacStored.getRoleKey( ), rbac1.getRoleKey( ) );
85          assertEquals( rbacStored.getResourceTypeKey( ), rbac1.getResourceTypeKey( ) );
86          assertEquals( rbacStored.getResourceId( ), rbac1.getResourceId( ) );
87          assertEquals( rbacStored.getPermissionKey( ), rbac1.getPermissionKey( ) );
88  
89          // Delete test
90          RBACHome.remove( rbac1.getRBACId( ) );
91          rbacStored = RBACHome.findByPrimaryKey( rbac1.getRBACId( ) );
92          assertNull( rbacStored );
93  
94          // add elements element for the lists (both rbac1 and rbac2 values are ...2 values at this point)
95          RBACHome.create( rbac1 );
96          RBACHome.create( rbac2 );
97  
98          // List Test
99          Collection list = RBACHome.findAll( );
100         assertTrue( list.size( ) > 0 );
101 
102         // list by role Test
103         Collection<RBAC> listByRole = RBACHome.findResourcesByCode( ROLEKEY2 );
104         assertTrue( listByRole.size( ) > 0 );
105 
106         for ( RBAC rbacListItem : listByRole )
107         {
108             assertEquals( ROLEKEY2, rbacListItem.getRoleKey( ) );
109         }
110 
111         // find roles from resource and permission Test
112         Collection<String> listOfRoles = RBACHome.findRoleKeys( RESOURCETYPE2, RESOURCEID2, PERMISSION2 );
113         assertTrue( list.size( ) > 0 );
114 
115         boolean bRoleFound = false;
116 
117         for ( String strRoleKey : listOfRoles )
118         {
119             if ( strRoleKey.equals( ROLEKEY2 ) )
120             {
121                 bRoleFound = true;
122             }
123         }
124 
125         assertTrue( bRoleFound );
126 
127         // update role keys for all Test
128         RBACHome.updateRoleKey( ROLEKEY2, ROLEKEY1 );
129 
130         Collection listByRoleUpdated = RBACHome.findResourcesByCode( ROLEKEY1 );
131         assertTrue( listByRoleUpdated.size( ) > 0 );
132 
133         // both rbac1 and rbac2 have ROLEKEY1 values at this point
134         RBACHome.removeForRoleKey( ROLEKEY1 );
135         rbacStored = RBACHome.findByPrimaryKey( rbac1.getRBACId( ) );
136         assertNull( rbacStored );
137         rbacStored = RBACHome.findByPrimaryKey( rbac2.getRBACId( ) );
138         assertNull( rbacStored );
139     }
140 }