Search in sources :

Example 1 with JsonInboundMessageMapper

use of org.springframework.integration.support.json.JsonInboundMessageMapper in project spring-integration by spring-projects.

the class AbstractJsonInboundMessageMapperTests method testToMessageWithHeadersAndListOfStringsPayload.

@Test
public void testToMessageWithHeadersAndListOfStringsPayload() throws Exception {
    UUID id = UUID.randomUUID();
    String jsonMessage = "{\"headers\":{\"timestamp\":1,\"id\":\"" + id + "\",\"foo\":123,\"bar\":\"abc\"}," + "\"payload\":[\"myPayloadStuff1\",\"myPayloadStuff2\",\"myPayloadStuff3\"]}";
    List<String> expectedList = Arrays.asList("myPayloadStuff1", "myPayloadStuff2", "myPayloadStuff3");
    Message<List<String>> expected = MessageBuilder.withPayload(expectedList).setHeader("foo", 123).setHeader("bar", "abc").build();
    Type type = new ParameterizedTypeReference<List<String>>() {
    }.getType();
    JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(type, getParser());
    Message<?> result = mapper.toMessage(jsonMessage);
    assertThat(result, sameExceptImmutableHeaders(expected));
}
Also used : JsonInboundMessageMapper(org.springframework.integration.support.json.JsonInboundMessageMapper) Type(java.lang.reflect.Type) List(java.util.List) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with JsonInboundMessageMapper

use of org.springframework.integration.support.json.JsonInboundMessageMapper in project spring-integration by spring-projects.

the class AbstractJsonInboundMessageMapperTests method testToMessageInvalidFormatHeadersAndStringPayloadWithMapToPayload.

@Test
public void testToMessageInvalidFormatHeadersAndStringPayloadWithMapToPayload() throws Exception {
    UUID id = UUID.randomUUID();
    String jsonMessage = "{\"headers\":{\"$timestamp\":1,\"$id\":\"" + id + "\"},\"payload\":\"myPayloadStuff\"}";
    JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class, getParser());
    mapper.setMapToPayload(true);
    try {
        mapper.toMessage(jsonMessage);
        fail();
    } catch (IllegalArgumentException ex) {
    // Expected
    }
}
Also used : JsonInboundMessageMapper(org.springframework.integration.support.json.JsonInboundMessageMapper) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with JsonInboundMessageMapper

use of org.springframework.integration.support.json.JsonInboundMessageMapper in project spring-integration by spring-projects.

the class AbstractJsonInboundMessageMapperTests method testToMessageInvalidFormatPayloadNoHeaders.

@Test
public void testToMessageInvalidFormatPayloadNoHeaders() throws Exception {
    String jsonMessage = "{\"payload\":\"myPayloadStuff\"}";
    JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class, getParser());
    try {
        mapper.toMessage(jsonMessage);
        fail();
    } catch (IllegalArgumentException ex) {
    // Expected
    }
}
Also used : JsonInboundMessageMapper(org.springframework.integration.support.json.JsonInboundMessageMapper) Test(org.junit.Test)

Example 4 with JsonInboundMessageMapper

use of org.springframework.integration.support.json.JsonInboundMessageMapper in project spring-integration by spring-projects.

the class AbstractJsonInboundMessageMapperTests method testToMessageWithHeadersAndListOfBeansPayload.

@Test
public void testToMessageWithHeadersAndListOfBeansPayload() throws Exception {
    TestBean bean1 = new TestBean();
    TestBean bean2 = new TestBean();
    UUID id = UUID.randomUUID();
    String jsonMessage = "{\"headers\":{\"timestamp\":1,\"id\":\"" + id + "\",\"foo\":123,\"bar\":\"abc\"},\"payload\":[" + getBeanAsJson(bean1) + "," + getBeanAsJson(bean2) + "]}";
    List<TestBean> expectedList = Arrays.asList(bean1, bean2);
    Message<List<TestBean>> expected = MessageBuilder.withPayload(expectedList).setHeader("foo", 123).setHeader("bar", "abc").build();
    Type type = new ParameterizedTypeReference<List<TestBean>>() {
    }.getType();
    JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(type, getParser());
    Message<?> result = mapper.toMessage(jsonMessage);
    assertThat(result, sameExceptImmutableHeaders(expected));
}
Also used : JsonInboundMessageMapper(org.springframework.integration.support.json.JsonInboundMessageMapper) Type(java.lang.reflect.Type) List(java.util.List) UUID(java.util.UUID) Test(org.junit.Test)

Example 5 with JsonInboundMessageMapper

use of org.springframework.integration.support.json.JsonInboundMessageMapper in project spring-integration by spring-projects.

the class AbstractJsonInboundMessageMapperTests method testToMessageWithStringPayload.

@Test
public void testToMessageWithStringPayload() throws Exception {
    String jsonMessage = "\"myPayloadStuff\"";
    String expected = "myPayloadStuff";
    JsonInboundMessageMapper mapper = new JsonInboundMessageMapper(String.class, getParser());
    mapper.setMapToPayload(true);
    Message<?> result = mapper.toMessage(jsonMessage);
    assertEquals(expected, result.getPayload());
}
Also used : JsonInboundMessageMapper(org.springframework.integration.support.json.JsonInboundMessageMapper) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 JsonInboundMessageMapper (org.springframework.integration.support.json.JsonInboundMessageMapper)17 UUID (java.util.UUID)11 Type (java.lang.reflect.Type)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Jackson2JsonMessageParser (org.springframework.integration.support.json.Jackson2JsonMessageParser)2 JsonOutboundMessageMapper (org.springframework.integration.support.json.JsonOutboundMessageMapper)2 CallableStatement (java.sql.CallableStatement)1 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)1 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1 Transactional (org.springframework.transaction.annotation.Transactional)1