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