Package com.sun.jersey.samples.contacts.models

Examples of com.sun.jersey.samples.contacts.models.Contact


        super.tearDown();
    }

    @Test
    public void testCreateUpdateDeleteContact() {
        Contact contact = new Contact();
        contact.setId("new_id");
        contact.setName("New Name");
        contact.setContent("New Content");
        client.createContact("admin", contact);
        contact = client.findContact("admin", "new_id");
        assertNotNull(contact);
        assertEquals("new_id", contact.getId());
        assertEquals("New Name", contact.getName());
        assertEquals("New Content", contact.getContent());
        List<Contact> contacts = client.findContacts("admin");
        assertEquals(1, contacts.size());
        contact = contacts.get(0);
        assertEquals("new_id", contact.getId());
        assertEquals("New Name", contact.getName());
        assertEquals("New Content", contact.getContent());
        contact.setName("Updated Name");
        contact.setContent("Updated Content");
        client.updateContact("admin", contact);
        contact = client.findContact("admin", "new_id");
        assertEquals("new_id", contact.getId());
        assertEquals("Updated Name", contact.getName());
        assertEquals("Updated Content", contact.getContent());
        contacts = client.findContacts("admin");
        assertEquals(1, contacts.size());
        contact = contacts.get(0);
        assertEquals("new_id", contact.getId());
        assertEquals("Updated Name", contact.getName());
        assertEquals("Updated Content", contact.getContent());
        client.deleteContact("admin", "new_id");
        contacts = client.findContacts("admin");
        assertEquals(0, contacts.size());
        try {
            client.findContact("admin", "new_id");
View Full Code Here


               "application/xml",
               "text/xml"})
    public Response post(Entry entry) {

        // Validate the incoming user information independent of the database
        Contact contact = Contact.fromEntry(entry);
        StringBuilder errors = new StringBuilder();
        if (contact.getContent() == null) {
            errors.append("Missing 'content' property\r\n");
        }
        if ((contact.getId() == null) || (contact.getId().length() < 1)) {
            contact.setId(UUID.randomUUID().toString());
        }
        if ((contact.getName() == null) || (contact.getName().length() < 1)) {
            errors.append("Missing 'name' property\r\n");
        }
        contact.setUpdated(new Date());

        if (errors.length() > 0) {
            return Response.status(400).
                    type("text/plain").
                    entity(errors.toString()).build();
        }

        // Validate conditions that require locking the database
        synchronized (Database.contacts) {

            // Verify user is valid and no contact with this id exists
            List<Contact> contacts = Database.contacts.get(username);
            if (contacts == null) {
                return Response.status(404).
                        type("text/plain").
                        entity("No contacts for user '" + username + "'\r\n").build();
            }
            for (Contact existing : contacts) {
                if (existing.getId().equals(contact.getId())) {
                    return Response.status(409).
                            type("text/plain").
                            entity("Contact with this ID already exists\r\n").build();
                }
            }

            // Update the database with the new contact
            contacts.add(contact);
            return Response.created(uriInfo.getRequestUriBuilder().path(contact.getId()).build()).
                    build();

        }

    }
View Full Code Here

               "application/xml",
               "text/xml"})
    public Response put(Entry entry) {

        // Validate the incoming user information independent of the database
        Contact contact = Contact.fromEntry(entry);
        StringBuilder errors = new StringBuilder();
        if (contact.getContent() == null) {
            errors.append("Missing 'content' property\r\n");
        }
        if ((contact.getName() == null) || (contact.getName().length() < 1)) {
            errors.append("Missing 'name' property\r\n");
        }
        contact.setUpdated(new Date());

        if (errors.length() > 0) {
            return Response.status(400).
                    type("text/plain").
                    entity(errors.toString()).build();
        }

        // Validate conditions that require locking the database
        synchronized (Database.contacts) {

            // Look up the original contact information
            List<Contact> contacts = Database.contacts.get(username);
            if (contacts == null) {
                return Response.status(404).
                        type("text/plain").
                        entity("No contacts for user '" + username + "'").
                        build();
            }
            Contact original = null;
            for (int i = 0; i < contacts.size(); i++) {
                if (id.equals(contacts.get(i).getId())) {
                    original = contacts.get(i);
                }
            }
            if (original == null) {
                return Response.status(404).
                        type("text/plain").
                        entity("No contact for user '" + username + "'\r\n").
                        build();
            }

            // Update the original contact information
            original.updateFrom(contact);
            return Response.
                    ok().
                    build();

        }
View Full Code Here

            System.out.println("Creating user bob with one contact");
        }

        client.createUser(bob);

        Contact c = new Contact();
        c.setName("bobs");
        c.setContent("Bob contacts");

        EmailAddress email = new EmailAddress("bob@example.com", "work e-mail", true, "work");

        c.getEmailAddresses().add(email);

        client.createContact(bob.getUsername(), c);
    }
View Full Code Here

    }

    @Test
    public void testPutEntryPositive() {
        String credentials = adminCredentials();
        Contact contact = null;
        Entry entry = null;
        Feed feed = null;
        for (String mediaType : ENTRY_MEDIA_TYPES) {
            addEntry(credentials, mediaType, "admin", "new_id");
            entry = getEntry(credentials, mediaType, "admin", "new_id");
            contact = Contact.fromEntry(entry);
            assertEquals("New content for media type " + mediaType, "new content", contact.getContent());
            assertEquals("New id for media type " + mediaType, "new_id", contact.getId());
            assertEquals("New title for media type " + mediaType, "new name", contact.getName());
            feed = getFeed(credentials, "application/atom+xml;type=feed", "admin");
            assertEquals("After entries for media type " + mediaType, 1, feed.getEntries().size());
            contact.setName("updated name");
            putEntry(credentials, mediaType, "admin", contact);
            entry = getEntry(credentials, mediaType, "admin", "new_id");
            contact = Contact.fromEntry(entry);
            assertEquals("Updated name for media type " + mediaType, "updated name", contact.getName());
            deleteEntry(credentials, mediaType, "admin", feed.getEntries().get(0).getId().toString());
            feed = getFeed(credentials, "application/atom+xml;type=feed", "admin");
            assertEquals("Cleaned entries for media type " + mediaType, 0, feed.getEntries().size());
        }
    }
View Full Code Here

            assertEquals("Cleaned entries for media type " + mediaType, 0, feed.getEntries().size());
        }
    }

    private void addEntry(String credentials, String mediaType, String username, String id) {
        Contact contact = new Contact();
        contact.setContent("new content");
        contact.setId(id);
        contact.setName("new name");
        contact.setUpdated(new Date());
        postEntry(credentials, mediaType, username, contact);
    }
View Full Code Here

TOP

Related Classes of com.sun.jersey.samples.contacts.models.Contact

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.