/**
 * Copyright 2010-2012 Ralph Schaer <ralphschaer@gmail.com>
 *
 * 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 ch.ralscha.extdirectspring.itest;

import static org.fest.assertions.Assertions.assertThat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;

public class ExceptionFormPostControlerTest extends JettyTest {

	private HttpClient client;

	private HttpPost post;

	@Before
	public void beforeTest() {
		client = new DefaultHttpClient();
		post = new HttpPost("http://localhost:9998/controller/router");
	}

	@Test
	public void testPost() throws ClientProtocolException, IOException {

		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("extTID", "3"));
		formparams.add(new BasicNameValuePair("extAction", "exceptionFormPostController"));
		formparams.add(new BasicNameValuePair("extMethod", "throwAException"));
		formparams.add(new BasicNameValuePair("extType", "rpc"));
		formparams.add(new BasicNameValuePair("extUpload", "false"));

		UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(formparams, "UTF-8");

		post.setEntity(postEntity);

		HttpResponse response = client.execute(post);
		HttpEntity entity = response.getEntity();
		assertThat(entity).isNotNull();
		String responseString = EntityUtils.toString(entity);

		ObjectMapper mapper = new ObjectMapper();
		@SuppressWarnings("unchecked")
		Map<String, Object> rootAsMap = mapper.readValue(responseString, Map.class);
		assertThat(rootAsMap).hasSize(6);
		assertThat(rootAsMap.get("method")).isEqualTo("throwAException");
		assertThat(rootAsMap.get("type")).isEqualTo("exception");
		assertThat(rootAsMap.get("action")).isEqualTo("exceptionFormPostController");
		assertThat(rootAsMap.get("tid")).isEqualTo(3);
		assertThat(rootAsMap.get("message")).isEqualTo("a null pointer");
		assertThat(rootAsMap.get("where")).isNull();
	}
}

@@ -2489,18 +2489,16 @@
 tity);%0D%0A
-%0D%0A
 %09%09Object
@@ -3059,19 +3059,230 @@
 .isNull();%0D%0A
+%09%09%0D%0A%09%09@SuppressWarnings(%22unchecked%22)%0D%0A%09%09Map%3CString, Object%3E result = (Map%3CString, Object%3E)rootAsMap.get(%22result%22);%0D%0A%09%09assertThat(result).hasSize(1);%0D%0A%09%09assertThat((Boolean)result.get(%22success%22)).isFalse();%0D%0A%09%09%0D%0A
 %09%7D%0D%0A%7D%0D%0A

fix text