2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.usermanager.internal;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.ConcurrentHashMap;
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;
30 * Unit Tests for UserManagerImpl
32 public class UserManagerImplTest {
34 private static UserManagerImpl um;
37 * @throws java.lang.Exception
40 public static void setUpBeforeClass() throws Exception {
42 IUserManager userManager = (IUserManager) ServiceHelper
43 .getGlobalInstance(IUserManager.class, new Object());
44 if (userManager instanceof UserManagerImpl) {
45 um = (UserManagerImpl) userManager;
47 um = new UserManagerImpl();
48 um.setAuthProviders(new ConcurrentHashMap<String, IAAAProvider>());
50 // mock up a remote server list with a dummy server
51 um.setRemoteServerConfigList(new ConcurrentHashMap<String, ServerConfig>() {
52 static final long serialVersionUID = 1L;
54 put("dummyServerConfig", new ServerConfig() {
55 // Server config can't be empty
56 static final long serialVersionUID = 8645L;
58 public String getAddress() {
62 public String getSecret() {
66 public String getProtocol() {
73 // mock up a localUserConfigList with an admin user
74 um.setLocalUserConfigList(new ConcurrentHashMap<String, UserConfig>() {
75 static final long serialVersionUID = 2L;
77 List<String> roles = new ArrayList<String>(1);
78 roles.add(UserLevel.SYSTEMADMIN.toString());
79 put("admin", new UserConfig("admin",
80 "7029,7455,8165,7029,7881", roles));
83 // instantiate an empty activeUser collection
84 um.setActiveUsers(new ConcurrentHashMap<String, AuthenticatedUser>());
92 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
96 public void testAddAAAProvider() {
97 // instantiate an anonymous AAAProvider
98 IAAAProvider a3p = new IAAAProvider() {
100 public AuthResponse authService(String userName, String password,
101 String server, String secretKey) {
102 return new AuthResponse();
105 public String getName() {
106 return "dummyAAAProvider";
110 um.addAAAProvider(a3p);
111 assertEquals(a3p, um.getAAAProvider("dummyAAAProvider"));
117 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#removeAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
119 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#getAAAProvider(java.lang.String)}
123 public void testRemoveAAAProvider() {
124 um.removeAAAProvider(um.getAAAProvider("dummyAAAProvider"));
125 assertTrue(um.getAAAProviderNames().isEmpty());
130 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(java.lang.String, java.lang.String)}
134 public void testAuthenticateStringString() {
135 List<String> roles = new ArrayList<String>(1);
136 roles.add(UserLevel.SYSTEMADMIN.toString());
137 UserConfig uc = new UserConfig("administrator", "admin", roles);
139 AuthResultEnum authResult = um.authenticate("administrator", "admin");
140 assertEquals(authResult, AuthResultEnum.AUTH_ACCEPT_LOC);
145 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addRemoveLocalUser(org.opendaylight.controller.usermanager.internal.UserConfig, boolean)}
149 public void testAddRemoveLocalUser() {
150 List<String> roles = new ArrayList<String>(1);
151 roles.add(UserLevel.SYSTEMADMIN.toString());
152 UserConfig uc = new UserConfig("sysadmin", "7029,7455,8165,7029,7881",
155 assertTrue(um.getLocalUserList().contains(uc));
156 um.removeLocalUser(uc);
157 assertFalse(um.getLocalUserList().contains(uc));
162 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#changeLocalUserPassword(java.lang.String, java.lang.String, java.lang.String)}
166 public void testChangeLocalUserPassword() {
167 // fail("Not yet implemented");
172 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userLogout(java.lang.String)}
176 public void testUserLogout() {
177 // fail("Not yet implemented");
182 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userTimedOut(java.lang.String)}
186 public void testUserTimedOut() {
187 // fail("Not yet implemented");
192 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(org.springframework.security.core.Authentication)}
196 public void testAuthenticateAuthentication() {
197 // fail("Not yet implemented");
202 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveLocalUserList()}
206 public void testSaveLocalUserList() {
207 // fail("Not yet implemented");
212 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAAAServerList()}
216 public void testSaveAAAServerList() {
217 // fail("Not yet implemented");
222 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAuthorizationList()}
226 public void testSaveAuthorizationList() {
227 // fail("Not yet implemented");
232 * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#readObject(java.io.ObjectInputStream)}
236 public void testReadObject() {
237 // fail("Not yet implemented");
241 public void testGetUserLevel() {
242 List<String> roles = new ArrayList<String>(2);
243 roles.add(UserLevel.SYSTEMADMIN.toString());
244 roles.add("App1_supervisor");
245 um.addLocalUser(new UserConfig("Jack", "password", roles));
246 um.authenticate("Jack", "password");
249 roles.add("App2Admin");
250 roles.add(UserLevel.NETWORKOPERATOR.toString());
251 um.addLocalUser(new UserConfig("John", "password", roles));
253 // Run the check on authenticated user
254 Assert.assertTrue(um.getUserLevel("Jack") == UserLevel.SYSTEMADMIN);
255 // Run the check on configured users
256 Assert.assertTrue(um.getUserLevel("John") == UserLevel.NETWORKOPERATOR);
257 Assert.assertTrue(um.getUserLevel("Andrew") == UserLevel.NOUSER);