Split out DefaultPasswordCredentials
[netconf.git] / plugins / netconf-auth-aaa / src / test / java / org / opendaylight / netconf / authprovider / 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.netconf.authprovider;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.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.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.junit.MockitoJUnitRunner;
21 import org.opendaylight.aaa.api.AuthenticationException;
22 import org.opendaylight.aaa.api.Claim;
23 import org.opendaylight.aaa.api.PasswordCredentialAuth;
24 import org.opendaylight.aaa.api.PasswordCredentials;
25 import org.opendaylight.netconf.auth.aaa.CredentialServiceAuthProvider;
26
27 @RunWith(MockitoJUnitRunner.StrictStubs.class)
28 public class CredentialServiceAuthProviderTest {
29     @Mock
30     private PasswordCredentialAuth credAuth;
31
32     @Test
33     public void testAuthenticatedTrue() throws Exception {
34         Claim claim = mock(Claim.class);
35         doReturn("domain").when(claim).domain();
36         doReturn(claim).when(credAuth).authenticate(any(PasswordCredentials.class));
37
38         CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(credAuth);
39         assertTrue(credentialServiceAuthProvider.authenticated("user", "pwd"));
40     }
41
42     @Test
43     public void testAuthenticatedFalse() throws Exception {
44         doThrow(AuthenticationException.class).when(credAuth).authenticate(any(PasswordCredentials.class));
45         CredentialServiceAuthProvider credentialServiceAuthProvider = new CredentialServiceAuthProvider(credAuth);
46         assertFalse(credentialServiceAuthProvider.authenticated("user", "pwd"));
47     }
48 }