Browse Source

extract before routing to ServerApp

chore-betterServerTestCoverage
Steffen Nitschke 4 years ago
parent
commit
3e23f6ff9c
  1. 29
      fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java
  2. 1
      fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java
  3. 53
      fh.fd.ci.server/src/test/java/de/fd/fh/ServerAppTest.java
  4. 86
      fh.fd.ci.server/src/test/java/de/fd/fh/server/ApiTestUtils.java

29
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 AccessRepository accessRepository;
private static UserRepository userRepository; private static UserRepository userRepository;
private static AccessController accessController;
private static UserController userController;
private static final Set<Observer> listeners = new HashSet<>(); private static final Set<Observer> listeners = new HashSet<>();
public static void main(String[] args) public static void main(String[] args)
{ {
initRepositories();
initListeners(); 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"); 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) private static Object addListeners(Observable service)
{ {
listeners.forEach(service::addObserver); listeners.forEach(service::addObserver);
@ -50,7 +68,8 @@ public class ServerApp
listeners.add(new UserContextEventListener(userRepository)); listeners.add(new UserContextEventListener(userRepository));
} }
private static void initRepositories() {
private static void initRepositories()
{
accessRepository = new AccessRepository(); accessRepository = new AccessRepository();
userRepository = new UserRepository(); userRepository = new UserRepository();
} }

1
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) public AccessController(final AccessService service)
{ {
this.service = service; this.service = service;
Spark.before("/*", (req, res) -> before(req));
post("/accounts/registrate", this::registrate); post("/accounts/registrate", this::registrate);

53
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<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());
}
}

86
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<T>
{
public TestResponse<T> request(
final String method,
final String path,
final String requestBody,
final Map<String, String> header,
final Class<T> 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<T>(connection.getResponseCode(), body, responseBodyType);
}
catch (final IOException e)
{
e.printStackTrace();
fail("Sending request failed: " + e.getMessage());
return null;
}
}
@Getter
@Setter
public static class TestResponse<T>
{
private final String body;
private final int status;
private final Class<T> type;
public TestResponse(
final int status,
final String body,
final Class<T> type)
{
this.status = status;
this.body = body;
this.type = type;
}
public T json() throws IOException
{
return new ObjectMapper().readValue(body, type);
}
}
}
Loading…
Cancel
Save