Commit 59e827cb authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Removed spring boot and fixed spotbugs issue

parent 38783768
......@@ -45,8 +45,6 @@ public class TodoListController {
return todoList;
}
ContextMenu cm;
/**
* Sets the TodoList managed by this controller.
* The corresponding views will be updated.
......
......@@ -162,8 +162,5 @@
<module>fxui</module>
<module>rest</module>
<module>integrationtests</module>
<!--
<module>springboot/restserver</module>
-->
</modules>
</project>
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>it1901.todolist</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<artifactId>springboot.restserver</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>it1901.todolist</groupId>
<artifactId>core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<!-- Her kommer web-server-avhengighetene -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.4</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<!-- Run the checkstyle code quality tool -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<!-- Run the spotbugs code quality tool -->
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
</plugin>
<!-- Configure jacoco code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.4</version>
</plugin>
</plugins>
</build>
</project>
module todolist.springboot.rest {
requires com.fasterxml.jackson.databind;
requires spring.web;
requires spring.beans;
requires spring.boot;
requires spring.boot.autoconfigure;
requires todolist.core;
}
package todolist.springboot.restserver;
import com.fasterxml.jackson.databind.Module;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import todolist.json.TodoPersistence;
import todolist.json.internal.TodoModule;
@SpringBootApplication
public class TodoModelApplication {
@Bean
public Module objectMapperModule() {
return TodoPersistence.createJacksonModule(false);
}
public static void main(String[] args) {
SpringApplication.run(TodoModelApplication.class, args);
}
}
package todolist.springboot.restserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import todolist.core.AbstractTodoList;
import todolist.core.TodoModel;
@RestController
@RequestMapping(TodoModelController.TODO_MODEL_SERVICE_PATH)
public class TodoModelController {
public static final String TODO_MODEL_SERVICE_PATH = "springboot/todo";
@Autowired
private TodoModelService todoModelService;
@GetMapping
public TodoModel getTodoModel() {
return todoModelService.getTodoModel();
}
private void autoSaveTodoModel() {
todoModelService.autoSaveTodoModel();
}
private void checkTodoList(AbstractTodoList todoList, String name) {
if (todoList == null) {
throw new IllegalArgumentException("No TodoList named \"" + name + "\"");
}
}
/**
* Gets the corresponding TodoList.
*
* @param name the name of the TodoList
* @return the corresponding TodoList
*/
@GetMapping(path = "/{name}")
public AbstractTodoList getTodoList(@PathVariable("name") String name) {
AbstractTodoList todoList = getTodoModel().getTodoList(name);
checkTodoList(todoList, name);
return todoList;
}
/**
* Replaces or adds a TodoList.
*
* @param name the name of the TodoList
* @param todoList the todoList to add
* @return true if it was added, false if it replaced
*/
@PutMapping(path = "/{name}")
public boolean putTodoList(@PathVariable("name") String name,
@RequestBody AbstractTodoList todoList) {
boolean added = getTodoModel().putTodoList(todoList) == null;
autoSaveTodoModel();
return added;
}
/**
* Renames the TodoList.
*
* @param name the name of the TodoList
* @param newName the new name
*/
@PostMapping(path = "/{name}/rename")
public boolean renameTodoList(@PathVariable("name") String name,
@RequestParam("newName") String newName) {
AbstractTodoList todoList = getTodoModel().getTodoList(name);
checkTodoList(todoList, name);
if (getTodoModel().getTodoList(newName) != null) {
throw new IllegalArgumentException("A TodoList named \"" + newName + "\" already exists");
}
todoList.setName(newName);
autoSaveTodoModel();
return true;
}
/**
* Removes the TodoList.
*
* @param name the name of the TodoList
*/
@DeleteMapping(path = "/{name}")
public boolean removeTodoList(@PathVariable("name") String name) {
AbstractTodoList todoList = getTodoModel().getTodoList(name);
checkTodoList(todoList, name);
getTodoModel().removeTodoList(todoList);
autoSaveTodoModel();
return true;
}
}
package todolist.springboot.restserver;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import org.springframework.stereotype.Service;
import todolist.core.TodoList;
import todolist.core.TodoModel;
import todolist.json.TodoPersistence;
@Service
public class TodoModelService {
private TodoModel todoModel;
private TodoPersistence todoPersistence;
public TodoModelService(TodoModel todoModel) {
this.todoModel = todoModel;
this.todoPersistence = new TodoPersistence();
todoPersistence.setSaveFile("springbootserver-todolist.json");
}
public TodoModelService() {
this(createDefaultTodoModel());
}
public TodoModel getTodoModel() {
return todoModel;
}
public void setTodoModel(TodoModel todoModel) {
this.todoModel = todoModel;
}
private static TodoModel createDefaultTodoModel() {
TodoPersistence todoPersistence = new TodoPersistence();
URL url = TodoModelService.class.getResource("default-todomodel.json");
if (url != null) {
try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) {
return todoPersistence.readTodoModel(reader);
} catch (IOException e) {
System.out.println("Couldn't read default-todomodel.json, so rigging TodoModel manually ("
+ e + ")");
}
}
TodoModel todoModel = new TodoModel();
todoModel.addTodoList(new TodoList("todo1"));
todoModel.addTodoList(new TodoList("todo2"));
return todoModel;
}
public void autoSaveTodoModel() {
if (todoPersistence != null) {
try {
todoPersistence.saveTodoModel(this.todoModel);
} catch (IllegalStateException | IOException e) {
System.err.println("Couldn't auto-save TodoModel: " + e);
}
}
}
}
\ No newline at end of file
package todolist.springboot.restserver;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.Iterator;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import todolist.core.AbstractTodoList;
import todolist.core.TodoModel;
import todolist.json.TodoPersistence;
@AutoConfigureMockMvc
@ContextConfiguration(classes = { TodoModelController.class, TodoModelService.class, TodoModelApplication.class })
@WebMvcTest
public class TodoModelApplicationTest {
@Autowired
private MockMvc mockMvc;
private ObjectMapper objectMapper;
@BeforeEach
public void setup() throws Exception {
objectMapper = TodoPersistence.createObjectMapper();
}
private String todoUrl(String... segments) {
String url = "/" + TodoModelController.TODO_MODEL_SERVICE_PATH;
for (String segment : segments) {
url = url + "/" + segment;
}
return url;
}
@Test
public void testGet_todo() throws Exception {
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get(todoUrl())
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
try {
TodoModel todoModel = objectMapper.readValue(result.getResponse().getContentAsString(), TodoModel.class);
Iterator<AbstractTodoList> it = todoModel.iterator();
assertTrue(it.hasNext());
AbstractTodoList todoList1 = it.next();
assertTrue(it.hasNext());
AbstractTodoList todoList2 = it.next();
assertFalse(it.hasNext());
assertEquals("todo1", todoList1.getName());
assertEquals("todo2", todoList2.getName());
} catch (JsonProcessingException e) {
fail(e.getMessage());
}
}
}
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