Fix checkstyle issues to enforce it
[aaa.git] / aaa-shiro / impl / src / test / java / org / opendaylight / aaa / shiro / filters / AuthenticationTokenUtilsTest.java
1 /*
2  * Copyright (c) 2016 - 2017 Brocade Communications 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
9 package org.opendaylight.aaa.shiro.filters;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
15 import org.apache.shiro.authc.AuthenticationToken;
16 import org.apache.shiro.authc.UsernamePasswordToken;
17 import org.junit.Test;
18
19 /**
20  * Tests authentication token output utilities.
21  */
22 public class AuthenticationTokenUtilsTest {
23
24     /**
25      * A sample non-UsernamePasswordToken implementation for testing.
26      */
27     private final class NotUsernamePasswordToken implements AuthenticationToken {
28
29         @Override
30         public Object getPrincipal() {
31             return null;
32         }
33
34         @Override
35         public Object getCredentials() {
36             return null;
37         }
38     }
39
40     @Test
41     public void testIsUsernamePasswordToken() throws Exception {
42         // null test
43         final AuthenticationToken nullUsernamePasswordToken = null;
44         assertFalse(AuthenticationTokenUtils.isUsernamePasswordToken(nullUsernamePasswordToken));
45
46         // alternate implementation of AuthenticationToken
47         final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken();
48         assertFalse(AuthenticationTokenUtils.isUsernamePasswordToken(notUsernamePasswordToken));
49
50         // positive test case
51         final AuthenticationToken positiveUsernamePasswordToken = new UsernamePasswordToken();
52         assertTrue(AuthenticationTokenUtils.isUsernamePasswordToken(positiveUsernamePasswordToken));
53
54     }
55
56     @Test
57     public void testExtractUsername() throws Exception {
58         // null test
59         final AuthenticationToken nullAuthenticationToken = null;
60         assertEquals(AuthenticationTokenUtils.DEFAULT_TOKEN,
61                 AuthenticationTokenUtils.extractUsername(nullAuthenticationToken));
62
63         // non-UsernamePasswordToken test
64         final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken();
65         assertEquals(AuthenticationTokenUtils.DEFAULT_TOKEN,
66                 AuthenticationTokenUtils.extractUsername(notUsernamePasswordToken));
67
68         // null username test
69         final UsernamePasswordToken nullUsername = new UsernamePasswordToken();
70         nullUsername.setUsername(null);
71         assertEquals(AuthenticationTokenUtils.DEFAULT_USERNAME,
72                 AuthenticationTokenUtils.extractUsername(nullUsername));
73
74         // positive test
75         final UsernamePasswordToken positiveUsernamePasswordToken = new UsernamePasswordToken();
76         final String testUsername = "testUser1";
77         positiveUsernamePasswordToken.setUsername(testUsername);
78         assertEquals(testUsername, AuthenticationTokenUtils.extractUsername(positiveUsernamePasswordToken));
79     }
80
81     @Test
82     public void testExtractHostname() throws Exception {
83         // null test
84         final AuthenticationToken nullAuthenticationToken = null;
85         assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME,
86                 AuthenticationTokenUtils.extractHostname(nullAuthenticationToken));
87
88         // non-UsernamePasswordToken test
89         final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken();
90         assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME,
91                 AuthenticationTokenUtils.extractHostname(notUsernamePasswordToken));
92
93         // null hostname test
94         final UsernamePasswordToken nullHostname = new UsernamePasswordToken();
95         nullHostname.setHost(null);
96         assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME,
97                 AuthenticationTokenUtils.extractHostname(nullHostname));
98
99         // positive test
100         final UsernamePasswordToken positiveUsernamePasswordToken = new UsernamePasswordToken();
101         final String testUsername = "testHostname1";
102         positiveUsernamePasswordToken.setHost(testUsername);
103         assertEquals(testUsername, AuthenticationTokenUtils.extractHostname(positiveUsernamePasswordToken));
104     }
105
106     @Test
107     public void testGenerateUnsuccessfulAuthenticationMessage() throws Exception {
108         final UsernamePasswordToken unsuccessfulToken = new UsernamePasswordToken();
109         unsuccessfulToken.setUsername("unsuccessfulUser1");
110         unsuccessfulToken.setHost("unsuccessfulHost1");
111         assertEquals("Unsuccessful authentication attempt by unsuccessfulUser1 from unsuccessfulHost1",
112                 AuthenticationTokenUtils.generateUnsuccessfulAuthenticationMessage(unsuccessfulToken));
113     }
114
115     @Test
116     public void testGenerateSuccessfulAuthenticationMessage() throws Exception {
117         final UsernamePasswordToken successfulToken = new UsernamePasswordToken();
118         successfulToken.setUsername("successfulUser1");
119         successfulToken.setHost("successfulHost1");
120         assertEquals("Successful authentication attempt by successfulUser1 from successfulHost1",
121                 AuthenticationTokenUtils.generateSuccessfulAuthenticationMessage(successfulToken));
122     }
123 }