Skip to content

Commit

Permalink
Merge pull request #38 from CJSCommonPlatform/improve-integation-test
Browse files Browse the repository at this point in the history
Improve integration tests
  • Loading branch information
mapingo committed Jun 4, 2018
2 parents b4d42e2 + 59826e7 commit a164e6f
Show file tree
Hide file tree
Showing 35 changed files with 840 additions and 604 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ public void process() {
"uk.gov.justice.services:persistence-jdbc",
"uk.gov.justice.services:event-buffer-core",
"uk.gov.justice.services:event-listener-interceptors",
"uk.gov.justice.services:messaging-adapter-core",
"uk.gov.justice.schema:catalog-core",
"uk.gov.justice.schema:schema-service",
"uk.gov.justice.utilities:utilities-core");
"uk.gov.justice.utilities:utilities-core"
);

final WebArchive excludeGeneratedApiClasses = create(WebArchive.class, "ExcludeGeneratedApiClasses")
.merge(webArchive, frameworkLibraries.exclusionFilter());
Expand Down
3 changes: 1 addition & 2 deletions replay-tool-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
</modules>

<properties>
<replay-tool-it-example-listener.version>1.0.0</replay-tool-it-example-listener.version>
<metrics-servlet.version>1.1.0</metrics-servlet.version>
</properties>

<artifactId>replay-tool-test</artifactId>

</project>
</project>
34 changes: 2 additions & 32 deletions replay-tool-test/replay-tool-integration-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,6 @@

<build>
<plugins>
<plugin>
<groupId>com.edugility</groupId>
<artifactId>h2-maven-plugin</artifactId>
<configuration>
<port>8092</port>
</configuration>
<executions>
<execution>
<id>Spawn a new H2 TCP server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>spawn</goal>
</goals>
</execution>
<execution>
<id>Stop a spawned H2 TCP server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${com.h2database.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Expand All @@ -132,7 +102,7 @@
<artifactItem>
<groupId>uk.gov.justice.framework.tools</groupId>
<artifactId>replay-tool-it-example-listener</artifactId>
<version>${replay-tool-it-example-listener.version}</version>
<version>${project.version}</version>
<type>war</type>
<overWrite>true</overWrite>
<outputDirectory>${project.basedir}/target/test-classes</outputDirectory>
Expand All @@ -154,4 +124,4 @@
</plugin>
</plugins>
</build>
</project>
</project>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package uk.gov.justice.framework.tools.replay;


import static java.util.Arrays.asList;
import static java.util.UUID.randomUUID;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import uk.gov.justice.framework.tools.replay.database.DatasourceCreator;
import uk.gov.justice.framework.tools.replay.database.EventInserter;
import uk.gov.justice.framework.tools.replay.database.LiquibaseRunner;
import uk.gov.justice.framework.tools.replay.events.User;
import uk.gov.justice.framework.tools.replay.events.UserFactory;
import uk.gov.justice.framework.tools.replay.h2.InMemoryDatabaseRunner;
import uk.gov.justice.framework.tools.replay.wildfly.WildflyRunner;
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event;

import java.util.List;
import java.util.UUID;

import javax.sql.DataSource;

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

public class EventOrderingIT {

private static final Boolean SHOULD_LOG_WILDFLY_PROCESS_TO_CONSOLE = true;
private static final Boolean ENABLE_REMOTE_DEBUGGING_FOR_WILDFLY = false;

private static final int WILDFLY_TIMEOUT_IN_SECONDS = 60;

private final LiquibaseRunner liquibaseRunner = new LiquibaseRunner();
private final DatasourceCreator datasourceCreator = new DatasourceCreator();
private final WildflyRunner wildflyRunner = new WildflyRunner();

private final DataSource viewStoreDataSource = datasourceCreator.createViewStoreDataSource();
private final DataSource eventStoreDataSource = datasourceCreator.createEventStoreDataSource();
private final EventInserter eventInserter = new EventInserter(eventStoreDataSource, viewStoreDataSource);
private final UserFactory userFactory = new UserFactory();
private final InMemoryDatabaseRunner inMemoryDatabaseRunner = new InMemoryDatabaseRunner();

@Before
public void startDatabase() {
inMemoryDatabaseRunner.startH2Database();
}

@Before
public void runLiquibase() throws Exception {
liquibaseRunner.createEventStoreSchema(eventStoreDataSource);
liquibaseRunner.createViewStoreSchema(viewStoreDataSource);
}

@After
public void stopDB() throws Exception {
inMemoryDatabaseRunner.stopH2Database();
}

@Test
public void shouldInsertEventsInTheCorrectOrder() throws Exception {

final UUID streamId = randomUUID();
final String eventName = "framework.update-user";

final UUID userId = randomUUID();


final User user = new User(userId, "Fred", "Bloggs");
final User updatedUser = new User(userId, "Billy", "Bloggs");


final List<Event> someEvents = userFactory.convertToEvents(asList(user, updatedUser), eventName, streamId);

eventInserter.insertEventsIntoVewstore(
streamId,
someEvents);

final boolean wildflyRanSuccessfully = wildflyRunner.run(
WILDFLY_TIMEOUT_IN_SECONDS,
SHOULD_LOG_WILDFLY_PROCESS_TO_CONSOLE,
ENABLE_REMOTE_DEBUGGING_FOR_WILDFLY
);

assertTrue("Wildfly process exited abnormally", wildflyRanSuccessfully);

final List<User> usersFromViewStore = eventInserter.getUsersFromViewStore();

assertThat(usersFromViewStore.size(), is(1));

assertThat(usersFromViewStore.get(0).getUserId(), is(userId));
assertThat(usersFromViewStore.get(0).getFirstName(), is("Billy"));
assertThat(usersFromViewStore.get(0).getLastName(), is("Bloggs"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package uk.gov.justice.framework.tools.replay;


import static java.lang.String.format;
import static java.util.UUID.randomUUID;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import uk.gov.justice.framework.tools.replay.database.DatasourceCreator;
import uk.gov.justice.framework.tools.replay.database.EventInserter;
import uk.gov.justice.framework.tools.replay.database.LiquibaseRunner;
import uk.gov.justice.framework.tools.replay.events.User;
import uk.gov.justice.framework.tools.replay.events.UserFactory;
import uk.gov.justice.framework.tools.replay.h2.InMemoryDatabaseRunner;
import uk.gov.justice.framework.tools.replay.wildfly.WildflyRunner;
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event;

import java.util.List;
import java.util.UUID;

import javax.sql.DataSource;

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

public class InsertAllEventsIntoViewStoreIT {

private static final Boolean SHOULD_LOG_WILDFLY_PROCESS_TO_CONSOLE = true;
private static final Boolean ENABLE_REMOTE_DEBUGGING_FOR_WILDFLY = false;

private static final int WILDFLY_TIMEOUT_IN_SECONDS = 60;
private static final int NUMBER_OF_EVENTS_TO_INSERT = 100;

private final LiquibaseRunner liquibaseRunner = new LiquibaseRunner();
private final DatasourceCreator datasourceCreator = new DatasourceCreator();
private final WildflyRunner wildflyRunner = new WildflyRunner();

private final DataSource viewStoreDataSource = datasourceCreator.createViewStoreDataSource();
private final DataSource eventStoreDataSource = datasourceCreator.createEventStoreDataSource();
private final EventInserter eventInserter = new EventInserter(eventStoreDataSource, viewStoreDataSource);
private final UserFactory userFactory = new UserFactory();
private final InMemoryDatabaseRunner inMemoryDatabaseRunner = new InMemoryDatabaseRunner();

@Before
public void startDatabase() {
inMemoryDatabaseRunner.startH2Database();
}

@Before
public void runLiquibase() throws Exception {
liquibaseRunner.createEventStoreSchema(eventStoreDataSource);
liquibaseRunner.createViewStoreSchema(viewStoreDataSource);
}

@After
public void stopDB() throws Exception {
inMemoryDatabaseRunner.stopH2Database();
}

@Test
public void shouldReadEventsFromEventStoreAndInsertIntoViewStore() throws Exception {

System.out.println(format("Inserting %d events with timeout of %d seconds", NUMBER_OF_EVENTS_TO_INSERT, WILDFLY_TIMEOUT_IN_SECONDS));

final UUID streamId = randomUUID();
final String eventName = "framework.update-user";

final List<User> users = userFactory.createSomeUsers(NUMBER_OF_EVENTS_TO_INSERT);
final List<Event> someEvents = userFactory.convertToEvents(users, eventName, streamId);

eventInserter.insertEventsIntoVewstore(
streamId,
someEvents);

final List<Event> insertedEvents = eventInserter.getAllFromEventStore().collect(toList());

assertThat(insertedEvents.size(), is(NUMBER_OF_EVENTS_TO_INSERT));

System.out.println(format("%d events inserted into view store", insertedEvents.size()));

final boolean wildflyRanSuccessfully = wildflyRunner.run(
WILDFLY_TIMEOUT_IN_SECONDS,
SHOULD_LOG_WILDFLY_PROCESS_TO_CONSOLE,
ENABLE_REMOTE_DEBUGGING_FOR_WILDFLY
);

assertTrue("Wildfly process exited abnormally", wildflyRanSuccessfully);

final List<User> usersFromViewStore = eventInserter.getUsersFromViewStore();


System.out.println(usersFromViewStore.size() + " events of " + NUMBER_OF_EVENTS_TO_INSERT + " were replayed into the view store");

assertThat(usersFromViewStore.size(), is(NUMBER_OF_EVENTS_TO_INSERT));

users.forEach(usersFromViewStore::remove);

assertTrue(usersFromViewStore.isEmpty());
}
}
Loading

0 comments on commit a164e6f

Please sign in to comment.