Browse Source

extract getUser method

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
45766f9070
  1. 34
      fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java
  2. 70
      fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java

34
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);
}
}

70
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<String> 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<Integer> 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();
}
}
Loading…
Cancel
Save