Search in sources :

Example 6 with Contact

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

the class ContactsRestClient method invocationCallBackTest.

// This test shows how to use javax.ws.rs.client.InvocationCallback
public void invocationCallBackTest() throws Exception {
    log.info("### Testing invocation callback ###");
    // 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 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(REST_TARGET_URL).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)

Example 7 with Contact

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

the class ContactsRestClient method cruedTest.

// This test shows basic operations
public void cruedTest() {
    log.info("### CRUD tests ###");
    // 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
    log.info("creating a new contact");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Contact persistedContact = ClientBuilder.newClient().target(REST_TARGET_URL).request().post(Entity.entity(c, MediaType.APPLICATION_JSON), Contact.class);
    Assert.assertEquals("A book should be persisted with Id=1!", (Long) 1L, (Long) persistedContact.getId());
    // 3 - Fetch Contact by Id
    log.info("fetching a contact by id");
    Contact fetchContctById = ClientBuilder.newClient().target(REST_TARGET_URL).path("/{contactId}").resolveTemplate("contactId", persistedContact.getId()).request().get(Contact.class);
    Assert.assertEquals("Fetched book with Id=1!", (Long) 1L, (Long) fetchContctById.getId());
    Assert.assertEquals("Fetched book with equal name", CONTACT_NAME, fetchContctById.getName());
    Assert.assertEquals("Fetched book with equal phone", CONTACT_PHONE, fetchContctById.getPhoneNumber());
    // 4 - Fetch all Contacts
    log.info("fetching all contacts");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    List<Contact> allContacts = ClientBuilder.newClient().target(REST_TARGET_URL).request().get(contactsListType);
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
    // 5 - Delete a Contact
    log.info("delete a contact by id");
    response = ClientBuilder.newClient().target(REST_TARGET_URL).path("/{contactId}").resolveTemplate("contactId", persistedContact.getId()).request().delete();
    Assert.assertEquals("Contact 1 should be dropped", Response.ok().build().getStatus(), response.getStatus());
}
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 8 with Contact

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

the class ContactsRestClient method asyncCrudTest.

// This test shows some basic operations using ASYNC invocations and java.util.concurrent.Future
public void asyncCrudTest() throws Exception {
    log.info("### CRUD tests  ASYNC ###");
    // 1 - drop all contacts ASYNC
    log.info("dropping all contacts ASYNC");
    Response response = ClientBuilder.newClient().target(REST_TARGET_URL).request().async().delete().get();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact ASYNC
    log.info("creating a new contact ASYNC");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Future<Contact> futureContact = ClientBuilder.newClient().target(REST_TARGET_URL).request().async().post(Entity.entity(c, MediaType.APPLICATION_JSON), Contact.class);
    Contact persistedContact = futureContact.get();
    Assert.assertEquals("A contact should be persisted with Id=1!", (Long) 1L, (Long) persistedContact.getId());
    // 3 - Delete a contact ASYNC
    log.info("delete a contact by id ASYNC");
    ClientBuilder.newClient().target(REST_TARGET_URL).path("{contactId}").resolveTemplate("contactId", persistedContact.getId()).request().async().delete().get();
    // 4 - Fetch All Contacts ASYNC
    log.info("fetching all contacts ASYNC");
    Future<List<Contact>> futureContacts = ClientBuilder.newClient().target(REST_TARGET_URL).request().async().get(new GenericType<List<Contact>>() {
    });
    List<Contact> allContacts = futureContacts.get();
    Assert.assertEquals("Should have no contacts", 0, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact)

Example 9 with Contact

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

the class ContactsRestClientIT method asyncCrudTest.

// This test shows some basic operations using ASYNC invocations and java.util.concurrent.Future
@Test
public void asyncCrudTest() throws Exception {
    log.info("### CRUD tests  ASYNC ###");
    // 1 - drop all contacts ASYNC
    log.info("dropping all contacts ASYNC");
    Response response = ClientBuilder.newClient().target(getRequestUrl()).request().async().delete().get();
    Assert.assertEquals("All contacts should be dropped", Response.ok().build().getStatus(), response.getStatus());
    // 2 - Create a new Contact ASYNC
    log.info("creating a new contact ASYNC");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Future<Contact> futureContact = ClientBuilder.newClient().target(getRequestUrl()).request().async().post(Entity.entity(c, MediaType.APPLICATION_JSON), Contact.class);
    Contact persistedContact = futureContact.get();
    Assert.assertEquals("A contact should be persisted with Id=1!", (Long) 1L, (Long) persistedContact.getId());
    // 3 - Delete a contact ASYNC
    log.info("delete a contact by id ASYNC");
    ClientBuilder.newClient().target(getRequestUrl()).path("{contactId}").resolveTemplate("contactId", persistedContact.getId()).request().async().delete().get();
    // 4 - Fetch All Contacts ASYNC
    log.info("fetching all contacts ASYNC");
    Future<List<Contact>> futureContacts = ClientBuilder.newClient().target(getRequestUrl()).request().async().get(new GenericType<List<Contact>>() {
    });
    List<Contact> allContacts = futureContacts.get();
    Assert.assertEquals("Should have no contacts", 0, allContacts.size());
}
Also used : Response(javax.ws.rs.core.Response) List(java.util.List) Contact(org.jboss.as.quickstarts.jaxrsclient.model.Contact) Test(org.junit.Test)

Example 10 with Contact

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

the class ContactsRestClientIT method cruedTest.

// This test shows basic operations
@Test
public void cruedTest() {
    log.info("### CRUD tests ###");
    // 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
    log.info("creating a new contact");
    Contact c = new Contact();
    c.setName(CONTACT_NAME);
    c.setPhoneNumber(CONTACT_PHONE);
    Contact persistedContact = ClientBuilder.newClient().target(getRequestUrl()).request().post(Entity.entity(c, MediaType.APPLICATION_JSON), Contact.class);
    Assert.assertEquals("A book should be persisted with Id=1!", (Long) 1L, (Long) persistedContact.getId());
    // 3 - Fetch Contact by Id
    log.info("fetching a contact by id");
    Contact fetchContctById = ClientBuilder.newClient().target(getRequestUrl()).path("/{contactId}").resolveTemplate("contactId", persistedContact.getId()).request().get(Contact.class);
    Assert.assertEquals("Fetched book with Id=1!", (Long) 1L, (Long) fetchContctById.getId());
    Assert.assertEquals("Fetched book with equal name", CONTACT_NAME, fetchContctById.getName());
    Assert.assertEquals("Fetched book with equal phone", CONTACT_PHONE, fetchContctById.getPhoneNumber());
    // 4 - Fetch all Contacts
    log.info("fetching all contacts");
    GenericType<List<Contact>> contactsListType = new GenericType<List<Contact>>() {
    };
    List<Contact> allContacts = ClientBuilder.newClient().target(getRequestUrl()).request().get(contactsListType);
    Assert.assertEquals("Should have a single contact", 1, allContacts.size());
    // 5 - Delete a Contact
    log.info("delete a contact by id");
    response = ClientBuilder.newClient().target(getRequestUrl()).path("/{contactId}").resolveTemplate("contactId", persistedContact.getId()).request().delete();
    Assert.assertEquals("Contact 1 should be dropped", Response.ok().build().getStatus(), response.getStatus());
}
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)

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