1c8235253d5e73064b27b8d46659981cbf260e58
[moon.git] /
1 /*
2  * Copyright (c) 2016 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.*;
12
13 import ch.qos.logback.classic.spi.LoggingEvent;
14
15 import java.util.List;
16
17 import org.apache.shiro.authc.AuthenticationException;
18 import org.apache.shiro.authc.SimpleAuthenticationInfo;
19 import org.apache.shiro.authc.UsernamePasswordToken;
20 import org.junit.Test;
21 import org.opendaylight.aaa.shiro.TestAppender;
22 import org.opendaylight.aaa.shiro.filters.AuthenticationListener;
23
24 /**
25  * Test AuthenticationListener, which is responsible for logging Accounting events.
26  *
27  * @author Ryan Goulding (ryandgoulding@gmail.com)
28  */
29 public class AuthenticationListenerTest {
30
31     @Test
32     public void testOnSuccess() throws Exception {
33         // sets up a successful authentication attempt
34         final AuthenticationListener authenticationListener = new AuthenticationListener();
35         final UsernamePasswordToken authenticationToken = new UsernamePasswordToken();
36         authenticationToken.setUsername("successfulUser1");
37         authenticationToken.setHost("successfulHost1");
38         final SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo();
39         // the following call produces accounting output
40         authenticationListener.onSuccess(authenticationToken, simpleAuthenticationInfo);
41
42         // grab the latest log output and make sure it is in line with what is expected
43         final List<LoggingEvent> loggingEvents = TestAppender.getCurrentInstance().getEvents();
44         // the latest logging event is the one we need to inspect
45         final int whichLoggingEvent = loggingEvents.size() - 1;
46         final LoggingEvent latestLoggingEvent = loggingEvents.get(whichLoggingEvent);
47         final String latestLogMessage = latestLoggingEvent.getMessage();
48         assertEquals("Successful authentication attempt by successfulUser1 from successfulHost1",
49                 latestLogMessage);
50     }
51
52     @Test
53     public void testOnFailure() throws Exception {
54         // variables for an unsucessful authentication attempt
55         final AuthenticationListener authenticationListener = new AuthenticationListener();
56         final UsernamePasswordToken authenticationToken = new UsernamePasswordToken();
57         authenticationToken.setUsername("unsuccessfulUser1");
58         authenticationToken.setHost("unsuccessfulHost1");
59         final AuthenticationException authenticationException =
60                 new AuthenticationException("test auth exception");
61         // produces unsuccessful authentication attempt output
62         authenticationListener.onFailure(authenticationToken, authenticationException);
63
64         // grab the latest log output and ensure it is in line with what is expected
65         final List<LoggingEvent> loggingEvents = TestAppender.getCurrentInstance().getEvents();
66         final int whichLoggingEvent = loggingEvents.size() - 1;
67         final LoggingEvent latestLoggingEvent = loggingEvents.get(whichLoggingEvent);
68         final String latestLogMessage = latestLoggingEvent.getMessage();
69         assertEquals("Unsuccessful authentication attempt by unsuccessfulUser1 from unsuccessfulHost1",
70                 latestLogMessage);
71     }
72 }