Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Update PostgreSQL tests to use testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
bscholtes1A committed Jul 11, 2023
1 parent 8eea0e5 commit d1cd95e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 40 deletions.
16 changes: 4 additions & 12 deletions .github/workflows/verify.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,24 @@ jobs:
if: always()


Postgresql-Integration-Tests:
Component-Tests:
runs-on: ubuntu-latest
env:
JACOCO: true

services:
postgres:
image: postgres:14.2
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: password

steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/gradle-setup

- name: Postgresql Tests
- name: Component Tests
uses: ./.github/actions/run-tests
with:
command: ./gradlew test -DincludeTags="PostgresqlIntegrationTest"
command: ./gradlew test -DincludeTags="ComponentTest"

Upload-Coverage-Report-To-Codecov:
needs:
- Test
- Postgresql-Integration-Tests
- Component-Tests
runs-on: ubuntu-latest
if: always()
steps:
Expand Down
8 changes: 6 additions & 2 deletions extensions/store/sql/participant-store-sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

Provides SQL persistence for participants


## Prerequisites

Please apply this [schema](docs/schema.sql) to your SQL database.


## Entity Diagram

![ER Diagram](docs/er.png)
Expand All @@ -17,3 +15,9 @@ Please apply this [schema](docs/schema.sql) to your SQL database.
| Key | Description | Mandatory |
|:--------------------------------|:----------------------------------|-----------|
| edc.datasource.participant.name | Datasource used by this extension | X |

## Test

```bash
./gradlew test -DincludeTags="ComponentTest"
```
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.edc.registration.store.sql.schema.ParticipantStatements;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;
import org.eclipse.edc.spi.result.StoreResult;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.store.AbstractSqlStore;
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
import org.eclipse.edc.transaction.spi.TransactionContext;
Expand All @@ -36,21 +37,19 @@
import java.util.stream.Collectors;

import static java.lang.String.format;
import static org.eclipse.edc.sql.SqlQueryExecutor.executeQuery;
import static org.eclipse.edc.sql.SqlQueryExecutor.executeQuerySingle;

/**
* SQL implementation for {@link ParticipantStore}
*/

public class SqlParticipantStore extends AbstractSqlStore implements ParticipantStore {


private final ParticipantStatements participantStatements;


public SqlParticipantStore(DataSourceRegistry dataSourceRegistry, String dataSourceName, TransactionContext transactionContext, ObjectMapper objectMapper, ParticipantStatements participantStatements) {
super(dataSourceRegistry, dataSourceName, transactionContext, objectMapper);
public SqlParticipantStore(DataSourceRegistry dataSourceRegistry, String dataSourceName, TransactionContext transactionContext,
ObjectMapper objectMapper, ParticipantStatements participantStatements, QueryExecutor queryExecutor) {
super(dataSourceRegistry, dataSourceName, transactionContext, objectMapper, queryExecutor);
this.participantStatements = Objects.requireNonNull(participantStatements);
}

Expand All @@ -73,7 +72,7 @@ public SqlParticipantStore(DataSourceRegistry dataSourceRegistry, String dataSou
public List<Participant> listParticipants() {
return transactionContext.execute(() -> {
try (var connection = getConnection()) {
try (var stream = executeQuery(connection, true, this::participantMapper, participantStatements.getSelectParticipantTemplate())) {
try (var stream = queryExecutor.query(connection, true, this::participantMapper, participantStatements.getSelectParticipantTemplate())) {
return stream.collect(Collectors.toList());
}
} catch (EdcPersistenceException e) {
Expand All @@ -86,7 +85,6 @@ public List<Participant> listParticipants() {

@Override
public StoreResult<Participant> save(Participant participant) {

transactionContext.execute(() -> {
try (var connection = getConnection()) {
var existingParticipant = findByDidInternal(connection, participant.getDid());
Expand All @@ -110,7 +108,7 @@ public StoreResult<Participant> save(Participant participant) {
public Collection<Participant> listParticipantsWithStatus(ParticipantStatus state) {
return transactionContext.execute(() -> {
try (var connection = getConnection()) {
try (var stream = executeQuery(connection, true, this::participantMapper, participantStatements.getSelectParticipantByStateTemplate(), state.code())) {
try (var stream = queryExecutor.query(connection, true, this::participantMapper, participantStatements.getSelectParticipantByStateTemplate(), state.code())) {
return stream.collect(Collectors.toList());
}
} catch (EdcPersistenceException e) {
Expand All @@ -122,11 +120,10 @@ public Collection<Participant> listParticipantsWithStatus(ParticipantStatus stat
}

private void update(Connection connection, Participant oldParticipant, Participant participant) {

if (!oldParticipant.getId().equals(participant.getId())) {
throw new EdcPersistenceException(format("Failed to update Participant with did %s: participant id didn't match", participant.getDid()));
}
executeQuery(connection, participantStatements.getUpdateParticipantTemplate(),
queryExecutor.execute(connection, participantStatements.getUpdateParticipantTemplate(),
participant.getState(),
participant.getStateCount(),
participant.getStateTimestamp(),
Expand All @@ -139,7 +136,7 @@ private void update(Connection connection, Participant oldParticipant, Participa
}

private void insert(Connection connection, Participant participant) {
executeQuery(connection, participantStatements.getInsertParticipantsTemplate(),
queryExecutor.execute(connection, participantStatements.getInsertParticipantsTemplate(),
participant.getId(),
participant.getDid(),
participant.getState(),
Expand All @@ -166,8 +163,9 @@ private Participant participantMapper(ResultSet resultSet) throws SQLException {
}

private Participant findByDidInternal(Connection connection, String did) {
return executeQuerySingle(connection, false, this::participantMapper, participantStatements.getSelectParticipantByDidTemplate(), did);
try (var stream = queryExecutor.query(connection, false, this::participantMapper, participantStatements.getSelectParticipantByDidTemplate(), did)) {
return stream.findFirst().orElse(null);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry;
import org.eclipse.edc.transaction.spi.TransactionContext;

Expand All @@ -46,19 +47,19 @@ public class SqlParticipantStoreExtension implements ServiceExtension {
private DataSourceRegistry dataSourceRegistry;
@Inject
private TransactionContext trxContext;

@Inject
private TypeManager typeManager;
@Inject
private QueryExecutor queryExecutor;

@Override
public String name() {
return NAME;
}


@Provider
public ParticipantStore participantStore(ServiceExtensionContext context) {
return new SqlParticipantStore(dataSourceRegistry, getDataSourceName(context), trxContext, typeManager.getMapper(), getStatementImpl());
return new SqlParticipantStore(dataSourceRegistry, getDataSourceName(context), trxContext, typeManager.getMapper(), getStatementImpl(), queryExecutor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

package org.eclipse.edc.registration.store.sql;

import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.registration.spi.model.Participant;
import org.eclipse.edc.registration.store.spi.ParticipantStore;
import org.eclipse.edc.registration.store.spi.ParticipantStoreTestBase;
import org.eclipse.edc.registration.store.sql.schema.BaseSqlParticipantStatements;
import org.eclipse.edc.registration.store.sql.schema.PostgresSqlParticipantStatements;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -37,31 +37,30 @@
import static org.eclipse.edc.registration.spi.model.ParticipantStatus.AUTHORIZED;


@PostgresqlDbIntegrationTest
@ComponentTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
class PostgresParticipantStoreTest extends ParticipantStoreTestBase {

private SqlParticipantStore store;


@BeforeEach
void setUp(PostgresqlStoreSetupExtension extension) throws IOException {
BaseSqlParticipantStatements statements = new PostgresSqlParticipantStatements();
void setUp(PostgresqlStoreSetupExtension extension, QueryExecutor queryExecutor) throws IOException {
var statements = new PostgresSqlParticipantStatements();

TypeManager manager = new TypeManager();
var manager = new TypeManager();
manager.registerTypes(Participant.class);

store = new SqlParticipantStore(extension.getDataSourceRegistry(), extension.getDatasourceName(), extension.getTransactionContext(), manager.getMapper(), statements);
store = new SqlParticipantStore(extension.getDataSourceRegistry(), extension.getDatasourceName(), extension.getTransactionContext(), manager.getMapper(), statements, queryExecutor);

var schema = Files.readString(Paths.get("docs/schema.sql"));
extension.runQuery(schema);
}

@Test
void saveAndListParticipants_removesDuplicates() {
var participant1 = createParticipant().did("some.test/url/2").status(AUTHORIZED).build();
var participant2 = createParticipant().did("some.test/url/2").status(AUTHORIZED).build();

Participant participant1 = createParticipant().did("some.test/url/2").status(AUTHORIZED).build();
Participant participant2 = createParticipant().did("some.test/url/2").status(AUTHORIZED).build();
getStore().save(participant1);

assertThatExceptionOfType(EdcPersistenceException.class).isThrownBy(() -> getStore().save(participant2))
Expand Down

0 comments on commit d1cd95e

Please sign in to comment.