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.

216 lines
6.6 KiB

package de.fd.fh;
import com.fasterxml.jackson.core.JsonProcessingException;
import de.fd.fh.server.ApiTestUtils;
import de.fd.fh.server.access.web.AccessController;
import de.fd.fh.server.game.web.GameController;
import de.fd.fh.server.user.web.UserController;
import de.fd.fh.shared.Utils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import spark.Response;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static spark.Spark.awaitInitialization;
import static spark.Spark.stop;
class ServerAppTest
{
private static UserController userController = mock(UserController.class);
private static AccessController accessController = mock(AccessController.class);
private static GameController gameController = mock(GameController.class);
@BeforeAll
static void before()
{
ServerApp.initController(accessController, userController, gameController);
ServerApp.main(null);
awaitInitialization();
}
@AfterAll
static void after()
{
stop();
}
@Test
void testHalloWorld()
{
String url = "/hello";
Map<String, String> headers = new HashMap<>();
headers.put(Utils.AUTHENTICATION_HEADER, "Bearer testToken");
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("GET", url, null, headers, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
assertEquals("Hello World", res.getBody());
}
@Test
void testRegistrate() throws IOException
{
when(accessController.registrate(any(), any())).thenReturn("Test");
String url = "/accounts/registrate";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("POST", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(accessController).should().registrate(any(), any());
}
@Test
void testLogin() throws JsonProcessingException
{
when(accessController.login(any(), any())).thenReturn("Test");
String url = "/accounts/login";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("POST", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(accessController).should().login(any(), any());
}
@Test
void testLogout()
{
when(accessController.logout(any(), any())).thenReturn("Test");
String url = "/accounts/logout";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("POST", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(accessController).should().logout(any(), any());
}
@Test
void testDeletePlayer()
{
final Response response = mock(Response.class);
when(accessController.deletePlayer(any(), any())).thenReturn(response);
String url = "/accounts/12345";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("DELETE", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(accessController).should().deletePlayer(any(), any());
}
@Test
void updateUser() throws IOException
{
when(userController.updateUser(any(), any())).thenReturn(mock(Response.class));
String url = "/users";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("POST", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(userController).should().updateUser(any(), any());
}
@Test
void getPlayers() throws JsonProcessingException
{
when(userController.getCompleteUser(any(), any())).thenReturn(mock(Response.class));
String url = "/users";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("GET", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(userController).should().getCompleteUser(any(), any());
}
@Test
void getPlayer() throws JsonProcessingException
{
when(userController.getUser(any(), any())).thenReturn(mock(Response.class));
String url = "/users/12345";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("GET", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(userController).should().getUser(any(), any());
}
@Test
void testCreateGame() throws IOException
{
when(gameController.createGame(any(), any())).thenReturn(mock(Response.class));
final String url = "/games";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("POST", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(gameController).should().createGame(any(), any());
then(gameController).shouldHaveNoMoreInteractions();
}
@Test
void testFindGameById() throws JsonProcessingException
{
when(gameController.findGameById(any(), any())).thenReturn(mock(Response.class));
final String url = "/games/12345";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("GET", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(gameController).should().findGameById(any(), any());
then(gameController).shouldHaveNoMoreInteractions();
}
@Test
void testFindRunningGamesOfUser() throws JsonProcessingException
{
when(gameController.findRunningGamesOfUser(any(), any())).thenReturn(mock(Response.class));
final String url = "/games/findByUser/12345";
ApiTestUtils.TestResponse<String> res = new ApiTestUtils<String>()
.request("GET", url, null, null, String.class);
assertNotNull(res);
assertEquals(200, res.getStatus());
then(gameController).should().findRunningGamesOfUser(any(), any());
then(gameController).shouldHaveNoMoreInteractions();
}
}