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.

146 lines
4.7 KiB

package de.fd.fh.server.game.web;
import com.fasterxml.jackson.core.JsonProcessingException;
import de.fd.fh.server.game.Game;
import de.fd.fh.server.game.GameId;
import de.fd.fh.server.game.GameService;
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 java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class GameControllerTest
{
@Mock
Request request;
@Mock
Response response;
@Mock
GameService service;
@BeforeEach
void before()
{
MockitoAnnotations.openMocks(this);
}
@Test
void testCreateGame() throws IOException
{
when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}");
when(service.createGame(any(), any())).thenReturn(GameId.of("56789"));
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).createGame(request, response);
verify(response).body(bodyCaptor.capture());
verify(response).status(statusCaptor.capture());
assertNotNull(bodyCaptor.getValue());
assertTrue(bodyCaptor.getValue().contains("56789"));
assertNotNull(statusCaptor.getValue());
assertEquals(201, statusCaptor.getValue());
}
@Test
void testFailedCreateGame() throws IOException
{
when(request.body()).thenReturn("{\"whiteUserId\":\"12345\",\"blackUserId\":\"98765\"}");
when(service.createGame(any(), any())).thenReturn(null);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).createGame(request, response);
verify(response).status(statusCaptor.capture());
assertNotNull(statusCaptor.getValue());
assertEquals(400, statusCaptor.getValue());
}
@Test
void testFindGameById() throws IOException
{
when(request.params(any())).thenReturn("12345");
when(service.findGameById(any())).thenReturn(new Game());
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).findGameById(request, response);
verify(response).body(bodyCaptor.capture());
verify(response).status(statusCaptor.capture());
assertNotNull(bodyCaptor.getValue());
assertNotNull(statusCaptor.getValue());
assertEquals(200, statusCaptor.getValue());
}
@Test
void testFailedFindGameById() throws IOException
{
when(request.params(any())).thenReturn("12345");
when(service.findGameById(any())).thenReturn(null);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).findGameById(request, response);
verify(response).status(statusCaptor.capture());
assertNotNull(statusCaptor.getValue());
assertEquals(404, statusCaptor.getValue());
}
@Test
void testFindRunningGamesOfUser() throws JsonProcessingException
{
when(request.params(any())).thenReturn("12345");
when(service.findRunningGamesOfUser(any())).thenReturn(List.of(new Game(), new Game()));
final ArgumentCaptor<String> bodyCaptor = ArgumentCaptor.forClass(String.class);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).findRunningGamesOfUser(request, response);
verify(response).body(bodyCaptor.capture());
verify(response).status(statusCaptor.capture());
assertNotNull(bodyCaptor.getValue());
assertNotNull(statusCaptor.getValue());
assertEquals(200, statusCaptor.getValue());
}
@Test
void testFailedFindRunningGamesOfUser() throws JsonProcessingException
{
when(request.params(any())).thenReturn("12345");
when(service.findRunningGamesOfUser(any())).thenReturn(null);
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class);
new GameController(service).findRunningGamesOfUser(request, response);
verify(response).status(statusCaptor.capture());
assertNotNull(statusCaptor.getValue());
assertEquals(404, statusCaptor.getValue());
}
}