4a379cdd2fa2b1d470110e8fd13d0ca128ffae9e
[controller.git] / opendaylight / usermanager / src / test / java / org / opendaylight / controller / usermanager / internal / AuthenticatedUserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.usermanager.internal;
10
11 import java.util.Arrays;
12 import java.util.List;
13
14 import org.junit.Assert;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17
18 import org.opendaylight.controller.sal.authorization.UserLevel;
19 import org.springframework.security.core.GrantedAuthority;
20
21 public class AuthenticatedUserTest {
22
23         static String[] roleArray;
24         static AuthenticatedUser user;
25
26         @BeforeClass
27         public static void testSetup() {
28                 roleArray = new String[] { UserLevel.NETWORKOPERATOR.toString(),
29                                 UserLevel.APPUSER.toString() };
30         }
31
32         @Test
33         public void testAuthenticatedUser() {
34                 user = new AuthenticatedUser("auser");
35
36                 Assert.assertFalse(user.getAccessDate().isEmpty());
37                 Assert.assertNull(user.getUserRoles());
38
39         }
40
41         @Test
42         public void testSetUserRoleList() {
43                 List<String> retrievedRoleList = null;
44                 List<String> roleList = Arrays.asList(roleArray);
45
46                 // list arg
47                 user = new AuthenticatedUser("auser");
48                 user.setRoleList(roleList);
49                 retrievedRoleList = user.getUserRoles();
50                 Assert.assertTrue(roleList.equals(retrievedRoleList));
51
52                 // array arg
53                 user = new AuthenticatedUser("auser");
54                 user.setRoleList(roleArray);
55                 retrievedRoleList = user.getUserRoles();
56                 for (int i = 0; i < roleArray.length; i++)
57                         Assert.assertTrue(roleArray[i].equals(retrievedRoleList.get(i)));
58
59                 // test addUserRole
60                 user.addUserRole("AnotherRole");
61                 Assert.assertTrue(user.getUserRoles().lastIndexOf("AnotherRole") != -1);
62
63         }
64
65         @Test
66         public void testGetGrantedAuthorities() {
67                 List<GrantedAuthority> gaList = user
68                                 .getGrantedAuthorities(UserLevel.NETWORKOPERATOR);
69                 Assert.assertTrue(gaList.get(0).getAuthority()
70                                 .equals("ROLE_NETWORK-OPERATOR"));
71         }
72
73 }