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.netconf.authprovider;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.doThrow;
15 import static org.mockito.Mockito.mock;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.mockito.invocation.InvocationOnMock;
22 import org.mockito.stubbing.Answer;
23 import org.opendaylight.aaa.api.AuthenticationException;
24 import org.opendaylight.aaa.api.Claim;
25 import org.opendaylight.aaa.api.CredentialAuth;
26 import org.opendaylight.aaa.api.PasswordCredentials;
27 import org.osgi.framework.ServiceListener;
28 import org.osgi.framework.ServiceReference;
30 public class CredentialServiceAuthProviderTest {
33 private CredentialAuth<PasswordCredentials> credAuth;
36 public void setUp() throws Exception {
37 MockitoAnnotations.initMocks(this);
42 public void testAuthenticatedTrue() throws Exception {
43 ServiceReference serviceRef = mock(ServiceReference.class);
45 ServiceListenerAnswer answer = new ServiceListenerAnswer();
47 Claim claim = mock(Claim.class);
48 doReturn("domain").when(claim).domain();
49 doReturn(claim).when(credAuth).authenticate(any(PasswordCredentials.class));
51 CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(credAuth);
52 assertTrue(credentialServiceAuthProvider.authenticated("user", "pwd"));
56 public void testAuthenticatedFalse() throws Exception {
57 doThrow(AuthenticationException.class).when(credAuth).authenticate(any(PasswordCredentials.class));
58 CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(credAuth);
59 assertFalse(credentialServiceAuthProvider.authenticated("user", "pwd"));
62 private static class ServiceListenerAnswer implements Answer {
64 ServiceListener serviceListener;
67 public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
68 serviceListener = (ServiceListener) invocationOnMock.getArguments()[0];