ec7b13603943cd348b3393a94a66749fdc608cba
[controller.git] / opendaylight / usermanager / src / test / java / org / opendaylight / controller / usermanager / internal / UserManagerImplTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.usermanager.internal;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
15
16 import java.util.Date;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import org.junit.Assert;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
23 import org.opendaylight.controller.sal.authorization.UserLevel;
24 import org.opendaylight.controller.sal.utils.ServiceHelper;
25 import org.opendaylight.controller.usermanager.AuthResponse;
26 import org.opendaylight.controller.usermanager.IAAAProvider;
27 import org.opendaylight.controller.usermanager.IUserManager;
28
29 /**
30  * Unit Tests for UserManagerImpl
31  */
32 public class UserManagerImplTest {
33
34         private static UserManagerImpl um;
35
36         /**
37          * @throws java.lang.Exception
38          */
39         @BeforeClass
40         public static void setUpBeforeClass() throws Exception {
41
42                 IUserManager userManager = (IUserManager) ServiceHelper
43                                 .getGlobalInstance(IUserManager.class, new Object());
44                 if (userManager instanceof UserManagerImpl) {
45                         um = (UserManagerImpl) userManager;
46                 } else {
47                         um = new UserManagerImpl();
48                         um.setAuthProviders(new ConcurrentHashMap<String, IAAAProvider>());
49
50                         // mock up a remote server list with a dummy server
51                         um.setRemoteServerConfigList(new ConcurrentHashMap<String, ServerConfig>() {
52                                 static final long serialVersionUID = 1L;
53                                 {
54                                         put("dummyServerConfig", new ServerConfig() { // Server config can't be empty
55                                                                 static final long serialVersionUID = 8645L;
56
57                                                                 public String getAddress() {
58                                                                         return "1.1.1.1";
59                                                                 }
60
61                                                                 public String getSecret() {
62                                                                         return "secret";
63                                                                 }
64
65                                                                 public String getProtocol() {
66                                                                         return "IPv4";
67                                                                 }
68                                                         });
69                                 }
70                         });
71
72                         // mock up a localUserConfigList with an admin user
73                         um.setLocalUserConfigList(new ConcurrentHashMap<String, UserConfig>() {
74                                 static final long serialVersionUID = 2L;
75                                 {
76                                         put("admin", new UserConfig("admin", "7029,7455,8165,7029,7881",
77                                                                         UserLevel.SYSTEMADMIN.toString()));
78                                 }
79                         });
80                         // instantiate an empty activeUser collection
81                         um.setActiveUsers(new ConcurrentHashMap<String, AuthenticatedUser>());
82
83                 }
84
85         }
86
87         /**
88          * Test method for
89          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
90          * .
91          */
92         @Test
93         public void testAddAAAProvider() {
94                 // instantiate an anonymous AAAProvider
95                 IAAAProvider a3p = new IAAAProvider() {
96
97                         public AuthResponse authService(String userName, String password,
98                                         String server, String secretKey) {
99                                 return new AuthResponse();
100                         };
101
102                         public String getName() {
103                                 return "dummyAAAProvider";
104                         }
105                 };
106
107                 um.addAAAProvider(a3p);
108                 assertEquals(a3p, um.getAAAProvider("dummyAAAProvider"));
109
110         }
111
112         /**
113          * Test method for
114          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#removeAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
115          * and for for
116          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#getAAAProvider(java.lang.String)}
117          * .
118          */
119         @Test
120         public void testRemoveAAAProvider() {
121                 um.removeAAAProvider(um.getAAAProvider("dummyAAAProvider"));
122                 assertTrue(um.getAAAProviderNames().isEmpty());
123         }
124
125         /**
126          * Test method for
127          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(java.lang.String, java.lang.String)}
128          * .
129          */
130         @Test
131         public void testAuthenticateStringString() {
132                 UserConfig uc = new UserConfig("administrator", "admin",
133                                 UserLevel.SYSTEMADMIN.toString());
134                 um.addLocalUser(uc);
135                 AuthResultEnum authResult = um.authenticate("administrator", "admin");
136                 assertEquals(authResult, AuthResultEnum.AUTH_ACCEPT_LOC);
137         }
138
139         /**
140          * Test method for
141          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addRemoveLocalUser(org.opendaylight.controller.usermanager.internal.UserConfig, boolean)}
142          * .
143          */
144         @Test
145         public void testAddRemoveLocalUser() {
146                 UserConfig uc = new UserConfig("sysadmin", "7029,7455,8165,7029,7881",
147                                 UserLevel.SYSTEMADMIN.toString());
148                 um.addLocalUser(uc);
149                 assertTrue(um.getLocalUserList().contains(uc));
150                 um.removeLocalUser(uc);
151                 assertFalse(um.getLocalUserList().contains(uc));
152         }
153
154         /**
155          * Test method for
156          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#changeLocalUserPassword(java.lang.String, java.lang.String, java.lang.String)}
157          * .
158          */
159         @Test
160         public void testChangeLocalUserPassword() {
161                 // fail("Not yet implemented");
162         }
163
164         /**
165          * Test method for
166          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userLogout(java.lang.String)}
167          * .
168          */
169         @Test
170         public void testUserLogout() {
171                 // fail("Not yet implemented");
172         }
173
174         /**
175          * Test method for
176          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userTimedOut(java.lang.String)}
177          * .
178          */
179         @Test
180         public void testUserTimedOut() {
181                 // fail("Not yet implemented");
182         }
183
184         /**
185          * Test method for
186          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(org.springframework.security.core.Authentication)}
187          * .
188          */
189         @Test
190         public void testAuthenticateAuthentication() {
191                 // fail("Not yet implemented");
192         }
193
194         /**
195          * Test method for
196          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveLocalUserList()}
197          * .
198          */
199         @Test
200         public void testSaveLocalUserList() {
201                 // fail("Not yet implemented");
202         }
203
204         /**
205          * Test method for
206          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAAAServerList()}
207          * .
208          */
209         @Test
210         public void testSaveAAAServerList() {
211                 // fail("Not yet implemented");
212         }
213
214         /**
215          * Test method for
216          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAuthorizationList()}
217          * .
218          */
219         @Test
220         public void testSaveAuthorizationList() {
221                 // fail("Not yet implemented");
222         }
223
224         /**
225          * Test method for
226          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#readObject(java.io.ObjectInputStream)}
227          * .
228          */
229         @Test
230         public void testReadObject() {
231                 // fail("Not yet implemented");
232         }
233         
234         @Test
235         public void testGetUserLevel() {
236                 um.addLocalUser(new UserConfig("Jack", "password",
237                                 UserLevel.SYSTEMADMIN.toString()));
238                 um.authenticate("Jack", "password");
239                 
240                 um.addLocalUser(new UserConfig("John", "password",
241                                 UserLevel.NETWORKOPERATOR.toString()));
242                 // Run the check on authenticated user
243                 Assert.assertTrue(um.getUserLevel("Jack") == UserLevel.SYSTEMADMIN);
244                 // Run the check on configured users
245                 Assert.assertTrue(um.getUserLevel("John") == UserLevel.NETWORKOPERATOR);
246                 Assert.assertTrue(um.getUserLevel("Andrew") == UserLevel.NOUSER);
247         }
248 }