Search in sources :

Example 36 with WebTarget

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

the class MoxyAppTest method testCustomer.

/**
     * Test that the expected response is sent back.
     * @throws java.lang.Exception
     */
@Test
public void testCustomer() throws Exception {
    final WebTarget webTarget = target().path("customer");
    Customer customer = webTarget.request(MediaType.APPLICATION_XML).get(Customer.class);
    customer.setName("Tom Dooley");
    webTarget.request(MediaType.APPLICATION_XML).put(Entity.entity(customer, MediaType.APPLICATION_XML));
    Customer updatedCustomer = webTarget.request(MediaType.APPLICATION_XML).get(Customer.class);
    assertEquals(customer, updatedCustomer);
}
Also used : Customer(org.glassfish.jersey.examples.xmlmoxy.beans.Customer) WebTarget(javax.ws.rs.client.WebTarget) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 37 with WebTarget

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

the class OverrideWadlResourceTest method testOverride.

@Test
public void testOverride() {
    WebTarget target = target("/application.wadl");
    assertEquals("OVERRIDE", target.request().get(String.class));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 38 with WebTarget

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

the class EventOutputTest method testGrizzlyConnectorWithEventSource.

@Test
public void testGrizzlyConnectorWithEventSource() throws InterruptedException {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 15000);
    clientConfig.property(ClientProperties.READ_TIMEOUT, 0);
    clientConfig.property(ClientProperties.ASYNC_THREADPOOL_SIZE, 8);
    clientConfig.connectorProvider(new GrizzlyConnectorProvider());
    Client client = ClientBuilder.newBuilder().withConfig(clientConfig).build();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> eventData = new AtomicReference<String>();
    final AtomicInteger counter = new AtomicInteger(0);
    WebTarget single = client.target(getBaseUri()).path("test/single");
    EventSource es = EventSource.target(single).build();
    es.register(new EventListener() {

        @Override
        public void onEvent(InboundEvent inboundEvent) {
            final int i = counter.incrementAndGet();
            if (i == 1) {
                eventData.set(inboundEvent.readData());
            }
            latch.countDown();
        }
    });
    boolean latchTimedOut;
    boolean closeTimedOut;
    try {
        es.open();
        latchTimedOut = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    } finally {
        closeTimedOut = es.close(5, TimeUnit.SECONDS);
    }
    assertEquals("Unexpected event count", 1, counter.get());
    assertEquals("Unexpected event data", "single", eventData.get());
    assertTrue("Event latch has timed out", latchTimedOut);
    assertTrue("EventSource.close() has timed out", closeTimedOut);
}
Also used : GrizzlyConnectorProvider(org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) EventSource(org.glassfish.jersey.media.sse.EventSource) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 39 with WebTarget

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

the class EventOutputTest method testReadCommentsOnlySseEvents.

/**
     * Reproducer for JERSEY-2912: Sending and receiving comments-only events.
     *
     * @throws Exception
     */
@Test
public void testReadCommentsOnlySseEvents() throws Exception {
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.property(ClientProperties.CONNECT_TIMEOUT, 15000);
    clientConfig.property(ClientProperties.READ_TIMEOUT, 0);
    clientConfig.property(ClientProperties.ASYNC_THREADPOOL_SIZE, 8);
    clientConfig.connectorProvider(new GrizzlyConnectorProvider());
    Client client = ClientBuilder.newBuilder().withConfig(clientConfig).build();
    final CountDownLatch latch = new CountDownLatch(2);
    final Queue<String> eventComments = new ArrayBlockingQueue<>(2);
    WebTarget single = client.target(getBaseUri()).path("test/comments-only");
    EventSource es = EventSource.target(single).build();
    es.register(new EventListener() {

        @Override
        public void onEvent(InboundEvent inboundEvent) {
            eventComments.add(inboundEvent.getComment());
            latch.countDown();
        }
    });
    boolean latchTimedOut;
    boolean closeTimedOut;
    try {
        es.open();
        latchTimedOut = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    } finally {
        closeTimedOut = es.close(5, TimeUnit.SECONDS);
    }
    assertEquals("Unexpected event count", 2, eventComments.size());
    for (int i = 1; i <= 2; i++) {
        assertEquals("Unexpected comment data on event #" + i, "No comment #" + i, eventComments.poll());
    }
    assertTrue("Event latch has timed out", latchTimedOut);
    assertTrue("EventSource.close() has timed out", closeTimedOut);
}
Also used : GrizzlyConnectorProvider(org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) EventSource(org.glassfish.jersey.media.sse.EventSource) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 40 with WebTarget

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

the class EventSourceWithNamedEventsTest method testWithEventSource.

@Test
public void testWithEventSource() throws IOException, NoSuchAlgorithmException, InterruptedException {
    final WebTarget endpoint = target().register(SseFeature.class).path("events");
    EventSource eventSource = EventSource.target(endpoint).build();
    final CountDownLatch count = new CountDownLatch(MSG_COUNT);
    final EventListener listener = new EventListener() {

        @Override
        public void onEvent(InboundEvent inboundEvent) {
            try {
                final Integer data = inboundEvent.readData(Integer.class);
                System.out.println(inboundEvent.getName() + "; " + data);
                Assert.assertEquals(SSE_NAME, inboundEvent.getName());
                Assert.assertEquals(MSG_COUNT - count.getCount(), data.intValue());
                count.countDown();
            } catch (ProcessingException ex) {
                throw new RuntimeException("Error when deserializing of data.", ex);
            }
        }
    };
    eventSource.register(listener, "message-to-client");
    eventSource.open();
    final boolean sent = latch.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were sent.", sent);
    final boolean handled = count.await(5 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS);
    Assert.assertTrue("Awaiting for SSE message has timeout. Not all message were handled by the listener.", handled);
}
Also used : EventSource(org.glassfish.jersey.media.sse.EventSource) InboundEvent(org.glassfish.jersey.media.sse.InboundEvent) WebTarget(javax.ws.rs.client.WebTarget) EventListener(org.glassfish.jersey.media.sse.EventListener) CountDownLatch(java.util.concurrent.CountDownLatch) SseFeature(org.glassfish.jersey.media.sse.SseFeature) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

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