-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't lose reference to the application class when an unchecked excep…
…tion occurs in dynamicClient.executeSync
- Loading branch information
Showing
2 changed files
with
96 additions
and
8 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
82 changes: 82 additions & 0 deletions
82
...vertx/src/test/java/io/smallrye/graphql/client/vertx/test/DynamicClientExceptionTest.java
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,82 @@ | ||
package io.smallrye.graphql.client.vertx.test; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import jakarta.json.JsonObject; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.smallrye.graphql.client.InvalidResponseException; | ||
import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient; | ||
import io.smallrye.graphql.client.vertx.dynamic.VertxDynamicGraphQLClientBuilder; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpServer; | ||
import io.vertx.core.http.HttpServerOptions; | ||
|
||
/** | ||
* Make sure than when executing a sync operation with a dynamic client and | ||
* it throws an unchecked exception, the resulting exception will contain a | ||
* reference to the application class that invoked the call. | ||
*/ | ||
public class DynamicClientExceptionTest { | ||
|
||
static Vertx vertx = Vertx.vertx(); | ||
|
||
@Test | ||
public void test() throws ExecutionException, InterruptedException, TimeoutException { | ||
HttpServer server = runMockServer(); | ||
try { | ||
DynamicGraphQLClient client = new VertxDynamicGraphQLClientBuilder() | ||
.url("http://localhost:" + server.actualPort()) | ||
.build(); | ||
try { | ||
JsonObject data = client.executeSync("{something-whatever}").getData(); | ||
Assertions.fail("Expected an exception"); | ||
} catch (Exception e) { | ||
Assertions.assertTrue(e instanceof InvalidResponseException); | ||
StackTraceElement element = findStackTraceElementThatContainsClass("DynamicClientExceptionTest", | ||
e.getStackTrace()); | ||
Assertions.assertNotNull(element, () -> { | ||
e.printStackTrace(); | ||
return "Expected the stack trace to contain a reference to the test class"; | ||
}); | ||
} | ||
} finally { | ||
server.close(); | ||
} | ||
} | ||
|
||
private StackTraceElement findStackTraceElementThatContainsClass(String string, StackTraceElement[] stackTrace) { | ||
for (StackTraceElement stackTraceElement : stackTrace) { | ||
if (stackTraceElement.getClassName().contains(string)) { | ||
return stackTraceElement; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
// mock server with websocket support which does not send any messages to the client, | ||
// so this should cause a timeout while waiting for the subscription to be acknowledged | ||
private HttpServer runMockServer() throws ExecutionException, InterruptedException, TimeoutException { | ||
HttpServerOptions options = new HttpServerOptions(); | ||
options.setHost("localhost"); | ||
HttpServer server = vertx.createHttpServer(options); | ||
server.requestHandler(new Handler<io.vertx.core.http.HttpServerRequest>() { | ||
@Override | ||
public void handle(io.vertx.core.http.HttpServerRequest event) { | ||
event.response().end("BAD RESPONSE"); | ||
} | ||
}); | ||
return server.listen(0).toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS); | ||
} | ||
|
||
@AfterAll | ||
public static void cleanup() { | ||
vertx.close(); | ||
} | ||
} |