Spring3 and REST Integration(II)

Spring3 and REST Integration(II)

5. How to test that service
I use RestTemplate to communicate with REST services, just now, I configured that in
controller-easyrestproxy-context.xml file:
<!-- Client -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>

RestTemplate supports other methods, including:
exchange: executes certain HTTP methods with request body and gets the response.
getForObject: executes the HTTP GET method and gets the response as an object.
postForObject: executes the HTTP POST method with a certain request body.
put: executes the HTTP PUT method with a certain request body.
delete: executes the HTTP DELETE method for a certain URI.

6. My junit test case will be like this PersonControllerTest.java:
package com.sillycat.easyrestproxy.controller;

import java.util.Date;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import com.sillycat.core.BaseTestCase;
import com.sillycat.easyjpa.model.Person;
import com.sillycat.easyrestproxy.model.PersonList;

public class PersonControllerTest extends BaseTestCase
{

    private RestTemplate        restTemplate;

    private static final String SERVER_URL = "http://localhost:8080/easyrestproxy";

    private Person              item;

    private HttpEntity<String>  entity_get;

    public void setUp() throws Exception
    {
        super.setUp();
        restTemplate = (RestTemplate) ctx.getBean("restTemplate");
        item = getItem();
        entity_get = prepareGet(MediaType.APPLICATION_JSON);
    }

    public void tearDown() throws Exception
    {
        super.tearDown();
        if (item != null && item.getId() != null)
        {
            restTemplate.delete(SERVER_URL + "/service/person/{id}", item.getId());
        }
    }

    public void testDumy()
    {
        assertNotNull(true);
    }

    public void testGet()
    {
        ResponseEntity<Person> response = restTemplate.exchange(SERVER_URL + "/service/person/1", HttpMethod.GET, entity_get, Person.class);
        assertNotNull(response);
        Person p = response.getBody();
        assertNotNull(p);
        assertNotNull(p.getId());
        assertNotNull(p.getName());
    }

    public void testGetAll()
    {
        ResponseEntity<PersonList> response = restTemplate.exchange(SERVER_URL + "/service/persons", HttpMethod.GET, entity_get, PersonList.class);

        assertNotNull(response);
        PersonList persons = response.getBody();
        assertNotNull(persons);
        assertTrue(persons.getAmount() > 0);
        List<Person> list = persons.getPersons();
        for (int i = 0; i < list.size(); i++)
        {
            Person p = list.get(i);
            assertNotNull(p);
            assertNotNull(p.getId());
            assertNotNull(p.getName());
        }
    }

    public void testAdd()
    {
        HttpEntity<Person> entity = new HttpEntity<Person>(item);
        ResponseEntity<Person> response = restTemplate.postForEntity(SERVER_URL + "/service/person", entity, Person.class);
        assertNotNull(response);
        Person p = response.getBody();
        assertNotNull(p);
        assertNotNull(p.getId());
        item.setId(p.getId());
    }

    public void testUpdate()
    {
        // add
        HttpEntity<Person> entity = new HttpEntity<Person>(item);
        ResponseEntity<Person> response = restTemplate.postForEntity(SERVER_URL + "/service/person", entity, Person.class);
        assertNotNull(response);
        Person p = response.getBody();
        assertNotNull(p);
        assertNotNull(p.getId());
        item.setId(p.getId());

        // update
        item.setName("fire in the hole");
        entity = new HttpEntity<Person>(item);
        restTemplate.put(SERVER_URL + "/service/person/{id}", entity, item.getId());

        // get
        response = restTemplate.exchange(SERVER_URL + "/service/person/" + item.getId(), HttpMethod.GET, entity_get, Person.class);
        assertNotNull(response);
        Person t = response.getBody();
        assertNotNull(t);
        assertNotNull(t.getId());
        assertNotNull(t.getName());
        assertEquals(t.getName(), "fire in the hole");
    }

    private Person getItem()
    {
        Person p = new Person();
        p.setAge((short) 1);
        p.setBirthday(new Date());
        p.setName("json");
        return p;
    }

    private HttpEntity<String> prepareGet(MediaType type)
    {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(type);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        return entity;
    }

}

That will work fine. My sample project named easyrestproxy.

references:
http://www.ibm.com/developerworks/web/library/wa-spring3webserv/index.html
http://www.ibm.com/developerworks/web/library/wa-restful/

你可能感兴趣的:(json,Web,IBM,REST,Exchange)