DATAREST-885, DATAREST-885 - Cleanups in JSON Patch code.

This commit is contained in:
Oliver Gierke
2016-09-12 10:52:53 +02:00
parent 2493d3c2f1
commit 2710b988b5
24 changed files with 257 additions and 261 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.
@@ -22,49 +22,47 @@ import java.util.List;
import org.junit.Test;
public class AddOperationTest {
public class AddOperationTests {
@Test
public void addBooleanPropertyValue() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
AddOperation add = new AddOperation("/1/complete", true);
add.perform(todos, Todo.class);
assertTrue(todos.get(1).isComplete());
}
@Test
public void addStringPropertyValue() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
AddOperation add = new AddOperation("/1/description", "BBB");
add.perform(todos, Todo.class);
assertEquals("BBB", todos.get(1).getDescription());
}
@Test
public void addItemToList() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
AddOperation add = new AddOperation("/1", new Todo(null, "D", true));
add.perform(todos, Todo.class);
assertEquals(4, todos.size());
assertEquals("A", todos.get(0).getDescription());
assertFalse(todos.get(0).isComplete());
@@ -75,5 +73,4 @@ public class AddOperationTest {
assertEquals("C", todos.get(3).getDescription());
assertFalse(todos.get(3).isComplete());
}
}

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.
@@ -22,16 +22,16 @@ import java.util.List;
import org.junit.Test;
public class CopyOperationTest {
public class CopyOperationTests {
@Test
public void copyBooleanPropertyValue() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
CopyOperation copy = new CopyOperation("/1/complete", "/0/complete");
copy.perform(todos, Todo.class);
@@ -40,12 +40,12 @@ public class CopyOperationTest {
@Test
public void copyStringPropertyValue() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
CopyOperation copy = new CopyOperation("/1/description", "/0/description");
copy.perform(todos, Todo.class);
@@ -54,12 +54,12 @@ public class CopyOperationTest {
@Test
public void copyBooleanPropertyValueIntoStringProperty() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
CopyOperation copy = new CopyOperation("/1/description", "/0/complete");
copy.perform(todos, Todo.class);
@@ -68,83 +68,87 @@ public class CopyOperationTest {
@Test
public void copyListElementToBeginningOfList() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", true));
todos.add(new Todo(3L, "C", false));
CopyOperation copy = new CopyOperation("/0", "/1");
copy.perform(todos, Todo.class);
assertEquals(4, todos.size());
assertEquals(2L, todos.get(0).getId().longValue()); // NOTE: This could be problematic if you try to save it to a DB because there'll be duplicate IDs
assertEquals(2L, todos.get(0).getId().longValue()); // NOTE: This could be problematic if you try to save it to a DB
// because there'll be duplicate IDs
assertEquals("B", todos.get(0).getDescription());
assertTrue(todos.get(0).isComplete());
}
@Test
public void copyListElementToMiddleOfList() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
CopyOperation copy = new CopyOperation("/2", "/0");
copy.perform(todos, Todo.class);
assertEquals(4, todos.size());
assertEquals(1L, todos.get(2).getId().longValue()); // NOTE: This could be problematic if you try to save it to a DB because there'll be duplicate IDs
assertEquals(1L, todos.get(2).getId().longValue()); // NOTE: This could be problematic if you try to save it to a DB
// because there'll be duplicate IDs
assertEquals("A", todos.get(2).getDescription());
assertTrue(todos.get(2).isComplete());
}
@Test
public void copyListElementToEndOfList_usingIndex() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
CopyOperation copy = new CopyOperation("/3", "/0");
copy.perform(todos, Todo.class);
assertEquals(4, todos.size());
assertEquals(1L, todos.get(3).getId().longValue()); // NOTE: This could be problematic if you try to save it to a DB because there'll be duplicate IDs
assertEquals(1L, todos.get(3).getId().longValue()); // NOTE: This could be problematic if you try to save it to a DB
// because there'll be duplicate IDs
assertEquals("A", todos.get(3).getDescription());
assertTrue(todos.get(3).isComplete());
}
@Test
public void copyListElementToEndOfList_usingTilde() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
CopyOperation copy = new CopyOperation("/~", "/0");
copy.perform(todos, Todo.class);
assertEquals(4, todos.size());
assertEquals(new Todo(1L, "A", true), todos.get(3)); // NOTE: This could be problematic if you try to save it to a DB because there'll be duplicate IDs
assertEquals(new Todo(1L, "A", true), todos.get(3)); // NOTE: This could be problematic if you try to save it to a
// DB because there'll be duplicate IDs
}
@Test
public void copyListElementFromEndOfList_usingTilde() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
CopyOperation copy = new CopyOperation("/0", "/~");
copy.perform(todos, Todo.class);
assertEquals(4, todos.size());
assertEquals(new Todo(3L, "C", false), todos.get(0)); // NOTE: This could be problematic if you try to save it to a DB because there'll be duplicate IDs
}
assertEquals(4, todos.size());
assertEquals(new Todo(3L, "C", false), todos.get(0)); // NOTE: This could be problematic if you try to save it to a
// DB because there'll be duplicate IDs
}
}

View File

@@ -36,11 +36,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* @author Oliver Gierke
* @author Mathias Düsterhöft
*/
public class JsonPatchTest {
public class JsonPatchTests {
@Test
public void manySuccessfulOperations() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
@@ -62,7 +62,7 @@ public class JsonPatchTest {
@Test
public void failureAtBeginning() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
@@ -80,7 +80,6 @@ public class JsonPatchTest {
assertEquals("Test against path '/5/description' failed.", e.getMessage());
}
// nothing should have changed
assertEquals(6, todos.size());
assertFalse(todos.get(1).isComplete());
assertEquals("D", todos.get(3).getDescription());
@@ -90,7 +89,7 @@ public class JsonPatchTest {
@Test
public void failureInMiddle() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
@@ -108,7 +107,6 @@ public class JsonPatchTest {
assertEquals("Test against path '/5/description' failed.", e.getMessage());
}
// nothing should have changed
assertEquals(6, todos.size());
assertFalse(todos.get(1).isComplete());
assertEquals("D", todos.get(3).getDescription());
@@ -117,11 +115,11 @@ public class JsonPatchTest {
}
private Patch readJsonPatch(String jsonPatchFile) throws IOException, JsonParseException, JsonMappingException {
ClassPathResource resource = new ClassPathResource(jsonPatchFile, getClass());
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readValue(resource.getInputStream(), JsonNode.class);
Patch patch = new JsonPatchPatchConverter(mapper).convert(node);
JsonNode node = new ObjectMapper().readValue(resource.getInputStream(), JsonNode.class);
Patch patch = new JsonPatchPatchConverter(new ObjectMapper()).convert(node);
return patch;
}
}

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.
@@ -22,16 +22,16 @@ import java.util.List;
import org.junit.Test;
public class MoveOperationTest {
public class MoveOperationTests {
@Test
public void moveBooleanPropertyValue() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
try {
MoveOperation move = new MoveOperation("/1/complete", "/0/complete");
move.perform(todos, Todo.class);
@@ -45,12 +45,12 @@ public class MoveOperationTest {
@Test
public void moveStringPropertyValue() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
MoveOperation move = new MoveOperation("/1/description", "/0/description");
move.perform(todos, Todo.class);
@@ -59,12 +59,12 @@ public class MoveOperationTest {
@Test
public void moveBooleanPropertyValueIntoStringProperty() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
try {
MoveOperation move = new MoveOperation("/1/description", "/0/complete");
move.perform(todos, Todo.class);
@@ -77,23 +77,23 @@ public class MoveOperationTest {
//
// NOTE: Moving an item about in a list probably has zero effect, as the order of the list is
// usually determined by the DB query that produced the list. Moving things around in a
// java.util.List and then saving those items really means nothing to the DB, as the
// properties that determined the original order are still the same and will result in
// the same order when the objects are queries again.
// usually determined by the DB query that produced the list. Moving things around in a
// java.util.List and then saving those items really means nothing to the DB, as the
// properties that determined the original order are still the same and will result in
// the same order when the objects are queries again.
//
@Test
public void moveListElementToBeginningOfList() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", true));
todos.add(new Todo(3L, "C", false));
MoveOperation move = new MoveOperation("/0", "/1");
move.perform(todos, Todo.class);
assertEquals(3, todos.size());
assertEquals(2L, todos.get(0).getId().longValue());
assertEquals("B", todos.get(0).getDescription());
@@ -102,53 +102,53 @@ public class MoveOperationTest {
@Test
public void moveListElementToMiddleOfList() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
MoveOperation move = new MoveOperation("/2", "/0");
move.perform(todos, Todo.class);
assertEquals(3, todos.size());
assertEquals(1L, todos.get(2).getId().longValue());
assertEquals("A", todos.get(2).getDescription());
assertTrue(todos.get(2).isComplete());
}
@Test
public void moveListElementToEndOfList_usingIndex() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
MoveOperation move = new MoveOperation("/2", "/0");
move.perform(todos, Todo.class);
assertEquals(3, todos.size());
assertEquals(1L, todos.get(2).getId().longValue());
assertEquals("A", todos.get(2).getDescription());
assertTrue(todos.get(2).isComplete());
}
@Test
public void moveListElementToBeginningOfList_usingTilde() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(3L, "C", false));
todos.add(new Todo(4L, "E", false));
todos.add(new Todo(2L, "G", false));
List<Todo> expected = new ArrayList<Todo>();
expected.add(new Todo(1L, "A", true));
expected.add(new Todo(2L, "G", false));
expected.add(new Todo(3L, "C", false));
expected.add(new Todo(4L, "E", false));
MoveOperation move = new MoveOperation("/1", "/~");
move.perform(todos, Todo.class);
assertEquals(expected, todos);
@@ -156,19 +156,19 @@ public class MoveOperationTest {
@Test
public void moveListElementToEndOfList_usingTilde() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", true));
todos.add(new Todo(2L, "G", false));
todos.add(new Todo(3L, "C", false));
todos.add(new Todo(4L, "E", false));
List<Todo> expected = new ArrayList<Todo>();
expected.add(new Todo(1L, "A", true));
expected.add(new Todo(3L, "C", false));
expected.add(new Todo(4L, "E", false));
expected.add(new Todo(2L, "G", false));
MoveOperation move = new MoveOperation("/~", "/1");
move.perform(todos, Todo.class);
assertEquals(expected, todos);

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.
@@ -23,26 +23,31 @@ import java.util.List;
import org.junit.Test;
import org.springframework.expression.Expression;
public class PathToSpelTest {
public class PathToSpelTests {
@Test
public void listIndex() {
Expression expr = PathToSpEL.pathToExpression("/1/description");
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
assertEquals("B", (String) expr.getValue(todos));
assertEquals("B", (String) expr.getValue(todos));
}
@Test
public void listTilde() {
Expression expr = PathToSpEL.pathToExpression("/~/description");
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
assertEquals("C", (String) expr.getValue(todos));
}
}

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.
@@ -22,31 +22,31 @@ import java.util.List;
import org.junit.Test;
public class RemoveOperationTest {
public class RemoveOperationTests {
@Test
public void removePropertyFromObject() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
new RemoveOperation("/1/description").perform(todos, Todo.class);
assertNull(todos.get(1).getDescription());
}
@Test
public void removeItemFromList() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
todos.add(new Todo(3L, "C", false));
new RemoveOperation("/1").perform(todos, Todo.class);
assertEquals(2, todos.size());
assertEquals("A", todos.get(0).getDescription());
assertEquals("C", todos.get(1).getDescription());

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.
@@ -24,11 +24,11 @@ import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ReplaceOperationTest {
public class ReplaceOperationTests {
@Test
public void replaceBooleanPropertyValue() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
@@ -42,7 +42,7 @@ public class ReplaceOperationTest {
@Test
public void replaceTextPropertyValue() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
@@ -56,7 +56,7 @@ public class ReplaceOperationTest {
@Test
public void replaceTextPropertyValueWithANumber() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", false));
@@ -76,8 +76,9 @@ public class ReplaceOperationTest {
Todo todo = new Todo(1L, "A", false);
ObjectMapper mapper = new ObjectMapper();
ReplaceOperation replace = new ReplaceOperation("/type",
new JsonLateObjectEvaluator(new ObjectMapper().readTree("{ \"value\" : \"new\" }")));
new JsonLateObjectEvaluator(mapper, mapper.readTree("{ \"value\" : \"new\" }")));
replace.perform(todo, Todo.class);
assertNotNull(todo.getType());

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.
@@ -20,16 +20,16 @@ import java.util.List;
import org.junit.Test;
public class TestOperationTest {
public class TestOperationTests {
@Test
public void testPropertyValueEquals() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", true));
todos.add(new Todo(3L, "C", false));
TestOperation test = new TestOperation("/0/complete", false);
test.perform(todos, Todo.class);
@@ -38,28 +38,27 @@ public class TestOperationTest {
}
@Test(expected=PatchException.class)
@Test(expected = PatchException.class)
public void testPropertyValueNotEquals() throws Exception {
// initial Todo list
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", true));
todos.add(new Todo(3L, "C", false));
TestOperation test = new TestOperation("/0/complete", true);
test.perform(todos, Todo.class);
}
@Test
public void testListElementEquals() throws Exception {
List<Todo> todos = new ArrayList<Todo>();
todos.add(new Todo(1L, "A", false));
todos.add(new Todo(2L, "B", true));
todos.add(new Todo(3L, "C", false));
TestOperation test = new TestOperation("/1", new Todo(2L, "B", true));
test.perform(todos, Todo.class);
}
}

View File

@@ -15,9 +15,12 @@
*/
package org.springframework.data.rest.webmvc.json.patch;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class TodoList implements Serializable {
private static final long serialVersionUID = 1L;
@@ -25,29 +28,4 @@ public class TodoList implements Serializable {
private List<Todo> todos;
private Todo[] todoArray;
private String name;
public List<Todo> getTodos() {
return todos;
}
public void setTodos(List<Todo> todos) {
this.todos = todos;
}
public Todo[] getTodoArray() {
return todoArray;
}
public void setTodoArray(Todo[] todoArray) {
this.todoArray = todoArray;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}