First pass at inbound/outbound JSON mapping for INT-1000 and INT-1001. Needs complete Javadoc'ing and still has some TODO's to address.
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
package org.springframework.integration.json;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.TypeReference;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageHeaders;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
|
||||
public class InboundJsonMessageMapperTests {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
public void testToMessageWithHeadersAndStringPayload() throws Exception {
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"},\"payload\":\"myPayloadStuff\"}";
|
||||
Message<String> expected = MessageBuilder.withPayload("myPayloadStuff").setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID,"myUniqueId").build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithStringPayload() throws Exception {
|
||||
String jsonMessage = "\"myPayloadStuff\"";
|
||||
String expected = "myPayloadStuff";
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
mapper.setMapToPayload(true);
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithHeadersAndBeanPayload() throws Exception {
|
||||
TestBean bean = new TestBean();
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"},\"payload\":"+getBeanAsJson(bean)+"}";
|
||||
Message<TestBean> expected = MessageBuilder.withPayload(bean).setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID,"myUniqueId").build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(TestBean.class);
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithBeanPayload() throws Exception {
|
||||
TestBean expected = new TestBean();
|
||||
String jsonMessage = getBeanAsJson(expected);
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(TestBean.class);
|
||||
mapper.setMapToPayload(true);
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithBeanHeaderAndStringPayload() throws Exception {
|
||||
TestBean bean = new TestBean();
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\", \"myHeader\":"+getBeanAsJson(bean)+"},\"payload\":\"myPayloadStuff\"}";
|
||||
Message<String> expected = MessageBuilder.withPayload("myPayloadStuff").
|
||||
setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID,"myUniqueId").setHeader("myHeader", bean).build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
Map<String, Class<?>> headerTypes = new HashMap<String, Class<?>>();
|
||||
headerTypes.put("myHeader", TestBean.class);
|
||||
mapper.setHeaderTypes(headerTypes);
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithHeadersAndListOfStringsPayload() throws Exception {
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"},\"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,"myUniqueId").build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(new TypeReference<List<String>>(){});
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithHeadersAndListOfBeansPayload() throws Exception {
|
||||
TestBean bean1 = new TestBean();
|
||||
TestBean bean2 = new TestBean();
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"},\"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,"myUniqueId").build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(new TypeReference<List<TestBean>>(){});
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageInvalidFormatPayloadAndHeadersReversed() throws Exception {
|
||||
String jsonMessage = "{\"payload\":\"myPayloadStuff\",\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"}}";
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
|
||||
try {
|
||||
mapper.toMessage(jsonMessage);
|
||||
fail();
|
||||
} catch(IllegalArgumentException ex) {
|
||||
//Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageInvalidFormatPayloadNoHeaders() throws Exception {
|
||||
String jsonMessage = "{\"payload\":\"myPayloadStuff\"}";
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
|
||||
try {
|
||||
mapper.toMessage(jsonMessage);
|
||||
fail();
|
||||
} catch(IllegalArgumentException ex) {
|
||||
//Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageInvalidFormatHeadersNoPayload() throws Exception {
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"}}";
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
|
||||
try {
|
||||
mapper.toMessage(jsonMessage);
|
||||
fail();
|
||||
} catch(IllegalArgumentException ex) {
|
||||
//Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageInvalidFormatHeadersAndStringPayloadWithMapToPayload() throws Exception {
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"},\"payload\":\"myPayloadStuff\"}";
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
mapper.setMapToPayload(true);
|
||||
try {
|
||||
mapper.toMessage(jsonMessage);
|
||||
fail();
|
||||
} catch(IllegalArgumentException ex) {
|
||||
//Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageInvalidFormatHeadersAndBeanPayloadWithMapToPayload() throws Exception {
|
||||
TestBean bean = new TestBean();
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"},\"payload\":"+getBeanAsJson(bean)+"}";
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(TestBean.class);
|
||||
mapper.setMapToPayload(true);
|
||||
try {
|
||||
mapper.toMessage(jsonMessage);
|
||||
fail();
|
||||
} catch(IllegalArgumentException ex) {
|
||||
//Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithHeadersAndPayloadTypeMappingFailure() throws Exception {
|
||||
TestBean bean = new TestBean();
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\"},\"payload\":"+getBeanAsJson(bean)+"}";
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(Long.class);
|
||||
try {
|
||||
mapper.toMessage(jsonMessage);
|
||||
fail();
|
||||
} catch(IllegalArgumentException ex) {
|
||||
//Expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithBeanHeaderTypeMappingFailure() throws Exception {
|
||||
TestBean bean = new TestBean();
|
||||
String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"myUniqueId\",\"myHeader\":"+getBeanAsJson(bean)+"},\"payload\":\"myPayloadStuff\"}";
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
Map<String, Class<?>> headerTypes = new HashMap<String, Class<?>>();
|
||||
headerTypes.put("myHeader", Long.class);
|
||||
mapper.setHeaderTypes(headerTypes);
|
||||
try {
|
||||
mapper.toMessage(jsonMessage);
|
||||
fail();
|
||||
} catch(IllegalArgumentException ex) {
|
||||
//Expected
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private String getBeanAsJson(TestBean bean) throws JsonGenerationException, JsonMappingException, IOException {
|
||||
StringWriter writer = new StringWriter();
|
||||
mapper.writeValue(writer, bean);
|
||||
return writer.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package org.springframework.integration.json;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.codehaus.jackson.JsonFactory;
|
||||
import org.codehaus.jackson.JsonParseException;
|
||||
import org.codehaus.jackson.JsonParser;
|
||||
import org.codehaus.jackson.JsonToken;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageHeaders;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
|
||||
|
||||
public class OutboundJsonMessageMapperTests {
|
||||
|
||||
private JsonFactory jsonFactory = new JsonFactory();
|
||||
private ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
public void testFromMessageWithHeadersAndStringPayload() throws Exception {
|
||||
Message<String> testMessage = MessageBuilder.withPayload("myPayloadStuff").build();
|
||||
String expected = "{\"headers\":{\"$timestamp\":"+testMessage.getHeaders().getTimestamp()+
|
||||
",\"$history\":[],\"$id\":\""+testMessage.getHeaders().getId()+"\"},\"payload\":\"myPayloadStuff\"}";
|
||||
OutboundJsonMessageMapper mapper = new OutboundJsonMessageMapper();
|
||||
String result = mapper.fromMessage(testMessage);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMessageExtractStringPayload() throws Exception {
|
||||
Message<String> testMessage = MessageBuilder.withPayload("myPayloadStuff").build();
|
||||
String expected = "\"myPayloadStuff\"";
|
||||
OutboundJsonMessageMapper mapper = new OutboundJsonMessageMapper();
|
||||
mapper.setShouldExtractPayload(true);
|
||||
String result = mapper.fromMessage(testMessage);
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMessageWithHeadersAndBeanPayload() throws Exception {
|
||||
TestBean payload = new TestBean();
|
||||
Message<TestBean> testMessage = MessageBuilder.withPayload(payload).setHeader(MessageHeaders.TIMESTAMP, new Long(1234)).setHeader(MessageHeaders.ID, "myUniqueId").build();
|
||||
String expectedHeaders = "\"headers\":{\"$timestamp\":"+testMessage.getHeaders().getTimestamp()+
|
||||
",\"$history\":[],\"$id\":\""+testMessage.getHeaders().getId()+"\"";
|
||||
OutboundJsonMessageMapper mapper = new OutboundJsonMessageMapper();
|
||||
String result = mapper.fromMessage(testMessage);
|
||||
assertTrue(result.contains(expectedHeaders));
|
||||
TestBean parsedPayload = extractJsonPayloadToTestBean(result);
|
||||
assertEquals(payload, parsedPayload);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMessageExtractBeanPayload() throws Exception {
|
||||
TestBean payload = new TestBean();
|
||||
Message<TestBean> testMessage = MessageBuilder.withPayload(payload).build();
|
||||
OutboundJsonMessageMapper mapper = new OutboundJsonMessageMapper();
|
||||
mapper.setShouldExtractPayload(true);
|
||||
String result = mapper.fromMessage(testMessage);
|
||||
assertTrue(!result.contains("headers"));
|
||||
TestBean parsedPayload = objectMapper.readValue(result, TestBean.class);
|
||||
assertEquals(payload, parsedPayload);
|
||||
}
|
||||
|
||||
private TestBean extractJsonPayloadToTestBean(String json) throws JsonParseException, IOException {
|
||||
JsonParser parser = jsonFactory.createJsonParser(json);
|
||||
do {
|
||||
parser.nextToken();
|
||||
} while(parser.getCurrentToken() != JsonToken.FIELD_NAME || !parser.getCurrentName().equals("payload"));
|
||||
parser.nextToken();
|
||||
return objectMapper.readValue(parser, TestBean.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package org.springframework.integration.json;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class TestBean {
|
||||
|
||||
private String value = "foo";
|
||||
|
||||
private boolean test = false;
|
||||
|
||||
private long number = 42;
|
||||
|
||||
private Date now = new Date();
|
||||
|
||||
private TestChildBean child = new TestChildBean();
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public boolean getTest() {
|
||||
return test;
|
||||
}
|
||||
|
||||
public long getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public Date getNow() {
|
||||
return this.now;
|
||||
}
|
||||
|
||||
public TestChildBean getChild() {
|
||||
return child;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setTest(boolean test) {
|
||||
this.test = test;
|
||||
}
|
||||
|
||||
public void setNumber(long number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public void setChild(TestChildBean child) {
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
public void setNow(Date now) {
|
||||
this.now = now;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((child == null) ? 0 : child.hashCode());
|
||||
result = prime * result + (int) (number ^ (number >>> 32));
|
||||
result = prime * result + (test ? 1231 : 1237);
|
||||
result = prime * result + ((value == null) ? 0 : value.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
TestBean other = (TestBean) obj;
|
||||
if (child == null) {
|
||||
if (other.child != null)
|
||||
return false;
|
||||
} else if (!child.equals(other.child))
|
||||
return false;
|
||||
if (number != other.number)
|
||||
return false;
|
||||
if (test != other.test)
|
||||
return false;
|
||||
if (value == null) {
|
||||
if (other.value != null)
|
||||
return false;
|
||||
} else if (!value.equals(other.value))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package org.springframework.integration.json;
|
||||
|
||||
public class TestChildBean {
|
||||
private String value = "bar";
|
||||
|
||||
private String baz = null;
|
||||
|
||||
private TestBean parent = null;
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String getBaz() {
|
||||
return baz;
|
||||
}
|
||||
|
||||
public TestBean getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(TestBean parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setBaz(String baz) {
|
||||
this.baz = baz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((baz == null) ? 0 : baz.hashCode());
|
||||
result = prime * result
|
||||
+ ((parent == null) ? 0 : parent.hashCode());
|
||||
result = prime * result + ((value == null) ? 0 : value.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
TestChildBean other = (TestChildBean) obj;
|
||||
if (baz == null) {
|
||||
if (other.baz != null)
|
||||
return false;
|
||||
} else if (!baz.equals(other.baz))
|
||||
return false;
|
||||
if (parent == null) {
|
||||
if (other.parent != null)
|
||||
return false;
|
||||
} else if (!parent.equals(other.parent))
|
||||
return false;
|
||||
if (value == null) {
|
||||
if (other.value != null)
|
||||
return false;
|
||||
} else if (!value.equals(other.value))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user