|
|
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.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 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);
@BeforeAll static void before() { ServerApp.initController(accessController, userController); 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()); } }
|