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.

215 lines
6.6 KiB

  1. package de.fd.fh;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import de.fd.fh.server.ApiTestUtils;
  4. import de.fd.fh.server.access.web.AccessController;
  5. import de.fd.fh.server.game.web.GameController;
  6. import de.fd.fh.server.user.web.UserController;
  7. import de.fd.fh.shared.Utils;
  8. import org.junit.jupiter.api.AfterAll;
  9. import org.junit.jupiter.api.BeforeAll;
  10. import org.junit.jupiter.api.Test;
  11. import spark.Response;
  12. import java.io.IOException;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import static org.junit.jupiter.api.Assertions.*;
  16. import static org.mockito.ArgumentMatchers.any;
  17. import static org.mockito.BDDMockito.then;
  18. import static org.mockito.Mockito.mock;
  19. import static org.mockito.Mockito.when;
  20. import static spark.Spark.awaitInitialization;
  21. import static spark.Spark.stop;
  22. class ServerAppTest
  23. {
  24. private static UserController userController = mock(UserController.class);
  25. private static AccessController accessController = mock(AccessController.class);
  26. private static GameController gameController = mock(GameController.class);
  27. @BeforeAll
  28. static void before()
  29. {
  30. ServerApp.initController(accessController, userController, gameController);
  31. ServerApp.main(null);
  32. awaitInitialization();
  33. }
  34. @AfterAll
  35. static void after()
  36. {
  37. stop();
  38. }
  39. @Test
  40. void testHalloWorld()
  41. {
  42. String url = "/hello";
  43. Map<String, String> headers = new HashMap<>();
  44. headers.put(Utils.AUTHENTICATION_HEADER, "Bearer testToken");
  45. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  46. .request("GET", url, null, headers, String.class);
  47. assertNotNull(res);
  48. assertEquals(200, res.getStatus());
  49. assertEquals("Hello World", res.getBody());
  50. }
  51. @Test
  52. void testRegistrate() throws IOException
  53. {
  54. when(accessController.registrate(any(), any())).thenReturn("Test");
  55. String url = "/accounts/registrate";
  56. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  57. .request("POST", url, null, null, String.class);
  58. assertNotNull(res);
  59. assertEquals(200, res.getStatus());
  60. then(accessController).should().registrate(any(), any());
  61. }
  62. @Test
  63. void testLogin() throws JsonProcessingException
  64. {
  65. when(accessController.login(any(), any())).thenReturn("Test");
  66. String url = "/accounts/login";
  67. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  68. .request("POST", url, null, null, String.class);
  69. assertNotNull(res);
  70. assertEquals(200, res.getStatus());
  71. then(accessController).should().login(any(), any());
  72. }
  73. @Test
  74. void testLogout()
  75. {
  76. when(accessController.logout(any(), any())).thenReturn("Test");
  77. String url = "/accounts/logout";
  78. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  79. .request("POST", url, null, null, String.class);
  80. assertNotNull(res);
  81. assertEquals(200, res.getStatus());
  82. then(accessController).should().logout(any(), any());
  83. }
  84. @Test
  85. void testDeletePlayer()
  86. {
  87. final Response response = mock(Response.class);
  88. when(accessController.deletePlayer(any(), any())).thenReturn(response);
  89. String url = "/accounts/12345";
  90. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  91. .request("DELETE", url, null, null, String.class);
  92. assertNotNull(res);
  93. assertEquals(200, res.getStatus());
  94. then(accessController).should().deletePlayer(any(), any());
  95. }
  96. @Test
  97. void updateUser() throws IOException
  98. {
  99. when(userController.updateUser(any(), any())).thenReturn(mock(Response.class));
  100. String url = "/users";
  101. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  102. .request("POST", url, null, null, String.class);
  103. assertNotNull(res);
  104. assertEquals(200, res.getStatus());
  105. then(userController).should().updateUser(any(), any());
  106. }
  107. @Test
  108. void getPlayers() throws JsonProcessingException
  109. {
  110. when(userController.getCompleteUser(any(), any())).thenReturn(mock(Response.class));
  111. String url = "/users";
  112. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  113. .request("GET", url, null, null, String.class);
  114. assertNotNull(res);
  115. assertEquals(200, res.getStatus());
  116. then(userController).should().getCompleteUser(any(), any());
  117. }
  118. @Test
  119. void getPlayer() throws JsonProcessingException
  120. {
  121. when(userController.getUser(any(), any())).thenReturn(mock(Response.class));
  122. String url = "/users/12345";
  123. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  124. .request("GET", url, null, null, String.class);
  125. assertNotNull(res);
  126. assertEquals(200, res.getStatus());
  127. then(userController).should().getUser(any(), any());
  128. }
  129. @Test
  130. void testCreateGame() throws IOException
  131. {
  132. when(gameController.createGame(any(), any())).thenReturn(mock(Response.class));
  133. final String url = "/games";
  134. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  135. .request("POST", url, null, null, String.class);
  136. assertNotNull(res);
  137. assertEquals(200, res.getStatus());
  138. then(gameController).should().createGame(any(), any());
  139. then(gameController).shouldHaveNoMoreInteractions();
  140. }
  141. @Test
  142. void testFindGameById() throws JsonProcessingException
  143. {
  144. when(gameController.findGameById(any(), any())).thenReturn(mock(Response.class));
  145. final String url = "/games/12345";
  146. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  147. .request("GET", url, null, null, String.class);
  148. assertNotNull(res);
  149. assertEquals(200, res.getStatus());
  150. then(gameController).should().findGameById(any(), any());
  151. then(gameController).shouldHaveNoMoreInteractions();
  152. }
  153. @Test
  154. void testFindRunningGamesOfUser() throws JsonProcessingException
  155. {
  156. when(gameController.findRunningGamesOfUser(any(), any())).thenReturn(mock(Response.class));
  157. final String url = "/games/findByUser/12345";
  158. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  159. .request("GET", url, null, null, String.class);
  160. assertNotNull(res);
  161. assertEquals(200, res.getStatus());
  162. then(gameController).should().findRunningGamesOfUser(any(), any());
  163. then(gameController).shouldHaveNoMoreInteractions();
  164. }
  165. }