|
24 | 24 | import org.junit.jupiter.api.Test; |
25 | 25 | import org.junit.jupiter.api.condition.DisabledOnJre; |
26 | 26 | import org.junit.jupiter.api.condition.JRE; |
| 27 | +import org.mockito.ArgumentCaptor; |
27 | 28 | import org.mockito.ArgumentMatchers; |
28 | 29 | import org.mockito.Mock; |
29 | 30 | import org.mockito.Mockito; |
|
46 | 47 | import static com.azure.messaging.eventhubs.checkpointstore.blob.BlobCheckpointStore.OWNER_ID; |
47 | 48 | import static com.azure.messaging.eventhubs.checkpointstore.blob.BlobCheckpointStore.SEQUENCE_NUMBER; |
48 | 49 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 50 | +import static org.junit.jupiter.api.Assertions.assertNull; |
49 | 51 | import static org.junit.jupiter.api.Assertions.assertThrows; |
50 | 52 | import static org.mockito.ArgumentMatchers.any; |
51 | 53 | import static org.mockito.ArgumentMatchers.anyMap; |
@@ -261,6 +263,109 @@ public void testUpdateCheckpointInvalid() { |
261 | 263 | assertThrows(IllegalStateException.class, () -> blobCheckpointStore.updateCheckpoint(new Checkpoint())); |
262 | 264 | } |
263 | 265 |
|
| 266 | + /** |
| 267 | + * Tests that {@link BlobCheckpointStore#updateCheckpoint(Checkpoint)} falls back to the deprecated |
| 268 | + * {@link Checkpoint#getOffset()} value when {@link Checkpoint#getOffsetString()} is not provided. Reproduces the |
| 269 | + * regression reported in https://github.com/Azure/azure-sdk-for-java/issues/46752. |
| 270 | + */ |
| 271 | + @Test |
| 272 | + public void testUpdateCheckpointFallsBackToOffsetWhenOffsetStringMissing() { |
| 273 | + Map<String, String> captured = captureUpdateCheckpointMetadata(new Checkpoint().setFullyQualifiedNamespace("ns") |
| 274 | + .setEventHubName("eh") |
| 275 | + .setConsumerGroup("cg") |
| 276 | + .setPartitionId("0") |
| 277 | + .setSequenceNumber(2L) |
| 278 | + .setOffset(100L)); |
| 279 | + |
| 280 | + assertEquals("2", captured.get(SEQUENCE_NUMBER)); |
| 281 | + assertEquals("100", captured.get(OFFSET)); |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * Tests that {@link BlobCheckpointStore#updateCheckpoint(Checkpoint)} writes the {@code offsetString} value into |
| 286 | + * blob metadata when only {@link Checkpoint#setOffsetString(String)} has been populated. |
| 287 | + */ |
| 288 | + @Test |
| 289 | + public void testUpdateCheckpointUsesOffsetStringWhenProvided() { |
| 290 | + Map<String, String> captured = captureUpdateCheckpointMetadata(new Checkpoint().setFullyQualifiedNamespace("ns") |
| 291 | + .setEventHubName("eh") |
| 292 | + .setConsumerGroup("cg") |
| 293 | + .setPartitionId("0") |
| 294 | + .setSequenceNumber(2L) |
| 295 | + .setOffsetString("offset-string-value")); |
| 296 | + |
| 297 | + assertEquals("2", captured.get(SEQUENCE_NUMBER)); |
| 298 | + assertEquals("offset-string-value", captured.get(OFFSET)); |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * Tests that when both {@code offset} and {@code offsetString} are populated, {@code offsetString} is preferred. |
| 303 | + */ |
| 304 | + @Test |
| 305 | + public void testUpdateCheckpointPrefersOffsetStringOverOffset() { |
| 306 | + Map<String, String> captured = captureUpdateCheckpointMetadata(new Checkpoint().setFullyQualifiedNamespace("ns") |
| 307 | + .setEventHubName("eh") |
| 308 | + .setConsumerGroup("cg") |
| 309 | + .setPartitionId("0") |
| 310 | + .setSequenceNumber(2L) |
| 311 | + .setOffset(100L) |
| 312 | + .setOffsetString("offset-string-value")); |
| 313 | + |
| 314 | + assertEquals("offset-string-value", captured.get(OFFSET)); |
| 315 | + } |
| 316 | + |
| 317 | + /** |
| 318 | + * Tests that an {@code offsetString}-only checkpoint is accepted by the validation guard. This is a behavior |
| 319 | + * change from the prior implementation, which only inspected {@code sequenceNumber} and the deprecated |
| 320 | + * {@code offset} (Long) and would have rejected a checkpoint that supplied only {@code offsetString}. |
| 321 | + */ |
| 322 | + @Test |
| 323 | + public void testUpdateCheckpointOffsetStringOnlyIsValid() { |
| 324 | + Map<String, String> captured = captureUpdateCheckpointMetadata(new Checkpoint().setFullyQualifiedNamespace("ns") |
| 325 | + .setEventHubName("eh") |
| 326 | + .setConsumerGroup("cg") |
| 327 | + .setPartitionId("0") |
| 328 | + .setOffsetString("offset-string-value")); |
| 329 | + |
| 330 | + assertNull(captured.get(SEQUENCE_NUMBER)); |
| 331 | + assertEquals("offset-string-value", captured.get(OFFSET)); |
| 332 | + } |
| 333 | + |
| 334 | + /** |
| 335 | + * Tests that a checkpoint with only {@code sequenceNumber} populated still succeeds and writes a {@code null} |
| 336 | + * offset metadata value, preserving prior behavior. |
| 337 | + */ |
| 338 | + @Test |
| 339 | + public void testUpdateCheckpointSequenceNumberOnly() { |
| 340 | + Map<String, String> captured = captureUpdateCheckpointMetadata(new Checkpoint().setFullyQualifiedNamespace("ns") |
| 341 | + .setEventHubName("eh") |
| 342 | + .setConsumerGroup("cg") |
| 343 | + .setPartitionId("0") |
| 344 | + .setSequenceNumber(2L)); |
| 345 | + |
| 346 | + assertEquals("2", captured.get(SEQUENCE_NUMBER)); |
| 347 | + assertNull(captured.get(OFFSET)); |
| 348 | + } |
| 349 | + |
| 350 | + @SuppressWarnings("unchecked") |
| 351 | + private Map<String, String> captureUpdateCheckpointMetadata(Checkpoint checkpoint) { |
| 352 | + final String legacyPrefix = getLegacyPrefix(checkpoint.getFullyQualifiedNamespace(), |
| 353 | + checkpoint.getEventHubName(), checkpoint.getConsumerGroup()); |
| 354 | + final String blobName = legacyPrefix + CHECKPOINT_PATH + checkpoint.getPartitionId(); |
| 355 | + |
| 356 | + when(blobContainerAsyncClient.getBlobAsyncClient(blobName)).thenReturn(blobAsyncClient); |
| 357 | + when(blobAsyncClient.getBlockBlobAsyncClient()).thenReturn(blockBlobAsyncClient); |
| 358 | + when(blobAsyncClient.exists()).thenReturn(Mono.just(true)); |
| 359 | + when(blobAsyncClient.setMetadata(ArgumentMatchers.<Map<String, String>>any())).thenReturn(Mono.empty()); |
| 360 | + |
| 361 | + BlobCheckpointStore store = new BlobCheckpointStore(blobContainerAsyncClient); |
| 362 | + StepVerifier.create(store.updateCheckpoint(checkpoint)).verifyComplete(); |
| 363 | + |
| 364 | + ArgumentCaptor<Map<String, String>> captor = ArgumentCaptor.forClass(Map.class); |
| 365 | + Mockito.verify(blobAsyncClient).setMetadata(captor.capture()); |
| 366 | + return captor.getValue(); |
| 367 | + } |
| 368 | + |
264 | 369 | /** |
265 | 370 | * Tests that will update checkpoint if one does not exist. |
266 | 371 | */ |
|
0 commit comments