Skip to content

Commit a9a7484

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 a9a7484

File tree

3 files changed

+191
-2
lines changed

3 files changed

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

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,103 @@
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+
private static class TestSessionFactory implements SessionFactory<String> {
92+
93+
@SuppressWarnings("unchecked")
94+
private final Session<String> mockSession = mock(Session.class);
95+
96+
@Override
97+
public Session<String> getSession() {
98+
return this.mockSession;
99+
}
100+
101+
}
102+
103+
}

0 commit comments

Comments
 (0)