Search in sources :

Example 41 with WebTarget

use of javax.ws.rs.client.WebTarget in project jersey by jersey.

the class SseEventSinkToEventSourceTest method testWithJerseyApi.

@Test
public void testWithJerseyApi() throws InterruptedException {
    final WebTarget endpoint = target().path("events");
    final EventSource eventSource = EventSource.target(endpoint).build();
    transmitLatch = new CountDownLatch(MSG_COUNT);
    final CountDownLatch receiveLatch = new CountDownLatch(MSG_COUNT);
    final List<Integer> results = new ArrayList<>();
    final EventListener listener = inboundEvent -> {
        try {
            results.add(inboundEvent.readData(Integer.class));
            receiveLatch.countDown();
            Assert.assertEquals(INTEGER_SSE_NAME, inboundEvent.getName());
        } catch (ProcessingException ex) {
            throw new RuntimeException("Error when deserializing of data.", ex);
        }
    };
    eventSource.register(listener, INTEGER_SSE_NAME);
    eventSource.open();
    Assert.assertTrue(transmitLatch.await(5000, TimeUnit.MILLISECONDS));
    Assert.assertTrue(receiveLatch.await(5000, TimeUnit.MILLISECONDS));
    Assert.assertEquals(10, results.size());
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) SseEventSink(javax.ws.rs.sse.SseEventSink) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) Application(javax.ws.rs.core.Application) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) MediaType(javax.ws.rs.core.MediaType) JerseyTest(org.glassfish.jersey.test.JerseyTest) Sse(javax.ws.rs.sse.Sse) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ExecutorService(java.util.concurrent.ExecutorService) Context(javax.ws.rs.core.Context) EventListener(org.glassfish.jersey.media.sse.EventListener) EventSource(org.glassfish.jersey.media.sse.EventSource) Test(org.junit.Test) Logger(java.util.logging.Logger) Executors(java.util.concurrent.Executors) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) InboundSseEvent(javax.ws.rs.sse.InboundSseEvent) ProcessingException(javax.ws.rs.ProcessingException) WebTarget(javax.ws.rs.client.WebTarget) Assert(org.junit.Assert) SseEventSource(javax.ws.rs.sse.SseEventSource) EventSource(org.glassfish.jersey.media.sse.EventSource) ArrayList(java.util.ArrayList) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) CountDownLatch(java.util.concurrent.CountDownLatch) ProcessingException(javax.ws.rs.ProcessingException) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 42 with WebTarget

use of javax.ws.rs.client.WebTarget in project jersey by jersey.

the class SseEventSinkToEventSourceTest method testWithEventSource.

@Test
public void testWithEventSource() throws InterruptedException {
    transmitLatch = new CountDownLatch(2 * MSG_COUNT);
    final WebTarget endpoint = target().path("events");
    final SseEventSource eventSource = SseEventSource.target(endpoint).named("my-sse-eventsource1").build();
    final CountDownLatch count1 = new CountDownLatch(3 * MSG_COUNT);
    final CountDownLatch count2 = new CountDownLatch(3 * MSG_COUNT);
    eventSource.subscribe(new InboundHandler("consumer1", count1));
    eventSource.subscribe(new InboundHandler("consumer2", count2));
    eventSource.open();
    final boolean sent = transmitLatch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were sent.", sent);
    final boolean handled2 = count2.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were handled by eventSource2.", handled2);
    final boolean handled1 = count1.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were handled by eventSource1.", handled1);
}
Also used : SseEventSource(javax.ws.rs.sse.SseEventSource) WebTarget(javax.ws.rs.client.WebTarget) CountDownLatch(java.util.concurrent.CountDownLatch) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 43 with WebTarget

use of javax.ws.rs.client.WebTarget in project jersey by jersey.

the class JaxRsInjectedCdiBeanTest method _testPathAndHeader.

public static void _testPathAndHeader(final WebTarget webTarget) {
    final WebTarget target = webTarget.path("non-jaxrs-bean-injected");
    final Response pathResponse = target.path("path/1").request().get();
    assertThat(pathResponse.getStatus(), is(200));
    final String path = pathResponse.readEntity(String.class);
    assertThat(path, is("non-jaxrs-bean-injected/path/1"));
    final Response headerResponse = target.path("header/1").request().header("x-test", "bummer").get();
    assertThat(headerResponse.getStatus(), is(200));
    final String header = headerResponse.readEntity(String.class);
    assertThat(header, is("bummer"));
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget)

Example 44 with WebTarget

use of javax.ws.rs.client.WebTarget in project jersey by jersey.

the class PerRequestDependentBeanTest method testGet.

@Test
public void testGet() {
    final WebTarget target = target().path("jcdibean/dependent/per-request").queryParam("x", x);
    String s = target.request().get(String.class);
    assertThat(s, containsString(target.getUri().toString()));
    assertThat(s, containsString(String.format("queryParam=%s", x)));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 45 with WebTarget

use of javax.ws.rs.client.WebTarget in project jersey by jersey.

the class QualifiedInjectionSetGetTest method testSetGet.

@Test
public void testSetGet() {
    final WebTarget factorTarget = target().path("stutter-service-factor");
    final WebTarget stutterTarget = target().path("stutter");
    factorTarget.request().put(Entity.text("3"));
    final String shouldBeTrippled = stutterTarget.queryParam("s", "lincoln").request().get(String.class);
    assertThat(shouldBeTrippled, is("lincolnlincolnlincoln"));
    factorTarget.request().put(Entity.text("2"));
    final String shouldBeDoubled = stutterTarget.queryParam("s", "lincoln").request().get(String.class);
    assertThat(shouldBeDoubled, is("lincolnlincoln"));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Aggregations

WebTarget (javax.ws.rs.client.WebTarget)688 Test (org.junit.Test)529 Response (javax.ws.rs.core.Response)354 JerseyTest (org.glassfish.jersey.test.JerseyTest)297 Client (javax.ws.rs.client.Client)122 ClientConfig (org.glassfish.jersey.client.ClientConfig)50 GenericType (javax.ws.rs.core.GenericType)39 Invocation (javax.ws.rs.client.Invocation)36 ClientBuilder (javax.ws.rs.client.ClientBuilder)30 ArrayList (java.util.ArrayList)29 List (java.util.List)28 InputStream (java.io.InputStream)26 IOException (java.io.IOException)25 CountDownLatch (java.util.concurrent.CountDownLatch)25 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)25 ProcessingException (javax.ws.rs.ProcessingException)22 Builder (javax.ws.rs.client.Invocation.Builder)22 Map (java.util.Map)21 URI (java.net.URI)19 Properties (java.util.Properties)19