package io.spacedog.server;

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

import com.fasterxml.jackson.databind.JsonNode;

import io.spacedog.utils.Exceptions;

public class JsonPayloadTest extends Assert {

	@Test
	public void shouldConvertRuntimeExceptionToJsonError() {
		JsonNode json = JsonPayload.toJson(new RuntimeException(new NullPointerException()), true);
		assertEquals("java.lang.RuntimeException", json.get("type").asText());
		assertEquals("java.lang.NullPointerException", json.get("message").asText());
		assertTrue(json.get("trace").size() > 5);
		assertEquals("java.lang.NullPointerException", json.get("cause").get("type").asText());
		assertNull(json.get("cause").get("message"));
		assertTrue(json.get("cause").get("trace").size() > 5);
	}

	@Test
	public void convertSpaceExceptionToJsonErrorDebugMode() {
		JsonNode json = JsonPayload.toJson(Exceptions.alreadyExists("credentials", "vince"), true);
		assertEquals("io.spacedog.utils.SpaceException", json.get("type").asText());
		assertEquals("[credentials][vince] already exists", json.get("message").asText());
		assertEquals("already-exists", json.get("code").asText());
		assertNull(json.get("cause"));
	}

	@Test
	public void convertSpaceExceptionToJsonErrorNoDebugMode() {
		JsonNode json = JsonPayload.toJson(Exceptions.alreadyExists("credentials", "vince"), false);
		assertNull(json.get("type"));
		assertEquals("[credentials][vince] already exists", json.get("message").asText());
		assertEquals("already-exists", json.get("code").asText());
		assertNull(json.get("cause"));
	}

}

@@ -120,16 +120,63 @@
 nNode;%0A%0A
+import io.spacedog.client.http.SpaceException;%0A
 import i
@@ -426,27 +426,16 @@
 tEquals(
-%22java.lang.
 RuntimeE
@@ -434,33 +434,48 @@
 RuntimeException
-%22
+.class.getName()
 , json.get(%22type
@@ -499,35 +499,24 @@
 ssertEquals(
-%22java.lang.
 NullPointerE
@@ -515,33 +515,48 @@
 PointerException
-%22
+.class.getName()
 , json.get(%22mess
@@ -635,19 +635,8 @@
 als(
-%22java.lang.
 Null
@@ -643,33 +643,48 @@
 PointerException
-%22
+.class.getName()
 , json.get(%22caus
@@ -998,42 +998,38 @@
 als(
-%22io.spacedog.utils.SpaceException%22
+SpaceException.class.getName()
 , js

Fix JsonPayload tests