-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(EdrCache): add transactional test
- Loading branch information
Showing
7 changed files
with
352 additions
and
34 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
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
126 changes: 126 additions & 0 deletions
126
...va/org/eclipse/tractusx/edc/edr/store/sql/PostgresqlTransactionalStoreSetupExtension.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,126 @@ | ||
/* | ||
* Copyright (c) 2022 Microsoft Corporation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.tractusx.edc.edr.store.sql; | ||
|
||
import org.eclipse.edc.spi.monitor.Monitor; | ||
import org.eclipse.edc.sql.testfixtures.PostgresqlLocalInstance; | ||
import org.eclipse.edc.transaction.datasource.spi.DataSourceRegistry; | ||
import org.eclipse.edc.transaction.local.LocalDataSourceRegistry; | ||
import org.eclipse.edc.transaction.local.LocalTransactionContext; | ||
import org.eclipse.edc.transaction.spi.TransactionContext; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.BeforeAllCallback; | ||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ParameterContext; | ||
import org.junit.jupiter.api.extension.ParameterResolutionException; | ||
import org.junit.jupiter.api.extension.ParameterResolver; | ||
|
||
import java.sql.Connection; | ||
import java.util.UUID; | ||
import javax.sql.DataSource; | ||
|
||
import static org.eclipse.edc.sql.SqlQueryExecutor.executeQuery; | ||
import static org.mockito.Mockito.doCallRealMethod; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Extension for running PG SQL store implementation. It automatically creates a test database and provided all the base data structure | ||
* for a SQL store to run such as {@link DataSourceRegistry}, {@link TransactionContext} and data source name which is automatically generated | ||
*/ | ||
public class PostgresqlTransactionalStoreSetupExtension implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback, ParameterResolver { | ||
|
||
private final String datasourceName; | ||
private DataSourceRegistry dataSourceRegistry = null; | ||
private DataSource dataSource = null; | ||
private Connection connection = null; | ||
private LocalTransactionContext transactionContext = null; | ||
private Monitor monitor = mock(Monitor.class); | ||
|
||
public PostgresqlTransactionalStoreSetupExtension(String datasourceName) { | ||
this.datasourceName = datasourceName; | ||
} | ||
|
||
public PostgresqlTransactionalStoreSetupExtension() { | ||
this(UUID.randomUUID().toString()); | ||
} | ||
|
||
|
||
public DataSource getDataSource() { | ||
return dataSource; | ||
} | ||
|
||
public String getDatasourceName() { | ||
return datasourceName; | ||
} | ||
|
||
public Connection getConnection() { | ||
return connection; | ||
} | ||
|
||
public int runQuery(String query) { | ||
return transactionContext.execute(() -> executeQuery(connection, query)); | ||
} | ||
|
||
|
||
public TransactionContext getTransactionContext() { | ||
return transactionContext; | ||
} | ||
|
||
public DataSourceRegistry getDataSourceRegistry() { | ||
return dataSourceRegistry; | ||
} | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
transactionContext = new LocalTransactionContext(monitor); | ||
dataSourceRegistry = new LocalDataSourceRegistry(transactionContext); | ||
dataSource = mock(DataSource.class); | ||
dataSourceRegistry.register(datasourceName, dataSource); | ||
connection = spy(PostgresqlLocalInstance.getTestConnection()); | ||
when(dataSource.getConnection()).thenReturn(connection); | ||
doNothing().when(connection).close(); | ||
} | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
doCallRealMethod().when(connection).close(); | ||
connection.close(); | ||
} | ||
|
||
@Override | ||
public void beforeAll(ExtensionContext context) throws Exception { | ||
PostgresqlLocalInstance.createTestDatabase(); | ||
} | ||
|
||
@Override | ||
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { | ||
var type = parameterContext.getParameter().getParameterizedType(); | ||
return type.equals(PostgresqlTransactionalStoreSetupExtension.class); | ||
} | ||
|
||
@Override | ||
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws | ||
ParameterResolutionException { | ||
var type = parameterContext.getParameter().getParameterizedType(); | ||
if (type.equals(PostgresqlTransactionalStoreSetupExtension.class)) { | ||
return this; | ||
} | ||
return null; | ||
} | ||
} |
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
147 changes: 147 additions & 0 deletions
147
...rg/eclipse/tractusx/edc/edr/store/sql/SqlEndpointDataReferenceCacheTransactionalTest.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,147 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.tractusx.edc.edr.store.sql; | ||
|
||
import org.eclipse.edc.junit.annotations.PostgresqlDbIntegrationTest; | ||
import org.eclipse.edc.spi.persistence.EdcPersistenceException; | ||
import org.eclipse.edc.spi.query.QuerySpec; | ||
import org.eclipse.edc.spi.result.Result; | ||
import org.eclipse.edc.spi.result.StoreResult; | ||
import org.eclipse.edc.spi.security.Vault; | ||
import org.eclipse.edc.spi.types.TypeManager; | ||
import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference; | ||
import org.eclipse.tractusx.edc.edr.store.sql.schema.EdrStatements; | ||
import org.eclipse.tractusx.edc.edr.store.sql.schema.postgres.PostgresEdrStatements; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.sql.SQLException; | ||
import java.time.Clock; | ||
|
||
import static java.util.UUID.randomUUID; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.eclipse.tractusx.edc.edr.spi.TestFunctions.edr; | ||
import static org.eclipse.tractusx.edc.edr.spi.TestFunctions.edrEntry; | ||
import static org.eclipse.tractusx.edc.edr.store.sql.SqlEndpointDataReferenceCache.VAULT_PREFIX; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@PostgresqlDbIntegrationTest | ||
@ExtendWith(PostgresqlTransactionalStoreSetupExtension.class) | ||
public class SqlEndpointDataReferenceCacheTransactionalTest { | ||
|
||
EdrStatements statements = new PostgresEdrStatements(); | ||
SqlEndpointDataReferenceCache cache; | ||
|
||
Clock clock = Clock.systemUTC(); | ||
|
||
Vault vault = mock(Vault.class); | ||
|
||
TypeManager typeManager = new TypeManager(); | ||
|
||
@BeforeEach | ||
void setUp(PostgresqlTransactionalStoreSetupExtension extension) throws IOException { | ||
|
||
when(vault.deleteSecret(any())).thenReturn(Result.success()); | ||
when(vault.storeSecret(any(), any())).thenReturn(Result.success()); | ||
|
||
cache = new SqlEndpointDataReferenceCache(extension.getDataSourceRegistry(), extension.getDatasourceName(), extension.getTransactionContext(), statements, typeManager.getMapper(), vault, clock); | ||
var schema = Files.readString(Paths.get("./docs/schema.sql")); | ||
extension.runQuery(schema); | ||
|
||
} | ||
|
||
@Test | ||
void save_shouldFail_whenVaultError() { | ||
|
||
var tpId = "tp1"; | ||
var assetId = "asset1"; | ||
var edrId = "edr1"; | ||
|
||
var edr = edr(edrId); | ||
var entry = edrEntry(assetId, randomUUID().toString(), tpId); | ||
|
||
when(vault.storeSecret(any(), any())).thenReturn(Result.failure("fail")); | ||
when(vault.resolveSecret(edr.getId())).thenReturn(typeManager.writeValueAsString(edr)); | ||
|
||
assertThatThrownBy(() -> cache.save(entry, edr)).isInstanceOf(EdcPersistenceException.class); | ||
|
||
assertThat(cache.resolveReference(tpId)) | ||
.isNull(); | ||
|
||
} | ||
|
||
@Test | ||
void save() { | ||
|
||
var tpId = "tp1"; | ||
var assetId = "asset1"; | ||
var edrId = "edr1"; | ||
|
||
var edr = edr(edrId); | ||
var entry = edrEntry(assetId, randomUUID().toString(), tpId); | ||
|
||
when(vault.storeSecret(any(), any())).thenReturn(Result.success()); | ||
when(vault.resolveSecret(edr.getId())).thenReturn(typeManager.writeValueAsString(edr)); | ||
|
||
cache.save(entry, edr); | ||
|
||
assertThat(cache.resolveReference(tpId)) | ||
.isNotNull() | ||
.extracting(EndpointDataReference::getId) | ||
.isEqualTo(edrId); | ||
|
||
var edrs = cache.referencesForAsset(assetId); | ||
assertThat(edrs.size()).isEqualTo(1); | ||
assertThat(edrs.get((0)).getId()).isEqualTo(edrId); | ||
|
||
verify(vault).storeSecret(eq(VAULT_PREFIX + edr.getId()), any()); | ||
|
||
} | ||
|
||
@Test | ||
void deleteByTransferProcessId_shouldDelete_WhenFound() { | ||
|
||
var entry = edrEntry("assetId", "agreementId", "tpId"); | ||
var edr = edr("edrId"); | ||
cache.save(entry, edr); | ||
|
||
assertThat(cache.deleteByTransferProcessId(entry.getTransferProcessId())) | ||
.extracting(StoreResult::getContent) | ||
.isEqualTo(entry); | ||
|
||
assertThat(cache.resolveReference(entry.getTransferProcessId())).isNull(); | ||
assertThat(cache.referencesForAsset(entry.getAssetId())).hasSize(0); | ||
assertThat(cache.queryForEntries(QuerySpec.max())).hasSize(0); | ||
|
||
verify(vault).storeSecret(eq(VAULT_PREFIX + edr.getId()), any()); | ||
verify(vault).deleteSecret(eq(VAULT_PREFIX + edr.getId())); | ||
} | ||
|
||
@AfterEach | ||
void tearDown(PostgresqlTransactionalStoreSetupExtension extension) throws SQLException { | ||
extension.runQuery("DROP TABLE " + statements.getEdrTable() + " CASCADE"); | ||
} | ||
|
||
} |
Oops, something went wrong.