-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b85366
commit 8dda800
Showing
6 changed files
with
130 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package barista; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.core.Filter.Result; | ||
import org.apache.logging.log4j.core.appender.ConsoleAppender; | ||
import org.apache.logging.log4j.core.config.Configurator; | ||
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; | ||
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; | ||
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; | ||
import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder; | ||
import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder; | ||
import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder; | ||
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; | ||
|
||
final class Logging { | ||
private static final String STDOUT = "stdout"; | ||
|
||
public static void configure() { | ||
ConfigurationBuilder<BuiltConfiguration> builder = | ||
ConfigurationBuilderFactory.newConfigurationBuilder() | ||
.setStatusLevel(Level.ERROR) | ||
.setConfigurationName("barista"); | ||
|
||
LayoutComponentBuilder layout = | ||
builder.newLayout("PatternLayout") | ||
.addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"); | ||
FilterComponentBuilder filter = | ||
builder.newFilter("MarkerFilter", Result.DENY, Result.NEUTRAL) | ||
.addAttribute("marker", "FLOW"); | ||
|
||
AppenderComponentBuilder appenderBuilder = | ||
builder.newAppender(STDOUT, "CONSOLE") | ||
.addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT) | ||
.add(layout) | ||
.add(filter); | ||
|
||
LoggerComponentBuilder logger = | ||
builder.newLogger("org.apache.logging.log4j", Level.DEBUG) | ||
.add(builder.newAppenderRef(STDOUT)) | ||
.addAttribute("additivity", false); | ||
|
||
builder.add(appenderBuilder) | ||
.add(logger) | ||
.add(builder.newRootLogger(Level.INFO).add(builder.newAppenderRef(STDOUT))); | ||
|
||
Configurator.initialize(builder.build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package barista; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class ServerTests { | ||
private static final HttpClient CLIENT = HttpClient.newHttpClient(); | ||
|
||
@Test | ||
void smokeTest() throws IOException, InterruptedException { | ||
Server.builder().port(8080).disableTls().endpoint(new HelloWorldEndpoint()).start(); | ||
|
||
assertResponse("http://localhost:8080/hello-world", 200, "\"Hello World\""); | ||
assertResponse("http://localhost:8080/missing", 404, "Unknown API Endpoint"); | ||
} | ||
|
||
private void assertResponse(String uri, int statusCode, String expectedResponseText) | ||
throws IOException, InterruptedException { | ||
HttpResponse<String> helloWorldResult = | ||
CLIENT.send( | ||
HttpRequest.newBuilder().uri(URI.create(uri)).GET().build(), | ||
BodyHandlers.ofString()); | ||
assertThat(helloWorldResult.statusCode()).isEqualTo(statusCode); | ||
assertThat(helloWorldResult.body()).isEqualTo(expectedResponseText); | ||
} | ||
|
||
private static final class HelloWorldEndpoint implements Endpoints.Open<Void, String> { | ||
@Override | ||
public String call(Void unused) { | ||
return "Hello World"; | ||
} | ||
|
||
@Override | ||
public Class<Void> requestClass() { | ||
return Void.class; | ||
} | ||
|
||
@Override | ||
public String path() { | ||
return "/hello-world"; | ||
} | ||
|
||
@Override | ||
public HttpMethod method() { | ||
return HttpMethod.GET; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters