|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.messaging.eventhubs.models; |
| 5 | + |
| 6 | +import com.azure.messaging.eventhubs.CheckpointStore; |
| 7 | +import com.azure.messaging.eventhubs.EventData; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.mockito.ArgumentCaptor; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.MockitoAnnotations; |
| 13 | +import reactor.core.publisher.Mono; |
| 14 | +import reactor.test.StepVerifier; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 18 | +import static org.mockito.ArgumentMatchers.any; |
| 19 | +import static org.mockito.Mockito.verify; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests for {@link EventContext}. |
| 24 | + */ |
| 25 | +class EventContextTest { |
| 26 | + private final PartitionContext partitionContext |
| 27 | + = new PartitionContext("TEST_NAMESPACE", "TEST_EVENT_HUB", "TEST_DEFAULT_GROUP", "TEST_PARTITION_ID"); |
| 28 | + |
| 29 | + @Mock |
| 30 | + private CheckpointStore checkpointStore; |
| 31 | + @Mock |
| 32 | + private EventData eventData; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void beforeEach() { |
| 36 | + MockitoAnnotations.initMocks(this); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Verifies that updateCheckpointAsync sets offsetString on the checkpoint. |
| 41 | + * Regression test for https://github.com/Azure/azure-sdk-for-java/issues/46752 |
| 42 | + * where only setOffset(Long) was called, causing BlobCheckpointStore to store |
| 43 | + * null offset because it reads getOffsetString(). |
| 44 | + */ |
| 45 | + @Test |
| 46 | + void updateCheckpointAsyncSetsOffsetString() { |
| 47 | + // Arrange |
| 48 | + final Long sequenceNumber = 10L; |
| 49 | + final Long offset = 15L; |
| 50 | + final String offsetString = "15"; |
| 51 | + |
| 52 | + when(eventData.getSequenceNumber()).thenReturn(sequenceNumber); |
| 53 | + when(eventData.getOffset()).thenReturn(offset); |
| 54 | + when(eventData.getOffsetString()).thenReturn(offsetString); |
| 55 | + when(checkpointStore.updateCheckpoint(any(Checkpoint.class))).thenReturn(Mono.empty()); |
| 56 | + |
| 57 | + final EventContext context = new EventContext(partitionContext, eventData, checkpointStore, null); |
| 58 | + |
| 59 | + // Act |
| 60 | + StepVerifier.create(context.updateCheckpointAsync()).verifyComplete(); |
| 61 | + |
| 62 | + // Assert - offsetString must be set on the checkpoint passed to the store |
| 63 | + ArgumentCaptor<Checkpoint> captor = ArgumentCaptor.forClass(Checkpoint.class); |
| 64 | + verify(checkpointStore).updateCheckpoint(captor.capture()); |
| 65 | + |
| 66 | + Checkpoint captured = captor.getValue(); |
| 67 | + assertEquals(partitionContext.getFullyQualifiedNamespace(), captured.getFullyQualifiedNamespace()); |
| 68 | + assertEquals(partitionContext.getEventHubName(), captured.getEventHubName()); |
| 69 | + assertEquals(partitionContext.getConsumerGroup(), captured.getConsumerGroup()); |
| 70 | + assertEquals(partitionContext.getPartitionId(), captured.getPartitionId()); |
| 71 | + assertEquals(sequenceNumber, captured.getSequenceNumber()); |
| 72 | + assertEquals(offset, captured.getOffset()); |
| 73 | + assertNotNull(captured.getOffsetString(), "offsetString must not be null - BlobCheckpointStore depends on it"); |
| 74 | + assertEquals(offsetString, captured.getOffsetString()); |
| 75 | + } |
| 76 | +} |
0 commit comments