Remove static AuthenticationManager instance 77/69577/4
authorTom Pantelis <tompantelis@gmail.com>
Sat, 17 Mar 2018 01:24:05 +0000 (21:24 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Mon, 19 Mar 2018 15:10:48 +0000 (11:10 -0400)
It's only used by UT's.

Change-Id: I25271cd06d578942b7cf9cd35a38a338c5527f29
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManager.java
aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/tokenauthrealm/auth/AuthenticationManagerTest.java

index 728e63c5dfe977506a444b1db6927f82e6cec638..5ba9573c04b2bb2a6a87f4fa7463ae904fc05757 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.aaa.shiro.tokenauthrealm.auth;
 
 import java.util.Dictionary;
-import java.util.Hashtable;
 import org.opendaylight.aaa.api.Authentication;
 import org.opendaylight.aaa.api.AuthenticationService;
 import org.osgi.service.cm.ConfigurationException;
@@ -23,25 +22,15 @@ public class AuthenticationManager implements AuthenticationService, ManagedServ
     private static final String AUTH_ENABLED_ERR = "Error setting authEnabled";
 
     protected static final String AUTH_ENABLED = "authEnabled";
-    protected static final Dictionary<String, String> DEFAULTS = new Hashtable<>();
-
-    static {
-        DEFAULTS.put(AUTH_ENABLED, Boolean.FALSE.toString());
-    }
 
     // In non-Karaf environments, authEnabled is set to false by default
-    private static volatile boolean authEnabled = false;
+    private volatile boolean authEnabled = false;
 
-    private static final AuthenticationManager AUTHENTICATION_MANAGER = new AuthenticationManager();
     private final ThreadLocal<Authentication> auth = new InheritableThreadLocal<>();
 
     public AuthenticationManager() {
     }
 
-    static AuthenticationManager instance() {
-        return AUTHENTICATION_MANAGER;
-    }
-
     @Override
     public Authentication get() {
         return auth.get();
@@ -59,7 +48,7 @@ public class AuthenticationManager implements AuthenticationService, ManagedServ
 
     @Override
     public boolean isAuthEnabled() {
-        return this.authEnabled;
+        return authEnabled;
     }
 
     @Override
index fae42b697fc34d815ee4917c79f3f64c3a3e719c..c202c17e5331c2d220b83a5e52df00bf0edd283b 100644 (file)
@@ -24,37 +24,36 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import org.junit.Test;
 import org.opendaylight.aaa.api.Authentication;
-import org.opendaylight.aaa.api.AuthenticationService;
 import org.osgi.service.cm.ConfigurationException;
 
 public class AuthenticationManagerTest {
+    private final AuthenticationManager authManager = new AuthenticationManager();
+
     @Test
     public void testAuthenticationCrudSameThread() {
         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
                 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
-        AuthenticationService as = AuthenticationManager.instance();
 
-        assertNotNull(as);
+        assertNotNull(authManager);
 
-        as.set(auth);
-        assertEquals(auth, as.get());
+        authManager.set(auth);
+        assertEquals(auth, authManager.get());
 
-        as.clear();
-        assertNull(as.get());
+        authManager.clear();
+        assertNull(authManager.get());
     }
 
     @Test
     public void testAuthenticationCrudSpawnedThread() throws InterruptedException,
             ExecutionException {
-        AuthenticationService as = AuthenticationManager.instance();
         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
                 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
 
-        as.set(auth);
+        authManager.set(auth);
         Future<Authentication> future = Executors.newSingleThreadExecutor().submit(new Worker());
         assertEquals(auth, future.get());
 
-        as.clear();
+        authManager.clear();
         future = Executors.newSingleThreadExecutor().submit(new Worker());
         assertNull(future.get());
     }
@@ -62,18 +61,17 @@ public class AuthenticationManagerTest {
     @Test
     public void testAuthenticationCrudSpawnedThreadPool() throws InterruptedException,
             ExecutionException {
-        AuthenticationService as = AuthenticationManager.instance();
         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
                 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
 
-        as.set(auth);
+        authManager.set(auth);
         List<Future<Authentication>> fs = Executors.newFixedThreadPool(2).invokeAll(
                 Arrays.asList(new Worker(), new Worker()));
         for (Future<Authentication> f : fs) {
             assertEquals(auth, f.get());
         }
 
-        as.clear();
+        authManager.clear();
         fs = Executors.newFixedThreadPool(2).invokeAll(Arrays.asList(new Worker(), new Worker()));
         for (Future<Authentication> f : fs) {
             assertNull(f.get());
@@ -83,51 +81,46 @@ public class AuthenticationManagerTest {
     @Test
     public void testUpdatedValid() throws ConfigurationException {
         Dictionary<String, String> props = new Hashtable<>();
-        AuthenticationManager as = AuthenticationManager.instance();
 
-        assertFalse(as.isAuthEnabled());
+        assertFalse(authManager.isAuthEnabled());
 
         props.put(AuthenticationManager.AUTH_ENABLED, "TrUe");
-        as.updated(props);
-        assertTrue(as.isAuthEnabled());
+        authManager.updated(props);
+        assertTrue(authManager.isAuthEnabled());
 
         props.put(AuthenticationManager.AUTH_ENABLED, "FaLsE");
-        as.updated(props);
-        assertFalse(as.isAuthEnabled());
+        authManager.updated(props);
+        assertFalse(authManager.isAuthEnabled());
     }
 
     @Test
     public void testUpdatedNullProperty() throws ConfigurationException {
-        AuthenticationManager as = AuthenticationManager.instance();
 
-        assertFalse(as.isAuthEnabled());
-        as.updated(null);
-        assertFalse(as.isAuthEnabled());
+        assertFalse(authManager.isAuthEnabled());
+        authManager.updated(null);
+        assertFalse(authManager.isAuthEnabled());
     }
 
     @Test(expected = ConfigurationException.class)
     public void testUpdatedInvalidValue() throws ConfigurationException {
-        AuthenticationManager as = AuthenticationManager.instance();
         Dictionary<String, String> props = new Hashtable<>();
 
         props.put(AuthenticationManager.AUTH_ENABLED, "yes");
-        as.updated(props);
+        authManager.updated(props);
     }
 
     @Test(expected = ConfigurationException.class)
     public void testUpdatedInvalidKey() throws ConfigurationException {
-        AuthenticationManager as = AuthenticationManager.instance();
         Dictionary<String, String> props = new Hashtable<>();
 
         props.put("Invalid Key", "true");
-        as.updated(props);
+        authManager.updated(props);
     }
 
     private class Worker implements Callable<Authentication> {
         @Override
         public Authentication call() throws Exception {
-            AuthenticationService as = AuthenticationManager.instance();
-            return as.get();
+            return authManager.get();
         }
     }
 }