Browse Source

add findByGameId-Method and Test

feat-serverGameRepresentation
Steffen Nitschke 4 years ago
parent
commit
750c6bfd2d
  1. 18
      fh.fd.ci.server/src/main/java/de/fd/fh/server/game/web/GameController.java
  2. 43
      fh.fd.ci.server/src/test/java/de/fd/fh/server/game/web/GameControllerTest.java

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

@ -1,6 +1,7 @@
package de.fd.fh.server.game.web; package de.fd.fh.server.game.web;
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.GameId; import de.fd.fh.server.game.GameId;
import de.fd.fh.server.game.GameService; import de.fd.fh.server.game.GameService;
import de.fd.fh.server.user.UserId; import de.fd.fh.server.user.UserId;
@ -37,4 +38,21 @@ public class GameController
response.status(201); response.status(201);
} }
} }
public void finGameById(final Request request, final Response response) throws IOException
{
final GameId gameId = GameId.of(request.params(":gameId"));
final Game game = service.findGameById(gameId);
if (game == null)
{
response.status(400);
}
else
{
response.body(objectMapper.writeValueAsString(game));
response.status(200);
}
}
} }

43
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 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;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -38,8 +39,8 @@ public class GameControllerTest
when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}"); when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}");
when(service.createGame(any(), any())).thenReturn(GameId.of("56789")); when(service.createGame(any(), any())).thenReturn(GameId.of("56789"));
ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).createGame(request, response); new GameController(service).createGame(request, response);
@ -59,7 +60,7 @@ public class GameControllerTest
when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}"); when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}");
when(service.createGame(any(), any())).thenReturn(null); when(service.createGame(any(), any())).thenReturn(null);
ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).createGame(request, response); new GameController(service).createGame(request, response);
@ -68,4 +69,40 @@ public class GameControllerTest
assertNotNull(statusCaptor.getValue()); assertNotNull(statusCaptor.getValue());
assertEquals(400, statusCaptor.getValue()); assertEquals(400, statusCaptor.getValue());
} }
@Test
void testFindGameById() throws IOException
{
when(request.params(any())).thenReturn("12345");
when(service.findGameById(any())).thenReturn(new Game());
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).finGameById(request, response);
verify(response).body(bodyCaptor.capture());
verify(response).status(statusCaptor.capture());
assertNotNull(bodyCaptor.getValue());
assertNotNull(statusCaptor.getValue());
assertEquals(200, statusCaptor.getValue());
}
@Test
void testFailedFindGameById() throws IOException
{
when(request.params(any())).thenReturn("12345");
when(service.findGameById(any())).thenReturn(null);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).finGameById(request, response);
verify(response).status(statusCaptor.capture());
assertNotNull(statusCaptor.getValue());
assertEquals(400, statusCaptor.getValue());
}
} }
Loading…
Cancel
Save