2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.aaa.odl;
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;
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;
37 public class CredentialServiceAuthProviderTest {
40 private CredentialAuth<PasswordCredentials> credAuth;
42 private BundleContext ctx;
45 public void setUp() throws Exception {
46 MockitoAnnotations.initMocks(this);
47 doReturn(mock(Filter.class)).when(ctx).createFilter(anyString());
50 @Test(expected = IllegalStateException.class)
51 public void testAuthenticatedNoDelegate() throws Exception {
52 CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
53 credentialServiceAuthProvider.authenticated("user", "pwd");
57 public void testAuthenticatedTrue() throws Exception {
58 ServiceReference serviceRef = mock(ServiceReference.class);
60 ServiceListenerAnswer answer = new ServiceListenerAnswer();
61 doAnswer(answer).when(ctx).addServiceListener(any(ServiceListener.class), anyString());
63 Claim claim = mock(Claim.class);
64 doReturn("domain").when(claim).domain();
65 doReturn(claim).when(credAuth).authenticate(any(PasswordCredentials.class));
67 doReturn(credAuth).when(ctx).getService(serviceRef);
68 CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
70 answer.serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
71 assertNotNull(answer.serviceListener);
73 assertTrue(credentialServiceAuthProvider.authenticated("user", "pwd"));
77 public void testAuthenticatedFalse() throws Exception {
78 ServiceReference serviceRef = mock(ServiceReference.class);
80 ServiceListenerAnswer answer = new ServiceListenerAnswer();
81 doAnswer(answer).when(ctx).addServiceListener(any(ServiceListener.class), anyString());
83 doThrow(AuthenticationException.class).when(credAuth).authenticate(any(PasswordCredentials.class));
85 doReturn(credAuth).when(ctx).getService(serviceRef);
86 CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
88 answer.serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
89 assertNotNull(answer.serviceListener);
91 assertFalse(credentialServiceAuthProvider.authenticated("user", "pwd"));
94 private static class ServiceListenerAnswer implements Answer {
96 ServiceListener serviceListener;
99 public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
100 serviceListener = (ServiceListener) invocationOnMock.getArguments()[0];