Skip to content

Commit a556e3f

Browse files
committed
spring-projectsGH-3869: Create The Context Holder Request Handler Advice.
spring-projectsGH-3869: Add unit tests. spring-projectsGH-3869: Create Advice. spring-projectsGH-3869: Correct typo.
1 parent ddddd1d commit a556e3f

File tree

3 files changed

+197
-2
lines changed

3 files changed

+197
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2015-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+
20+
import java.util.function.Consumer;
21+
import java.util.function.Function;
22+
23+
import org.springframework.messaging.Message;
24+
25+
26+
27+
/**
28+
* Provides a key for the context holder.This key could for example be stored in a message header.
29+
*
30+
* @author Adel Haidar
31+
* @since 6.0
32+
*/
33+
public class ContextHolderRequestHandlerAdvice {
34+
public Function<Message<?>, Object> keyProvider;
35+
36+
public Consumer<Object> contextSetHook;
37+
38+
public Consumer<Object> contextClearHook;
39+
40+
41+
/**
42+
* Returns the key provider function.
43+
* @return The key provider function.
44+
*
45+
*/
46+
public Function<Message<?>, Object> getKeyProvider() {
47+
return this.keyProvider;
48+
}
49+
50+
/**
51+
* Sets the key provider function.
52+
* @param keyProvider the key provider.
53+
*/
54+
public void setKeyProvider(Function<Message<?>, Object> keyProvider) {
55+
this.keyProvider = keyProvider;
56+
}
57+
58+
/**
59+
* Returns the context set hook function.
60+
* @return The context set hook function.
61+
*
62+
*/
63+
public Consumer<Object> getContextSetHook() {
64+
return this.contextSetHook;
65+
}
66+
67+
/**
68+
* Sets the context set hook function.
69+
* @param contextSetHook the context set hook function.
70+
*/
71+
public void setContextSetHook(Consumer<Object> contextSetHook) {
72+
this.contextSetHook = contextSetHook;
73+
}
74+
75+
/**
76+
* Returns the context clear hook function.
77+
* @return The context clear hook function.
78+
*/
79+
public Consumer<Object> getContextClearHook() {
80+
return this.contextClearHook;
81+
}
82+
83+
/**
84+
* Sets the context clear hook function.
85+
* @param contextClearHook the context clear hook function.
86+
*/
87+
public void setContextClearHook(Consumer<Object> contextClearHook) {
88+
this.contextClearHook = contextClearHook;
89+
}
90+
}

spring-integration-file/src/main/java/org/springframework/integration/file/remote/session/DelegatingSessionFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2021 the original author or authors.
2+
* Copyright 2015-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@ public DelegatingSessionFactory(Map<Object, SessionFactory<F>> factories, Sessio
5252
* @param factoryLocator the factory.
5353
*/
5454
public DelegatingSessionFactory(SessionFactoryLocator<F> factoryLocator) {
55-
Assert.notNull(factoryLocator, "'factoryFactory' cannot be null");
55+
Assert.notNull(factoryLocator, "'factoryLocator' cannot be null");
5656
this.factoryLocator = factoryLocator;
5757
}
5858

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)