/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.springframework.security.oauth2.web.authentication;

import static org.easymock.EasyMock.createMock;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.web.OAuth2ExceptionRenderer;
import org.springframework.web.context.request.ServletWebRequest;

/**
 *
 * @author Rob Winch
 */
public class TestOAuth2AuthenticationFailureHandler {
	@Rule
	public ExpectedException thrown = ExpectedException.none();

	private OAuth2ExceptionRendererStub renderer;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private AuthenticationException originalException;

	private OAuth2AuthenticationFailureHandler handler;

	@Before
	public void setUp() {
		renderer = new OAuth2ExceptionRendererStub();
		request = createMock(HttpServletRequest.class);
		response = createMock(HttpServletResponse.class);
		originalException = new UsernameNotFoundException("not found");

		handler = new OAuth2AuthenticationFailureHandler();
		handler.setExceptionRenderer(renderer);
	}

	@Test
	public void onAuthenticationFailure() throws Exception {
		handler.onAuthenticationFailure(request, response, originalException);
		Object body = renderer.entity.getBody();
		assertTrue("The entity should be an InvalidClientException. Got "+body,body instanceof InvalidClientException);
		assertEquals(HttpStatus.UNAUTHORIZED,renderer.entity.getStatusCode());
		assertSame(request,renderer.webRequest.getNativeRequest());
		assertSame(response,renderer.webRequest.getNativeResponse());
	}

	@Test
	public void setExceptionRendererNullExceptionRenderer() {
		thrown.expect(IllegalArgumentException.class);
		thrown.expectMessage("exceptionRenderer cannot be null");
		handler.setExceptionRenderer(null);
	}

	/**
	 * Rather than deal with EasyMock class extension just capture the arguments using this stub
	 */
	private static class OAuth2ExceptionRendererStub extends OAuth2ExceptionRenderer {
		private ResponseEntity<?> entity;
		private ServletWebRequest webRequest;
		@Override
		public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest)
				throws Exception {
			this.entity = (ResponseEntity<?>) responseEntity;
			this.webRequest = webRequest;
		}
	}
}

@@ -664,56 +664,8 @@
 n;%0A%0A
-import static org.easymock.EasyMock.createMock;%0A
 impo
@@ -705,16 +705,16 @@
 Equals;%0A
+
 import s
@@ -991,32 +991,137 @@
 ectedException;%0A
+import org.junit.runner.RunWith;%0Aimport org.mockito.Mock;%0Aimport org.mockito.runners.MockitoJUnitRunner;%0A
 import org.sprin
@@ -1651,16 +1651,51 @@
 nch%0A */%0A
+@RunWith(MockitoJUnitRunner.class)%0A
 public c
@@ -1856,16 +1856,23 @@
 nderer;%0A
+%09@Mock%0A
 %09private
@@ -1900,16 +1900,23 @@
 equest;%0A
+%09@Mock%0A
 %09private
@@ -2085,16 +2085,16 @@
 tUp() %7B%0A
+
 %09%09render
@@ -2137,110 +2137,8 @@
 ();%0A
-%09%09request = createMock(HttpServletRequest.class);%0A%09%09response = createMock(HttpServletResponse.class);%0A
 %09%09or

update test from patch applied out of order