Search in sources :

Example 31 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration-samples by spring-projects.

the class TcpServerConnectionDeserializeTest method testHappyPath.

@Test
public void testHappyPath() {
    // add a listener to this channel, otherwise there is not one defined
    // the reason we use a listener here is so we can assert truths on the
    // message and/or payload
    SubscribableChannel channel = (SubscribableChannel) incomingServerChannel;
    channel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            byte[] payload = (byte[]) requestMessage.getPayload();
            // we assert during the processing of the messaging that the
            // payload is just the content we wanted to send without the
            // framing bytes (STX/ETX)
            assertEquals("Hello World!", new String(payload));
            return requestMessage;
        }
    });
    String sourceMessage = wrapWithStxEtx("Hello World!");
    String result = gw.send(sourceMessage);
    System.out.println(result);
    assertEquals("Hello World!", result);
}
Also used : AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test) SpringIntegrationTest(org.springframework.integration.test.context.SpringIntegrationTest)

Example 32 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-cloud-stream by spring-cloud.

the class BindingServiceConfigurationTests method valdateImportedConfiguartionHandlerPostProcessing.

@Test
public void valdateImportedConfiguartionHandlerPostProcessing() {
    ApplicationContext context = new SpringApplicationBuilder(TestChannelBinderConfiguration.getCompleteConfiguration(RootConfiguration.class)).web(WebApplicationType.NONE).run();
    Map<String, AbstractReplyProducingMessageHandler> beansOfType = context.getBeansOfType(AbstractReplyProducingMessageHandler.class);
    for (AbstractReplyProducingMessageHandler handler : beansOfType.values()) {
        assertTrue(handler.getNotPropagatedHeaders().contains("contentType"));
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Example 33 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class EnricherParserTests4 method nullResultIntegrationTest.

@Test
public void nullResultIntegrationTest() {
    SubscribableChannel requests = context.getBean("requests", SubscribableChannel.class);
    class NullFoo extends AbstractReplyProducingMessageHandler {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return null;
        }
    }
    NullFoo foo = new NullFoo();
    foo.setOutputChannel(context.getBean("replies", MessageChannel.class));
    requests.subscribe(foo);
    Target original = new Target();
    Message<?> request = MessageBuilder.withPayload(original).setHeader("sourceName", "test").setHeader("notOverwrite", "test").build();
    context.getBean("input", MessageChannel.class).send(request);
    Message<?> reply = context.getBean("output", PollableChannel.class).receive(0);
    Target enriched = (Target) reply.getPayload();
    assertEquals("Could not determine the name", enriched.getName());
    assertEquals(11, enriched.getAge());
    assertEquals(null, enriched.getGender());
    assertTrue(enriched.isMarried());
    assertNotSame(original, enriched);
    assertEquals(1, adviceCalled);
    MessageHeaders headers = reply.getHeaders();
    assertEquals("Could not determine the foo", headers.get("foo"));
    assertEquals("Could not determine the testBean", headers.get("testBean"));
    assertEquals("Could not determine the sourceName", headers.get("sourceName"));
    assertEquals("test", headers.get("notOverwrite"));
    adviceCalled--;
    requests.unsubscribe(foo);
}
Also used : Message(org.springframework.messaging.Message) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MessageHeaders(org.springframework.messaging.MessageHeaders) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 34 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class EnricherParserWithRequestPayloadExpressionTests method integrationTest.

@Test
public void integrationTest() {
    SubscribableChannel requests = context.getBean("requests", SubscribableChannel.class);
    requests.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            assertTrue("Expected the payload of the requestMessage to be a String", requestMessage.getPayload() instanceof Integer);
            Integer payload = (Integer) requestMessage.getPayload();
            assertEquals("Expected value: 99", Integer.valueOf(99), payload);
            return new Source(String.valueOf(payload));
        }
    });
    Target original = new Target();
    original.setAge(99);
    Message<?> request = MessageBuilder.withPayload(original).build();
    context.getBean("input", MessageChannel.class).send(request);
    Message<?> reply = context.getBean("output", PollableChannel.class).receive(0);
    Target enriched = (Target) reply.getPayload();
    assertEquals("Name as SpEL", enriched.getName());
    assertEquals(99, enriched.getAge());
    assertSame(original, enriched);
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 35 with AbstractReplyProducingMessageHandler

use of org.springframework.integration.handler.AbstractReplyProducingMessageHandler in project spring-integration by spring-projects.

the class ContentEnricherTests method testErrorChannel.

/**
 * In this test a {@link Target} message is passed into a {@link ContentEnricher}.
 * The Enricher passes the message to a "request-channel" to a handler which throws
 * an exception. The {@link ContentEnricher} then uses the error flow and consults
 * the "error-channel" which returns a alternative {@link Target}.
 */
@Test
public void testErrorChannel() throws Exception {
    final DirectChannel requestChannel = new DirectChannel();
    requestChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            throw new RuntimeException();
        }
    });
    final DirectChannel errorChannel = new DirectChannel();
    errorChannel.subscribe(new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return new Target("failed");
        }
    });
    final QueueChannel replyChannel = new QueueChannel();
    final ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    enricher.setErrorChannel(errorChannel);
    SpelExpressionParser parser = new SpelExpressionParser();
    Map<String, Expression> propertyExpressions = new HashMap<String, Expression>();
    propertyExpressions.put("name", parser.parseExpression("payload.name + ' target'"));
    enricher.setPropertyExpressions(propertyExpressions);
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    final Target target = new Target("replace me");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    enricher.handleMessage(requestMessage);
    Message<?> reply = replyChannel.receive(10000);
    Target result = (Target) reply.getPayload();
    assertEquals("failed target", result.getName());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Test(org.junit.Test)

Aggregations

AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)52 Test (org.junit.Test)46 BeanFactory (org.springframework.beans.factory.BeanFactory)37 QueueChannel (org.springframework.integration.channel.QueueChannel)29 Message (org.springframework.messaging.Message)28 DirectChannel (org.springframework.integration.channel.DirectChannel)23 GenericMessage (org.springframework.messaging.support.GenericMessage)20 Matchers.containsString (org.hamcrest.Matchers.containsString)19 ErrorMessage (org.springframework.messaging.support.ErrorMessage)18 ArrayList (java.util.ArrayList)14 AdviceMessage (org.springframework.integration.message.AdviceMessage)14 Advice (org.aopalliance.aop.Advice)13 HashMap (java.util.HashMap)12 MessageHandlingException (org.springframework.messaging.MessageHandlingException)12 Expression (org.springframework.expression.Expression)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 MessageHandlingExpressionEvaluatingAdviceException (org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException)9 MessagingException (org.springframework.messaging.MessagingException)9