OpenDaylight Controller functional modules.
[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.concurrent.ConcurrentHashMap;
17
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
21 import org.opendaylight.controller.sal.authorization.UserLevel;
22 import org.opendaylight.controller.sal.utils.ServiceHelper;
23 import org.opendaylight.controller.usermanager.AuthResponse;
24 import org.opendaylight.controller.usermanager.IAAAProvider;
25 import org.opendaylight.controller.usermanager.IUserManager;
26
27 /**
28  * Unit Tests for UserManagerImpl
29  */
30 public class UserManagerImplTest {
31
32         private static UserManagerImpl um;
33
34         /**
35          * @throws java.lang.Exception
36          */
37         @BeforeClass
38         public static void setUpBeforeClass() throws Exception {
39
40                 IUserManager userManager = (IUserManager) ServiceHelper
41                                 .getGlobalInstance(IUserManager.class, new Object());
42                 if (userManager instanceof UserManagerImpl) {
43                         um = (UserManagerImpl) userManager;
44                 } else {
45                         um = new UserManagerImpl();
46                         um.setAuthProviders(new ConcurrentHashMap<String, IAAAProvider>());
47
48                         // mock up a remote server list with a dummy server
49                         um.setRemoteServerConfigList(new ConcurrentHashMap<String, ServerConfig>() {
50                                 static final long serialVersionUID = 1L;
51                                 {
52                                         put("dummyServerConfig", new ServerConfig() { // Server config can't be empty
53                                                                 static final long serialVersionUID = 8645L;
54
55                                                                 public String getAddress() {
56                                                                         return "1.1.1.1";
57                                                                 }
58
59                                                                 public String getSecret() {
60                                                                         return "secret";
61                                                                 }
62
63                                                                 public String getProtocol() {
64                                                                         return "IPv4";
65                                                                 }
66                                                         });
67                                 }
68                         });
69
70                         // mock up a localUserConfigList with an admin user
71                         um.setLocalUserConfigList(new ConcurrentHashMap<String, UserConfig>() {
72                                 static final long serialVersionUID = 2L;
73                                 {
74                                         put("admin", new UserConfig("admin", "7029,7455,8165,7029,7881",
75                                                                         UserLevel.SYSTEMADMIN.toString()));
76                                 }
77                         });
78                         // instantiate an empty activeUser collection
79                         um.setActiveUsers(new ConcurrentHashMap<String, AuthenticatedUser>());
80
81                 }
82
83         }
84
85         /**
86          * Test method for
87          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
88          * .
89          */
90         @Test
91         public void testAddAAAProvider() {
92                 // instantiate an anonymous AAAProvider
93                 IAAAProvider a3p = new IAAAProvider() {
94
95                         public AuthResponse authService(String userName, String password,
96                                         String server, String secretKey) {
97                                 return new AuthResponse();
98                         };
99
100                         public String getName() {
101                                 return "dummyAAAProvider";
102                         }
103                 };
104
105                 um.addAAAProvider(a3p);
106                 assertEquals(a3p, um.getAAAProvider("dummyAAAProvider"));
107
108         }
109
110         /**
111          * Test method for
112          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#removeAAAProvider(org.opendaylight.controller.usermanager.IAAAProvider)}
113          * and for for
114          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#getAAAProvider(java.lang.String)}
115          * .
116          */
117         @Test
118         public void testRemoveAAAProvider() {
119                 um.removeAAAProvider(um.getAAAProvider("dummyAAAProvider"));
120                 assertTrue(um.getAAAProviderNames().isEmpty());
121         }
122
123         /**
124          * Test method for
125          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(java.lang.String, java.lang.String)}
126          * .
127          */
128         @Test
129         public void testAuthenticateStringString() {
130                 UserConfig uc = new UserConfig("administrator", "admin",
131                                 UserLevel.SYSTEMADMIN.toString());
132                 um.addLocalUser(uc);
133                 AuthResultEnum authResult = um.authenticate("administrator", "admin");
134                 assertEquals(authResult, AuthResultEnum.AUTH_ACCEPT_LOC);
135         }
136
137         /**
138          * Test method for
139          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#addRemoveLocalUser(org.opendaylight.controller.usermanager.internal.UserConfig, boolean)}
140          * .
141          */
142         @Test
143         public void testAddRemoveLocalUser() {
144                 UserConfig uc = new UserConfig("sysadmin", "7029,7455,8165,7029,7881",
145                                 UserLevel.SYSTEMADMIN.toString());
146                 um.addLocalUser(uc);
147                 assertTrue(um.getLocalUserList().contains(uc));
148                 um.removeLocalUser(uc);
149                 assertFalse(um.getLocalUserList().contains(uc));
150         }
151
152         /**
153          * Test method for
154          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#changeLocalUserPassword(java.lang.String, java.lang.String, java.lang.String)}
155          * .
156          */
157         @Test
158         public void testChangeLocalUserPassword() {
159                 // fail("Not yet implemented");
160         }
161
162         /**
163          * Test method for
164          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userLogout(java.lang.String)}
165          * .
166          */
167         @Test
168         public void testUserLogout() {
169                 // fail("Not yet implemented");
170         }
171
172         /**
173          * Test method for
174          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#userTimedOut(java.lang.String)}
175          * .
176          */
177         @Test
178         public void testUserTimedOut() {
179                 // fail("Not yet implemented");
180         }
181
182         /**
183          * Test method for
184          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#authenticate(org.springframework.security.core.Authentication)}
185          * .
186          */
187         @Test
188         public void testAuthenticateAuthentication() {
189                 // fail("Not yet implemented");
190         }
191
192         /**
193          * Test method for
194          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveLocalUserList()}
195          * .
196          */
197         @Test
198         public void testSaveLocalUserList() {
199                 // fail("Not yet implemented");
200         }
201
202         /**
203          * Test method for
204          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAAAServerList()}
205          * .
206          */
207         @Test
208         public void testSaveAAAServerList() {
209                 // fail("Not yet implemented");
210         }
211
212         /**
213          * Test method for
214          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#saveAuthorizationList()}
215          * .
216          */
217         @Test
218         public void testSaveAuthorizationList() {
219                 // fail("Not yet implemented");
220         }
221
222         /**
223          * Test method for
224          * {@link org.opendaylight.controller.usermanager.internal.UserManagerImpl#readObject(java.io.ObjectInputStream)}
225          * .
226          */
227         @Test
228         public void testReadObject() {
229                 // fail("Not yet implemented");
230         }
231 }