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

[RESTEASY-1809] Tests for CompletionStage resource method return type #1417

Merged
merged 1 commit into from Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,8 +2,6 @@

import java.net.InetAddress;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;

Expand All @@ -14,6 +12,7 @@
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.test.response.resource.AsyncResponseCallback;
import org.jboss.resteasy.test.response.resource.CompletionStageProxy;
import org.jboss.resteasy.test.response.resource.CompletionStageResponseMessageBodyWriter;
import org.jboss.resteasy.test.response.resource.CompletionStageResponseResource;
import org.jboss.resteasy.test.response.resource.CompletionStageResponseTestClass;
Expand All @@ -29,6 +28,8 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.concurrent.Future;

/**
* @tpSubChapter CompletionStage response type
* @tpChapter Integration tests
Expand All @@ -38,13 +39,14 @@
@RunAsClient
public class CompletionStageResponseTest {

static Client client;
static boolean serverIsLocal;
static ResteasyClient client;

@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(CompletionStageResponseTest.class.getSimpleName());
war.addClass(CompletionStageResponseTestClass.class);
war.addClass(CompletionStageProxy.class);
war.addAsLibrary(TestUtil.resolveDependency("io.reactivex.rxjava2:rxjava:2.1.3"));
war.setManifest(new StringAsset("Manifest-Version: 1.0\n"
+ "Dependencies: org.reactivestreams\n"));
Expand All @@ -59,7 +61,7 @@ private static String generateURL(String path) {

@BeforeClass
public static void setup() throws Exception {
client = ClientBuilder.newClient();
client = new ResteasyClientBuilder().build();

// Undertow's default behavior is to send an HTML error page only if the client and
// server are communicating on a loopback connection. Otherwise, it returns "".
Expand Down Expand Up @@ -245,4 +247,39 @@ public void testTextSingle() throws Exception
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(CompletionStageResponseResource.HELLO, entity);
}

/**
* @tpTestDetails Resource method returns CompletionStage<String>, data are computed after end-point method ends
* @tpSince RESTEasy 3.5
*/
@Test
public void getDataWithDelayTest() throws Exception
{
Invocation.Builder request = client.target(generateURL("/sleep")).request();
Future<Response> future = request.async().get();
Assert.assertFalse(future.isDone());
Response response = future.get();
String entity = response.readEntity(String.class);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(CompletionStageResponseResource.HELLO, entity);
}


/**
* @tpTestDetails Resource method returns CompletionStage<String>, client try to use proxy
* Regression check for https://issues.jboss.org/browse/RESTEASY-1798
* - RESTEasy proxy client can't use RxClient and CompletionStage
* @tpSince RESTEasy 3.5
*/
@Test
@Category({ExpectedFailing.class})
public void proxyTest() throws Exception
{
CompletionStageProxy proxy = client.target(generateURL("/")).proxy(CompletionStageProxy.class);
Future<String> future = proxy.sleep().toCompletableFuture();
Assert.assertFalse(future.isDone());
Assert.assertEquals(CompletionStageResponseResource.HELLO, future.get());
}


}
@@ -0,0 +1,15 @@
package org.jboss.resteasy.test.response.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.concurrent.CompletionStage;


@Path("")
public interface CompletionStageProxy {
@GET
@Path("sleep")
@Produces("text/plain")
CompletionStage<String> sleep();
}
Expand Up @@ -164,4 +164,21 @@ public String callbackCalledWithError() {
public String getHost(@Context UriInfo uri) {
return uri.getRequestUri().getHost();
}

@GET
@Path("sleep")
@Produces("text/plain")
public CompletionStage<String> sleep() {
CompletableFuture<String> cs = new CompletableFuture<>();
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
cs.complete(HELLO);
});
return cs;
}
}