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.
175 lines
7.1 KiB
175 lines
7.1 KiB
package de.fd.fh.server.user.web;
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import de.fd.fh.server.access.AccessToken;
|
|
import de.fd.fh.server.user.User;
|
|
import de.fd.fh.server.user.UserId;
|
|
import de.fd.fh.server.user.UserService;
|
|
import de.fd.fh.shared.network.messages.UserRequest;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.mockito.ArgumentCaptor;
|
|
import org.mockito.Mock;
|
|
import org.mockito.MockitoAnnotations;
|
|
import spark.Request;
|
|
import spark.Response;
|
|
import spark.Session;
|
|
|
|
import java.io.IOException;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.BDDMockito.then;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
class UserControllerTest
|
|
{
|
|
@Mock
|
|
Request request;
|
|
@Mock
|
|
Response response;
|
|
@Mock
|
|
Session session;
|
|
@Mock
|
|
UserService service;
|
|
|
|
@BeforeEach
|
|
void before()
|
|
{
|
|
MockitoAnnotations.openMocks(this);
|
|
}
|
|
|
|
@Test
|
|
void getUserTest() throws JsonProcessingException
|
|
{
|
|
when(request.params(any())).thenReturn("12345");
|
|
when(service.getSmallPlayer(any())).thenReturn(new UserRequest("12345", "TestName"));
|
|
|
|
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
|
|
|
|
final Response result = new UserController(service).getUser(request, response);
|
|
|
|
verify(response).body(captor.capture());
|
|
|
|
assertEquals(response, result, "Should return the incomming response.");
|
|
assertEquals("{\"id\":\"12345\",\"name\":\"TestName\"}", captor.getValue(),
|
|
"Should return UserRequest in Body.");
|
|
then(service).should().getSmallPlayer(any());
|
|
}
|
|
|
|
@Test
|
|
void failedGetUserTest() throws JsonProcessingException
|
|
{
|
|
when(request.params(any())).thenReturn("12345");
|
|
when(service.getSmallPlayer(any())).thenReturn(null);
|
|
|
|
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
|
|
|
|
final Response result = new UserController(service).getUser(request, response);
|
|
|
|
verify(response).status(statusCaptor.capture());
|
|
|
|
assertEquals(response, result, "Should return the incomming response.");
|
|
assertEquals(Integer.valueOf(404), statusCaptor.getValue(), "Should return not found status code.");
|
|
then(service).should().getSmallPlayer(any());
|
|
then(service).shouldHaveNoMoreInteractions();
|
|
}
|
|
|
|
@Test
|
|
void getCompleteUserTest() throws JsonProcessingException
|
|
{
|
|
when(request.session()).thenReturn(session);
|
|
when(session.attribute(any())).thenReturn(
|
|
new AccessToken(null, null, null, UserId.of("12345")));
|
|
when(service.getPlayer(any(UserId.class))).thenReturn(User.of("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).getCompleteUser(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\":null,\"name\":\"TestName\"}", bodyCapture.getValue(), "Should return user as json.");
|
|
|
|
then(service).should().getPlayer(any());
|
|
then(service).shouldHaveNoMoreInteractions();
|
|
}
|
|
|
|
@Test
|
|
void failedGetCompleteUserTest() throws JsonProcessingException
|
|
{
|
|
when(request.session()).thenReturn(session);
|
|
when(session.attribute(any())).thenReturn(
|
|
new AccessToken(null, null, null, UserId.of("12345")));
|
|
when(service.getPlayer(any(UserId.class))).thenReturn(null);
|
|
|
|
final ArgumentCaptor<Integer> capture = ArgumentCaptor.forClass(Integer.class);
|
|
|
|
final Response result = new UserController(service).getCompleteUser(request, response);
|
|
|
|
verify(response).status(capture.capture());
|
|
|
|
assertEquals(response, result, "Should return the incoming Response");
|
|
assertEquals(Integer.valueOf(404), capture.getValue(), "Should return status code 200.");
|
|
then(service).should().getPlayer(any());
|
|
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();
|
|
}
|
|
}
|