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.

174 lines
7.1 KiB

  1. package de.fd.fh.server.user.web;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import de.fd.fh.server.access.AccessToken;
  4. import de.fd.fh.server.user.User;
  5. import de.fd.fh.server.user.UserId;
  6. import de.fd.fh.server.user.UserService;
  7. import de.fd.fh.shared.network.messages.UserRequest;
  8. import org.junit.jupiter.api.BeforeEach;
  9. import org.junit.jupiter.api.Test;
  10. import org.mockito.ArgumentCaptor;
  11. import org.mockito.Mock;
  12. import org.mockito.MockitoAnnotations;
  13. import spark.Request;
  14. import spark.Response;
  15. import spark.Session;
  16. import java.io.IOException;
  17. import static org.junit.jupiter.api.Assertions.*;
  18. import static org.mockito.ArgumentMatchers.any;
  19. import static org.mockito.BDDMockito.then;
  20. import static org.mockito.Mockito.verify;
  21. import static org.mockito.Mockito.when;
  22. class UserControllerTest
  23. {
  24. @Mock
  25. Request request;
  26. @Mock
  27. Response response;
  28. @Mock
  29. Session session;
  30. @Mock
  31. UserService service;
  32. @BeforeEach
  33. void before()
  34. {
  35. MockitoAnnotations.openMocks(this);
  36. }
  37. @Test
  38. void getUserTest() throws JsonProcessingException
  39. {
  40. when(request.params(any())).thenReturn("12345");
  41. when(service.getSmallPlayer(any())).thenReturn(new UserRequest("12345", "TestName"));
  42. final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
  43. final Response result = new UserController(service).getUser(request, response);
  44. verify(response).body(captor.capture());
  45. assertEquals(response, result, "Should return the incomming response.");
  46. assertEquals("{\"id\":\"12345\",\"name\":\"TestName\"}", captor.getValue(),
  47. "Should return UserRequest in Body.");
  48. then(service).should().getSmallPlayer(any());
  49. }
  50. @Test
  51. void failedGetUserTest() throws JsonProcessingException
  52. {
  53. when(request.params(any())).thenReturn("12345");
  54. when(service.getSmallPlayer(any())).thenReturn(null);
  55. final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
  56. final Response result = new UserController(service).getUser(request, response);
  57. verify(response).status(statusCaptor.capture());
  58. assertEquals(response, result, "Should return the incomming response.");
  59. assertEquals(Integer.valueOf(404), statusCaptor.getValue(), "Should return not found status code.");
  60. then(service).should().getSmallPlayer(any());
  61. then(service).shouldHaveNoMoreInteractions();
  62. }
  63. @Test
  64. void getCompleteUserTest() throws JsonProcessingException
  65. {
  66. when(request.session()).thenReturn(session);
  67. when(session.attribute(any())).thenReturn(
  68. new AccessToken(null, null, null, UserId.of("12345")));
  69. when(service.getPlayer(any(UserId.class))).thenReturn(User.of("TestName"));
  70. final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class);
  71. final ArgumentCaptor<String> bodyCapture = ArgumentCaptor.forClass(String.class);
  72. final ArgumentCaptor<String> typeCapture = ArgumentCaptor.forClass(String.class);
  73. final Response result = new UserController(service).getCompleteUser(request, response);
  74. verify(response).status(statusCapture.capture());
  75. verify(response).type(typeCapture.capture());
  76. verify(response).body(bodyCapture.capture());
  77. assertEquals(response, result, "Should return the incoming Response");
  78. assertEquals(Integer.valueOf(200), statusCapture.getValue(), "Should return status code 200.");
  79. assertEquals("application/json", typeCapture.getValue(), "Should have content type json.");
  80. assertEquals("{\"id\":null,\"name\":\"TestName\"}", bodyCapture.getValue(), "Should return user as json.");
  81. then(service).should().getPlayer(any());
  82. then(service).shouldHaveNoMoreInteractions();
  83. }
  84. @Test
  85. void failedGetCompleteUserTest() throws JsonProcessingException
  86. {
  87. when(request.session()).thenReturn(session);
  88. when(session.attribute(any())).thenReturn(
  89. new AccessToken(null, null, null, UserId.of("12345")));
  90. when(service.getPlayer(any(UserId.class))).thenReturn(null);
  91. final ArgumentCaptor<Integer> capture = ArgumentCaptor.forClass(Integer.class);
  92. final Response result = new UserController(service).getCompleteUser(request, response);
  93. verify(response).status(capture.capture());
  94. assertEquals(response, result, "Should return the incoming Response");
  95. assertEquals(Integer.valueOf(404), capture.getValue(), "Should return status code 200.");
  96. then(service).should().getPlayer(any());
  97. then(service).shouldHaveNoMoreInteractions();
  98. }
  99. @Test
  100. void updateUserTest() throws IOException
  101. {
  102. when(request.session()).thenReturn(session);
  103. when(session.attribute(any())).thenReturn(
  104. new AccessToken(null, null, null, UserId.of("12345")));
  105. when(request.body()).thenReturn("{\"name\":\"TestName\",\"password\":\"TestPassword\"}");
  106. when(service.changePlayer(any(), any())).thenReturn(new User(UserId.of("12345"), "TestName"));
  107. final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class);
  108. final ArgumentCaptor<String> bodyCapture = ArgumentCaptor.forClass(String.class);
  109. final ArgumentCaptor<String> typeCapture = ArgumentCaptor.forClass(String.class);
  110. final Response result = new UserController(service).updateUser(request, response);
  111. verify(response).status(statusCapture.capture());
  112. verify(response).type(typeCapture.capture());
  113. verify(response).body(bodyCapture.capture());
  114. assertEquals(response, result, "Should return the incoming Response");
  115. assertEquals(Integer.valueOf(200), statusCapture.getValue(), "Should return status code 200.");
  116. assertEquals("application/json", typeCapture.getValue(), "Should have content type json.");
  117. assertEquals("{\"id\":{\"identifier\":\"12345\"},\"name\":\"TestName\"}", bodyCapture.getValue(), "Should return user as json.");
  118. then(service).should().changePlayer(any(), any());
  119. then(service).shouldHaveNoMoreInteractions();
  120. }
  121. @Test
  122. void failedUpdateUserTest() throws IOException
  123. {
  124. when(request.session()).thenReturn(session);
  125. when(session.attribute(any())).thenReturn(
  126. new AccessToken(null, null, null, UserId.of("12345")));
  127. when(request.body()).thenReturn("{\"name\":\"TestName\",\"password\":\"TestPassword\"}");
  128. when(service.changePlayer(any(), any())).thenReturn(null);
  129. final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class);
  130. final Response result = new UserController(service).updateUser(request, response);
  131. verify(response).status(statusCapture.capture());
  132. assertEquals(response, result, "Should return the incoming Response");
  133. assertEquals(Integer.valueOf(400), statusCapture.getValue(), "Should return status code 400.");
  134. then(service).should().changePlayer(any(), any());
  135. then(service).shouldHaveNoMoreInteractions();
  136. }
  137. }