Search in sources :

Example 1 with Contact

use of org.jboss.as.quickstarts.jaxrsclient.model.Contact in project quickstart by wildfly.

the class ContactsRestClientIT method invocationCallBackTest.

// This test shows how to use javax.ws.rs.client.InvocationCallback
@Test
public void invocationCallBackTest() throws Exception {
    log.info("### Testing invocation callback ###");
    // 1 - drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(getRequestUrl()).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a InvocationCallback
    log.info("Creating a InvocationCallback");
    InvocationCallback<List<Contact>> invocationCallback = new InvocationCallback<List<Contact>>() {

        @Override
        public void completed(List<Contact> allContacts) {
            // Completed the invocation with no contact
            Assert.assertEquals("Should have no contacts", 0, allContacts.size());
        }

        @Override
        public void failed(Throwable throwable) {
            // It should fail
            Assert.fail(throwable.getMessage());
        }
    };
    // 3 - Invoke the service
    log.info("Invoking a service using the InvocationCallback");
    ClientBuilder.newClient().target(getRequestUrl()).request().async().get(invocationCallback).get();
}
Also used : Response(javax.ws.rs.core.Response) InvocationCallback(javax.ws.rs.client.InvocationCallback) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact) Test(org.junit.Test)

Example 2 with Contact

use of org.jboss.as.quickstarts.jaxrsclient.model.Contact in project quickstart by wildfly.

the class ContactsRestClientIT method delayedInvocationTest.

// Shows how to use a delayed REST invocation
@Test
public void delayedInvocationTest() throws Exception {
    log.info("### Testing Delayed invocaton ###");
    // 1 - Drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(getRequestUrl()).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact Invocation
    log.info("Creating a new contact invocation");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Invocation saveContactInvocation = ClientBuilder.newClient().target(getRequestUrl()).request().buildPost(Entity.entity(c, MediaType.APPLICATION_JSON));
    // 3 - Create a new list Contacts Invocation
    log.info("Creating list all contacts invocation");
    Invocation listContactsInvocation = ClientBuilder.newClient().target(getRequestUrl()).request().buildGet();
    // 4 - Synch Save contact
    log.info("invoking the new contact");
    Contact persistedContact = saveContactInvocation.invoke(Contact.class);
    Assert.assertEquals("A contacts should be persisted with Id=1!", (Long) 1L, (Long) persistedContact.getId());
    // 5 - Async List contacts
    log.info("invoking list all contacts ASYNC");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    Future<List<Contact>> futureAllContacts = listContactsInvocation.submit(contactsListType);
    List<Contact> allContacts = futureAllContacts.get();
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Invocation(javax.ws.rs.client.Invocation) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact) Test(org.junit.Test)

Example 3 with Contact

use of org.jboss.as.quickstarts.jaxrsclient.model.Contact in project quickstart by wildfly.

the class ContactsRestClientIT method requestResponseFiltersTest.

// Shows how to use Request and Response filters
@Test
public void requestResponseFiltersTest() {
    log.info("### Testing Request and Response Filters ###");
    // 1 - Drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(getRequestUrl()).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact Invocation
    log.info("Invoking create new contact using a ClientRequestFilter");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Contact persistedContact = ClientBuilder.newClient().register(SavedByClientRequestFilter.class).target(getRequestUrl()).request().post(Entity.entity(c, MediaType.APPLICATION_JSON), Contact.class);
    Assert.assertEquals("A contact should be persisted with savedBy", SavedByClientRequestFilter.USERNAME, persistedContact.getSavedBy());
    // 3 - Fetch all Contacts
    log.info("Invoking list all contacts using a ClientResponseFilter");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    List<Contact> allContacts = ClientBuilder.newClient().register(LogResponseFilter.class).target(getRequestUrl()).request().get(contactsListType);
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact) Test(org.junit.Test)

Example 4 with Contact

use of org.jboss.as.quickstarts.jaxrsclient.model.Contact in project quickstart by wildfly.

the class ContactsRestClient method requestResponseFiltersTest.

// Shows how to use Request and Response filters
public void requestResponseFiltersTest() {
    log.info("### Testing Request and Response Filters ###");
    // 1 - Drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(REST_TARGET_URL).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact Invocation
    log.info("Invoking create new contact using a ClientRequestFilter");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Contact persistedContact = ClientBuilder.newClient().register(SavedByClientRequestFilter.class).target(REST_TARGET_URL).request().post(Entity.entity(c, MediaType.APPLICATION_JSON), Contact.class);
    Assert.assertEquals("A contact should be persisted with savedBy", SavedByClientRequestFilter.USERNAME, persistedContact.getSavedBy());
    // 3 - Fetch all Contacts
    log.info("Invoking list all contacts using a ClientResponseFilter");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    List<Contact> allContacts = ClientBuilder.newClient().register(LogResponseFilter.class).target(REST_TARGET_URL).request().get(contactsListType);
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact)

Example 5 with Contact

use of org.jboss.as.quickstarts.jaxrsclient.model.Contact in project quickstart by wildfly.

the class ContactsRestClient method delayedInvocationTest.

// Shows how to use a delayed REST invocation
public void delayedInvocationTest() throws Exception {
    log.info("### Testing Delayed invocaton ###");
    // 1 - Drop all contacts
    log.info("dropping all contacts");
    Response response = ClientBuilder.newClient().target(REST_TARGET_URL).request().delete();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact Invocation
    log.info("Creating a new contact invocation");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Invocation saveContactInvocation = ClientBuilder.newClient().target(REST_TARGET_URL).request().buildPost(Entity.entity(c, MediaType.APPLICATION_JSON));
    // 3 - Create a new list Contacts Invocation
    log.info("Creating list all contacts invocation");
    Invocation listContactsInvocation = ClientBuilder.newClient().target(REST_TARGET_URL).request().buildGet();
    // 4 - Synch Save contact
    log.info("invoking the new contact");
    Contact persistedContact = saveContactInvocation.invoke(Contact.class);
    Assert.assertEquals("A contacts should be persisted with Id=1!", (Long) 1L, (Long) persistedContact.getId());
    // 5 - Async List contacts
    log.info("invoking list all contacts ASYNC");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    Future<List<Contact>> futureAllContacts = listContactsInvocation.submit(contactsListType);
    List<Contact> allContacts = futureAllContacts.get();
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) Invocation(javax.ws.rs.client.Invocation) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact)

Aggregations

Contact (org.jboss.as.quickstarts.jaxrsclient.model.Contact)11 List (java.util.List)10 Response (javax.ws.rs.core.Response)10 GenericType (javax.ws.rs.core.GenericType)6 Test (org.junit.Test)5 Invocation (javax.ws.rs.client.Invocation)2 InvocationCallback (javax.ws.rs.client.InvocationCallback)2