INT-1000 polishing JsonInboundMessageMapper

This commit is contained in:
Mark Fisher
2010-10-17 16:03:09 -04:00
parent 52a1ba50df
commit df6d360441
2 changed files with 50 additions and 43 deletions

View File

@@ -45,21 +45,23 @@ import org.springframework.integration.support.MessageBuilder;
* @author Mark Fisher
* @author Dave Syer
*/
public class InboundJsonMessageMapperTests {
public class JsonInboundMessageMapperTests {
private ObjectMapper mapper = new ObjectMapper();
@Factory
public static Matcher<Message<?>> sameExceptImmutableHeaders(Message<?> operand) {
return new MessageMatcher(operand);
}
@Test
public void testToMessageWithHeadersAndStringPayload() throws Exception {
UUID id = UUID.randomUUID();
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"},\"payload\":\"myPayloadStuff\"}";
Message<String> expected = MessageBuilder.withPayload("myPayloadStuff").setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).build();
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class);
Message<?> result = mapper.toMessage(jsonMessage);
assertThat(result, sameExceptImmutableHeaders(expected));
}
@@ -68,7 +70,7 @@ public class InboundJsonMessageMapperTests {
public void testToMessageWithStringPayload() throws Exception {
String jsonMessage = "\"myPayloadStuff\"";
String expected = "myPayloadStuff";
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class);
mapper.setMapToPayload(true);
Message<?> result = mapper.toMessage(jsonMessage);
assertEquals(expected, result.getPayload());
@@ -80,7 +82,7 @@ public class InboundJsonMessageMapperTests {
UUID id = UUID.randomUUID();
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"},\"payload\":" + getBeanAsJson(bean) + "}";
Message<TestBean> expected = MessageBuilder.withPayload(bean).setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).build();
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(TestBean.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(TestBean.class);
Message<?> result = mapper.toMessage(jsonMessage);
assertThat(result, sameExceptImmutableHeaders(expected));
}
@@ -89,7 +91,7 @@ public class InboundJsonMessageMapperTests {
public void testToMessageWithBeanPayload() throws Exception {
TestBean expected = new TestBean();
String jsonMessage = getBeanAsJson(expected);
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(TestBean.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(TestBean.class);
mapper.setMapToPayload(true);
Message<?> result = mapper.toMessage(jsonMessage);
assertEquals(expected, result.getPayload());
@@ -102,7 +104,7 @@ public class InboundJsonMessageMapperTests {
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\", \"myHeader\":" + getBeanAsJson(bean) + "},\"payload\":\"myPayloadStuff\"}";
Message<String> expected = MessageBuilder.withPayload("myPayloadStuff").
setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).setHeader("myHeader", bean).build();
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class);
Map<String, Class<?>> headerTypes = new HashMap<String, Class<?>>();
headerTypes.put("myHeader", TestBean.class);
mapper.setHeaderTypes(headerTypes);
@@ -116,7 +118,7 @@ public class InboundJsonMessageMapperTests {
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"},\"payload\":[\"myPayloadStuff1\",\"myPayloadStuff2\",\"myPayloadStuff3\"]}";
List<String> expectedList = Arrays.asList(new String[]{"myPayloadStuff1", "myPayloadStuff2", "myPayloadStuff3"});
Message<List<String>> expected = MessageBuilder.withPayload(expectedList).setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).build();
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(new TypeReference<List<String>>(){});
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(new TypeReference<List<String>>(){});
Message<?> result = mapper.toMessage(jsonMessage);
assertThat(result, sameExceptImmutableHeaders(expected));
}
@@ -129,16 +131,16 @@ public class InboundJsonMessageMapperTests {
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"},\"payload\":[" + getBeanAsJson(bean1) + "," + getBeanAsJson(bean2) + "]}";
List<TestBean> expectedList = Arrays.asList(new TestBean[]{bean1, bean2});
Message<List<TestBean>> expected = MessageBuilder.withPayload(expectedList).setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).build();
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(new TypeReference<List<TestBean>>(){});
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(new TypeReference<List<TestBean>>(){});
Message<?> result = mapper.toMessage(jsonMessage);
assertThat(result, sameExceptImmutableHeaders(expected));
}
@Test
public void testToMessageInvalidFormatPayloadAndHeadersReversed() throws Exception {
UUID id = UUID.randomUUID();
String jsonMessage = "{\"payload\":\"myPayloadStuff\",\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"}}";
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class);
try {
mapper.toMessage(jsonMessage);
fail();
@@ -151,7 +153,7 @@ public class InboundJsonMessageMapperTests {
@Test
public void testToMessageInvalidFormatPayloadNoHeaders() throws Exception {
String jsonMessage = "{\"payload\":\"myPayloadStuff\"}";
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class);
try {
mapper.toMessage(jsonMessage);
fail();
@@ -165,7 +167,7 @@ public class InboundJsonMessageMapperTests {
public void testToMessageInvalidFormatHeadersNoPayload() throws Exception {
UUID id = UUID.randomUUID();
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"}}";
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class);
try {
mapper.toMessage(jsonMessage);
fail();
@@ -179,7 +181,7 @@ public class InboundJsonMessageMapperTests {
public void testToMessageInvalidFormatHeadersAndStringPayloadWithMapToPayload() throws Exception {
UUID id = UUID.randomUUID();
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"},\"payload\":\"myPayloadStuff\"}";
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class);
mapper.setMapToPayload(true);
try {
mapper.toMessage(jsonMessage);
@@ -195,7 +197,7 @@ public class InboundJsonMessageMapperTests {
TestBean bean = new TestBean();
UUID id = UUID.randomUUID();
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"},\"payload\":" + getBeanAsJson(bean) + "}";
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(TestBean.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(TestBean.class);
mapper.setMapToPayload(true);
try {
mapper.toMessage(jsonMessage);
@@ -211,7 +213,7 @@ public class InboundJsonMessageMapperTests {
TestBean bean = new TestBean();
UUID id = UUID.randomUUID();
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"},\"payload\":" + getBeanAsJson(bean) + "}";
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(Long.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(Long.class);
try {
mapper.toMessage(jsonMessage);
fail();
@@ -226,7 +228,7 @@ public class InboundJsonMessageMapperTests {
TestBean bean = new TestBean();
UUID id = UUID.randomUUID();
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\",\"myHeader\":" + getBeanAsJson(bean) + "},\"payload\":\"myPayloadStuff\"}";
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class);
Map<String, Class<?>> headerTypes = new HashMap<String, Class<?>>();
headerTypes.put("myHeader", Long.class);
mapper.setHeaderTypes(headerTypes);