From 1d5bfe4b412d466f47eba3c081bff56e59885c61 Mon Sep 17 00:00:00 2001 From: Josh Long Date: Sat, 27 Aug 2011 02:15:51 -0700 Subject: [PATCH] more tests, lots of polish. --- .../obm/AvroHttpMessageConverterTest.java | 89 ++++++++++++++++- ...seMarshallingHttpMessageConverterTest.java | 97 ------------------- .../MessagePackHttpMessageConverterTest.java | 83 +++++++++++++++- .../ProtocolBuffersMessageConverterTest.java | 65 +------------ .../obm/ThriftHttpMessageConverterTest.java | 33 ++++--- .../springframework/obm/messagepack/Cat.java | 18 ++-- .../TestMessagePackMarshaller.java | 2 +- ...cherServletJettyConfigurationCallback.java | 2 +- .../util/EndpointTestUtils.java | 10 +- 9 files changed, 203 insertions(+), 196 deletions(-) delete mode 100644 obm/src/test/java/org/springframework/http/converter/obm/BaseMarshallingHttpMessageConverterTest.java diff --git a/obm/src/test/java/org/springframework/http/converter/obm/AvroHttpMessageConverterTest.java b/obm/src/test/java/org/springframework/http/converter/obm/AvroHttpMessageConverterTest.java index f784cb5..5477006 100644 --- a/obm/src/test/java/org/springframework/http/converter/obm/AvroHttpMessageConverterTest.java +++ b/obm/src/test/java/org/springframework/http/converter/obm/AvroHttpMessageConverterTest.java @@ -15,11 +15,31 @@ */ 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} @@ -27,6 +47,9 @@ * @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(); @@ -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); } @@ -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 mapOfVars = new HashMap(); + 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; + } + } + } \ No newline at end of file diff --git a/obm/src/test/java/org/springframework/http/converter/obm/BaseMarshallingHttpMessageConverterTest.java b/obm/src/test/java/org/springframework/http/converter/obm/BaseMarshallingHttpMessageConverterTest.java deleted file mode 100644 index 01aa688..0000000 --- a/obm/src/test/java/org/springframework/http/converter/obm/BaseMarshallingHttpMessageConverterTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.http.converter.obm; - -import junit.framework.Assert; -import org.junit.Test; -import org.mockito.Mockito; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpInputMessage; -import org.springframework.http.HttpOutputMessage; -import org.springframework.http.MediaType; -import org.springframework.obm.BaseMarshallerTest; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; - -/** - * Tests the {@link org.springframework.http.converter.obm.MarshallingHttpMessageConverter} - * - * @author Josh Long - */ -public class BaseMarshallingHttpMessageConverterTest extends BaseMarshallerTest { - protected MarshallingHttpMessageConverter marshallingHttpMessageConverter; - - protected MediaType mediaType; - - public void setMediaType(MediaType mediaType) { - this.mediaType = mediaType; - } - - public void setHttpMessageConverter(MarshallingHttpMessageConverter marshallingHttpMessageConverter) { - this.marshallingHttpMessageConverter = marshallingHttpMessageConverter; - } - - private void setup() { - Assert.assertNotNull(this.marshallingHttpMessageConverter); - Assert.assertNotNull(this.marshaller); - Assert.assertNotNull(this.unmarshaller); - Assert.assertNotNull(this.mediaType); - } - - protected void doTestHttpWriting(Class clazz, Object output) throws Throwable { - setup(); - - Assert.assertNotNull("object to output can't be null", output); - - Assert.assertTrue("the thriftMarshaller must be able to read this class ", - marshallingHttpMessageConverter.supports(clazz) && - this.unmarshaller.supports(clazz)); - HttpHeaders headers = Mockito.mock(HttpHeaders.class); - HttpOutputMessage httpOutputMessage = Mockito.mock(HttpOutputMessage.class); - Mockito.when(httpOutputMessage.getHeaders()).thenReturn(headers); - Mockito.when(headers.getContentType()).thenReturn(this.mediaType); - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - Mockito.when(httpOutputMessage.getBody()).thenReturn(byteArrayOutputStream); - marshallingHttpMessageConverter.write(output, this.mediaType, httpOutputMessage); - byteArrayOutputStream.flush(); - byteArrayOutputStream.close(); - byte[] bytesWritten = byteArrayOutputStream.toByteArray(); - - doTestHttpReading(clazz, output, new ByteArrayInputStream(bytesWritten)); - } - - private void doTestHttpReading(Class clazz, Object output, InputStream inputStream) throws Throwable { - setup(); - Assert.assertNotNull("object to output can't be null", output); - Assert.assertTrue("the thriftMarshaller must be able to read this class ", - marshallingHttpMessageConverter.supports(clazz) && - this.unmarshaller.supports(clazz)); - HttpHeaders headers = Mockito.mock(HttpHeaders.class); - HttpInputMessage inputMessage = Mockito.mock(HttpInputMessage.class); - Mockito.when(headers.getContentType()).thenReturn(this.mediaType); - Mockito.when(inputMessage.getHeaders()).thenReturn(headers); - Mockito.when(inputMessage.getBody()).thenReturn(inputStream); - Object result = marshallingHttpMessageConverter.read(clazz, inputMessage); - Assert.assertNotNull(result); - Assert.assertEquals("the resulting objects must be .equals()", result, output); - } - - @Test - public void doFoo() { - } -} diff --git a/obm/src/test/java/org/springframework/http/converter/obm/MessagePackHttpMessageConverterTest.java b/obm/src/test/java/org/springframework/http/converter/obm/MessagePackHttpMessageConverterTest.java index d60f2f5..ea7b81a 100644 --- a/obm/src/test/java/org/springframework/http/converter/obm/MessagePackHttpMessageConverterTest.java +++ b/obm/src/test/java/org/springframework/http/converter/obm/MessagePackHttpMessageConverterTest.java @@ -16,22 +16,45 @@ 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(); @@ -39,7 +62,7 @@ public void before() throws Throwable { setMarshaller(marshaller); setUnmarshaller(marshaller); - setMediaType(new MediaType("application", "x-msgpack")); + setMediaType(MEDIA_TYPE); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(marshaller); setHttpMessageConverter(converter); } @@ -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 mapOfVars = new HashMap(); + 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); + } + } } diff --git a/obm/src/test/java/org/springframework/http/converter/obm/ProtocolBuffersMessageConverterTest.java b/obm/src/test/java/org/springframework/http/converter/obm/ProtocolBuffersMessageConverterTest.java index 3f9e983..a43a651 100644 --- a/obm/src/test/java/org/springframework/http/converter/obm/ProtocolBuffersMessageConverterTest.java +++ b/obm/src/test/java/org/springframework/http/converter/obm/ProtocolBuffersMessageConverterTest.java @@ -10,6 +10,7 @@ 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.util.IntegrationTestUtils; import org.springframework.obm.Marshaller; import org.springframework.obm.protocolbuffers.ProtocolBuffersMarshaller; @@ -61,69 +62,5 @@ public void before() throws Throwable { public void testHttpReading() throws Throwable { doTestHttpWriting(customer.getClass(), this.customer); } - - @Test - public void testSimpleIntegration() throws Throwable { - - Map tupleOfClientAndServer = IntegrationTestUtils.startServiceAndConnect(MyService.class); - RestTemplate clientRestTemplate = tupleOfClientAndServer.keySet().iterator().next(); - Server server = tupleOfClientAndServer.values().iterator().next(); - - Assert.assertNotNull(clientRestTemplate); - - Map mapOfVars = new HashMap(); - mapOfVars.put("customerId", 3); - - Customer customer = clientRestTemplate.getForEntity("http://localhost:8080/ws/customers/{customerId}", Customer.class, mapOfVars).getBody(); - Assert.assertNotNull(customer.getFirstName()); - Assert.assertNotNull(customer.getLastName()); - Assert.assertNotNull(customer.getEmail()); - - if (log.isDebugEnabled()) { - log.debug("response payload: " + ToStringBuilder.reflectionToString(customer)); - } - - IntegrationTestUtils.stopServerQuietly(server) ; - } - - @Configuration - @EnableWebMvc - static public class MyService extends IntegrationTestUtils.AbstractRestServiceConfiguration { - @Bean - public CrmRestController controller() { - return new CrmRestController(); - } - - @Bean - public ThriftCrmService crmService() { - return new ThriftCrmService(); - } - - @Override - public Marshaller getMarshaller() { - return new ThriftMarshaller(); - } - - @Override - public MediaType getMediaType() { - return MEDIA_TYPE; - } - } - - @Controller - @RequestMapping(value = "/ws/") - public static class CrmRestController { - - @Inject - private ThriftCrmService crmService; - - @RequestMapping(value = "/customers/{id}", method = RequestMethod.GET) - @ResponseBody - public Customer customer(@PathVariable("id") int id) { - return crmService.getCustomerById(id); - } - } - - } diff --git a/obm/src/test/java/org/springframework/http/converter/obm/ThriftHttpMessageConverterTest.java b/obm/src/test/java/org/springframework/http/converter/obm/ThriftHttpMessageConverterTest.java index 7fea08e..50b759b 100644 --- a/obm/src/test/java/org/springframework/http/converter/obm/ThriftHttpMessageConverterTest.java +++ b/obm/src/test/java/org/springframework/http/converter/obm/ThriftHttpMessageConverterTest.java @@ -26,12 +26,13 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; -import org.springframework.util.IntegrationTestUtils; +import org.springframework.http.converter.obm.support.BaseMarshallingHttpMessageConverterTest; import org.springframework.obm.Marshaller; import org.springframework.obm.thrift.ThriftCrmService; import org.springframework.obm.thrift.ThriftMarshaller; import org.springframework.obm.thrift.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; @@ -74,25 +75,25 @@ public void testHttpReading() throws Throwable { @Test public void testSimpleIntegration() throws Throwable { - Map tupleOfClientAndServer = IntegrationTestUtils.startServiceAndConnect(MyService.class); - RestTemplate clientRestTemplate = tupleOfClientAndServer.keySet().iterator().next(); - Server server = tupleOfClientAndServer.values().iterator().next(); + IntegrationTestUtils.startServiceAndConnect(MyService.class, new IntegrationTestUtils.ServerExecutionCallback() { + @Override + public void doWithServer(RestTemplate clientRestTemplate, Server server) throws Throwable { - Assert.assertNotNull(clientRestTemplate); + Assert.assertNotNull(clientRestTemplate); - Map mapOfVars = new HashMap(); - mapOfVars.put("customerId", 3); + Map mapOfVars = new HashMap(); + mapOfVars.put("customerId", 3); - Customer customer = clientRestTemplate.getForEntity("http://localhost:8080/ws/customers/{customerId}", Customer.class, mapOfVars).getBody(); - Assert.assertNotNull(customer.getFirstName()); - Assert.assertNotNull(customer.getLastName()); - Assert.assertNotNull(customer.getEmail()); - - if (log.isDebugEnabled()) { - log.debug("response payload: " + ToStringBuilder.reflectionToString(customer)); - } + Customer customer = clientRestTemplate.getForEntity("http://localhost:8080/ws/customers/{customerId}", Customer.class, mapOfVars).getBody(); + Assert.assertNotNull(customer.getFirstName()); + Assert.assertNotNull(customer.getLastName()); + Assert.assertNotNull(customer.getEmail()); - IntegrationTestUtils.stopServerQuietly(server ) ; + if (log.isDebugEnabled()) { + log.debug("response payload: " + ToStringBuilder.reflectionToString(customer)); + } + } + }); } @Configuration diff --git a/obm/src/test/java/org/springframework/obm/messagepack/Cat.java b/obm/src/test/java/org/springframework/obm/messagepack/Cat.java index a896b46..238bf22 100644 --- a/obm/src/test/java/org/springframework/obm/messagepack/Cat.java +++ b/obm/src/test/java/org/springframework/obm/messagepack/Cat.java @@ -8,7 +8,7 @@ public class Cat { private String name; - private int age; + private int id; public String getName() { return name; @@ -18,17 +18,17 @@ public void setName(String name) { this.name = name; } - public int getAge() { - return age; + public int getId() { + return id; } - public void setAge(int age) { - this.age = age; + public void setId(int id) { + this.id = id; } - public Cat(String name, int age) { + public Cat(String name, int id) { this.name = name; - this.age = age; + this.id = id; } public Cat() { @@ -45,14 +45,14 @@ public boolean equals(Object o) { Cat that = (Cat) o; - return age == that.age && !(name != null ? !name.equals(that.name) : that.name != null); + return id == that.id && !(name != null ? !name.equals(that.name) : that.name != null); } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; - result = 31 * result + age; + result = 31 * result + id; return result; } } \ No newline at end of file diff --git a/obm/src/test/java/org/springframework/obm/messagepack/TestMessagePackMarshaller.java b/obm/src/test/java/org/springframework/obm/messagepack/TestMessagePackMarshaller.java index 1156d04..9e88679 100644 --- a/obm/src/test/java/org/springframework/obm/messagepack/TestMessagePackMarshaller.java +++ b/obm/src/test/java/org/springframework/obm/messagepack/TestMessagePackMarshaller.java @@ -16,7 +16,7 @@ public class TestMessagePackMarshaller extends BaseMarshallerTest { @Before public void before() throws Throwable { - cat.setAge(4); + cat.setId(4); cat.setName("Felix"); MessagePackMarshaller messagePackMarshaller = new MessagePackMarshaller(); diff --git a/obm/src/test/java/org/springframework/util/DispatcherServletJettyConfigurationCallback.java b/obm/src/test/java/org/springframework/util/DispatcherServletJettyConfigurationCallback.java index 95c4c9f..ba73268 100644 --- a/obm/src/test/java/org/springframework/util/DispatcherServletJettyConfigurationCallback.java +++ b/obm/src/test/java/org/springframework/util/DispatcherServletJettyConfigurationCallback.java @@ -12,7 +12,7 @@ * * @author Josh Long */ -public class DispatcherServletJettyConfigurationCallback implements EndpointTestUtils.JettyContextConfigurationCallback { +public class DispatcherServletJettyConfigurationCallback implements JettyContextConfigurationCallback { private Class configurationClass; diff --git a/obm/src/test/java/org/springframework/util/EndpointTestUtils.java b/obm/src/test/java/org/springframework/util/EndpointTestUtils.java index 55977a9..b3c0f78 100644 --- a/obm/src/test/java/org/springframework/util/EndpointTestUtils.java +++ b/obm/src/test/java/org/springframework/util/EndpointTestUtils.java @@ -5,15 +5,15 @@ import org.mortbay.jetty.servlet.Context; /** + * provides support for launching a Jetty {@link Server} instance. Users may implement {@link JettyContextConfigurationCallback} + * to tailor the deployed web applications in the Jetty context as required. + * * @author Josh Long + * @see DispatcherServletJettyConfigurationCallback + * @see Server */ abstract public class EndpointTestUtils { - - public static interface JettyContextConfigurationCallback { - void configure(Context c) throws Exception; - } - /** * Launches a Jetty server and then preconfigures Spring's web infrastructure and the bootstraps the * configuration object that's passed in as a parameter