//
// This file is part of the OpenNMS(R) Application.
//
// OpenNMS(R) is Copyright (C) 2006 The OpenNMS Group, Inc.  All rights reserved.
// OpenNMS(R) is a derivative work, containing both original code, included code and modified
// code that was published under the GNU General Public License. Copyrights for modified
// and included code are below.
//
// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
//
// Original code base Copyright (C) 1999-2001 Oculan Corp.  All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// For more information contact:
//      OpenNMS Licensing       <license@opennms.org>
//      http://www.opennms.org/
//      http://www.opennms.com/
//
package org.opennms.web.acegisecurity;

import org.acegisecurity.Authentication;
import org.acegisecurity.BadCredentialsException;
import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.providers.dao.DaoAuthenticationProvider;
import org.opennms.test.ThrowableAnticipator;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;


public class AuthenticationIntegrationTest extends AbstractDependencyInjectionSpringContextTests {
	private DaoAuthenticationProvider m_provider; 

	@Override
	protected String[] getConfigLocations() {
		return new String[] {
                "org/opennms/web/acegisecurity/applicationContext-authenticationIntegrationTest.xml"
        		};
	}
	
	public void setDaoAuthenticationProvider(DaoAuthenticationProvider provider) {
		m_provider = provider;
	}
	public DaoAuthenticationProvider getDaoAuthenticationProvider() {
		return m_provider;
	}
	
	public void testAuthenticateAdmin() {
		Authentication authentication = new UsernamePasswordAuthenticationToken("admin", "admin");
		Authentication authenticated = m_provider.authenticate(authentication);
		assertNotNull("authenticated Authentication object not null", authenticated);
		GrantedAuthority[] authorities = authenticated.getAuthorities();
		assertNotNull("GrantedAuthorities should not be null", authorities);
		assertEquals("GrantedAuthorities size", 3, authorities.length);
		assertEquals("GrantedAuthorities zero role", "ROLE_USER", authorities[0].getAuthority());
		assertEquals("GrantedAuthorities one name", "ROLE_RTC", authorities[1].getAuthority());
		assertEquals("GrantedAuthorities two name", "ROLE_ADMIN", authorities[2].getAuthority());
	}
	
	public void testAuthenticateRtc() {
		Authentication authentication = new UsernamePasswordAuthenticationToken("rtc", "rtc");
		Authentication authenticated = m_provider.authenticate(authentication);
		assertNotNull("authenticated Authentication object not null", authenticated);
		GrantedAuthority[] authorities = authenticated.getAuthorities();
		assertNotNull("GrantedAuthorities should not be null", authorities);
		assertEquals("GrantedAuthorities size", 2, authorities.length);
		assertEquals("GrantedAuthorities zero role", "ROLE_USER", authorities[0].getAuthority());
		assertEquals("GrantedAuthorities one name", "ROLE_RTC", authorities[1].getAuthority());
	}
	
	public void testAuthenticateTempUser() {
		Authentication authentication = new UsernamePasswordAuthenticationToken("tempuser", "mike");
		Authentication authenticated = m_provider.authenticate(authentication);
		assertNotNull("authenticated Authentication object not null", authenticated);
		GrantedAuthority[] authorities = authenticated.getAuthorities();
		assertNotNull("GrantedAuthorities should not be null", authorities);
		assertEquals("GrantedAuthorities size", 1, authorities.length);
		assertEquals("GrantedAuthorities zero role", "ROLE_USER", authorities[0].getAuthority());
	}
	
	public void testAuthenticateBadUsername() {
		Authentication authentication = new UsernamePasswordAuthenticationToken("badUsername", "admin");
		
		ThrowableAnticipator ta = new ThrowableAnticipator();
		ta.anticipate(new BadCredentialsException("Bad credentials"));
		try {
			m_provider.authenticate(authentication);
		} catch (Throwable t) {
			ta.throwableReceived(t);
		}
		ta.verifyAnticipated();
	}
	
	public void testAuthenticateBadPassword() {
		Authentication authentication = new UsernamePasswordAuthenticationToken("admin", "badPassword");

		ThrowableAnticipator ta = new ThrowableAnticipator();
		ta.anticipate(new BadCredentialsException("Bad credentials"));
		try {
			m_provider.authenticate(authentication);
		} catch (Throwable t) {
			ta.throwableReceived(t);
		}
		ta.verifyAnticipated();
	}
}

@@ -2827,17 +2827,17 @@
  size%22, 
-3
+2
 , author
@@ -2947,98 +2947,8 @@
 ));%0A
-%09%09assertEquals(%22GrantedAuthorities one name%22, %22ROLE_RTC%22, authorities%5B1%5D.getAuthority());%0A
 %09%09as
@@ -3015,17 +3015,17 @@
 orities%5B
-2
+1
 %5D.getAut
@@ -3492,33 +3492,33 @@
 horities size%22, 
-2
+1
 , authorities.le
@@ -3528,100 +3528,8 @@
 h);%0A
-%09%09assertEquals(%22GrantedAuthorities zero role%22, %22ROLE_USER%22, authorities%5B0%5D.getAuthority());%0A
 %09%09as
@@ -3586,33 +3586,33 @@
 C%22, authorities%5B
-1
+0
 %5D.getAuthority()

Fix integration tests