|
| 1 | +/** |
| 2 | + * Copyright 2005-2026 Qlik |
| 3 | + *<p> |
| 4 | + * The content of this file is subject to the terms of the Apache 2.0 open |
| 5 | + * source license available at https://www.opensource.org/licenses/apache-2.0 |
| 6 | + *<p> |
| 7 | + * Restlet is a registered trademark of QlikTech International AB. |
| 8 | + */ |
| 9 | +package org.restlet.ext.graphql; |
| 10 | + |
| 11 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 12 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 14 | + |
| 15 | +import graphql.ExecutionResult; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.concurrent.CountDownLatch; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.reactivestreams.Subscriber; |
| 25 | +import org.reactivestreams.Subscription; |
| 26 | +import org.restlet.Client; |
| 27 | +import org.restlet.Component; |
| 28 | +import org.restlet.Request; |
| 29 | +import org.restlet.Response; |
| 30 | +import org.restlet.Restlet; |
| 31 | +import org.restlet.data.MediaType; |
| 32 | +import org.restlet.data.Method; |
| 33 | +import org.restlet.data.Preference; |
| 34 | +import org.restlet.data.Protocol; |
| 35 | +import org.restlet.data.Status; |
| 36 | +import org.restlet.ext.graphql.internal.GraphqlJson; |
| 37 | +import org.restlet.representation.StringRepresentation; |
| 38 | + |
| 39 | +/** Edge cases of the {@code graphql-sse} binding: request errors, restarts and demand. */ |
| 40 | +class GraphqlSseEdgeCasesTestCase { |
| 41 | + |
| 42 | + private final AtomicInteger requests = new AtomicInteger(); |
| 43 | + |
| 44 | + private Component component; |
| 45 | + |
| 46 | + private Client client; |
| 47 | + |
| 48 | + private String base; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUp() throws Exception { |
| 52 | + component = new Component(); |
| 53 | + component.getServers().add(Protocol.HTTP, 0); |
| 54 | + GraphqlServerAdapter sse = new GraphqlServerAdapter(); |
| 55 | + sse.setGraphQL(GraphqlTestSchemas.graphQL(new AtomicInteger())); |
| 56 | + sse.getSubscriptionTransports().add(GraphqlTransport.SSE); |
| 57 | + component.getDefaultHost().attach("/graphql", sse); |
| 58 | + component |
| 59 | + .getDefaultHost() |
| 60 | + .attach( |
| 61 | + "/counted", |
| 62 | + new Restlet() { |
| 63 | + @Override |
| 64 | + public void handle(Request request, Response response) { |
| 65 | + requests.incrementAndGet(); |
| 66 | + response.setStatus(Status.CLIENT_ERROR_NOT_FOUND); |
| 67 | + } |
| 68 | + }); |
| 69 | + component.start(); |
| 70 | + base = "http://localhost:" + component.getServers().get(0).getActualPort(); |
| 71 | + client = new Client(Protocol.HTTP); |
| 72 | + client.start(); |
| 73 | + } |
| 74 | + |
| 75 | + @AfterEach |
| 76 | + void tearDown() throws Exception { |
| 77 | + client.stop(); |
| 78 | + component.stop(); |
| 79 | + } |
| 80 | + |
| 81 | + private Response stream(Map<String, Object> body) { |
| 82 | + Request request = new Request(Method.POST, base + "/graphql"); |
| 83 | + request.setEntity( |
| 84 | + new StringRepresentation( |
| 85 | + GraphqlJson.writeString(body), MediaType.APPLICATION_JSON)); |
| 86 | + request.getClientInfo() |
| 87 | + .getAcceptedMediaTypes() |
| 88 | + .add(new Preference<>(MediaType.TEXT_EVENT_STREAM)); |
| 89 | + return client.handle(request); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void documentErrorsAreBadRequests() { |
| 94 | + assertEquals( |
| 95 | + Status.CLIENT_ERROR_BAD_REQUEST, |
| 96 | + stream(Map.of("query", "subscription {")).getStatus()); |
| 97 | + assertEquals( |
| 98 | + Status.CLIENT_ERROR_BAD_REQUEST, |
| 99 | + stream(Map.of("query", "{ hello }", "operationName", "Unknown")).getStatus()); |
| 100 | + assertEquals( |
| 101 | + Status.CLIENT_ERROR_BAD_REQUEST, |
| 102 | + stream(Map.of("query", "query A { hello } query B { hello }")).getStatus()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void adapterWithoutTransportsRestarts() throws Exception { |
| 107 | + GraphqlServerAdapter adapter = new GraphqlServerAdapter(); |
| 108 | + adapter.setGraphQL(GraphqlTestSchemas.graphQL(new AtomicInteger())); |
| 109 | + adapter.start(); |
| 110 | + adapter.stop(); |
| 111 | + adapter.start(); |
| 112 | + assertTrue(adapter.isStarted()); |
| 113 | + assertTrue(adapter.getSubscriptionTransports().isEmpty()); |
| 114 | + adapter.stop(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void nonPositiveDemandFailsWithoutRequest() throws Exception { |
| 119 | + GraphqlClientAdapter graphql = new GraphqlClientAdapter(); |
| 120 | + graphql.setClient(client); |
| 121 | + graphql.setEndpoint(base + "/counted"); |
| 122 | + graphql.start(); |
| 123 | + CountDownLatch done = new CountDownLatch(1); |
| 124 | + AtomicReference<Throwable> error = new AtomicReference<>(); |
| 125 | + graphql.subscribe("subscription { ticks(count: 1) }", null, null) |
| 126 | + .subscribe( |
| 127 | + new Subscriber<ExecutionResult>() { |
| 128 | + @Override |
| 129 | + public void onSubscribe(Subscription subscription) { |
| 130 | + subscription.request(0); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void onNext(ExecutionResult item) {} |
| 135 | + |
| 136 | + @Override |
| 137 | + public void onError(Throwable throwable) { |
| 138 | + error.set(throwable); |
| 139 | + done.countDown(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onComplete() { |
| 144 | + done.countDown(); |
| 145 | + } |
| 146 | + }); |
| 147 | + assertTrue(done.await(5, TimeUnit.SECONDS)); |
| 148 | + assertInstanceOf(IllegalArgumentException.class, error.get()); |
| 149 | + assertEquals(0, requests.get()); |
| 150 | + graphql.stop(); |
| 151 | + } |
| 152 | +} |
0 commit comments