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
8 package org.opendaylight.aaa.odl;
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;
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;
36 public class CredentialServiceAuthProviderTest {
39 private CredentialAuth<PasswordCredentials> credAuth;
41 private BundleContext ctx;
44 public void setUp() throws Exception {
45 MockitoAnnotations.initMocks(this);
46 doReturn(mock(Filter.class)).when(ctx).createFilter(anyString());
49 @Test(expected = IllegalStateException.class)
50 public void testAuthenticatedNoDelegate() throws Exception {
51 CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
52 credentialServiceAuthProvider.authenticated("user", "pwd");
56 public void testAuthenticatedTrue() throws Exception {
57 ServiceReference serviceRef = mock(ServiceReference.class);
59 ServiceListenerAnswer answer = new ServiceListenerAnswer();
60 doAnswer(answer).when(ctx).addServiceListener(any(ServiceListener.class), anyString());
62 Claim claim = mock(Claim.class);
63 doReturn("domain").when(claim).domain();
64 doReturn(claim).when(credAuth).authenticate(any(PasswordCredentials.class));
66 doReturn(credAuth).when(ctx).getService(serviceRef);
67 CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
69 answer.serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
70 assertNotNull(answer.serviceListener);
72 assertTrue(credentialServiceAuthProvider.authenticated("user", "pwd"));
76 public void testAuthenticatedFalse() throws Exception {
77 ServiceReference serviceRef = mock(ServiceReference.class);
79 ServiceListenerAnswer answer = new ServiceListenerAnswer();
80 doAnswer(answer).when(ctx).addServiceListener(any(ServiceListener.class), anyString());
82 doThrow(AuthenticationException.class).when(credAuth).authenticate(any(PasswordCredentials.class));
84 doReturn(credAuth).when(ctx).getService(serviceRef);
85 CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(ctx);
87 answer.serviceListener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, serviceRef));
88 assertNotNull(answer.serviceListener);
90 assertFalse(credentialServiceAuthProvider.authenticated("user", "pwd"));
93 private static class ServiceListenerAnswer implements Answer {
95 ServiceListener serviceListener;
98 public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
99 serviceListener = (ServiceListener) invocationOnMock.getArguments()[0];