/*
 * Copyright 2012-2015 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.boot.autoconfigure.security.oauth2.resource;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.OAuth2RestOperations;
import org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;

import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;

/**
 * Tests for {@link UserInfoTokenServices}.
 *
 * @author Dave Syer
 */
public class UserInfoTokenServicesTests {

	@Rule
	public ExpectedException expected = ExpectedException.none();

	private UserInfoTokenServices services = new UserInfoTokenServices(
			"http://example.com", "foo");

	private BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();

	private OAuth2RestOperations template = mock(OAuth2RestOperations.class);

	private Map<String, Object> map = new LinkedHashMap<String, Object>();

	@Before
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void init() {
		this.resource.setClientId("foo");
		given(this.template.getForEntity(any(String.class), any(Class.class)))
				.willReturn(new ResponseEntity<Map>(this.map, HttpStatus.OK));
		given(this.template.getAccessToken()).willReturn(
				new DefaultOAuth2AccessToken("FOO"));
		given(this.template.getResource()).willReturn(this.resource);
		given(this.template.getOAuth2ClientContext()).willReturn(
				mock(OAuth2ClientContext.class));
	}

	@Test
	public void sunnyDay() {
		this.services.setRestTemplate(this.template);
		assertEquals("unknown", this.services.loadAuthentication("FOO").getName());
	}

	@SuppressWarnings("unchecked")
	@Test
	public void badToken() {
		this.services.setRestTemplate(this.template);
		given(this.template.getForEntity(any(String.class), any(Class.class))).willThrow(
				new UserRedirectRequiredException("foo:bar", Collections
						.<String, String>emptyMap()));
		this.expected.expect(InvalidTokenException.class);
		assertEquals("unknown", this.services.loadAuthentication("FOO").getName());
	}

	@Test
	public void userId() {
		this.map.put("userid", "spencer");
		this.services.setRestTemplate(this.template);
		assertEquals("spencer", this.services.loadAuthentication("FOO").getName());
	}

}

@@ -1597,16 +1597,55 @@
 rs.any;%0A
+import static org.mockito.Matchers.eq;%0A
 import s
@@ -2221,17 +2221,8 @@
 );%0A%0A
-%09@Before%0A
 %09@Su
@@ -2240,36 +2240,28 @@
 ngs(
-%7B %22unchecked%22, %22rawtypes%22 %7D)
+%22rawtypes%22)%0A%09@Before
 %0A%09pu
@@ -2365,33 +2365,30 @@
 ing.class), 
-any(Class
+eq(Map
 .class)))%0A%09%09
@@ -2384,21 +2384,16 @@
 class)))
-%0A%09%09%09%09
 .willRet
@@ -2396,16 +2396,21 @@
 lReturn(
+%0A%09%09%09%09
 new Resp
@@ -2879,40 +2879,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22unchecked%22)%0A
 %09@Te
@@ -3014,17 +3014,14 @@
 s), 
-any(Class
+eq(Map
 .cla

Fix compile errors in Eclipse (STS 3.6.4) in UserInfoTokenServicesTests