|
| 1 | +/* |
| 2 | + * Copyright 2013-2022 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.file.remote.session; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.integration.support.MessageBuilder; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | + |
| 30 | +public class ContextHolderRequestHandlerAdviceTests { |
| 31 | + |
| 32 | + private ContextHolderRequestHandlerAdvice advice; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + public void setUp() { |
| 36 | + advice = new ContextHolderRequestHandlerAdvice(); |
| 37 | + } |
| 38 | + |
| 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(); |
| 44 | + |
| 45 | + // when |
| 46 | + final var sessionFactory = advice.getKeyProvider().apply(message); |
| 47 | + |
| 48 | + // then |
| 49 | + assertThat(sessionFactory).isNotNull(); |
| 50 | + assertThat(sessionFactory).isInstanceOf(TestSessionFactory.class); |
| 51 | + } |
| 52 | + |
| 53 | + @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); |
| 70 | + } |
| 71 | + |
| 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); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + private static class TestSessionFactory implements SessionFactory<String> { |
| 93 | + |
| 94 | + @SuppressWarnings("unchecked") |
| 95 | + private final Session<String> mockSession = mock(Session.class); |
| 96 | + |
| 97 | + @Override |
| 98 | + public Session<String> getSession() { |
| 99 | + return this.mockSession; |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | +} |
0 commit comments