Skip to content

Commit

Permalink
fix: add JVM HTTP tests and fix a bug (#2794)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas authored Sep 23, 2024
1 parent 2580e66 commit 8e258b6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
public class HTTPProcessor {

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
@Record(ExecutionTime.STATIC_INIT)
public MethodScannerBuildItem methodScanners(TopicsBuildItem topics,
VerbClientBuildItem verbClients, FTLRecorder recorder) {
return new MethodScannerBuildItem(new MethodScanner() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ public class FtlJavaRuntimeResource {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/post")
public String post(Person person) {
return "Hello " + person.first() + " " + person.last();
public String post(Person person, HelloClient helloClient) {
return helloClient.call(person.first() + " " + person.last());
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/bytes")
public String bytesHttp(byte[] data) {
return "Hello " + new String(data, StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package xyz.block.ftl.java.runtime.it;

import xyz.block.ftl.VerbClient;
import xyz.block.ftl.VerbClientDefinition;

@VerbClientDefinition(name = "hello")
public interface HelloClient extends VerbClient<String, String> {
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package xyz.block.ftl.java.runtime.it;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.function.Function;

import jakarta.inject.Inject;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand All @@ -19,6 +18,8 @@
import ftl.echo.EchoResponse;
import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import xyz.block.ftl.VerbClient;
import xyz.block.ftl.VerbClientDefinition;
import xyz.block.ftl.VerbClientSink;
Expand All @@ -42,14 +43,6 @@ public class FtlJavaRuntimeResourceTest {
@Inject
BytesClient bytesClient;

@FTLManaged
@Inject
PostClient postClient;

@FTLManaged
@Inject
BytesHTTPClient bytesHttpClient;

@Test
public void testHelloEndpoint() {
TestVerbServer.registerFakeVerb("echo", "echo", new Function<EchoRequest, EchoResponse>() {
Expand All @@ -76,47 +69,33 @@ public void testBytesSerialization() {

@Test
public void testHttpPost() {
HttpRequest<Person> request = new HttpRequest<Person>()
.setMethod("POST")
.setPath("/test/post")
.setQuery(new HashMap<>())
.setPathParameters(new HashMap<>())
.setHeaders(new HashMap<>())
.setBody(new Person("Stuart", "Douglas"));
HttpResponse<String, String> response = postClient.call(request);
Assertions.assertEquals("Hello Stuart Douglas", response.getBody());
RestAssured.with().body(new Person("Stuart", "Douglas"))
.contentType(ContentType.JSON)
.post("/test/post")
.then()
.statusCode(200)
.body(Matchers.equalTo("Hello Stuart Douglas"));
}

@Test
public void testHttpBytes() {
HttpRequest<byte[]> request = new HttpRequest<byte[]>()
.setMethod("POST")
.setPath("/test/bytes")
.setQuery(new HashMap<>())
.setPathParameters(new HashMap<>())
.setHeaders(new HashMap<>())
.setBody("Stuart Douglas".getBytes(java.nio.charset.StandardCharsets.UTF_8));
HttpResponse<String, String> response = bytesHttpClient.call(request);
Assertions.assertArrayEquals("Hello Stuart Douglas".getBytes(StandardCharsets.UTF_8),
Base64.getDecoder().decode(response.getBody()));

RestAssured.with().body("Stuart Douglas".getBytes(java.nio.charset.StandardCharsets.UTF_8))
.contentType(ContentType.JSON)
.post("/test/bytes")
.then()
.statusCode(200)
.body(Matchers.equalTo("Hello Stuart Douglas"));
}

@VerbClientDefinition(name = "publish")
interface PublishVerbClient extends VerbClientSink<Person> {
}

@VerbClientDefinition(name = "hello")
interface HelloClient extends VerbClient<String, String> {
}

@VerbClientDefinition(name = "bytes")
interface BytesClient extends VerbClient<byte[], byte[]> {
}

@VerbClientDefinition(name = "post")
interface PostClient extends VerbClient<HttpRequest<Person>, HttpResponse<String, String>> {
}

@VerbClientDefinition(name = "bytesHttp")
interface BytesHTTPClient extends VerbClient<HttpRequest<byte[]>, HttpResponse<String, String>> {
}
Expand Down

0 comments on commit 8e258b6

Please sign in to comment.