Browse Source

extract getCompleteUser method

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
7821fb47e2
  1. 51
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java
  2. 53
      fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java

51
fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java

@ -32,39 +32,36 @@ public class UserController
return response; 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", get("/users",
(request, response) -> (request, response) ->

53
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; package de.fd.fh.server.user.web;
import com.fasterxml.jackson.core.JsonProcessingException; 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 de.fd.fh.server.user.UserService;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -24,6 +27,8 @@ class UserControllerTest
@Mock @Mock
Response response; Response response;
@Mock @Mock
Session session;
@Mock
UserService service; UserService service;
@BeforeEach @BeforeEach
@ -67,4 +72,52 @@ class UserControllerTest
then(service).should().getSmallPlayer(any()); then(service).should().getSmallPlayer(any());
then(service).shouldHaveNoMoreInteractions(); 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<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class);
final ArgumentCaptor<String> bodyCapture = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<String> 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<Integer> 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();
}
} }
Loading…
Cancel
Save