Skip to content

Commit

Permalink
more tests, lots of polish.
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Long committed Aug 27, 2011
1 parent 3f116ca commit 1d5bfe4
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 196 deletions.
Expand Up @@ -15,18 +15,41 @@
*/
package org.springframework.http.converter.obm;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.obm.support.BaseMarshallingHttpMessageConverterTest;
import org.springframework.obm.Marshaller;
import org.springframework.obm.avro.AvroMarshaller;
import org.springframework.obm.avro.crm.Customer;
import org.springframework.stereotype.Controller;
import org.springframework.util.IntegrationTestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.HashMap;
import java.util.Map;

/**
* test the {@link org.springframework.http.converter.HttpMessageConverter avro http converter}
*
* @author Josh Long
*/
public class AvroHttpMessageConverterTest extends BaseMarshallingHttpMessageConverterTest {
static MediaType MEDIA_TYPE = new MediaType("avro", "binary");

private Log log = LogFactory.getLog(getClass());

private Customer customer = new Customer();

Expand All @@ -41,7 +64,7 @@ public void before() throws Throwable {

setMarshaller(marshaller);
setUnmarshaller(marshaller);
setMediaType(new MediaType("avro", "binary"));
setMediaType(MEDIA_TYPE);
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
setHttpMessageConverter(converter);
}
Expand All @@ -51,4 +74,68 @@ public void testAvroMarshaller() throws Throwable {
doTestHttpWriting(customer.getClass(), customer);
}

static private String fn = "george", ln = "harrison", email = "geore@email.com";

@Test
public void testSimpleIntegration() throws Throwable {
IntegrationTestUtils.startServiceAndConnect(MyService.class, new IntegrationTestUtils.ServerExecutionCallback() {
@Override
public void doWithServer(RestTemplate restTemplate, Server server) throws Throwable {
Assert.assertNotNull(restTemplate);

int id = 344;
Map<String, Object> mapOfVars = new HashMap<String, Object>();
mapOfVars.put("cid", id);

Customer customer = restTemplate.getForEntity("http://localhost:8080/ws/customers/{cid}", Customer.class, mapOfVars).getBody();
Assert.assertTrue(customer.id == id);
Assert.assertTrue(customer.firstName.toString().equals(fn));
Assert.assertTrue(customer.lastName.toString().equals(ln));
Assert.assertTrue(customer.email.toString().equals(email));

if (log.isDebugEnabled()) {
log.debug("response payload: " + ToStringBuilder.reflectionToString(customer));
}

}
});

}

@Configuration
@EnableWebMvc
static public class MyService extends IntegrationTestUtils.AbstractRestServiceConfiguration {
@Bean
public CustomerController controller() {
return new CustomerController();
}

@Override
public Marshaller getMarshaller() {
return new AvroMarshaller();
}

@Override
public MediaType getMediaType() {
return MEDIA_TYPE;
}
}


@Controller
@RequestMapping(value = "/ws/")
public static class CustomerController {
@RequestMapping(value = "/customers/{id}", method = RequestMethod.GET)
@ResponseBody
public Customer customer(@PathVariable("id") int id) {

Customer avroCustomer = new Customer();
avroCustomer.id = id;
avroCustomer.firstName = fn;
avroCustomer.lastName = ln;
avroCustomer.email = email;
return avroCustomer;
}
}

}

This file was deleted.

Expand Up @@ -16,30 +16,53 @@

package org.springframework.http.converter.obm;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.obm.support.BaseMarshallingHttpMessageConverterTest;
import org.springframework.obm.Marshaller;
import org.springframework.obm.messagepack.Cat;
import org.springframework.obm.messagepack.MessagePackMarshaller;
import org.springframework.obm.thrift.ThriftMarshaller;
import org.springframework.stereotype.Controller;
import org.springframework.util.IntegrationTestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.HashMap;
import java.util.Map;

/**
* @author Josh Long
*/
public class MessagePackHttpMessageConverterTest extends BaseMarshallingHttpMessageConverterTest {

static MediaType MEDIA_TYPE = new MediaType("application", "x-msgpack") ;
private Log log = LogFactory.getLog(getClass()) ;
private Cat cat = new Cat();

@Before
public void before() throws Throwable {
cat.setAge(4);
cat.setId(4);
cat.setName("Felix");

MessagePackMarshaller marshaller = new MessagePackMarshaller();
marshaller.afterPropertiesSet();

setMarshaller(marshaller);
setUnmarshaller(marshaller);
setMediaType(new MediaType("application", "x-msgpack"));
setMediaType(MEDIA_TYPE);
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller);
setHttpMessageConverter(converter);
}
Expand All @@ -48,5 +71,61 @@ public void before() throws Throwable {
public void testHttpReading() throws Throwable {
doTestHttpWriting(cat.getClass(), cat);
}


@Test
public void testSimpleIntegration() throws Throwable {
IntegrationTestUtils.startServiceAndConnect(MyService.class , new IntegrationTestUtils.ServerExecutionCallback(){
@Override
public void doWithServer(RestTemplate clientRestTemplate, Server server) throws Throwable {

Assert.assertNotNull(clientRestTemplate);

int id = 344;
Map<String, Object> mapOfVars = new HashMap<String, Object>();
mapOfVars.put("cat", id);

Cat customer = clientRestTemplate.getForEntity("http://localhost:8080/ws/cats/{cat}", Cat.class, mapOfVars).getBody();
Assert.assertTrue(customer.getId() == id);
Assert.assertNotNull(customer.getName());

if (log.isDebugEnabled()) {
log.debug("response payload: " + ToStringBuilder.reflectionToString(customer));
}

}
});

}

@Configuration
@EnableWebMvc
static public class MyService extends IntegrationTestUtils.AbstractRestServiceConfiguration {
@Bean
public CatController controller() {
return new CatController();
}

@Override
public Marshaller getMarshaller() {
return new MessagePackMarshaller();
}

@Override
public MediaType getMediaType() {
return MEDIA_TYPE;
}
}


@Controller
@RequestMapping(value = "/ws/")
public static class CatController {
@RequestMapping(value = "/cats/{id}", method = RequestMethod.GET)
@ResponseBody
public Cat customer(@PathVariable("id") int id) {
return new Cat(Math.random() > .5 ? "Felix" :"Garfield",id);
}
}
}

0 comments on commit 1d5bfe4

Please sign in to comment.