diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java b/fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java index d275c81..1c87d4d 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java @@ -22,21 +22,39 @@ public class ServerApp private static AccessRepository accessRepository; private static UserRepository userRepository; + private static AccessController accessController; + private static UserController userController; + private static final Set listeners = new HashSet<>(); public static void main(String[] args) { - initRepositories(); - initListeners(); - new AccessController((AccessService) addListeners(new AccessService(accessRepository))); - new UserController((UserService) addListeners(new UserService(userRepository))); + if (accessController == null && userController == null) + { + initRepositories(); + accessController = new AccessController((AccessService) + addListeners(new AccessService(accessRepository))); + userController = new UserController((UserService) + addListeners(new UserService(userRepository))); + } + + before("/*", (req, res) -> accessController.before(req)); get("/hello", (req, res) -> "Hello World"); } + public static void initController( + final AccessController accessController, + final UserController userController + ) + { + ServerApp.userController = userController; + ServerApp.accessController = accessController; + } + private static Object addListeners(Observable service) { listeners.forEach(service::addObserver); @@ -50,7 +68,8 @@ public class ServerApp listeners.add(new UserContextEventListener(userRepository)); } - private static void initRepositories() { + private static void initRepositories() + { accessRepository = new AccessRepository(); userRepository = new UserRepository(); } diff --git a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java index c74fa87..390730f 100644 --- a/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java +++ b/fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java @@ -101,7 +101,6 @@ public class AccessController public AccessController(final AccessService service) { this.service = service; - Spark.before("/*", (req, res) -> before(req)); post("/accounts/registrate", this::registrate); diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/ServerAppTest.java b/fh.fd.ci.server/src/test/java/de/fd/fh/ServerAppTest.java new file mode 100644 index 0000000..e5fdb7e --- /dev/null +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/ServerAppTest.java @@ -0,0 +1,53 @@ +package de.fd.fh; + +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.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +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 headers = new HashMap<>(); + headers.put(Utils.AUTHENTICATION_HEADER, "Bearer testToken"); + + ApiTestUtils.TestResponse res = new ApiTestUtils() + .request("GET", url, null, headers, String.class); + + assertNotNull(res); + assertEquals(200, res.getStatus()); + assertEquals("Hello World", res.getBody()); + } +} \ No newline at end of file diff --git a/fh.fd.ci.server/src/test/java/de/fd/fh/server/ApiTestUtils.java b/fh.fd.ci.server/src/test/java/de/fd/fh/server/ApiTestUtils.java new file mode 100644 index 0000000..fd8ff3b --- /dev/null +++ b/fh.fd.ci.server/src/test/java/de/fd/fh/server/ApiTestUtils.java @@ -0,0 +1,86 @@ +package de.fd.fh.server; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Getter; +import lombok.Setter; +import spark.utils.IOUtils; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.fail; + +public class ApiTestUtils +{ + public TestResponse request( + final String method, + final String path, + final String requestBody, + final Map header, + final Class responseBodyType) + { + try + { + URL url = new URL("http://localhost:4567" + path); + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(method); + connection.setDoOutput(true); + + if (requestBody != null) + { + final OutputStream outputStream = connection.getOutputStream(); + final OutputStreamWriter outputStreamWriter = + new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); + outputStreamWriter.write(requestBody); + outputStreamWriter.flush(); + outputStreamWriter.close(); + } + + if (header != null) + { + header.forEach(connection::addRequestProperty); + } + + connection.connect(); + + final String body = IOUtils.toString(connection.getInputStream()); + return new TestResponse(connection.getResponseCode(), body, responseBodyType); + } + catch (final IOException e) + { + e.printStackTrace(); + fail("Sending request failed: " + e.getMessage()); + return null; + } + } + + @Getter + @Setter + public static class TestResponse + { + + private final String body; + private final int status; + private final Class type; + + public TestResponse( + final int status, + final String body, + final Class type) + { + this.status = status; + this.body = body; + this.type = type; + } + + public T json() throws IOException + { + return new ObjectMapper().readValue(body, type); + } + } +}