Steffen Nitschke
4 years ago
4 changed files with 163 additions and 6 deletions
-
29fh.fd.ci.server/src/main/java/de/fd/fh/ServerApp.java
-
1fh.fd.ci.server/src/main/java/de/fd/fh/server/access/web/AccessController.java
-
53fh.fd.ci.server/src/test/java/de/fd/fh/ServerAppTest.java
-
86fh.fd.ci.server/src/test/java/de/fd/fh/server/ApiTestUtils.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()); |
||||
|
} |
||||
|
} |
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue