|
16 | 16 |
|
17 | 17 | package org.springframework.integration.file.remote.session;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
19 | 20 | import java.util.HashMap;
|
20 | 21 | import java.util.Map;
|
21 | 22 |
|
22 |
| -import org.junit.jupiter.api.BeforeEach; |
23 | 23 | import org.junit.jupiter.api.Test;
|
24 | 24 |
|
25 |
| -import org.springframework.integration.support.MessageBuilder; |
26 |
| - |
27 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.annotation.Bean; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.context.annotation.ImportResource; |
| 29 | +import org.springframework.integration.config.EnableIntegration; |
| 30 | +import org.springframework.messaging.MessageChannel; |
| 31 | +import org.springframework.messaging.PollableChannel; |
| 32 | +import org.springframework.messaging.support.GenericMessage; |
| 33 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 34 | + |
| 35 | +import static org.mockito.ArgumentMatchers.anyString; |
| 36 | +import static org.mockito.BDDMockito.given; |
28 | 37 | import static org.mockito.Mockito.mock;
|
| 38 | +import static org.mockito.Mockito.verify; |
29 | 39 |
|
| 40 | +/** |
| 41 | + * @author Adel Haidar |
| 42 | + * |
| 43 | + * @since 6.1 |
| 44 | + * |
| 45 | + */ |
| 46 | +@SpringJUnitConfig |
30 | 47 | public class ContextHolderRequestHandlerAdviceTests {
|
31 | 48 |
|
32 |
| - private ContextHolderRequestHandlerAdvice advice; |
| 49 | + @Autowired |
| 50 | + TestSessionFactory foo; |
33 | 51 |
|
34 |
| - @BeforeEach |
35 |
| - public void setUp() { |
36 |
| - advice = new ContextHolderRequestHandlerAdvice(); |
37 |
| - } |
| 52 | + @Autowired |
| 53 | + TestSessionFactory bar; |
38 | 54 |
|
39 |
| - @Test |
40 |
| - public void shouldSetKeyProvider() { |
41 |
| - // given |
42 |
| - advice.setKeyProvider(message -> message.getHeaders().get("test-key")); |
43 |
| - final var message = MessageBuilder.withPayload("test-payload").setHeader("test-key", new TestSessionFactory()).build(); |
| 55 | + @Autowired |
| 56 | + ContextHolderRequestHandlerAdvice advice; |
44 | 57 |
|
45 |
| - // when |
46 |
| - final var sessionFactory = advice.getKeyProvider().apply(message); |
| 58 | + @Autowired |
| 59 | + MessageChannel in; |
47 | 60 |
|
48 |
| - // then |
49 |
| - assertThat(sessionFactory).isNotNull(); |
50 |
| - assertThat(sessionFactory).isInstanceOf(TestSessionFactory.class); |
51 |
| - } |
| 61 | + @Autowired |
| 62 | + PollableChannel out; |
| 63 | + |
| 64 | + @Autowired |
| 65 | + DefaultSessionFactoryLocator<String> sessionFactoryLocator; |
52 | 66 |
|
53 | 67 | @Test
|
54 |
| - void shouldSetContextSetHook() { |
55 |
| - // given |
56 |
| - final Map<Object, SessionFactory<String>> factories = new HashMap<>(); |
57 |
| - final var testSessionFactory = new TestSessionFactory(); |
58 |
| - factories.put("test-key", testSessionFactory); |
59 |
| - final var defaultFactory = new TestSessionFactory(); |
60 |
| - factories.put("default", defaultFactory); |
61 |
| - |
62 |
| - final DelegatingSessionFactory<String> delegatingSessionFactory = new DelegatingSessionFactory<>(factories, testSessionFactory); |
63 |
| - advice.setContextSetHook(delegatingSessionFactory::setThreadKey); |
64 |
| - |
65 |
| - // when |
66 |
| - advice.getContextSetHook().accept("test-key"); |
67 |
| - |
68 |
| - // then |
69 |
| - assertThat(delegatingSessionFactory.getSession()).isNotEqualTo(defaultFactory.mockSession).isEqualTo(testSessionFactory.mockSession); |
| 68 | + public void testFlow() throws IOException { |
| 69 | + given(foo.mockSession.list(anyString())) |
| 70 | + .willReturn(new String[0]); |
| 71 | + final Map<String, Object> headers = Map.of("foo", "foo"); |
| 72 | + this.in.send(new GenericMessage<>("foo", headers)); |
| 73 | + verify(foo.mockSession).list("foo/"); |
70 | 74 | }
|
71 | 75 |
|
72 |
| - @Test |
73 |
| - void shouldSetContextClearHook() { |
74 |
| - // given |
75 |
| - final Map<Object, SessionFactory<String>> factories = new HashMap<>(); |
76 |
| - final var testSessionFactory = new TestSessionFactory(); |
77 |
| - factories.put("test-key", testSessionFactory); |
78 |
| - final var defaultFactory = new TestSessionFactory(); |
79 |
| - factories.put("default", defaultFactory); |
80 |
| - |
81 |
| - final DelegatingSessionFactory<String> delegatingSessionFactory = new DelegatingSessionFactory<>(factories, defaultFactory); |
82 |
| - advice.setContextClearHook(key -> delegatingSessionFactory.clearThreadKey()); |
83 |
| - |
84 |
| - // when |
85 |
| - advice.getContextClearHook().accept("test-key"); |
86 |
| - |
87 |
| - // then |
88 |
| - assertThat(delegatingSessionFactory.getSession()).isNotEqualTo(testSessionFactory.mockSession).isEqualTo(defaultFactory.mockSession); |
| 76 | + @Configuration |
| 77 | + @ImportResource( |
| 78 | + "classpath:/org/springframework/integration/file/remote/session/context-holder-request-handler-advice-context.xml") |
| 79 | + @EnableIntegration |
| 80 | + public static class Config { |
| 81 | + |
| 82 | + @Bean |
| 83 | + TestSessionFactory foo() { |
| 84 | + return new TestSessionFactory(); |
| 85 | + } |
| 86 | + |
| 87 | + @Bean |
| 88 | + TestSessionFactory bar() { |
| 89 | + return new TestSessionFactory(); |
| 90 | + } |
| 91 | + |
| 92 | + @Bean |
| 93 | + ContextHolderRequestHandlerAdvice advice() { |
| 94 | + final DelegatingSessionFactory<String> dsf = dsf(); |
| 95 | + final var contextHolderRequestHandlerAdvice = new ContextHolderRequestHandlerAdvice(); |
| 96 | + contextHolderRequestHandlerAdvice.setKeyProvider(m -> m.getHeaders().get("foo")); |
| 97 | + contextHolderRequestHandlerAdvice.setContextSetHook(dsf::setThreadKey); |
| 98 | + contextHolderRequestHandlerAdvice.setContextClearHook(c -> dsf.clearThreadKey()); |
| 99 | + return contextHolderRequestHandlerAdvice; |
| 100 | + } |
| 101 | + |
| 102 | + @Bean |
| 103 | + DelegatingSessionFactory<String> dsf() { |
| 104 | + SessionFactoryLocator<String> sff = sessionFactoryLocator(); |
| 105 | + return new DelegatingSessionFactory<>(sff); |
| 106 | + } |
| 107 | + |
| 108 | + @Bean |
| 109 | + public SessionFactoryLocator<String> sessionFactoryLocator() { |
| 110 | + Map<Object, SessionFactory<String>> factories = new HashMap<>(); |
| 111 | + factories.put("foo", foo()); |
| 112 | + TestSessionFactory bar = bar(); |
| 113 | + factories.put("bar", bar); |
| 114 | + return new DefaultSessionFactoryLocator<>(factories, bar); |
| 115 | + } |
| 116 | + |
89 | 117 | }
|
90 | 118 |
|
91 | 119 | private static class TestSessionFactory implements SessionFactory<String> {
|
|
0 commit comments