Skip to content
Snippets Groups Projects
Commit 2ebf4a21 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Rigget opp rest-api og -server (#14)

parent cd482c3a
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,6 @@
<jerseyVersion>2.28</jerseyVersion>
</properties>
<dependencies>
<dependency>
<groupId>it1901.todolist</groupId>
......@@ -58,6 +57,9 @@
<artifactId>mockito-core</artifactId>
</dependency>
<!-- Her kommer web-server-avhengighetene -->
<!-- Logging med slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
......@@ -69,11 +71,7 @@
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jerseyVersion}</version>
</dependency>
<!-- Kompileringsavhengigheter for Jersey -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
......@@ -90,6 +88,33 @@
<version>${jerseyVersion}</version>
</dependency>
<!-- Støtte for testing med Jersey og Grizzly2 -->
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-util</artifactId>
<version>${jerseyVersion}</version>
<scope>test</scope>
</dependency>
<!-- See https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/deployment.html for various server options -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>${jerseyVersion}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jerseyVersion}</version>
<scope>test</scope>
</dependency>
<!-- Kjøretidsavhengigheter for Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jerseyVersion}</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
......
......@@ -10,19 +10,22 @@ import todolist.restapi.TodoModelService;
public class TodoConfig extends ResourceConfig {
private TodoModel todoModel;
/**
* Initialize this TodoConfig.
*
* @param todoModel todoModel instance to serve
*/
public TodoConfig(TodoModel todoModel) {
setTodoModel(todoModel);
register(TodoModelService.class);
register(TodoModuleObjectMapperProvider.class);
register(JacksonFeature.class);
register(new AbstractBinder() {
@Override
protected void configure() {
bind(todoModel);
bind(TodoConfig.this.todoModel);
}
});
}
......@@ -34,6 +37,14 @@ public class TodoConfig extends ResourceConfig {
this(createDefaultTodoModel());
}
public TodoModel getTodoModel() {
return todoModel;
}
public void setTodoModel(TodoModel todoModel) {
this.todoModel = todoModel;
}
private static TodoModel createDefaultTodoModel() {
TodoModel todoModel = new TodoModel();
TodoList todoList1 = new TodoList();
......
package todolist.restserver;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.Iterator;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import todolist.core.TodoList;
import todolist.core.TodoModel;
import todolist.restapi.TodoModelService;
import todolist.restapi.TodoModuleObjectMapperProvider;
public class TodoServiceTest extends JerseyTest {
protected boolean shouldLog() {
return false;
}
@Override
protected ResourceConfig configure() {
final TodoConfig config = new TodoConfig();
if (shouldLog()) {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
config.property(LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL_SERVER, "WARNING");
}
System.out.println(config.getTodoModel());
return config;
}
@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new GrizzlyTestContainerFactory();
}
private ObjectMapper objectMapper;
@Override
@BeforeEach
public void setUp() throws Exception {
super.setUp();
objectMapper = new TodoModuleObjectMapperProvider().getContext(getClass());
}
@Test
public void testGet_todo() {
Response getResponse = target(TodoModelService.TODO_MODEL_SERVICE_PATH)
.request(MediaType.APPLICATION_JSON + ";" + MediaType.CHARSET_PARAMETER + "=UTF-8").get();
assertEquals(200, getResponse.getStatus());
try {
TodoModel todoModel = objectMapper.readValue(getResponse.readEntity(String.class), TodoModel.class);
Iterator<TodoList> it = todoModel.iterator();
assertTrue(it.hasNext());
TodoList todoList1 = it.next();
assertTrue(it.hasNext());
TodoList todoList2 = it.next();
assertFalse(todoList1.iterator().hasNext());
assertFalse(todoList2.iterator().hasNext());
} catch (JsonProcessingException e) {
fail(e.getMessage());
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment