Directly inject CredentialAuth dependency
[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.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;
16
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;
29
30 public class CredentialServiceAuthProviderTest {
31
32     @Mock
33     private CredentialAuth<PasswordCredentials> credAuth;
34
35     @Before
36     public void setUp() throws Exception {
37         MockitoAnnotations.initMocks(this);
38     }
39
40
41     @Test
42     public void testAuthenticatedTrue() throws Exception {
43         ServiceReference serviceRef = mock(ServiceReference.class);
44
45         ServiceListenerAnswer answer = new ServiceListenerAnswer();
46
47         Claim claim = mock(Claim.class);
48         doReturn("domain").when(claim).domain();
49         doReturn(claim).when(credAuth).authenticate(any(PasswordCredentials.class));
50
51         CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(credAuth);
52         assertTrue(credentialServiceAuthProvider.authenticated("user", "pwd"));
53     }
54
55     @Test
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"));
60     }
61
62     private static class ServiceListenerAnswer implements Answer {
63
64         ServiceListener serviceListener;
65
66         @Override
67         public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
68             serviceListener = (ServiceListener) invocationOnMock.getArguments()[0];
69             return null;
70         }
71     }
72 }