Search in sources :

Example 46 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class DefaultSplitterTests method correlationIdCopiedFromMessageId.

@Test
public void correlationIdCopiedFromMessageId() {
    Message<String> message = MessageBuilder.withPayload("test").build();
    DirectChannel inputChannel = new DirectChannel();
    QueueChannel outputChannel = new QueueChannel(1);
    DefaultMessageSplitter splitter = new DefaultMessageSplitter();
    splitter.setOutputChannel(outputChannel);
    EventDrivenConsumer endpoint = new EventDrivenConsumer(inputChannel, splitter);
    endpoint.start();
    assertTrue(inputChannel.send(message));
    Message<?> reply = outputChannel.receive(0);
    assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply).getCorrelationId());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) Test(org.junit.Test)

Example 47 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class HttpRequestHandlingControllerTests method requestReplyWithCustomReplyKey.

@Test
public void requestReplyWithCustomReplyKey() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return requestMessage.getPayload().toString().toUpperCase();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.setReplyKey("myReply");
    controller.afterPropertiesSet();
    controller.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("howdy".getBytes());
    // request.setContentType("text/plain"); //Works in Spring 3.1.2.RELEASE but NOT in 3.0.7.RELEASE
    // Instead do:
    request.addHeader("Content-Type", "text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("foo", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    assertNull(modelAndView.getModel().get("reply"));
    Object reply = modelAndView.getModel().get("myReply");
    assertEquals("HOWDY", reply);
}
Also used : Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) ModelAndView(org.springframework.web.servlet.ModelAndView) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 48 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class HttpRequestHandlingControllerTests method requestReplyViewExpressionView.

@Test
public void requestReplyViewExpressionView() throws Exception {
    final View view = mock(View.class);
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Message<String> handleRequestMessage(Message<?> requestMessage) {
            return MessageBuilder.withPayload("foo").setHeader("bar", view).build();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    Expression viewExpression = new SpelExpressionParser().parseExpression("headers['bar']");
    controller.setViewExpression(viewExpression);
    controller.afterPropertiesSet();
    controller.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("hello".getBytes());
    request.setContentType("text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertSame(view, modelAndView.getView());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertEquals("foo", reply);
}
Also used : Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) ModelAndView(org.springframework.web.servlet.ModelAndView) View(org.springframework.web.servlet.View) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 49 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class HttpRequestHandlingControllerTests method requestReply.

@Test
public void requestReply() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return requestMessage.getPayload().toString().toUpperCase();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.afterPropertiesSet();
    controller.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    // request.setContentType("text/plain"); //Works in Spring 3.1.2.RELEASE but NOT in 3.0.7.RELEASE
    // Instead do:
    request.addHeader("Content-Type", "text/plain");
    // For Spring 3.0.7.RELEASE the Content must be set,
    request.setContent("hello".getBytes());
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("foo", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertEquals("HELLO", reply);
}
Also used : Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) ModelAndView(org.springframework.web.servlet.ModelAndView) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 50 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-integration by spring-projects.

the class HttpRequestHandlingControllerTests method requestReplyWithFullMessageInModel.

@Test
public void requestReplyWithFullMessageInModel() throws Exception {
    DirectChannel requestChannel = new DirectChannel();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            return requestMessage.getPayload().toString().toUpperCase();
        }
    };
    requestChannel.subscribe(handler);
    HttpRequestHandlingController controller = new HttpRequestHandlingController(true);
    controller.setBeanFactory(mock(BeanFactory.class));
    controller.setRequestChannel(requestChannel);
    controller.setViewName("foo");
    controller.setExtractReplyPayload(false);
    controller.afterPropertiesSet();
    controller.start();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContent("abc".getBytes());
    // request.setContentType("text/plain"); //Works in Spring 3.1.2.RELEASE but NOT in 3.0.7.RELEASE
    // Instead do:
    request.addHeader("Content-Type", "text/plain");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);
    assertEquals("foo", modelAndView.getViewName());
    assertEquals(1, modelAndView.getModel().size());
    Object reply = modelAndView.getModel().get("reply");
    assertNotNull(reply);
    assertTrue(reply instanceof Message<?>);
    assertEquals("ABC", ((Message<?>) reply).getPayload());
}
Also used : Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BeanFactory(org.springframework.beans.factory.BeanFactory) ModelAndView(org.springframework.web.servlet.ModelAndView) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)215 Test (org.junit.Test)182 MessageChannel (org.springframework.messaging.MessageChannel)71 Message (org.springframework.messaging.Message)68 QueueChannel (org.springframework.integration.channel.QueueChannel)63 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)38 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 CountDownLatch (java.util.concurrent.CountDownLatch)27 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 HashMap (java.util.HashMap)22 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 MessagingException (org.springframework.messaging.MessagingException)18 Properties (java.util.Properties)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 SubscribableChannel (org.springframework.messaging.SubscribableChannel)15 Expression (org.springframework.expression.Expression)14