Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration test for PublishedEvent Rebuild #35

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions example-context/example-service/example-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.event-store</groupId>
<artifactId>test-utils-event</artifactId>
<version>${event-store.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package uk.gov.justice.services.example.cakeshop.it;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.util.UUID.randomUUID;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event;
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.PublishedEvent;
import uk.gov.justice.services.example.cakeshop.it.helpers.CommandSender;
import uk.gov.justice.services.example.cakeshop.it.helpers.DatabaseManager;
import uk.gov.justice.services.example.cakeshop.it.helpers.EventFactory;
import uk.gov.justice.services.example.cakeshop.it.helpers.MBeanHelper;
import uk.gov.justice.services.example.cakeshop.it.helpers.RestEasyClientFactory;
import uk.gov.justice.services.jmx.Rebuild;
import uk.gov.justice.services.jmx.RebuildMBean;
import uk.gov.justice.services.test.utils.core.messaging.Poller;
import uk.gov.justice.services.test.utils.events.TestEventInserter;
import uk.gov.justice.services.test.utils.persistence.DatabaseCleaner;
import uk.gov.justice.services.test.utils.persistence.SequenceSetter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.sql.DataSource;
import javax.ws.rs.client.Client;

import org.junit.Before;
import org.junit.Test;

public class RebuildIT {

private final DataSource eventStoreDataSource = new DatabaseManager().initEventStoreDb();
private final EventFactory eventFactory = new EventFactory();
private final DatabaseCleaner databaseCleaner = new DatabaseCleaner();
private final TestEventInserter testEventInserter = new TestEventInserter(eventStoreDataSource);

private CommandSender commandSender;
private final SequenceSetter sequenceSetter = new SequenceSetter();
private final MBeanHelper mBeanHelper = new MBeanHelper();

private final Poller poller = new Poller();


@Before
public void before() throws Exception {
final Client client = new RestEasyClientFactory().createResteasyClient();
commandSender = new CommandSender(client, eventFactory);

databaseCleaner.cleanEventStoreTables("framework");
databaseCleaner.cleanViewStoreTables("framework", "cake", "cake_order", "recipe", "ingredient");
}

@Test
public void shouldRenumberTheEventLogTableAndRebuldPublishedEvents() throws Exception {

final long startNumber = 1000L;
sequenceSetter.setSequenceTo(startNumber, "event_sequence_seq", eventStoreDataSource);

commandSender.addRecipe(randomUUID().toString(), "cake 1");
commandSender.addRecipe(randomUUID().toString(), "cake 2");
commandSender.addRecipe(randomUUID().toString(), "cake 3");

final List<PublishedEvent> publishedEvents = getPublishedEvents();

assertThat(publishedEvents.size(), is(3));
assertThat(publishedEvents.get(0).getEventNumber(), is(of(startNumber)));

assertThat(eventNumbersLinkedCorrectly(publishedEvents), is(true));

final List<UUID> eventIds = publishedEvents.stream()
.map(Event::getId)
.collect(toList());

invokeRebuild();

final List<PublishedEvent> rebuiltEvents = getPublishedEvents();
assertThat(rebuiltEvents.size(), is(3));

final List<UUID> rebuiltEventIds = rebuiltEvents.stream()
.map(Event::getId)
.collect(toList());

assertThat(rebuiltEvents.get(0).getEventNumber(), is(of(1L)));

assertThat(rebuiltEventIds, hasItem(eventIds.get(0)));
assertThat(rebuiltEventIds, hasItem(eventIds.get(1)));
assertThat(rebuiltEventIds, hasItem(eventIds.get(2)));

assertThat(eventNumbersLinkedCorrectly(rebuiltEvents), is(true));
}

private void invokeRebuild() throws Exception {
try (final JMXConnector jmxConnector = mBeanHelper.getJMXConnector()) {

final MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
final ObjectName objectName = new ObjectName("rebuild", "type", Rebuild.class.getSimpleName());
final RebuildMBean rebuildMBean = mBeanHelper.getMbeanProxy(connection, objectName, RebuildMBean.class);

rebuildMBean.doRebuildRequested();
}
}

private List<PublishedEvent> getPublishedEvents() {

final Optional<List<PublishedEvent>> publishedEvents = poller.pollUntilFound(() -> {

final List<PublishedEvent> events = doGetPublishedEvents();

if (events.size() == 3) {
return of(events);
}

return empty();
});

if (publishedEvents.isPresent()) {
return publishedEvents.get();
}

fail();

return new ArrayList<>();
}

private List<PublishedEvent> doGetPublishedEvents() {
try {
return testEventInserter.findAllPublishedEventsOrderedByEventNumber();
} catch (SQLException e) {
throw new RuntimeException("Failed to get published events", e);
}
}

private boolean eventNumbersLinkedCorrectly(final List<PublishedEvent> publishedEvents) {

long previousEventNumber = 0L;

for(final PublishedEvent publishedEvent: publishedEvents) {

if(publishedEvent.getPreviousEventNumber() != previousEventNumber) {
return false;
}

previousEventNumber = publishedEvent.getEventNumber().orElse(-1L);

if (previousEventNumber == -1L) {

return false;
}
}

return true;
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<common-bom.version>2.0.2</common-bom.version>
<framework.version>6.0.0-M26</framework.version>
<embedded-artemis.version>1.2.0</embedded-artemis.version>
<event-store.version>2.0.0-M27</event-store.version>
<event-store.version>2.0.0-M30</event-store.version>
<framework-generators.version>2.0.0-M20</framework-generators.version>
<file.service.version>1.17.7</file.service.version>
<framework-api.version>4.0.0-M21</framework-api.version>
Expand Down