Browse Source

extract updateUser method

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

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

@ -58,20 +58,19 @@ public class UserController
return response;
}
public UserController(final UserService service)
{
this.service = service;
post("/users", this::getCompleteUser);
get("/users",
(request, response) ->
public Response updateUser(Request request, Response response) throws java.io.IOException
{
final UserId userId =
((AccessToken) request.session()
.attribute("userId"))
.getUserId();
final User user = service.getPlayer(userId);
final ChangeUserRequest message = objectMapper.readValue(request.body(),
ChangeUserRequest.class);
final User user = service.changePlayer(
userId,
message);
if (user == null)
{
@ -82,12 +81,20 @@ public class UserController
response.status(200);
response.type("application/json");
return objectMapper.writeValueAsString(user);
response.body(objectMapper.writeValueAsString(user));
return response;
}
return response;
}
);
public UserController(final UserService service)
{
this.service = service;
post("/users", this::getCompleteUser);
get("/users", this::updateUser);
get("/users/:user_id", this::getUser);
}

51
fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java

@ -14,6 +14,8 @@ import spark.Request;
import spark.Response;
import spark.Session;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
@ -120,4 +122,53 @@ class UserControllerTest
then(service).shouldHaveNoMoreInteractions();
}
@Test
void updateUserTest() throws IOException
{
when(request.session()).thenReturn(session);
when(session.attribute(any())).thenReturn(
new AccessToken(null, null, null, UserId.of("12345")));
when(request.body()).thenReturn("{\"name\":\"TestName\",\"password\":\"TestPassword\"}");
when(service.changePlayer(any(), any())).thenReturn(new User(UserId.of("12345"), "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).updateUser(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\":{\"identifier\":\"12345\"},\"name\":\"TestName\"}", bodyCapture.getValue(), "Should return user as json.");
then(service).should().changePlayer(any(), any());
then(service).shouldHaveNoMoreInteractions();
}
@Test
void failedUpdateUserTest() throws IOException
{
when(request.session()).thenReturn(session);
when(session.attribute(any())).thenReturn(
new AccessToken(null, null, null, UserId.of("12345")));
when(request.body()).thenReturn("{\"name\":\"TestName\",\"password\":\"TestPassword\"}");
when(service.changePlayer(any(), any())).thenReturn(null);
final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class);
final Response result = new UserController(service).updateUser(request, response);
verify(response).status(statusCapture.capture());
assertEquals(response, result, "Should return the incoming Response");
assertEquals(Integer.valueOf(400), statusCapture.getValue(), "Should return status code 400.");
then(service).should().changePlayer(any(), any());
then(service).shouldHaveNoMoreInteractions();
}
}
Loading…
Cancel
Save