Skip to content

Commit

Permalink
feat: add update method to AccessTokenDataStore (#3981)
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger authored Mar 8, 2024
1 parent 3b1e083 commit 3c4c192
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public StoreResult<Void> store(AccessTokenData accessTokenData) {
.orElse(StoreResult.success());
}

@Override
public StoreResult<Void> 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<Void> deleteById(String id) {
var prev = store.remove(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ public StoreResult<Void> store(AccessTokenData accessTokenData) {
});
}

@Override
public StoreResult<Void> 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<Void> deleteById(String id) {
return transactionContext.execute(() -> {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ default String getAdditionalPropertiesColumn() {

String getDeleteTemplate();

SqlQueryStatement createQuery(QuerySpec querySpec);
String getUpdateTemplate();

SqlQueryStatement createQuery(QuerySpec querySpec);
}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public interface AccessTokenDataStore {
*/
StoreResult<Void> 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<Void> update(AccessTokenData accessTokenData);

/**
* Deletes an {@link AccessTokenData} entity with the given ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 3c4c192

Please sign in to comment.