diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonLateObjectEvaluator.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonLateObjectEvaluator.java index fe2231d8d..889c01a61 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonLateObjectEvaluator.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonLateObjectEvaluator.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; * {@link LateObjectEvaluator} implementation that assumes values represented as JSON objects. * * @author Craig Walls + * @author Oliver Trosien + * @author Oliver Gierke */ @RequiredArgsConstructor class JsonLateObjectEvaluator implements LateObjectEvaluator { @@ -42,7 +44,7 @@ class JsonLateObjectEvaluator implements LateObjectEvaluator { try { return mapper.readValue(valueNode.traverse(), type); } catch (Exception e) { - throw new PatchException("JSON deserialization exception", e); + throw new PatchException(String.format("Could not read %s into %s!", valueNode, type), e); } } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchPatchConverter.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchPatchConverter.java index d9a66f157..2ab981972 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchPatchConverter.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchPatchConverter.java @@ -140,6 +140,8 @@ public class JsonPatchPatchConverter implements PatchConverter { } else if (valueNode.isObject() || (valueNode.isArray())) { return new JsonLateObjectEvaluator(mapper, valueNode); } - throw new PatchException("Unrecognized valueNode type at path: " + path + ". valueNode: " + valueNode.toString()); + + throw new PatchException( + String.format("Unrecognized valueNode type at path %s and value node %s.", path, valueNode)); } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchTests.java index c31911ca2..cf899a1e5 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/json/patch/JsonPatchTests.java @@ -23,7 +23,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.springframework.core.io.ClassPathResource; import com.fasterxml.jackson.core.JsonParseException; @@ -41,6 +43,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; */ public class JsonPatchTests { + public @Rule ExpectedException exception = ExpectedException.none(); + @Test public void manySuccessfulOperations() throws Exception { @@ -118,7 +122,7 @@ public class JsonPatchTests { } /** - * @see DATAREST-885 + * @see DATAREST-889 */ @Test public void patchArray() throws Exception { @@ -132,34 +136,39 @@ public class JsonPatchTests { assertEquals(Arrays.asList("one", "two", "three"), patchedTodo.getItems()); } + /** + * @see DATAREST-889 + */ @Test public void patchUnknownType() throws Exception { + Todo todo = new Todo(); todo.setAmount(BigInteger.ONE); - try { - Patch patch = readJsonPatch("patch-biginteger.json"); - assertEquals(1, patch.size()); - assertEquals(new BigInteger("18446744073709551616"), patch.getOperations().get(0).value); - } catch (PatchException e) { - assertEquals("Unrecognized valueNode type at path: /amount. valueNode: 18446744073709551616", e.getMessage()); - } + exception.expect(PatchException.class); + exception.expectMessage("/amount"); + exception.expectMessage("18446744073709551616"); + + readJsonPatch("patch-biginteger.json"); } + /** + * @see DATAREST-889 + */ @Test public void failureWithInvalidPatchContent() throws Exception { + Todo todo = new Todo(); todo.setDescription("Description"); Patch patch = readJsonPatch("patch-failing-with-invalid-content.json"); - assertEquals(1, patch.size()); - try { - Todo patchedTodo = patch.apply(todo, Todo.class); - assertEquals("Description", patchedTodo.getDescription()); - } catch (PatchException e) { - assertEquals("JSON deserialization exception", e.getMessage()); - } + exception.expect(PatchException.class); + exception.expectMessage("content"); + exception.expectMessage("blabla"); + exception.expectMessage(String.class.toString()); + + patch.apply(todo, Todo.class); } private Patch readJsonPatch(String jsonPatchFile) throws IOException, JsonParseException, JsonMappingException {