From 7821fb47e21cd01dece5415e4fa5e2c816d48352 Mon Sep 17 00:00:00 2001 From: Steffen Nitschke Date: Sun, 31 Jan 2021 03:31:19 +0100 Subject: [PATCH] extract getCompleteUser method --- .../fd/fh/server/user/web/UserController.java | 51 +++++++++--------- .../server/user/web/UserControllerTest.java | 53 +++++++++++++++++++ 2 files changed, 77 insertions(+), 27 deletions(-) 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 4f43bba..8c225ba 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 @@ -32,39 +32,36 @@ public class UserController return response; } - public UserController(final UserService service) + public Response getCompleteUser(Request request, Response response) throws JsonProcessingException { - this.service = service; - post("/users", - ((request, response) -> - { - final UserId userId = - ((AccessToken) request.session() - .attribute("userId")) - .getUserId(); + final UserId userId = + ((AccessToken) request.session() + .attribute("userId")) + .getUserId(); - final ChangeUserRequest message = objectMapper.readValue(request.body(), - ChangeUserRequest.class); + final User user = service.getPlayer(userId); - final User user = service.changePlayer( - userId, - message); + if (user == null) + { + response.status(404); + } + else + { + response.status(200); + response.type("application/json"); - if (user == null) - { - response.status(400); - } - else - { - response.status(200); - response.type("application/json"); + response.body(objectMapper.writeValueAsString(user)); - return objectMapper.writeValueAsString(user); - } + return response; + } - return response; - } - )); + return response; + } + + public UserController(final UserService service) + { + this.service = service; + post("/users", this::getCompleteUser); get("/users", (request, response) -> 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 index 452814a..5ac91c7 100644 --- 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 @@ -1,6 +1,9 @@ package de.fd.fh.server.user.web; import com.fasterxml.jackson.core.JsonProcessingException; +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 org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -24,6 +27,8 @@ class UserControllerTest @Mock Response response; @Mock + Session session; + @Mock UserService service; @BeforeEach @@ -67,4 +72,52 @@ class UserControllerTest then(service).should().getSmallPlayer(any()); then(service).shouldHaveNoMoreInteractions(); } + + @Test + void getCompleteUserTest() throws JsonProcessingException + { + when(request.session()).thenReturn(session); + when(session.attribute(any())).thenReturn( + new AccessToken(null, null, null, UserId.of("12345"))); + when(service.getPlayer(any(UserId.class))).thenReturn(User.of("TestName")); + + final ArgumentCaptor statusCapture = ArgumentCaptor.forClass(Integer.class); + final ArgumentCaptor bodyCapture = ArgumentCaptor.forClass(String.class); + final ArgumentCaptor typeCapture = ArgumentCaptor.forClass(String.class); + + final Response result = new UserController(service).getCompleteUser(request, response); + + verify(response).status(statusCapture.capture()); + verify(response).type(typeCapture.capture()); + verify(response).body(bodyCapture.capture()); + + assertEquals(response, result, "Should return the incoming Response"); + assertEquals(Integer.valueOf(200), statusCapture.getValue(), "Should return status code 200."); + assertEquals("application/json", typeCapture.getValue(), "Should have content type json."); + assertEquals("{\"id\":null,\"name\":\"TestName\"}", bodyCapture.getValue(), "Should return user as json."); + + then(service).should().getPlayer(any()); + then(service).shouldHaveNoMoreInteractions(); + } + + @Test + void failedGetCompleteUserTest() throws JsonProcessingException + { + when(request.session()).thenReturn(session); + when(session.attribute(any())).thenReturn( + new AccessToken(null, null, null, UserId.of("12345"))); + when(service.getPlayer(any(UserId.class))).thenReturn(null); + + final ArgumentCaptor capture = ArgumentCaptor.forClass(Integer.class); + + final Response result = new UserController(service).getCompleteUser(request, response); + + verify(response).status(capture.capture()); + + assertEquals(response, result, "Should return the incoming Response"); + assertEquals(Integer.valueOf(404), capture.getValue(), "Should return status code 200."); + then(service).should().getPlayer(any()); + then(service).shouldHaveNoMoreInteractions(); + } + } \ No newline at end of file