diff --git a/core/data-plane/data-plane-core/src/main/java/org/eclipse/edc/connector/dataplane/framework/store/InMemoryAccessTokenDataStore.java b/core/data-plane/data-plane-core/src/main/java/org/eclipse/edc/connector/dataplane/framework/store/InMemoryAccessTokenDataStore.java index 454c1917e0e..d897efa6d31 100644 --- a/core/data-plane/data-plane-core/src/main/java/org/eclipse/edc/connector/dataplane/framework/store/InMemoryAccessTokenDataStore.java +++ b/core/data-plane/data-plane-core/src/main/java/org/eclipse/edc/connector/dataplane/framework/store/InMemoryAccessTokenDataStore.java @@ -52,6 +52,15 @@ public StoreResult store(AccessTokenData accessTokenData) { .orElse(StoreResult.success()); } + @Override + public StoreResult update(AccessTokenData accessTokenData) { + if (store.containsKey(accessTokenData.id())) { + store.put(accessTokenData.id(), accessTokenData); + return StoreResult.success(); + } + return StoreResult.notFound(OBJECT_NOT_FOUND.formatted(accessTokenData.id())); + } + @Override public StoreResult deleteById(String id) { var prev = store.remove(id); diff --git a/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/SqlAccessTokenDataStore.java b/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/SqlAccessTokenDataStore.java index 25cefcd0a8e..8fd1b946e28 100644 --- a/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/SqlAccessTokenDataStore.java +++ b/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/SqlAccessTokenDataStore.java @@ -84,6 +84,21 @@ public StoreResult store(AccessTokenData accessTokenData) { }); } + @Override + public StoreResult update(AccessTokenData accessTokenData) { + return transactionContext.execute(() -> { + try (var connection = getConnection()) { + if (findByIdInternal(connection, accessTokenData.id()) != null) { + update(connection, accessTokenData); + return StoreResult.success(); + } + return StoreResult.notFound(OBJECT_NOT_FOUND.formatted(accessTokenData.id())); + } catch (SQLException e) { + throw new EdcPersistenceException(e); + } + }); + } + @Override public StoreResult deleteById(String id) { return transactionContext.execute(() -> { @@ -122,6 +137,18 @@ private void insert(Connection connection, AccessTokenData dataFlow) { ); } + private void update(Connection connection, AccessTokenData data) { + + var sql = statements.getUpdateTemplate(); + queryExecutor.execute(connection, sql, + data.id(), + toJson(data.claimToken()), + toJson(data.dataAddress()), + toJson(data.additionalProperties()), + data.id() + ); + } + private AccessTokenData mapAccessTokenData(ResultSet resultSet) throws SQLException { var claimToken = fromJson(resultSet.getString(statements.getClaimTokenColumn()), ClaimToken.class); diff --git a/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/schema/AccessTokenDataStatements.java b/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/schema/AccessTokenDataStatements.java index ed7a724e71f..1eba79c750d 100644 --- a/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/schema/AccessTokenDataStatements.java +++ b/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/schema/AccessTokenDataStatements.java @@ -49,7 +49,8 @@ default String getAdditionalPropertiesColumn() { String getDeleteTemplate(); - SqlQueryStatement createQuery(QuerySpec querySpec); + String getUpdateTemplate(); + SqlQueryStatement createQuery(QuerySpec querySpec); } diff --git a/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/schema/BaseSqlAccessTokenStatements.java b/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/schema/BaseSqlAccessTokenStatements.java index 46a647c24e8..ab8b71833ba 100644 --- a/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/schema/BaseSqlAccessTokenStatements.java +++ b/extensions/data-plane/store/sql/accesstokendata-store-sql/src/main/java/org/eclipse/edc/connector/dataplane/store/sql/schema/BaseSqlAccessTokenStatements.java @@ -48,6 +48,16 @@ public String getDeleteTemplate() { .delete(getTableName(), getIdColumn()); } + @Override + public String getUpdateTemplate() { + return executeStatement() + .column(getIdColumn()) + .jsonColumn(getClaimTokenColumn()) + .jsonColumn(getDataAddressColumn()) + .jsonColumn(getAdditionalPropertiesColumn()) + .update(getTableName(), getIdColumn()); + } + @Override public SqlQueryStatement createQuery(QuerySpec querySpec) { return new SqlQueryStatement(getSelectTemplate(), querySpec, new AccessTokenDataMapping(this), operatorTranslator); diff --git a/spi/data-plane/data-plane-spi/src/main/java/org/eclipse/edc/connector/dataplane/spi/store/AccessTokenDataStore.java b/spi/data-plane/data-plane-spi/src/main/java/org/eclipse/edc/connector/dataplane/spi/store/AccessTokenDataStore.java index ccc95611082..4c2d5ea3efe 100644 --- a/spi/data-plane/data-plane-spi/src/main/java/org/eclipse/edc/connector/dataplane/spi/store/AccessTokenDataStore.java +++ b/spi/data-plane/data-plane-spi/src/main/java/org/eclipse/edc/connector/dataplane/spi/store/AccessTokenDataStore.java @@ -46,6 +46,14 @@ public interface AccessTokenDataStore { */ StoreResult store(AccessTokenData accessTokenData); + /** + * Replaces an {@link AccessTokenData} object int the persistence layer. Will return a failure if an object with that ID does not exist. + * + * @param accessTokenData the new object + * @return success if updated, a failure if an object with the same ID does not exist. + */ + StoreResult update(AccessTokenData accessTokenData); + /** * Deletes an {@link AccessTokenData} entity with the given ID. * diff --git a/spi/data-plane/data-plane-spi/src/testFixtures/java/org/eclipse/edc/connector/dataplane/spi/store/AccessTokenDataTestBase.java b/spi/data-plane/data-plane-spi/src/testFixtures/java/org/eclipse/edc/connector/dataplane/spi/store/AccessTokenDataTestBase.java index f4514186907..ebd42091b6f 100644 --- a/spi/data-plane/data-plane-spi/src/testFixtures/java/org/eclipse/edc/connector/dataplane/spi/store/AccessTokenDataTestBase.java +++ b/spi/data-plane/data-plane-spi/src/testFixtures/java/org/eclipse/edc/connector/dataplane/spi/store/AccessTokenDataTestBase.java @@ -154,6 +154,21 @@ void query_defaultQuerySpec() { assertThat(getStore().query(q)).hasSize(50); } + @Test + void update() { + var object = accessTokenData("1"); + getStore().store(object); + + var update = new AccessTokenData("1", object.claimToken(), object.dataAddress(), Map.of("fizz", "buzz")); + + assertThat(getStore().update(update).succeeded()).isTrue(); + } + + @Test + void update_whenNotExist() { + var object = accessTokenData("1"); + assertThat(getStore().update(object).failed()).isTrue(); + } protected abstract AccessTokenDataStore getStore();