| 
 | 1 | +/*  | 
 | 2 | + * Copyright 2019 the original author or authors.  | 
 | 3 | + *  | 
 | 4 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + * you may not use this file except in compliance with the License.  | 
 | 6 | + * You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + *      https://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 11 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 13 | + * See the License for the specific language governing permissions and  | 
 | 14 | + * limitations under the License.  | 
 | 15 | + */  | 
 | 16 | + | 
 | 17 | +package org.springframework.integration.handler.support;  | 
 | 18 | + | 
 | 19 | +import static org.assertj.core.api.Assertions.assertThat;  | 
 | 20 | + | 
 | 21 | +import java.util.Collections;  | 
 | 22 | +import java.util.Map;  | 
 | 23 | + | 
 | 24 | +import org.junit.jupiter.api.Test;  | 
 | 25 | + | 
 | 26 | +import org.springframework.beans.factory.annotation.Autowired;  | 
 | 27 | +import org.springframework.context.annotation.Bean;  | 
 | 28 | +import org.springframework.context.annotation.Configuration;  | 
 | 29 | +import org.springframework.integration.channel.QueueChannel;  | 
 | 30 | +import org.springframework.integration.config.EnableIntegration;  | 
 | 31 | +import org.springframework.integration.dsl.IntegrationFlow;  | 
 | 32 | +import org.springframework.integration.handler.GenericHandler;  | 
 | 33 | +import org.springframework.messaging.Message;  | 
 | 34 | +import org.springframework.messaging.MessageHeaders;  | 
 | 35 | +import org.springframework.messaging.support.GenericMessage;  | 
 | 36 | +import org.springframework.test.annotation.DirtiesContext;  | 
 | 37 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;  | 
 | 38 | + | 
 | 39 | +/**  | 
 | 40 | + * @author Gary Russell  | 
 | 41 | + * @since 5.2  | 
 | 42 | + *  | 
 | 43 | + */  | 
 | 44 | +@SpringJUnitConfig  | 
 | 45 | +@DirtiesContext  | 
 | 46 | +public class MessagingMethodInvocableHelperTests {  | 
 | 47 | + | 
 | 48 | +	@Autowired  | 
 | 49 | +	private Config config;  | 
 | 50 | + | 
 | 51 | +	@Test  | 
 | 52 | +	void cachedHandler() {  | 
 | 53 | +		this.config.sampleFlow().getInputChannel().send(new GenericMessage<>(Collections.singletonMap("key", "value")));  | 
 | 54 | +		Message<?> received = this.config.queue().receive(0);  | 
 | 55 | +		assertThat(received).isNotNull();  | 
 | 56 | +		assertThat(received.getPayload()).isEqualTo("Hello value World!");  | 
 | 57 | +	}  | 
 | 58 | + | 
 | 59 | +	@Configuration  | 
 | 60 | +	@EnableIntegration  | 
 | 61 | +	public static class Config {  | 
 | 62 | + | 
 | 63 | +		@Bean  | 
 | 64 | +		public IntegrationFlow sampleFlow() {  | 
 | 65 | +			return f -> f  | 
 | 66 | +					.handle(new MapHandler())  | 
 | 67 | +					.handle(new StringHandler())  | 
 | 68 | +					.channel(queue());  | 
 | 69 | +		}  | 
 | 70 | + | 
 | 71 | +		@Bean  | 
 | 72 | +		public QueueChannel queue() {  | 
 | 73 | +			return new QueueChannel();  | 
 | 74 | +		}  | 
 | 75 | + | 
 | 76 | +		public static class MapHandler implements GenericHandler<Map<String, String>> {  | 
 | 77 | + | 
 | 78 | +			@Override  | 
 | 79 | +			public String handle(Map<String, String> mapPayload, MessageHeaders messageHeaders) {  | 
 | 80 | +				return "Hello " + mapPayload.get("key");  | 
 | 81 | +			}  | 
 | 82 | +		}  | 
 | 83 | + | 
 | 84 | +		public static class StringHandler implements GenericHandler<String> {  | 
 | 85 | + | 
 | 86 | +			@Override  | 
 | 87 | +			public String handle(String stringPayload, MessageHeaders messageHeaders) {  | 
 | 88 | +				return stringPayload + " World!";  | 
 | 89 | +			}  | 
 | 90 | +		}  | 
 | 91 | + | 
 | 92 | +	}  | 
 | 93 | + | 
 | 94 | +}  | 
0 commit comments