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.

87 lines
2.5 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.user.web.UserController;
  6. import de.fd.fh.shared.Utils;
  7. import org.junit.jupiter.api.AfterAll;
  8. import org.junit.jupiter.api.BeforeAll;
  9. import org.junit.jupiter.api.Test;
  10. import java.io.IOException;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import static org.junit.jupiter.api.Assertions.*;
  14. import static org.mockito.ArgumentMatchers.any;
  15. import static org.mockito.BDDMockito.then;
  16. import static org.mockito.Mockito.mock;
  17. import static org.mockito.Mockito.when;
  18. import static spark.Spark.awaitInitialization;
  19. import static spark.Spark.stop;
  20. class ServerAppTest
  21. {
  22. private static UserController userController = mock(UserController.class);
  23. private static AccessController accessController = mock(AccessController.class);
  24. @BeforeAll
  25. static void before()
  26. {
  27. ServerApp.initController(accessController, userController);
  28. ServerApp.main(null);
  29. awaitInitialization();
  30. }
  31. @AfterAll
  32. static void after()
  33. {
  34. stop();
  35. }
  36. @Test
  37. void testHalloWorld()
  38. {
  39. String url = "/hello";
  40. Map<String, String> headers = new HashMap<>();
  41. headers.put(Utils.AUTHENTICATION_HEADER, "Bearer testToken");
  42. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  43. .request("GET", url, null, headers, String.class);
  44. assertNotNull(res);
  45. assertEquals(200, res.getStatus());
  46. assertEquals("Hello World", res.getBody());
  47. }
  48. @Test
  49. void testRegistrate() throws IOException
  50. {
  51. when(accessController.registrate(any(), any())).thenReturn("Test");
  52. String url = "/accounts/registrate";
  53. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  54. .request("POST", url, null, null, String.class);
  55. assertNotNull(res);
  56. assertEquals(200, res.getStatus());
  57. then(accessController).should().registrate(any(), any());
  58. }
  59. @Test
  60. void testLogin() throws JsonProcessingException
  61. {
  62. when(accessController.login(any(), any())).thenReturn("Test");
  63. String url = "/accounts/login";
  64. ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
  65. .request("POST", url, null, null, String.class);
  66. assertNotNull(res);
  67. assertEquals(200, res.getStatus());
  68. then(accessController).should().login(any(), any());
  69. }
  70. }