19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
20
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
21
21
import static org .assertj .core .api .Assertions .assertThatNullPointerException ;
22
+ import static org .mockito .BDDMockito .given ;
22
23
23
24
import java .math .BigInteger ;
24
25
import java .time .Clock ;
28
29
29
30
import org .junit .jupiter .api .BeforeEach ;
30
31
import org .junit .jupiter .api .Test ;
32
+ import org .junit .jupiter .api .extension .ExtendWith ;
33
+ import org .mockito .Mock ;
34
+ import org .mockito .junit .jupiter .MockitoExtension ;
31
35
36
+ import org .springframework .context .ApplicationContext ;
37
+ import org .springframework .context .event .ContextRefreshedEvent ;
32
38
import org .springframework .kafka .listener .ListenerExecutionFailedException ;
33
39
import org .springframework .kafka .listener .TimestampedException ;
34
40
35
41
/**
36
42
* @author Tomaz Fernandes
43
+ * @author Yvette Quinby
37
44
* @since 2.7
38
45
*/
46
+ @ ExtendWith (MockitoExtension .class )
39
47
class DefaultDestinationTopicResolverTests extends DestinationTopicTests {
40
48
41
49
private Map <String , DefaultDestinationTopicResolver .DestinationTopicHolder > destinationTopicMap ;
42
50
51
+ @ Mock
52
+ private ApplicationContext applicationContext ;
53
+
54
+ @ Mock
55
+ private ApplicationContext otherApplicationContext ;
56
+
43
57
private final Clock clock = TestClockUtils .CLOCK ;
44
58
45
- private final DestinationTopicResolver defaultDestinationTopicContainer = new DefaultDestinationTopicResolver ( clock ) ;
59
+ private DestinationTopicResolver defaultDestinationTopicContainer ;
46
60
47
61
private final long originalTimestamp = Instant .now (this .clock ).toEpochMilli ();
48
62
@@ -53,6 +67,7 @@ class DefaultDestinationTopicResolverTests extends DestinationTopicTests {
53
67
@ BeforeEach
54
68
public void setup () {
55
69
70
+ defaultDestinationTopicContainer = new DefaultDestinationTopicResolver (clock , applicationContext );
56
71
defaultDestinationTopicContainer .addDestinationTopics (allFirstDestinationsTopics );
57
72
defaultDestinationTopicContainer .addDestinationTopics (allSecondDestinationTopics );
58
73
defaultDestinationTopicContainer .addDestinationTopics (allThirdDestinationTopics );
@@ -152,8 +167,28 @@ private long getExpectedNextExecutionTime(DestinationTopic destinationTopic) {
152
167
153
168
@ Test
154
169
void shouldThrowIfAddsDestinationsAfterClosed () {
155
- ((DefaultDestinationTopicResolver ) defaultDestinationTopicContainer ).onApplicationEvent (null );
170
+ given (applicationContext .getId ()).willReturn ("the-context_id" );
171
+ ((DefaultDestinationTopicResolver ) defaultDestinationTopicContainer )
172
+ .onApplicationEvent (new ContextRefreshedEvent (applicationContext ));
156
173
assertThatIllegalStateException ().isThrownBy (() ->
157
174
defaultDestinationTopicContainer .addDestinationTopics (Collections .emptyList ()));
158
175
}
176
+
177
+ @ Test
178
+ void shouldCloseContainerOnContextRefresh () {
179
+ given (applicationContext .getId ()).willReturn ("the-context_id" );
180
+ ((DefaultDestinationTopicResolver ) defaultDestinationTopicContainer )
181
+ .onApplicationEvent (new ContextRefreshedEvent (applicationContext ));
182
+ assertThat (((DefaultDestinationTopicResolver ) defaultDestinationTopicContainer ).isContainerClosed ()).isTrue ();
183
+ }
184
+
185
+ @ Test
186
+ void shouldNotCloseContainerOnOtherContextRefresh () {
187
+ given (applicationContext .getId ()).willReturn ("the-context_id" );
188
+ given (otherApplicationContext .getId ()).willReturn ("other-context_id" );
189
+ ((DefaultDestinationTopicResolver ) defaultDestinationTopicContainer )
190
+ .onApplicationEvent (new ContextRefreshedEvent (otherApplicationContext ));
191
+ assertThat (((DefaultDestinationTopicResolver ) defaultDestinationTopicContainer ).isContainerClosed ()).isFalse ();
192
+ }
193
+
159
194
}
0 commit comments