-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
amckenzie
committed
Sep 10, 2019
1 parent
7c534e1
commit 6836afa
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
...e/services/eventsourcing/publishedevent/rebuild/BatchProcessingDetailsCalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package uk.gov.justice.services.eventsourcing.publishedevent.rebuild; | ||
|
||
import static java.util.Arrays.asList; | ||
import static java.util.Collections.emptyList; | ||
import static java.util.Optional.of; | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.sameInstance; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mockito.Mockito.*; | ||
|
||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.PublishedEvent; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class BatchProcessingDetailsCalculatorTest { | ||
|
||
@InjectMocks | ||
private BatchProcessingDetailsCalculator batchProcessingDetailsCalculator; | ||
|
||
@Test | ||
public void shouldStartWithProcessingDetailsOfZero() throws Exception { | ||
|
||
final BatchProcessDetails batchProcessDetails = batchProcessingDetailsCalculator.createFirstBatchProcessDetails(); | ||
|
||
assertThat(batchProcessDetails.getCurrentEventNumber().get(), is(0L)); | ||
assertThat(batchProcessDetails.getPreviousEventNumber().get(), is(0L)); | ||
assertThat(batchProcessDetails.getProcessCount(), is(0)); | ||
assertThat(batchProcessDetails.isComplete(), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldCalculateTheNextProcessingDetailsBasedOnTheCurrentProcessedEvents() throws Exception { | ||
|
||
final int processedCount = 20; | ||
final boolean complete = false; | ||
|
||
final BatchProcessDetails previousProcessDetails = new BatchProcessDetails( | ||
new AtomicLong(19), | ||
new AtomicLong(20), | ||
processedCount, | ||
complete | ||
); | ||
|
||
final PublishedEvent event_21 = mock(PublishedEvent.class); | ||
final PublishedEvent event_22 = mock(PublishedEvent.class); | ||
final PublishedEvent event_23 = mock(PublishedEvent.class); | ||
|
||
final List<PublishedEvent> publishedEvents = asList(event_21, event_22, event_23); | ||
|
||
final AtomicLong previousEventNumber = new AtomicLong(22); | ||
final long currentEventNumber = 23L; | ||
|
||
when(event_23.getEventNumber()).thenReturn(of(currentEventNumber)); | ||
|
||
final BatchProcessDetails nextBatchProcessDetails = batchProcessingDetailsCalculator.calculateNextBatchProcessDetails( | ||
previousProcessDetails, | ||
previousEventNumber, | ||
publishedEvents); | ||
|
||
assertThat(nextBatchProcessDetails.isComplete(), is(false)); | ||
assertThat(nextBatchProcessDetails.getProcessCount(), is(23)); | ||
assertThat(nextBatchProcessDetails.getPreviousEventNumber(), is(previousEventNumber)); | ||
assertThat(nextBatchProcessDetails.getCurrentEventNumber().get(), is(23L)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAsCompleteIfThereAreNoProcessedPublishedEvents() throws Exception { | ||
|
||
final int processedCount = 20; | ||
final boolean complete = false; | ||
|
||
final AtomicLong previousEventNumber = new AtomicLong(19); | ||
final AtomicLong currentEventNumber = new AtomicLong(20); | ||
|
||
final BatchProcessDetails previousProcessDetails = new BatchProcessDetails( | ||
previousEventNumber, | ||
currentEventNumber, | ||
processedCount, | ||
complete | ||
); | ||
|
||
|
||
final List<PublishedEvent> publishedEvents = emptyList(); | ||
|
||
final BatchProcessDetails nextBatchProcessDetails = batchProcessingDetailsCalculator.calculateNextBatchProcessDetails( | ||
previousProcessDetails, | ||
previousEventNumber, | ||
publishedEvents); | ||
|
||
assertThat(nextBatchProcessDetails.isComplete(), is(true)); | ||
assertThat(nextBatchProcessDetails.getProcessCount(), is(processedCount)); | ||
assertThat(nextBatchProcessDetails.getPreviousEventNumber(), is(previousEventNumber)); | ||
assertThat(nextBatchProcessDetails.getCurrentEventNumber(), is(currentEventNumber)); | ||
} | ||
} |