-
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.
Reintruduce TestEventStore from framework
- Loading branch information
amckenzie
committed
Nov 2, 2018
1 parent
7d4139a
commit 7fd73d0
Showing
19 changed files
with
293 additions
and
1 deletion.
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
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,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>event-store</artifactId> | ||
<groupId>uk.gov.justice.event-store</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>test-utils-event-store</artifactId> | ||
<packaging>pom</packaging> | ||
<modules> | ||
<module>test-utils-event</module> | ||
<module>test-utils-persistence</module> | ||
</modules> | ||
|
||
|
||
</project> |
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,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>test-utils-event-store</artifactId> | ||
<groupId>uk.gov.justice.event-store</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>test-utils-event</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>uk.gov.justice.event-store</groupId> | ||
<artifactId>event-repository-jdbc</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>uk.gov.justice.services</groupId> | ||
<artifactId>test-utils-common</artifactId> | ||
<version>${framework.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>uk.gov.justice.services</groupId> | ||
<artifactId>test-utils-core</artifactId> | ||
<version>${framework.version}</version> | ||
</dependency> | ||
|
||
<!--Test dependencies--> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
95 changes: 95 additions & 0 deletions
95
...rc/main/java/uk/gov/justice/services/test/utils/core/eventsource/TestEventRepository.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,95 @@ | ||
package uk.gov.justice.services.test.utils.core.eventsource; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
import static uk.gov.justice.services.test.utils.common.host.TestHostProvider.getHost; | ||
|
||
import uk.gov.justice.services.eventsourcing.repository.jdbc.AnsiSQLEventLogInsertionStrategy; | ||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event; | ||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.EventJdbcRepository; | ||
import uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidPositionException; | ||
import uk.gov.justice.services.jdbc.persistence.JdbcRepositoryHelper; | ||
import uk.gov.justice.services.test.utils.common.reflection.ReflectionUtils; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.apache.commons.dbcp2.BasicDataSource; | ||
|
||
/** | ||
* Standalone repository class to access event streams. To be used in integration testing. Moved | ||
* here from framework by Allan | ||
*/ | ||
public class TestEventRepository { | ||
|
||
private static final String POSTGRES_DRIVER_NAME = "org.postgresql.Driver"; | ||
|
||
private final EventJdbcRepository eventJdbcRepository; | ||
private final DataSource dbSource; | ||
|
||
public TestEventRepository(final DataSource dbSource) { | ||
this.dbSource = dbSource; | ||
eventJdbcRepository = new EventJdbcRepository( | ||
new AnsiSQLEventLogInsertionStrategy(), | ||
new JdbcRepositoryHelper(), | ||
null, | ||
"", | ||
getLogger(EventJdbcRepository.class)); | ||
|
||
ReflectionUtils.setField(eventJdbcRepository, "dataSource", dbSource); | ||
} | ||
|
||
public TestEventRepository(final String url, final String username, final String password, final String driverClassName) { | ||
final BasicDataSource dataSource = new BasicDataSource(); | ||
dataSource.setDriverClassName(driverClassName); | ||
dataSource.setUrl(url); | ||
dataSource.setUsername(username); | ||
dataSource.setPassword(password); | ||
|
||
this.dbSource = dataSource; | ||
|
||
eventJdbcRepository = new EventJdbcRepository( | ||
new AnsiSQLEventLogInsertionStrategy(), | ||
new JdbcRepositoryHelper(), | ||
null, | ||
"", | ||
getLogger(EventJdbcRepository.class)); | ||
|
||
ReflectionUtils.setField(eventJdbcRepository, "dataSource", dbSource); | ||
} | ||
|
||
public TestEventRepository(final String contextName) { | ||
this(jdbcUrlFrom(contextName), contextName, contextName, POSTGRES_DRIVER_NAME); | ||
} | ||
|
||
public static TestEventRepository forContext(final String contextName) { | ||
return new TestEventRepository(contextName); | ||
} | ||
|
||
public List<Event> eventsOfStreamId(final UUID streamId) { | ||
try (final Stream<Event> events = eventJdbcRepository.findByStreamIdOrderByPositionAsc(streamId)) { | ||
return events.collect(toList()); | ||
} | ||
} | ||
|
||
public List<Event> allEvents() { | ||
try (final Stream<Event> events = eventJdbcRepository.findAll()) { | ||
return events.collect(toList()); | ||
} | ||
} | ||
|
||
public void insert(final Event event) throws InvalidPositionException { | ||
eventJdbcRepository.insert(event); | ||
} | ||
|
||
public DataSource getDataSource() { | ||
return dbSource; | ||
} | ||
|
||
private static String jdbcUrlFrom(final String contextName) { | ||
return String.format("jdbc:postgresql://%s/%seventstore", getHost(), contextName); | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
.../test/java/uk/gov/justice/services/test/utils/core/eventsource/TestEventRepositoryIT.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,133 @@ | ||
package uk.gov.justice.services.test.utils.core.eventsource; | ||
|
||
import static java.util.UUID.randomUUID; | ||
import static org.hamcrest.CoreMatchers.hasItem; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.CoreMatchers.sameInstance; | ||
import static org.junit.Assert.assertThat; | ||
import static uk.gov.justice.services.test.utils.common.host.TestHostProvider.getHost; | ||
|
||
import uk.gov.justice.services.common.util.UtcClock; | ||
import uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.apache.commons.dbcp2.BasicDataSource; | ||
import org.junit.Test; | ||
|
||
public class TestEventRepositoryIT { | ||
|
||
@Test | ||
public void shouldGetAConnectionToTheEventStore() throws Exception { | ||
|
||
final TestEventRepository testEventRepository = TestEventRepository.forContext("framework"); | ||
|
||
try (final Connection connection = testEventRepository.getDataSource().getConnection()) { | ||
try (final PreparedStatement preparedStatement = connection.prepareStatement("SELECT * from event_log")) { | ||
try (final ResultSet resultSet = preparedStatement.executeQuery()) { | ||
assertThat(resultSet, is(notNullValue())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldBeInstantiatedUsingADatasource() throws Exception { | ||
|
||
final String url = "jdbc:postgresql://localhost/frameworkeventstore"; | ||
final String driverName = "org.postgresql.Driver"; | ||
final String username = "framework"; | ||
final String password = "framework"; | ||
|
||
final BasicDataSource dataSource = new BasicDataSource(); | ||
dataSource.setDriverClassName(driverName); | ||
dataSource.setUrl(url); | ||
dataSource.setUsername(username); | ||
dataSource.setPassword(password); | ||
|
||
final TestEventRepository testEventRepository = new TestEventRepository(dataSource); | ||
|
||
assertThat(testEventRepository.getDataSource(), is(sameInstance(dataSource))); | ||
} | ||
|
||
@Test | ||
public void shouldInsertAndFindEvents() throws Exception { | ||
|
||
cleanDatabase(); | ||
|
||
final UUID steamId_1 = randomUUID(); | ||
final UUID steamId_2 = randomUUID(); | ||
final UUID steamId_3 = randomUUID(); | ||
|
||
final Event event_1 = anEvent(1L, steamId_1); | ||
final Event event_2 = anEvent(2L, steamId_2); | ||
final Event event_3 = anEvent(3L, steamId_3); | ||
|
||
final TestEventRepository testEventRepository = TestEventRepository.forContext("framework"); | ||
|
||
testEventRepository.insert(event_1); | ||
testEventRepository.insert(event_2); | ||
testEventRepository.insert(event_3); | ||
|
||
final List<Event> events = testEventRepository.allEvents(); | ||
|
||
assertThat(events.size(), is(3)); | ||
|
||
assertThat(events, hasItem(event_1)); | ||
assertThat(events, hasItem(event_2)); | ||
assertThat(events, hasItem(event_3)); | ||
} | ||
|
||
@Test | ||
public void shouldFindEventsByStreamId() throws Exception { | ||
|
||
cleanDatabase(); | ||
|
||
final UUID steamId_1 = randomUUID(); | ||
final UUID steamId_2 = randomUUID(); | ||
|
||
final Event event_1 = anEvent(1L, steamId_1); | ||
final Event event_2 = anEvent(2L, steamId_2); | ||
final Event event_3 = anEvent(3L, steamId_2); | ||
|
||
final TestEventRepository testEventRepository = TestEventRepository.forContext("framework"); | ||
|
||
testEventRepository.insert(event_1); | ||
testEventRepository.insert(event_2); | ||
testEventRepository.insert(event_3); | ||
|
||
final List<Event> events = testEventRepository.eventsOfStreamId(steamId_2); | ||
|
||
assertThat(events.size(), is(2)); | ||
|
||
assertThat(events, hasItem(event_2)); | ||
assertThat(events, hasItem(event_3)); | ||
} | ||
|
||
private Event anEvent(final Long sequenceId, final UUID steamId) { | ||
return new Event( | ||
randomUUID(), | ||
steamId, | ||
sequenceId, | ||
"event.name_" + sequenceId, | ||
"metadata " + sequenceId, | ||
"payload " + sequenceId, | ||
new UtcClock().now() | ||
); | ||
} | ||
|
||
private void cleanDatabase() throws Exception { | ||
final TestEventRepository testEventRepository = TestEventRepository.forContext("framework"); | ||
|
||
try(final Connection connection = testEventRepository.getDataSource().getConnection()) { | ||
try(final PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM event_log")) { | ||
preparedStatement.executeUpdate(); | ||
} | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.