package tech.greenfield.vertx.irked;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Test;

import io.vertx.core.buffer.Buffer;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import tech.greenfield.vertx.irked.annotations.Get;
import tech.greenfield.vertx.irked.base.TestBase;
import tech.greenfield.vertx.irked.status.OK;

public class TestSending extends TestBase {

	public final static byte[] data = new byte[]{ 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x10, 0x20, 0x30, 0x40 };

	public class TestController extends Controller {
		
		@Get("/sendtext")
		public void text(Request r) {
			r.sendContent("hello world", new OK(), "text/plain");
		}

		@Get("/sendbinary")
		public void binary(Request r) {
			r.sendContent(Buffer.buffer(data), new OK(), "application/octet-stream");
		};
		
	}

	@Before
	public void deployServer(TestContext context) {
		deployController(new TestController(), context.asyncAssertSuccess());
	}

	@Test
	public void testTextSending(TestContext context) {
		Async async = context.async();
		getClient().get(port, "localhost", "/sendtext").exceptionHandler(t -> context.fail(t)).handler(res -> {
			context.assertEquals(200, res.statusCode(), "Request failed");
			res.exceptionHandler(t -> context.fail(t)).bodyHandler(body -> {
				try {
					context.assertEquals("hello world", body.toString());
				} catch (Exception e) {
					context.fail(e);
				}
			});
			async.complete();
		}).end();
	}

	@Test
	public void testBinarySending(TestContext context) {
		Async async = context.async();
		getClient().get(port, "localhost", "/sendbinary").exceptionHandler(t -> context.fail(t)).handler(res -> {
			context.assertEquals(200, res.statusCode(), "Request failed");
			res.exceptionHandler(t -> context.fail(t)).bodyHandler(body -> {
				try {
					context.assertTrue(Arrays.equals(data, body.getBytes()));
				} catch (Exception e) {
					context.fail(e);
				}
			});
			async.complete();
		}).end("{}");
	}

}

@@ -55,16 +55,67 @@
 .Arrays;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -875,16 +875,166 @@
 %09%09%7D;%0A%09%09%0A
+%09%09@Get(%22/sendlist%22)%0A%09%09public void list(Request r) %7B%0A%09%09%09List%3CString%3E l = new ArrayList%3C%3E();%0A%09%09%09l.add(%22hello%22);%0A%09%09%09l.add(%22world%22);%0A%09%09%09r.send(l);%0A%09%09%7D%0A%09%09%0A
 %09%7D%0A%0A%09@Be
@@ -1750,32 +1750,32 @@
 ontext.async();%0A
-
 %09%09getClient().ge
@@ -2177,10 +2177,519 @@
 %22);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testListSending(TestContext context) %7B%0A%09%09Async async = context.async();%0A%09%09getClient().get(port, %22localhost%22, %22/sendlist%22).exceptionHandler(t -%3E context.fail(t)).handler(res -%3E %7B%0A%09%09%09context.assertEquals(200, res.statusCode(), %22Request failed%22);%0A%09%09%09res.exceptionHandler(t -%3E context.fail(t)).bodyHandler(body -%3E %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09context.assertTrue(Arrays.equals(data, body.getBytes()));%0A%09%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09%09context.fail(e);%0A%09%09%09%09%7D%0A%09%09%09%7D);%0A%09%09%09async.complete();%0A%09%09%7D).end(%22%7B%7D%22);%0A%09%7D%0A%0A
 %7D%0A

add test showing that we should support sending lists in Irked Request.send()