package org.jscookie;

import java.util.Arrays;

import javax.servlet.http.Cookie;

import org.jscookie.testutils.BaseTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith( MockitoJUnitRunner.class )
public class CookiesJSONWriteTest extends BaseTest {
	private Cookies cookies;

	@Before
	public void before() {
		cookies = new Cookies( request, response );
	}

	@Test
	public void write_int_type() throws CookieSerializationException {
		cookies.set( "c", 1 );

		ArgumentCaptor<Cookie> argument = ArgumentCaptor.forClass( Cookie.class );
		Mockito.verify( response ).addCookie( argument.capture() );

		Cookie actual = argument.getValue();
		Assert.assertEquals( "1", actual.getValue() );
	}

	@Test
	public void write_boolean_type() throws CookieSerializationException {
		cookies.set( "c", true );

		ArgumentCaptor<Cookie> argument = ArgumentCaptor.forClass( Cookie.class );
		Mockito.verify( response ).addCookie( argument.capture() );

		Cookie actual = argument.getValue();
		Assert.assertEquals( "true", actual.getValue() );
	}

	@Test
	public void write_JSON_array_with_string() throws CookieSerializationException {
		cookies.set( "c", Arrays.asList( "v" ) );

		ArgumentCaptor<Cookie> argument = ArgumentCaptor.forClass( Cookie.class );
		Mockito.verify( response ).addCookie( argument.capture() );

		Cookie actual = argument.getValue();
		Assert.assertEquals( "%5B%22v%22%5D", actual.getValue() );
	}

	@Test
	public void write_custom_type_with_string_prop() throws CookieSerializationException {
		cookies.set( "c", new CustomTypeString( "v" ) );

		ArgumentCaptor<Cookie> argument = ArgumentCaptor.forClass( Cookie.class );
		Mockito.verify( response ).addCookie( argument.capture() );

		Cookie actual = argument.getValue();
		Assert.assertEquals( "%7B%22property%22%3A%22v%22%7D", actual.getValue() );
	}

	@Test
	public void write_custom_type_with_boolean_prop() throws CookieSerializationException {
		cookies.set( "c", new CustomTypeBoolean( true ) );

		ArgumentCaptor<Cookie> argument = ArgumentCaptor.forClass( Cookie.class );
		Mockito.verify( response ).addCookie( argument.capture() );

		Cookie actual = argument.getValue();
		Assert.assertEquals( "%7B%22property%22%3Atrue%7D", actual.getValue() );
	}

	@Test
	public void write_custom_type_with_number_prop() throws CookieSerializationException {
		cookies.set( "c", new CustomTypeInteger( 1 ) );

		ArgumentCaptor<Cookie> argument = ArgumentCaptor.forClass( Cookie.class );
		Mockito.verify( response ).addCookie( argument.capture() );

		Cookie actual = argument.getValue();
		Assert.assertEquals( "1", actual.getValue() );
	}

	class CustomTypeString implements CookieValue {
		private String property;
		private CustomTypeString( String property ) {
			this.property = property;
		}
		public String getProperty() {
			return property;
		}
	}

	class CustomTypeBoolean implements CookieValue {
		private Boolean property;
		private CustomTypeBoolean( Boolean property ) {
			this.property = property;
		}
		public Boolean getProperty() {
			return property;
		}
	}

	class CustomTypeInteger implements CookieValue {
		private Integer property;
		private CustomTypeInteger( Integer property ) {
			this.property = property;
		}
		public Integer getProperty() {
			return property;
		}
	}
}

@@ -2745,33 +2745,56 @@
 .assertEquals( %22
-1
+%257B%2522property%2522%253A1%257D
 %22, actual.getVal

Fix test expectation