/*
 * Copyright 2002-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.provider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Collections;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;

/**
 * @author Dave Syer
 * 
 */
public class TestBaseClientDetails {

	/**
	 * test default constructor
	 */
	@Test
	public void testBaseClientDetailsDefaultConstructor() {
		BaseClientDetails details = new BaseClientDetails();
		assertEquals("[]", details.getResourceIds().toString());
		assertEquals("[]", details.getScope().toString());
		assertEquals("[]", details.getAuthorizedGrantTypes().toString());
		assertEquals("[]", details.getAuthorities().toString());
	}

	/**
	 * test explicit convenience constructor
	 */
	@Test
	public void testBaseClientDetailsConvenienceConstructor() {
		BaseClientDetails details = new BaseClientDetails("foo", "", "foo,bar", "authorization_code", "ROLE_USER");
		assertEquals("[]", details.getResourceIds().toString());
		assertEquals("[bar, foo]", details.getScope().toString());
		assertEquals("[authorization_code]", details.getAuthorizedGrantTypes().toString());
		assertEquals("[ROLE_USER]", details.getAuthorities().toString());
	}

	@Test
	public void testJsonSerialize() throws Exception {
		BaseClientDetails details = new BaseClientDetails("foo", "", "foo,bar", "authorization_code", "ROLE_USER");
		details.setClientId("foo");
		details.setClientSecret("bar");
		details.setAdditionalInformation(Collections.singletonMap("foo", "bar"));
		String value = new ObjectMapper().writeValueAsString(details);
		assertTrue(value.contains("client_id"));
		assertTrue(value.contains("client_secret"));
		assertTrue(value.contains("authorized_grant_types"));
		assertTrue(value.contains("[\"ROLE_USER\"]"));
		assertTrue(value.contains("\"foo\":\"bar\""));
	}

	@Test
	public void testJsonDeserialize() throws Exception {
		String value = "{\"foo\":\"bar\",\"client_id\":\"foo\",\"scope\":[\"bar\",\"foo\"],\"authorized_grant_types\":[\"authorization_code\"],\"access_token_validity\":0,\"authorities\":[\"ROLE_USER\"]}";
		BaseClientDetails details = new ObjectMapper().readValue(value, BaseClientDetails.class);
		BaseClientDetails expected = new BaseClientDetails("foo", "", "foo,bar", "authorization_code", "ROLE_USER");
		expected.setAdditionalInformation(Collections.singletonMap("foo", (Object)"bar"));
		assertEquals(expected, details);
	}

	@Test
	public void testJsonDeserializeWithArraysAsStrings() throws Exception {
		// Collection values can be deserialized from space or comma-separated lists
		String value = "{\"foo\":\"bar\",\"client_id\":\"foo\",\"scope\":\"bar  foo\",\"authorized_grant_types\":\"authorization_code\",\"access_token_validity\":0,\"authorities\":\"ROLE_USER,ROLE_ADMIN\"}";
		BaseClientDetails details = new ObjectMapper().readValue(value, BaseClientDetails.class);
		BaseClientDetails expected = new BaseClientDetails("foo", "", "foo,bar", "authorization_code", "ROLE_USER,ROLE_ADMIN");
		expected.setAdditionalInformation(Collections.singletonMap("foo", (Object)"bar"));
		assertEquals(expected, details);
	}

}

@@ -2077,84 +2077,8 @@
 %22);%0A
-%09%09details.setAdditionalInformation(Collections.singletonMap(%22foo%22, %22bar%22));%0A
 %09%09St
@@ -2138,16 +2138,16 @@
 tails);%0A
+
 %09%09assert
@@ -2280,24 +2280,24 @@
 t_types%22));%0A
-
 %09%09assertTrue
@@ -2329,24 +2329,389 @@
 USER%5C%22%5D%22));%0A
+%09%7D%0A%0A%09@Test%0A%09public void testJsonSerializeAdditionalInformation() throws Exception %7B%0A%09%09BaseClientDetails details = new BaseClientDetails(%22foo%22, %22%22, %22foo,bar%22, %22authorization_code%22, %22ROLE_USER%22);%0A%09%09details.setClientId(%22foo%22);%0A%09%09details.setAdditionalInformation(Collections.singletonMap(%22foo%22, %22bar%22));%0A%09%09String value = new ObjectMapper().writeValueAsString(details);%0A
 %09%09assertTrue

add additional test case