Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertx Graphql test enhancement for websocket based connections #5912

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
package io.quarkus.vertx.graphql.it;

import static io.restassured.RestAssured.given;
import static java.lang.String.format;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.WebSocket;
import io.vertx.core.http.WebSocketConnectOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.handler.graphql.ApolloWSMessageType;

@QuarkusTest
class VertxGraphqlTest {
private static Vertx vertx;

public static int getPortFromConfig() {
return ConfigProvider.getConfig().getOptionalValue("quarkus.http.test-port", Integer.class).orElse(8081);
}

private static Vertx vertx;

@BeforeAll
public static void initializeVertx() {
vertx = Vertx.vertx();
Expand All @@ -51,9 +57,34 @@ public void testWebSocketSubProtocol() throws Exception {
HttpClient httpClient = vertx.createHttpClient();
WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(getPortFromConfig())
.addSubProtocol("graphql-ws").setURI("/graphql");
CompletableFuture<Boolean> wsFuture = new CompletableFuture<>();
httpClient.webSocket(options, event -> wsFuture.complete(event.succeeded()));
Assertions.assertTrue(wsFuture.get(1, TimeUnit.MINUTES));
String graphql = "{\"id\" : \"2\", \"type\" : \"start\", \"payload\" : { \"query\" : \"{ hello }\" } }";
CompletableFuture<JsonObject> wsFuture = new CompletableFuture<>();
wsFuture.whenComplete((r, t) -> httpClient.close());
httpClient.webSocket(options, ws -> {
if (ws.succeeded()) {
WebSocket webSocket = ws.result();
webSocket.handler(message -> {
JsonObject json = message.toJsonObject();
String type = json.getString("type");
if (ApolloWSMessageType.DATA.getText().equals(type)) {
wsFuture.complete(message.toJsonObject());
} else {
wsFuture.completeExceptionally(new RuntimeException(
format("Unexpected message type: %s\nMessage: %s", type, message.toString())));
}
});

webSocket.write(Buffer.buffer(graphql));
} else {
wsFuture.completeExceptionally(ws.cause());
}
});

JsonObject json = wsFuture.get(1, TimeUnit.MINUTES);
assertNotNull(json);
assertEquals("2", json.getString("id"));
assertEquals("data", json.getString("type"));
assertEquals("world", json.getJsonObject("payload").getJsonObject("data").getString("hello"));
}

}