Directly inject CredentialAuth dependency
[netconf.git] / netconf / aaa-authn-odl-plugin / src / test / java / org / opendaylight / aaa / odl / CredentialServiceAuthProviderTest.java
index 08456118c3bc2fe95604df3cc161749392c35110..ea7e1e93fc2f29cbe177d65d13265c2ec89fcd93 100644 (file)
@@ -8,11 +8,8 @@
 package org.opendaylight.aaa.odl;
 
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
@@ -27,9 +24,6 @@ import org.opendaylight.aaa.api.AuthenticationException;
 import org.opendaylight.aaa.api.Claim;
 import org.opendaylight.aaa.api.CredentialAuth;
 import org.opendaylight.aaa.api.PasswordCredentials;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Filter;
-import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
 import org.osgi.framework.ServiceReference;
 
@@ -37,56 +31,31 @@ public class CredentialServiceAuthProviderTest {
 
     @Mock
     private CredentialAuth<PasswordCredentials> credAuth;
-    @Mock
-    private BundleContext ctx;
 
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        doReturn(mock(Filter.class)).when(ctx).createFilter(anyString());
     }
 
-    @Test(expected = IllegalStateException.class)
-    public void testAuthenticatedNoDelegate() throws Exception {
-        CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
-        credentialServiceAuthProvider.authenticated("user", "pwd");
-    }
 
     @Test
     public void testAuthenticatedTrue() throws Exception {
         ServiceReference serviceRef = mock(ServiceReference.class);
 
         ServiceListenerAnswer answer = new ServiceListenerAnswer();
-        doAnswer(answer).when(ctx).addServiceListener(any(ServiceListener.class), anyString());
 
         Claim claim = mock(Claim.class);
         doReturn("domain").when(claim).domain();
         doReturn(claim).when(credAuth).authenticate(any(PasswordCredentials.class));
 
-        doReturn(credAuth).when(ctx).getService(serviceRef);
-        CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
-
-        answer.serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
-        assertNotNull(answer.serviceListener);
-
+        CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(credAuth);
         assertTrue(credentialServiceAuthProvider.authenticated("user", "pwd"));
     }
 
     @Test
     public void testAuthenticatedFalse() throws Exception {
-        ServiceReference serviceRef = mock(ServiceReference.class);
-
-        ServiceListenerAnswer answer = new ServiceListenerAnswer();
-        doAnswer(answer).when(ctx).addServiceListener(any(ServiceListener.class), anyString());
-
         doThrow(AuthenticationException.class).when(credAuth).authenticate(any(PasswordCredentials.class));
-
-        doReturn(credAuth).when(ctx).getService(serviceRef);
-        CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
-
-        answer.serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
-        assertNotNull(answer.serviceListener);
-
+        CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(credAuth);
         assertFalse(credentialServiceAuthProvider.authenticated("user", "pwd"));
     }