You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.3 KiB

  1. package de.fd.fh.server.game.web;
  2. import de.fd.fh.server.game.Game;
  3. import de.fd.fh.server.game.GameId;
  4. import de.fd.fh.server.game.GameService;
  5. import org.junit.jupiter.api.BeforeEach;
  6. import org.junit.jupiter.api.Test;
  7. import org.mockito.ArgumentCaptor;
  8. import org.mockito.Mock;
  9. import org.mockito.MockitoAnnotations;
  10. import spark.Request;
  11. import spark.Response;
  12. import java.io.IOException;
  13. import static org.junit.jupiter.api.Assertions.*;
  14. import static org.mockito.ArgumentMatchers.any;
  15. import static org.mockito.Mockito.verify;
  16. import static org.mockito.Mockito.when;
  17. public class GameControllerTest
  18. {
  19. @Mock
  20. Request request;
  21. @Mock
  22. Response response;
  23. @Mock
  24. GameService service;
  25. @BeforeEach
  26. void before()
  27. {
  28. MockitoAnnotations.openMocks(this);
  29. }
  30. @Test
  31. void testCreateGame() throws IOException
  32. {
  33. when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}");
  34. when(service.createGame(any(), any())).thenReturn(GameId.of("56789"));
  35. final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
  36. final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
  37. new GameController(service).createGame(request, response);
  38. verify(response).body(bodyCaptor.capture());
  39. verify(response).status(statusCaptor.capture());
  40. assertNotNull(bodyCaptor.getValue());
  41. assertTrue(bodyCaptor.getValue().contains("56789"));
  42. assertNotNull(statusCaptor.getValue());
  43. assertEquals(201, statusCaptor.getValue());
  44. }
  45. @Test
  46. void testFailedCreateGame() throws IOException
  47. {
  48. when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}");
  49. when(service.createGame(any(), any())).thenReturn(null);
  50. final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
  51. new GameController(service).createGame(request, response);
  52. verify(response).status(statusCaptor.capture());
  53. assertNotNull(statusCaptor.getValue());
  54. assertEquals(400, statusCaptor.getValue());
  55. }
  56. @Test
  57. void testFindGameById() throws IOException
  58. {
  59. when(request.params(any())).thenReturn("12345");
  60. when(service.findGameById(any())).thenReturn(new Game());
  61. final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
  62. final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
  63. new GameController(service).finGameById(request, response);
  64. verify(response).body(bodyCaptor.capture());
  65. verify(response).status(statusCaptor.capture());
  66. assertNotNull(bodyCaptor.getValue());
  67. assertNotNull(statusCaptor.getValue());
  68. assertEquals(200, statusCaptor.getValue());
  69. }
  70. @Test
  71. void testFailedFindGameById() throws IOException
  72. {
  73. when(request.params(any())).thenReturn("12345");
  74. when(service.findGameById(any())).thenReturn(null);
  75. final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
  76. new GameController(service).finGameById(request, response);
  77. verify(response).status(statusCaptor.capture());
  78. assertNotNull(statusCaptor.getValue());
  79. assertEquals(400, statusCaptor.getValue());
  80. }
  81. }