Skip to content

Conversation

@bmg13
Copy link
Contributor

@bmg13 bmg13 commented May 30, 2025

What this PR changes/adds

Include fix to enable retrieving Contract Definitions sorted by their creation date. Simply updated the mapping for this entity.

Why it does that

We should be able to sort Contract Definitions by their creation date, similar to what is seen with other entities (like assets and policies).

Linked Issue(s)

Closes #4921

@bmg13 bmg13 requested a review from paullatzelsperger as a code owner May 30, 2025 13:18
@bmg13 bmg13 self-assigned this May 30, 2025
@bmg13 bmg13 added the bug Something isn't working label May 30, 2025
@bmg13 bmg13 force-pushed the fix/allow_sort_contract_definition_with_created_time branch from 1ef851a to a135a72 Compare May 30, 2025 13:34
public class ContractDefinitionMapping extends TranslationMapping {
public ContractDefinitionMapping(ContractDefinitionStatements statements) {
add("id", statements.getIdColumn());
add("createdAt", statements.getCreatedAtColumn());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be a test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated/Adde tests regarding ContractDefinition's createdAt, but no UT related with that specific block, since similar mappings do not seem to have them. Would you propose to create a ContractDefinitionMappingTest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests you provided do not represent the behavior that you described as failing in the attached issue. Please write a test that checks if the result of a contract definition api request is correctly ordered by creation date, when the request has a query spec contains such an ordering criteria.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added suggested test. Keep in mind that the createdAt field is not returned, so I enforced the result extraction as a list to keep the order and validate it with the return ids.

@bmg13 bmg13 requested a review from paullatzelsperger June 3, 2025 15:10
@bmg13 bmg13 requested a review from ndr-brt as a code owner June 8, 2025 08:40
@bmg13 bmg13 requested a review from rafaelmag110 June 9, 2025 13:23
var id1 = UUID.randomUUID().toString();
var id2 = UUID.randomUUID().toString();
var id3 = UUID.randomUUID().toString();
Stream.of(id1, id2, id3).forEach(id -> store.save(createContractDefinition(id).createdAt(System.nanoTime()).build()));
Copy link
Contributor

@jimmarino jimmarino Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't do this. Start with a time and then increment using an offset so it is deterministic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

followed suggestion, changed in here.

Stream.of(id1, id2, id3).forEach(id -> store.save(createContractDefinition(id).createdAt(System.nanoTime()).build()));
var createdAtTime = 1000L;
var increment = new AtomicInteger(0);
Stream.of(id1, id2, id3).forEach(id -> store.save(createContractDefinition(id).createdAt(createdAtTime + increment.getAndIncrement()).build()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use an AtomicLong and have it as one operation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, simplified in here.

@bmg13 bmg13 requested a review from jimmarino June 11, 2025 16:58

var querySpec = QuerySpec.Builder.newInstance().sortField("createdAt");

assertThat(getContractDefinitionStore().findAll(querySpec.sortOrder(SortOrder.ASC).build())).isSortedAccordingTo(Comparator.comparing(ContractDefinition::getCreatedAt));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format this so the method calls appear on subsequent lines - it makes it easier to read

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in here.

}

@Test
void queryContractDefinitions_sortByCreatedDate(ManagementEndToEndTestContext context, ContractDefinitionStore store) throws JsonProcessingException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you doing this in an end-to-end test? This should be verified by individual unit tests and an integration test for Postgres. End-to-end tests should only verify complete codepaths and should not be used to verify individual functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning is due to the raised issue was not having the ordered response and with this test that can be confirmed, being a PostgresqlIntegrationTest.
Similar to what found in here for TransferProcesss.
If the end result was just the existence of a field, it would not make much sense, but having the order on the final response I believe it adds value.
What if I update this test to be queryContractDefinitions_withQuerySpec since it is broader and is not currently covered?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see verifySortingCreatedAt covers the case for all store implementations. I don't understand the comment:

What if I update this test to be queryContractDefinitions_withQuerySpec since it is broader and is not currently covered?

Is this test intended to verify that ordering is preserved end-to-end, or does it do something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is for the ordering only.

…w_sort_contract_definition_with_created_time
@bmg13 bmg13 requested a review from jimmarino June 12, 2025 15:17
@ndr-brt
Copy link
Member

ndr-brt commented Jun 20, 2025

@jimmarino could you review it again? I'd be ready to merge this

}

@Test
void queryContractDefinitions_sortByCreatedDate(ManagementEndToEndTestContext context, ContractDefinitionStore store) throws JsonProcessingException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see verifySortingCreatedAt covers the case for all store implementations. I don't understand the comment:

What if I update this test to be queryContractDefinitions_withQuerySpec since it is broader and is not currently covered?

Is this test intended to verify that ordering is preserved end-to-end, or does it do something else?

var createdAtTime = new AtomicLong(1000L);
Stream.of(id1, id2, id3).forEach(id -> store.save(createContractDefinition(id).createdAt(createdAtTime.getAndIncrement()).build()));

var content = """
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a JSON string created and parsed into a query object like this. The query object should be created in code directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was based on existing testing for timestamp sorting, like this one

@github-actions
Copy link

This pull request is stale because it has been open for 7 days with no activity.

@github-actions github-actions bot added the stale Open for x days with no activity label Jun 28, 2025
@github-actions
Copy link

github-actions bot commented Jul 6, 2025

This pull request was closed because it has been inactive for 7 days since being marked as stale.

@github-actions github-actions bot closed this Jul 6, 2025
…w_sort_contract_definition_with_created_time
@bmg13 bmg13 reopened this Jul 23, 2025
@bmg13 bmg13 requested a review from jimmarino July 23, 2025 13:35
@github-actions github-actions bot removed the stale Open for x days with no activity label Jul 24, 2025
@github-actions
Copy link

github-actions bot commented Aug 5, 2025

This pull request is stale because it has been open for 7 days with no activity.

@github-actions github-actions bot added the stale Open for x days with no activity label Aug 5, 2025
@github-actions github-actions bot removed the stale Open for x days with no activity label Aug 13, 2025
@ndr-brt
Copy link
Member

ndr-brt commented Aug 18, 2025

I will merge this as it has been taking too long and we'd like to have it as part of 0.14.0

@ndr-brt ndr-brt merged commit 1a35597 into eclipse-edc:main Aug 18, 2025
48 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix allowing sorting Contract Definitions by creation date

5 participants