08456118c3bc2fe95604df3cc161749392c35110
[netconf.git] / netconf / aaa-authn-odl-plugin / src / test / java / org / opendaylight / aaa / odl / CredentialServiceAuthProviderTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.aaa.odl;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.doAnswer;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.mock;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.opendaylight.aaa.api.AuthenticationException;
27 import org.opendaylight.aaa.api.Claim;
28 import org.opendaylight.aaa.api.CredentialAuth;
29 import org.opendaylight.aaa.api.PasswordCredentials;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.Filter;
32 import org.osgi.framework.ServiceEvent;
33 import org.osgi.framework.ServiceListener;
34 import org.osgi.framework.ServiceReference;
35
36 public class CredentialServiceAuthProviderTest {
37
38     @Mock
39     private CredentialAuth<PasswordCredentials> credAuth;
40     @Mock
41     private BundleContext ctx;
42
43     @Before
44     public void setUp() throws Exception {
45         MockitoAnnotations.initMocks(this);
46         doReturn(mock(Filter.class)).when(ctx).createFilter(anyString());
47     }
48
49     @Test(expected = IllegalStateException.class)
50     public void testAuthenticatedNoDelegate() throws Exception {
51         CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
52         credentialServiceAuthProvider.authenticated("user", "pwd");
53     }
54
55     @Test
56     public void testAuthenticatedTrue() throws Exception {
57         ServiceReference serviceRef = mock(ServiceReference.class);
58
59         ServiceListenerAnswer answer = new ServiceListenerAnswer();
60         doAnswer(answer).when(ctx).addServiceListener(any(ServiceListener.class), anyString());
61
62         Claim claim = mock(Claim.class);
63         doReturn("domain").when(claim).domain();
64         doReturn(claim).when(credAuth).authenticate(any(PasswordCredentials.class));
65
66         doReturn(credAuth).when(ctx).getService(serviceRef);
67         CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
68
69         answer.serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
70         assertNotNull(answer.serviceListener);
71
72         assertTrue(credentialServiceAuthProvider.authenticated("user", "pwd"));
73     }
74
75     @Test
76     public void testAuthenticatedFalse() throws Exception {
77         ServiceReference serviceRef = mock(ServiceReference.class);
78
79         ServiceListenerAnswer answer = new ServiceListenerAnswer();
80         doAnswer(answer).when(ctx).addServiceListener(any(ServiceListener.class), anyString());
81
82         doThrow(AuthenticationException.class).when(credAuth).authenticate(any(PasswordCredentials.class));
83
84         doReturn(credAuth).when(ctx).getService(serviceRef);
85         CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
86
87         answer.serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
88         assertNotNull(answer.serviceListener);
89
90         assertFalse(credentialServiceAuthProvider.authenticated("user", "pwd"));
91     }
92
93     private static class ServiceListenerAnswer implements Answer {
94
95         ServiceListener serviceListener;
96
97         @Override
98         public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
99             serviceListener = (ServiceListener) invocationOnMock.getArguments()[0];
100             return null;
101         }
102     }
103 }