package com.kurento.kmf.jsonrpcconnector.test;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;

import com.kurento.kmf.jsonrpcconnector.DefaultJsonRpcHandler;
import com.kurento.kmf.jsonrpcconnector.Transaction;
import com.kurento.kmf.jsonrpcconnector.client.JsonRpcClient;
import com.kurento.kmf.jsonrpcconnector.client.JsonRpcErrorException;
import com.kurento.kmf.jsonrpcconnector.internal.message.Request;
import com.kurento.kmf.jsonrpcconnector.test.base.JsonRpcConnectorBaseTest;

public class ErrorServerTest extends JsonRpcConnectorBaseTest {

	public static class Handler extends DefaultJsonRpcHandler<String> {

		@Override
		public void handleRequest(final Transaction transaction,
				Request<String> request) throws Exception {

			String method = request.getMethod();

			if (method.equals("explicitError")) {

				transaction.sendError(-1, "Exception message", "Data");

			} else if (method.equals("asyncError")) {

				transaction.startAsync();

				// Poor man method scheduling
				new Thread() {
					public void run() {
						try {
							Thread.sleep(1000);
							transaction.sendError(-1, "Exception message",
									"Data");
						} catch (Exception e) {
						}
					}
				}.start();

			} else if (method.equals("exceptionError")) {

				// 1, e.getMessage(), null
				throw new RuntimeException("Exception message");
			}
		}
	}

	@Test
	public void test() throws IOException, InterruptedException {

		JsonRpcClient client = createJsonRpcClient("/error_handler");

		try {

			client.sendRequest("explicitError");

			Assert.fail("An exception should be thrown");

		} catch (JsonRpcErrorException e) {

			Assert.assertEquals("Exception message. Data: Data", e.getMessage());
			Assert.assertEquals("Data", e.getData());
			Assert.assertEquals(-1, e.getCode());
		}

		try {

			client.sendRequest("asyncError");

			Assert.fail("An exception should be thrown");

		} catch (JsonRpcErrorException e) {

			Assert.assertEquals("Exception message. Data: Data", e.getMessage());
			Assert.assertEquals("Data", e.getData());
			Assert.assertEquals(-1, e.getCode());
		}

		try {

			client.sendRequest("exceptionError");

			Assert.fail("An exception should be thrown");

		} catch (RuntimeException e) {

			String expected = "RuntimeException:Exception message. Data: java.lang.RuntimeException: Exception message";
			Assert.assertEquals(expected, e.getMessage().substring(0,expected.length()));
		}

		client.close();

	}

}

@@ -1665,461 +1665,245 @@
 %0A%09%09%09
-Assert.assertEquals(%22Exception message. Data: Data%22, e.getMessage());%0A%09%09%09Assert.assertEquals(%22Data%22, e.getData());%0A%09%09%09Assert.assertEquals(-1, e.getCode());%0A%09%09%7D%0A%0A%09%09try %7B%0A%0A%09%09%09client.sendRequest(%22asyncError%22);%0A%0A%09%09%09Assert.fail(%22An exception should be thrown%22);%0A%0A%09%09%7D catch (JsonRpcErrorException e) %7B%0A%0A%09%09%09Assert.assertEquals(%22Exception message. Data: Data%22, e.getMessage());%0A%09%09%09Assert.assertEquals(%22Data%22, e.getData());%0A%09%09%09Assert.assertEquals(-1, e.getCode()
+checkException(e, %22Exception message%22, %22Data%22);%0A%09%09%7D%0A%0A%09%09try %7B%0A%0A%09%09%09client.sendRequest(%22asyncError%22);%0A%0A%09%09%09Assert.fail(%22An exception should be thrown%22);%0A%0A%09%09%7D catch (JsonRpcErrorException e) %7B%0A%0A%09%09%09checkException(e, %22Exception message%22, %22Data%22
 );%0A%09
@@ -2018,23 +2018,28 @@
  catch (
-Runtime
+JsonRpcError
 Exceptio
@@ -2053,25 +2053,25 @@
 %0A%09%09%09
-String expected =
+checkException(e,
  %22Ru
@@ -2107,15 +2107,17 @@
 age.
- Data: 
+%22,%0A%09%09%09%09%09%22
 java
@@ -2143,26 +2143,107 @@
 ion:
- Exception
+%22);%0A%09%09%7D%0A%0A%09%09client.close();%0A%09%7D%0A%0A%09private void checkException(JsonRpcErrorException e, String
  message
 %22;%0A%09
@@ -2242,115 +2242,522 @@
 sage
-%22;
+,
 %0A%09%09%09
-Assert.assertEquals(expected, e.getMessage().substring(0,expected.length()));%0A%09%09%7D%0A%0A%09%09client.clos
+String data) %7B%0A%0A%09%09boolean expectedStartMessage = e.getMessage().startsWith(message);%0A%09%09Assert.assertTrue(%22Exception should be an error starting with: '%22%0A%09%09%09%09+ message + %22' but it is '%22 + e.getMessage() + %22'%22,%0A%09%09%09%09expectedStartMessage);%0A%0A%09%09boolean expectedStartData = e.getData().toString().startsWith(data);%0A%09%09Assert.assertTrue(%0A%09%09%09%09%22Exception should have an error with data starting with: '%22%0A%09%09%09%09%09%09+ data + %22' but it is '%22 + e.getData() + %22'%22,%0A%09%09%09%09expectedStartData);%0A%0A%09%09Assert.assertEquals(-1, e.getCod
 e()
+)
 ;
-%0A
 %0A%09%7D%0A

Fix broken test about jsonrpc errors