ContactsClient { public List<Contact> findContacts(String username) { Feed feed = null; try { feed = service. path("contacts"). path(username). accept(FEED_MEDIA_TYPE). header(AUTHENTICATION_HEADER, authentication). get(Feed.class); } catch (UniformInterfaceException e) { if (e.getResponse().getStatus() == 401) { throw new IllegalStateException(e); } else if (e.getResponse().getStatus() == 403) { throw new IllegalStateException(e); } else if (e.getResponse().getStatus() == 404) { throw new IllegalArgumentException("Username '" + username + "' does not exist"); } else { throw e; } } List<Contact> results = new ArrayList<Contact>(); for (Entry entry : feed.getEntries()) { results.add(Contact.fromEntry(entry)); } return results; } ContactsClient(String uri, String username, String password); String createContact(String username, Contact contact); void deleteContact(String username, String id); Contact findContact(String username, String id); List<Contact> findContacts(String username); void updateContact(String username, Contact contact); boolean doesUserExist(String username); String createUser(User user); void deleteUser(String username); User findUser(String username); List<User> findUsers(); void updateUser(User user); String encodeCredentialsBasic(String username, String password); static void main(String[] args); static final URI BASE_URI; }
@Test public void testFindContactsNegative() { try { client.findContacts("badusername"); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException e) { } }
@Test public void testFindContactsPositive() { List<Contact> contacts = client.findContacts("admin"); assertEquals(0, contacts.size()); }