From 45766f907042512c042772e2f5a773346bf2b383 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 03:26:59 +0100 Subject: [PATCH] extract getUser method --- .../fd/fh/server/user/web/UserController.java | 34 +++++---- .../server/user/web/UserControllerTest.java | 70 +++++++++++++++++++ 2 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java index 67a932d..4f43bba 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java @@ -1,10 +1,13 @@ package de.fd.fh.server.user.web; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import de.fd.fh.server.access.AccessToken; import de.fd.fh.server.user.User; import de.fd.fh.server.user.UserId; import de.fd.fh.server.user.UserService; +import spark.Request; +import spark.Response; import static spark.Spark.get; import static spark.Spark.post; @@ -13,8 +16,25 @@ public class UserController { private ObjectMapper objectMapper = new ObjectMapper(); + private final UserService service; + + public Response getUser(Request request, Response response) throws JsonProcessingException + { + final UserId userId = UserId.of(request.params(":user_id")); + final UserRequest user = service.getSmallPlayer(userId); + + if (user == null) + { + response.status(404); + return response; + } + response.body(objectMapper.writeValueAsString(user)); + return response; + } + public UserController(final UserService service) { + this.service = service; post("/users", ((request, response) -> { @@ -72,18 +92,6 @@ public class UserController } ); - get("/users/:user_id", - (request, response) -> - { - final UserId userId = UserId.of(request.params(":user_id")); - final UserRequest user = service.getSmallPlayer(userId); - - if (user == null) - { - response.status(404); - } - response.body(objectMapper.writeValueAsString(user)); - return response; - }); + get("/users/:user_id", this::getUser); } } diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java new file mode 100644 index 0000000..452814a --- /dev/null +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java @@ -0,0 +1,70 @@ +package de.fd.fh.server.user.web; + +import com.fasterxml.jackson.core.JsonProcessingException; +import de.fd.fh.server.user.UserService; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import spark.Request; +import spark.Response; +import spark.Session; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class UserControllerTest +{ + @Mock + Request request; + @Mock + Response response; + @Mock + UserService service; + + @BeforeEach + void before() + { + MockitoAnnotations.openMocks(this); + } + + @Test + void getUserTest() throws JsonProcessingException + { + when(request.params(any())).thenReturn("12345"); + when(service.getSmallPlayer(any())).thenReturn(new UserRequest("12345", "TestName")); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + + final Response result = new UserController(service).getUser(request, response); + + verify(response).body(captor.capture()); + + assertEquals(response, result, "Should return the incomming response."); + assertEquals("{\"id\":\"12345\",\"name\":\"TestName\"}", captor.getValue(), + "Should return UserRequest in Body."); + then(service).should().getSmallPlayer(any()); + } + + @Test + void failedGetUserTest() throws JsonProcessingException + { + when(request.params(any())).thenReturn("12345"); + when(service.getSmallPlayer(any())).thenReturn(null); + + final ArgumentCaptor statusCaptor = ArgumentCaptor.forClass(Integer.class); + + final Response result = new UserController(service).getUser(request, response); + + verify(response).status(statusCaptor.capture()); + + assertEquals(response, result, "Should return the incomming response."); + assertEquals(Integer.valueOf(404), statusCaptor.getValue(), "Should return not found status code."); + then(service).should().getSmallPlayer(any()); + then(service).shouldHaveNoMoreInteractions(); + } +} \ No newline at end of file