DATAREST-889 - Polishing.

Improved test cases by using JUnit's ExpectedException for simpler assertions.

Original pull request: #227.
This commit is contained in:
Oliver Gierke
2016-09-12 16:09:39 +02:00
parent 12a870a6ae
commit 24b49dd2d1
3 changed files with 31 additions and 18 deletions

View File

@@ -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);
}
}
}

View File

@@ -140,6 +140,8 @@ public class JsonPatchPatchConverter implements PatchConverter<JsonNode> {
} 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));
}
}

View File

@@ -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 {