Generation of Notifications and RPCs from YANG
[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         @Test
41         public void testSetUserRoleList() {
42                 List<String> retrievedRoleList = null;
43                 List<String> roleList = Arrays.asList(roleArray);
44
45                 // list arg
46                 user = new AuthenticatedUser("auser");
47                 user.setRoleList(roleList);
48                 retrievedRoleList = user.getUserRoles();
49                 Assert.assertTrue(roleList.equals(retrievedRoleList));
50
51                 // array arg
52                 user = new AuthenticatedUser("auser");
53                 user.setRoleList(roleArray);
54                 retrievedRoleList = user.getUserRoles();
55                 for (int i = 0; i < roleArray.length; i++)
56                         Assert.assertTrue(roleArray[i].equals(retrievedRoleList.get(i)));
57
58                 // test addUserRole
59                 user.addUserRole("AnotherRole");
60                 Assert.assertTrue(user.getUserRoles().lastIndexOf("AnotherRole") != -1);
61
62         }
63
64         @Test
65         public void testGetGrantedAuthorities() {
66                 List<GrantedAuthority> gaList = user
67                                 .getGrantedAuthorities(UserLevel.NETWORKOPERATOR);
68                 Assert.assertTrue(gaList.get(0).getAuthority()
69                                 .equals("ROLE_NETWORK-OPERATOR"));
70         }
71
72 }