Browse Source

add findRunningGamesOfUser-Method and Test

feat-serverGameRepresentation
Steffen Nitschke 4 years ago
parent
commit
654d69f218
  1. 25
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/web/GameController.java
  2. 44
      fh.fd.ci.server/src/test/java/de/fd/fh/server/game/web/GameControllerTest.java

25
fh.fd.ci.server/src/main/java/de/fd/fh/server/game/web/GameController.java

@ -1,5 +1,6 @@
package de.fd.fh.server.game.web; package de.fd.fh.server.game.web;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import de.fd.fh.server.game.Game; import de.fd.fh.server.game.Game;
import de.fd.fh.server.game.GameId; import de.fd.fh.server.game.GameId;
@ -11,6 +12,7 @@ import spark.Request;
import spark.Response; import spark.Response;
import java.io.IOException; import java.io.IOException;
import java.util.List;
@RequiredArgsConstructor @RequiredArgsConstructor
public class GameController public class GameController
@ -39,15 +41,15 @@ public class GameController
} }
} }
public void finGameById(final Request request, final Response response) throws IOException
public void findGameById(final Request request, final Response response) throws JsonProcessingException
{ {
final GameId gameId = GameId.of(request.params(":gameId"));
final GameId gameId = GameId.of(request.params(":game_id"));
final Game game = service.findGameById(gameId); final Game game = service.findGameById(gameId);
if (game == null) if (game == null)
{ {
response.status(400);
response.status(404);
} }
else else
{ {
@ -55,4 +57,21 @@ public class GameController
response.status(200); response.status(200);
} }
} }
public void findRunningGamesOfUser(final Request request, final Response response) throws JsonProcessingException
{
final UserId userId = UserId.of(request.params(":user_id"));
final List<Game> games = service.findRunningGamesOfUser(userId);
if (games == null)
{
response.status(404);
}
else
{
response.body(objectMapper.writeValueAsString(games));
response.status(200);
}
}
} }

44
fh.fd.ci.server/src/test/java/de/fd/fh/server/game/web/GameControllerTest.java

@ -1,5 +1,6 @@
package de.fd.fh.server.game.web; package de.fd.fh.server.game.web;
import com.fasterxml.jackson.core.JsonProcessingException;
import de.fd.fh.server.game.Game; import de.fd.fh.server.game.Game;
import de.fd.fh.server.game.GameId; import de.fd.fh.server.game.GameId;
import de.fd.fh.server.game.GameService; import de.fd.fh.server.game.GameService;
@ -12,6 +13,7 @@ import spark.Request;
import spark.Response; import spark.Response;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@ -79,7 +81,7 @@ public class GameControllerTest
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class); final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).finGameById(request, response);
new GameController(service).findGameById(request, response);
verify(response).body(bodyCaptor.capture()); verify(response).body(bodyCaptor.capture());
verify(response).status(statusCaptor.capture()); verify(response).status(statusCaptor.capture());
@ -98,11 +100,47 @@ public class GameControllerTest
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).finGameById(request, response);
new GameController(service).findGameById(request, response);
verify(response).status(statusCaptor.capture()); verify(response).status(statusCaptor.capture());
assertNotNull(statusCaptor.getValue()); assertNotNull(statusCaptor.getValue());
assertEquals(400, statusCaptor.getValue());
assertEquals(404, statusCaptor.getValue());
}
@Test
void testFindRunningGamesOfUser() throws JsonProcessingException
{
when(request.params(any())).thenReturn("12345");
when(service.findRunningGamesOfUser(any())).thenReturn(List.of(new Game(), new Game()));
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).findRunningGamesOfUser(request, response);
verify(response).body(bodyCaptor.capture());
verify(response).status(statusCaptor.capture());
assertNotNull(bodyCaptor.getValue());
assertNotNull(statusCaptor.getValue());
assertEquals(200, statusCaptor.getValue());
}
@Test
void testFailedFindRunningGamesOfUser() throws JsonProcessingException
{
when(request.params(any())).thenReturn("12345");
when(service.findRunningGamesOfUser(any())).thenReturn(null);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).findRunningGamesOfUser(request, response);
verify(response).status(statusCaptor.capture());
assertNotNull(statusCaptor.getValue());
assertEquals(404, statusCaptor.getValue());
} }
} }
Loading…
Cancel
Save