package com.fieldexpert.fbapi4j.http;

import static com.fieldexpert.fbapi4j.http.server.IOUtils.string;
import static java.util.Arrays.asList;
import static junit.framework.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.fieldexpert.fbapi4j.common.Attachment;
import com.fieldexpert.fbapi4j.http.server.MockServer;

public class HttpTest {

	private static MockServer server;
	private static URL url;
	private Map<String, String> params;

	@Test
	public void get() throws IOException {
		InputStream is = Http.get(url, params);

		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		Map<String, String> returnedParams = params(br.readLine());

		assertEquals(params, returnedParams);
	}

	@Test
	public void post() throws IOException {
		InputStream is = Http.post(url, params);

		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		Map<String, String> returnedParams = params(br.readLine());

		assertEquals(params, returnedParams);
	}

	@Test
	public void attachment() throws IOException {
		List<Attachment> attachments = asList(new Attachment("foo.txt", "text/plain", "This is my test file."));
		InputStream is = Http.post(url, params, attachments);

		Map<String, String> returnedParams = params(string(is));

		params.put("nFileCount", "1");
		params.put("File1", "This is my test file.");

		assertEquals(params, returnedParams);
	}

	private Map<String, String> params(String response) {
		Map<String, String> result = new HashMap<String, String>();
		String[] params = response.replaceAll("\\{|\\}", "").split(",");
		for (String param : params) {
			String[] tuple = param.split("="); // Obviously doesn't work if the file contains equal signs.
			result.put(tuple[0].trim(), tuple[1].trim());
		}
		return result;
	}

	@Before
	public void before() {
		params = new HashMap<String, String>();
		params.put("foo", "bar");
		params.put("baz", "buck");
	}

	@BeforeClass
	public static void startServer() {
		String host = "localhost", path = "/echo";
		int port = 9000;
		server = new MockServer(host, port, path);
		server.start();

		try {
			url = new URL("http", host, port, path);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

	@AfterClass
	public static void stopServer() {
		server.stop();
	}
}

@@ -187,16 +187,65 @@
 tEquals;
+%0Aimport static junit.framework.Assert.assertTrue;
 %0A%0Aimport
@@ -261,32 +261,53 @@
 BufferedReader;%0A
+import java.io.File;%0A
 import java.io.I
@@ -1787,16 +1787,529 @@
 s);%0A%09%7D%0A%0A
+%09@Test%0A%09public void attachments() throws IOException %7B%0A%09%09List%3CAttachment%3E attachments = asList(new Attachment(%22foo.txt%22, %22text/plain%22, %22This is my test file.%22), new Attachment(new File(%22test/fogbugz.xml%22)));%0A%09%09InputStream is = Http.post(url, params, attachments);%0A%0A%09%09Map%3CString, String%3E returnedParams = params(string(is));%0A%0A%09%09params.put(%22nFileCount%22, %222%22);%0A%09%09params.put(%22File1%22, %22This is my test file.%22);%0A%0A%09%09assertTrue(returnedParams.containsKey(%22File1%22));%0A%09%09assertTrue(returnedParams.containsKey(%22File2%22));%0A%09%7D%0A%0A
 %09private

Add testcase for multiple attachments