|
@ -14,6 +14,8 @@ import spark.Request; |
|
|
import spark.Response; |
|
|
import spark.Response; |
|
|
import spark.Session; |
|
|
import spark.Session; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
import static org.mockito.BDDMockito.then; |
|
|
import static org.mockito.BDDMockito.then; |
|
@ -120,4 +122,53 @@ class UserControllerTest |
|
|
then(service).shouldHaveNoMoreInteractions(); |
|
|
then(service).shouldHaveNoMoreInteractions(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void updateUserTest() throws IOException |
|
|
|
|
|
{ |
|
|
|
|
|
when(request.session()).thenReturn(session); |
|
|
|
|
|
when(session.attribute(any())).thenReturn( |
|
|
|
|
|
new AccessToken(null, null, null, UserId.of("12345"))); |
|
|
|
|
|
when(request.body()).thenReturn("{\"name\":\"TestName\",\"password\":\"TestPassword\"}"); |
|
|
|
|
|
when(service.changePlayer(any(), any())).thenReturn(new User(UserId.of("12345"), "TestName")); |
|
|
|
|
|
|
|
|
|
|
|
final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class); |
|
|
|
|
|
final ArgumentCaptor<String> bodyCapture = ArgumentCaptor.forClass(String.class); |
|
|
|
|
|
final ArgumentCaptor<String> typeCapture = ArgumentCaptor.forClass(String.class); |
|
|
|
|
|
|
|
|
|
|
|
final Response result = new UserController(service).updateUser(request, response); |
|
|
|
|
|
|
|
|
|
|
|
verify(response).status(statusCapture.capture()); |
|
|
|
|
|
verify(response).type(typeCapture.capture()); |
|
|
|
|
|
verify(response).body(bodyCapture.capture()); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(response, result, "Should return the incoming Response"); |
|
|
|
|
|
assertEquals(Integer.valueOf(200), statusCapture.getValue(), "Should return status code 200."); |
|
|
|
|
|
assertEquals("application/json", typeCapture.getValue(), "Should have content type json."); |
|
|
|
|
|
assertEquals("{\"id\":{\"identifier\":\"12345\"},\"name\":\"TestName\"}", bodyCapture.getValue(), "Should return user as json."); |
|
|
|
|
|
|
|
|
|
|
|
then(service).should().changePlayer(any(), any()); |
|
|
|
|
|
then(service).shouldHaveNoMoreInteractions(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void failedUpdateUserTest() throws IOException |
|
|
|
|
|
{ |
|
|
|
|
|
when(request.session()).thenReturn(session); |
|
|
|
|
|
when(session.attribute(any())).thenReturn( |
|
|
|
|
|
new AccessToken(null, null, null, UserId.of("12345"))); |
|
|
|
|
|
when(request.body()).thenReturn("{\"name\":\"TestName\",\"password\":\"TestPassword\"}"); |
|
|
|
|
|
when(service.changePlayer(any(), any())).thenReturn(null); |
|
|
|
|
|
|
|
|
|
|
|
final ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class); |
|
|
|
|
|
|
|
|
|
|
|
final Response result = new UserController(service).updateUser(request, response); |
|
|
|
|
|
|
|
|
|
|
|
verify(response).status(statusCapture.capture()); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(response, result, "Should return the incoming Response"); |
|
|
|
|
|
assertEquals(Integer.valueOf(400), statusCapture.getValue(), "Should return status code 400."); |
|
|
|
|
|
|
|
|
|
|
|
then(service).should().changePlayer(any(), any()); |
|
|
|
|
|
then(service).shouldHaveNoMoreInteractions(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |