2 * Copyright (c) 2016 Brocade Communications 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
9 package org.opendaylight.aaa.shiro.filters;
11 import static org.junit.Assert.*;
13 import ch.qos.logback.classic.spi.LoggingEvent;
15 import java.util.List;
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;
25 * Test AuthenticationListener, which is responsible for logging Accounting events.
27 * @author Ryan Goulding (ryandgoulding@gmail.com)
29 public class AuthenticationListenerTest {
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);
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",
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);
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",