|
28 | 28 |
|
29 | 29 | import java.time.Duration; |
30 | 30 | import java.time.Instant; |
| 31 | +import java.time.LocalDateTime; |
| 32 | +import java.time.ZoneOffset; |
31 | 33 | import java.util.ArrayList; |
| 34 | +import java.util.Date; |
32 | 35 | import java.util.HashMap; |
33 | 36 | import java.util.List; |
34 | 37 | import java.util.Map; |
35 | 38 | import java.util.UUID; |
36 | 39 |
|
37 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
38 | | -import static org.junit.jupiter.api.Assertions.assertNull; |
39 | | -import static org.junit.jupiter.api.Assertions.assertThrows; |
| 40 | +import static org.junit.jupiter.api.Assertions.*; |
40 | 41 | import static org.mockito.Mockito.mock; |
41 | 42 | import static org.mockito.Mockito.when; |
42 | 43 |
|
@@ -190,6 +191,85 @@ void testStreamConfigWithStreamArnOnly() { |
190 | 191 | assertEquals("streamName", expectedIdentifier.streamName()); |
191 | 192 | } |
192 | 193 |
|
| 194 | + @Test |
| 195 | + void testStreamConfigWithAtTimeStampInitialPositionWithInitialPositionTimestamp() { |
| 196 | + KinesisStreamConfig streamConfig = mock(KinesisStreamConfig.class); |
| 197 | + final String streamArnString = "arn:aws:kinesis:us-east-1:123456789012:stream/streamName"; |
| 198 | + when(streamConfig.getStreamArn()).thenReturn(streamArnString); |
| 199 | + when(streamConfig.getInitialPosition()).thenReturn(InitialPositionInStream.AT_TIMESTAMP); |
| 200 | + when(streamConfig.getInitialTimestamp()).thenReturn(LocalDateTime.of(2024, 1, 15, 10, 30)); |
| 201 | + when(kinesisSourceConfig.getStreams()).thenReturn(List.of(streamConfig)); |
| 202 | + |
| 203 | + StreamIdentifier expectedIdentifier = StreamIdentifier.multiStreamInstance(Arn.fromString(streamArnString), 100L); |
| 204 | + when(kinesisClientAPIHandler.getStreamIdentifier(streamConfig.getStreamArn())) |
| 205 | + .thenReturn(expectedIdentifier); |
| 206 | + final String expectedConsumerArn = UUID.randomUUID().toString(); |
| 207 | + when(kinesisClientAPIHandler.getConsumerArnForStream(streamConfig.getStreamArn(), APPLICATION_NAME)) |
| 208 | + .thenReturn(expectedConsumerArn); |
| 209 | + |
| 210 | + List<StreamConfig> configs = createObjectUnderTest().streamConfigList(); |
| 211 | + |
| 212 | + assertEquals(1, configs.size()); |
| 213 | + StreamConfig resultConfig = configs.get(0); |
| 214 | + assertEquals(expectedIdentifier, resultConfig.streamIdentifier()); |
| 215 | + assertEquals(expectedConsumerArn, resultConfig.consumerArn()); |
| 216 | + assertEquals("streamName", expectedIdentifier.streamName()); |
| 217 | + assertEquals(InitialPositionInStreamExtended.newInitialPositionAtTimestamp(Date.from( |
| 218 | + streamConfig.getInitialTimestamp().atOffset(ZoneOffset.UTC).toInstant())), resultConfig.initialPositionInStreamExtended()); |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + void testStreamConfigWithAtTimeStampInitialPositionWithInitialPositionLookback() { |
| 223 | + KinesisStreamConfig streamConfig = mock(KinesisStreamConfig.class); |
| 224 | + final String streamArnString = "arn:aws:kinesis:us-east-1:123456789012:stream/streamName"; |
| 225 | + when(streamConfig.getStreamArn()).thenReturn(streamArnString); |
| 226 | + when(streamConfig.getInitialPosition()).thenReturn(InitialPositionInStream.AT_TIMESTAMP); |
| 227 | + when(streamConfig.getLookbackDuration()).thenReturn(Duration.ofMinutes(30)); |
| 228 | + when(kinesisSourceConfig.getStreams()).thenReturn(List.of(streamConfig)); |
| 229 | + |
| 230 | + StreamIdentifier expectedIdentifier = StreamIdentifier.multiStreamInstance(Arn.fromString(streamArnString), 100L); |
| 231 | + when(kinesisClientAPIHandler.getStreamIdentifier(streamConfig.getStreamArn())) |
| 232 | + .thenReturn(expectedIdentifier); |
| 233 | + final String expectedConsumerArn = UUID.randomUUID().toString(); |
| 234 | + when(kinesisClientAPIHandler.getConsumerArnForStream(streamConfig.getStreamArn(), APPLICATION_NAME)) |
| 235 | + .thenReturn(expectedConsumerArn); |
| 236 | + |
| 237 | + List<StreamConfig> configs = createObjectUnderTest().streamConfigList(); |
| 238 | + |
| 239 | + assertEquals(1, configs.size()); |
| 240 | + StreamConfig resultConfig = configs.get(0); |
| 241 | + assertEquals(expectedIdentifier, resultConfig.streamIdentifier()); |
| 242 | + assertEquals(expectedConsumerArn, resultConfig.consumerArn()); |
| 243 | + assertEquals("streamName", expectedIdentifier.streamName()); |
| 244 | + assertEquals(InitialPositionInStream.AT_TIMESTAMP, resultConfig.initialPositionInStreamExtended().getInitialPositionInStream()); |
| 245 | + |
| 246 | + long actualTime = resultConfig.initialPositionInStreamExtended().getTimestamp().getTime(); |
| 247 | + assertTrue(actualTime <= System.currentTimeMillis() - Duration.ofMinutes(30).toMillis()); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + void testStreamConfigWithAtTimeStampInitialPositionWithNoLookbackDurationAndNoInitialTimestamp() { |
| 252 | + KinesisStreamConfig streamConfig = mock(KinesisStreamConfig.class); |
| 253 | + final String streamArnString = "arn:aws:kinesis:us-east-1:123456789012:stream/streamName"; |
| 254 | + when(streamConfig.getStreamArn()).thenReturn(streamArnString); |
| 255 | + when(streamConfig.getInitialPosition()).thenReturn(InitialPositionInStream.AT_TIMESTAMP); |
| 256 | + when(streamConfig.getLookbackDuration()).thenReturn(null); |
| 257 | + when(streamConfig.getInitialTimestamp()).thenReturn(null); |
| 258 | + when(kinesisSourceConfig.getStreams()).thenReturn(List.of(streamConfig)); |
| 259 | + |
| 260 | + StreamIdentifier expectedIdentifier = StreamIdentifier.multiStreamInstance(Arn.fromString(streamArnString), 100L); |
| 261 | + when(kinesisClientAPIHandler.getStreamIdentifier(streamConfig.getStreamArn())) |
| 262 | + .thenReturn(expectedIdentifier); |
| 263 | + |
| 264 | + KinesisMultiStreamTracker tracker = createObjectUnderTest(); |
| 265 | + |
| 266 | + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, |
| 267 | + tracker::streamConfigList); |
| 268 | + assertEquals("Either initial_timestamp or lookback_duration must be " + |
| 269 | + "specified when using AT_TIMESTAMP initial_position", |
| 270 | + exception.getMessage()); |
| 271 | + } |
| 272 | + |
193 | 273 | @Test |
194 | 274 | void testStreamConfigWithNoArnOrName() { |
195 | 275 | KinesisStreamConfig streamConfig = mock(KinesisStreamConfig.class); |
|
0 commit comments