Commit 44ee0ad2 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Added todoSettings to json format for TodoModel

parent b21a9d62
......@@ -11,12 +11,15 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import todolist.core.AbstractTodoList;
import todolist.core.TodoModel;
import todolist.core.TodoSettings;
class TodoModelDeserializer extends JsonDeserializer<TodoModel> {
private TodoListDeserializer todoListDeserializer = new TodoListDeserializer();
private TodoSettingsDeserializer todoSettingsDeserializer = new TodoSettingsDeserializer();
/*
* format: { "lists": [ ... ] }
* format: { "lists": [ ... ], "settings": ... }
*/
@Override
......@@ -38,6 +41,10 @@ class TodoModelDeserializer extends JsonDeserializer<TodoModel> {
}
}
}
if (objectNode.has("settings")) {
TodoSettings settings = todoSettingsDeserializer.deserialize(objectNode.get("settings"));
model.setSettings(settings);
}
return model;
}
return null;
......
......@@ -42,6 +42,8 @@ class TodoModelSerializer extends JsonSerializer<TodoModel> {
}
}
jsonGen.writeEndArray();
jsonGen.writeFieldName("settings");
jsonGen.writeObject(model.getSettings());
jsonGen.writeEndObject();
}
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ public class TodoModuleTest {
mapper = TodoPersistence.createObjectMapper();
}
private final static String todoListWithTwoItems = """
final static String todoListWithTwoItemsAndSettings = """
{
"lists": [
{
......@@ -45,12 +45,14 @@ public class TodoModuleTest {
}
]
}
]
],
"settings" : {
"todoItemsSortOrder" : "CHECKED_UNCHECKED"
}
}
""";
@Test
public void testSerializers() {
static TodoModel createTodoListWithTwoItemsAndSettings() {
TodoModel model = new TodoModel();
TodoList list = new TodoList("todo");
model.addTodoList(list);
......@@ -62,8 +64,15 @@ public class TodoModuleTest {
item2.setDeadline(LocalDateTime.parse("2020-10-01T14:53:11"));
list.addTodoItem(item1);
list.addTodoItem(item2);
model.getSettings().setTodoItemsSortOrder(TodoItemsSortOrder.CHECKED_UNCHECKED);
return model;
}
@Test
public void testSerializers() {
TodoModel model = createTodoListWithTwoItemsAndSettings();
try {
assertEquals(todoListWithTwoItems.replaceAll("\\s+", ""), mapper.writeValueAsString(model));
assertEquals(todoListWithTwoItemsAndSettings.replaceAll("\\s+", ""), mapper.writeValueAsString(model));
} catch (JsonProcessingException e) {
fail(e.getMessage());
}
......@@ -82,7 +91,7 @@ public class TodoModuleTest {
@Test
public void testDeserializers() {
try {
TodoModel model = mapper.readValue(todoListWithTwoItems, TodoModel.class);
TodoModel model = mapper.readValue(todoListWithTwoItemsAndSettings, TodoModel.class);
assertTrue(model.iterator().hasNext());
AbstractTodoList list = model.iterator().next();
assertEquals("todo", list.getName());
......@@ -92,6 +101,7 @@ public class TodoModuleTest {
assertTrue(it.hasNext());
checkTodoItem(it.next(), "item2", true, LocalDateTime.parse("2020-10-01T14:53:11"));
assertFalse(it.hasNext());
assertEquals(TodoItemsSortOrder.CHECKED_UNCHECKED, model.getSettings().getTodoItemsSortOrder());
} catch (JsonProcessingException e) {
fail(e.getMessage());
}
......
......@@ -14,6 +14,7 @@ import todolist.core.AbstractTodoList;
import todolist.core.TodoItem;
import todolist.core.TodoList;
import todolist.core.TodoModel;
import todolist.core.TodoSettings.TodoItemsSortOrder;
public class TodoPersistenceTest {
......@@ -32,6 +33,7 @@ public class TodoPersistenceTest {
item2.setChecked(true);
item2.setDeadline(LocalDateTime.parse("2020-10-01T14:53:11"));
list.addTodoItem(item2);
model.getSettings().setTodoItemsSortOrder(TodoItemsSortOrder.CHECKED_UNCHECKED);
try {
StringWriter writer = new StringWriter();
todoPersistence.writeTodoModel(model, writer);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment