-
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
Showing
5 changed files
with
268 additions
and
35 deletions.
There are no files selected for viewing
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
140 changes: 140 additions & 0 deletions
140
...ample-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/EventValidationIT.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,140 @@ | ||
package uk.gov.justice.services.example.cakeshop.it; | ||
|
||
import static java.lang.Integer.valueOf; | ||
import static java.lang.System.getProperty; | ||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE; | ||
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED; | ||
import static uk.gov.justice.services.jmx.system.command.client.connection.JmxParametersBuilder.jmxParameters; | ||
import static uk.gov.justice.services.test.utils.common.host.TestHostProvider.getHost; | ||
|
||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event; | ||
import uk.gov.justice.services.example.cakeshop.it.helpers.BatchEventInserter; | ||
import uk.gov.justice.services.example.cakeshop.it.helpers.CakeshopEventGenerator; | ||
import uk.gov.justice.services.example.cakeshop.it.helpers.DatabaseManager; | ||
import uk.gov.justice.services.example.cakeshop.it.helpers.PositionInStreamIterator; | ||
import uk.gov.justice.services.jmx.api.command.ValidatePublishedEventsCommand; | ||
import uk.gov.justice.services.jmx.api.domain.CommandState; | ||
import uk.gov.justice.services.jmx.api.domain.SystemCommandStatus; | ||
import uk.gov.justice.services.jmx.api.mbean.SystemCommanderMBean; | ||
import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClient; | ||
import uk.gov.justice.services.jmx.system.command.client.TestSystemCommanderClientFactory; | ||
import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters; | ||
import uk.gov.justice.services.test.utils.core.messaging.Poller; | ||
import uk.gov.justice.services.test.utils.persistence.DatabaseCleaner; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.junit.Test; | ||
|
||
public class EventValidationIT { | ||
|
||
private static final int BATCH_INSERT_SIZE = 10_000; | ||
|
||
private static final String CONTEXT_NAME = "example"; | ||
|
||
private final DataSource eventStoreDataSource = new DatabaseManager().initEventStoreDb(); | ||
private final DataSource viewStoreDataSource = new DatabaseManager().initViewStoreDb(); | ||
|
||
private static final String HOST = getHost(); | ||
private static final int PORT = valueOf(getProperty("random.management.port")); | ||
|
||
private final TestSystemCommanderClientFactory systemCommanderClientFactory = new TestSystemCommanderClientFactory(); | ||
private final DatabaseCleaner databaseCleaner = new DatabaseCleaner(); | ||
|
||
private final Poller longPoller = new Poller(2400, 1000L); | ||
private final BatchEventInserter batchEventInserter = new BatchEventInserter(eventStoreDataSource, BATCH_INSERT_SIZE); | ||
|
||
@Test | ||
public void shouldRunValidateEventsCommand() throws Exception { | ||
|
||
final int numberOfStreams = 2; | ||
final int numberOfEventsPerStream = 3; | ||
|
||
addEventsToEventLog(numberOfStreams, numberOfEventsPerStream); | ||
|
||
final JmxParameters jmxParameters = jmxParameters() | ||
.withHost(HOST) | ||
.withPort(PORT) | ||
.build(); | ||
|
||
final Optional<SystemCommandStatus> systemCommandStatus = runEventValidationCommand(jmxParameters); | ||
|
||
if (systemCommandStatus.isPresent()) { | ||
assertThat(systemCommandStatus.get().getMessage(), is("All PublishedEvents successfully passed schema validation")); | ||
} else { | ||
fail(); | ||
} | ||
} | ||
|
||
private Optional<SystemCommandStatus> runEventValidationCommand(final JmxParameters jmxParameters) { | ||
try (final SystemCommanderClient systemCommanderClient = systemCommanderClientFactory.create(jmxParameters)) { | ||
|
||
final SystemCommanderMBean systemCommanderMBean = systemCommanderClient.getRemote(CONTEXT_NAME); | ||
final UUID commandId = systemCommanderMBean | ||
.call(new ValidatePublishedEventsCommand()); | ||
|
||
return longPoller.pollUntilFound(() -> commandNoLongerInProgress(systemCommanderMBean, commandId)); | ||
} | ||
} | ||
|
||
private void addEventsToEventLog(final int numberOfStreams, final int numberOfEventsPerStream) throws Exception { | ||
|
||
final CakeshopEventGenerator cakeshopEventGenerator = new CakeshopEventGenerator(); | ||
|
||
final List<Event> events = new ArrayList<>(); | ||
final List<UUID> streamIds = new ArrayList<>(); | ||
|
||
for (int seed = 0; seed < numberOfStreams; seed++) { | ||
|
||
final PositionInStreamIterator positionInStreamIterator = new PositionInStreamIterator(); | ||
|
||
final Event recipeAddedEvent = cakeshopEventGenerator.createRecipeAddedEvent(seed, positionInStreamIterator); | ||
final UUID recipeId = recipeAddedEvent.getStreamId(); | ||
|
||
if (!streamIds.contains(recipeId)) { | ||
streamIds.add(recipeId); | ||
} | ||
|
||
events.add(recipeAddedEvent); | ||
|
||
for (int renameNumber = 1; renameNumber < numberOfEventsPerStream; renameNumber++) { | ||
final Event recipeRenamedEvent = cakeshopEventGenerator.createRecipeRenamedEvent(recipeId, seed, renameNumber, positionInStreamIterator); | ||
events.add(recipeRenamedEvent); | ||
} | ||
} | ||
|
||
batchEventInserter.updateEventStreamTable(streamIds); | ||
batchEventInserter.updateEventLogTable(events); | ||
|
||
final JmxParameters jmxParameters = jmxParameters() | ||
.withHost(HOST) | ||
.withPort(PORT) | ||
.build(); | ||
|
||
runEventValidationCommand(jmxParameters); | ||
|
||
} | ||
|
||
private Optional<SystemCommandStatus> commandNoLongerInProgress(final SystemCommanderMBean systemCommanderMBean, final UUID commandId) { | ||
|
||
final SystemCommandStatus systemCommandStatus = systemCommanderMBean.getCommandStatus(commandId); | ||
|
||
final CommandState commandState = systemCommandStatus.getCommandState(); | ||
if (commandState == COMMAND_COMPLETE || commandState == COMMAND_FAILED) { | ||
return of(systemCommandStatus); | ||
|
||
} | ||
|
||
return empty(); | ||
} | ||
} |
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
Oops, something went wrong.