-
Notifications
You must be signed in to change notification settings - Fork 266
fix: allowing sorting Contract Definitions by creation date #5020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
a135a72
86c621c
bf44068
af5b9bd
7172a21
8b49c1e
c5cee4e
54d5d0a
4f5ec4d
c957151
72e1c59
c3f9e1c
6456ff3
6d8995b
52d6903
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,6 +393,16 @@ void verifySorting() { | |
assertThat(getContractDefinitionStore().findAll(QuerySpec.Builder.newInstance().sortField("id").sortOrder(SortOrder.DESC).build())).hasSize(10).isSortedAccordingTo((c1, c2) -> c2.getId().compareTo(c1.getId())); | ||
} | ||
|
||
@Test | ||
void verifySortingCreatedAt() { | ||
IntStream.range(0, 10).mapToObj(i -> createContractDefinition("id" + i)).forEach(getContractDefinitionStore()::save); | ||
|
||
var querySpec = QuerySpec.Builder.newInstance().sortField("createdAt"); | ||
|
||
assertThat(getContractDefinitionStore().findAll(querySpec.sortOrder(SortOrder.ASC).build())).isSortedAccordingTo(Comparator.comparing(ContractDefinition::getCreatedAt)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in here. |
||
assertThat(getContractDefinitionStore().findAll(querySpec.sortOrder(SortOrder.DESC).build())).isSortedAccordingTo((c1, c2) -> Long.compare(c2.getCreatedAt(), c1.getCreatedAt())); | ||
} | ||
|
||
@Test | ||
void verifySorting_invalidProperty() { | ||
IntStream.range(0, 10).mapToObj(i -> createContractDefinition("id" + i)).forEach(getContractDefinitionStore()::save); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,14 @@ | |
|
||
package org.eclipse.edc.test.e2e.managementapi; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import jakarta.json.JsonArray; | ||
import jakarta.json.JsonObject; | ||
import jakarta.json.JsonObjectBuilder; | ||
import jakarta.json.JsonValue; | ||
import org.eclipse.edc.connector.controlplane.contract.spi.offer.store.ContractDefinitionStore; | ||
import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractDefinition; | ||
import org.eclipse.edc.jsonld.util.JacksonJsonLd; | ||
import org.eclipse.edc.junit.annotations.EndToEndTest; | ||
import org.eclipse.edc.junit.annotations.PostgresqlIntegrationTest; | ||
import org.eclipse.edc.sql.testfixtures.PostgresqlEndToEndExtension; | ||
|
@@ -28,7 +31,11 @@ | |
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Stream; | ||
|
||
import static io.restassured.http.ContentType.JSON; | ||
import static jakarta.json.Json.createArrayBuilder; | ||
|
@@ -117,6 +124,46 @@ void queryPolicyDefinitionWithSimplePrivateProperties(ManagementEndToEndTestCont | |
.body("size()", is(0)); | ||
} | ||
|
||
@Test | ||
void queryContractDefinitions_sortByCreatedDate(ManagementEndToEndTestContext context, ContractDefinitionStore store) throws JsonProcessingException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see
Is this test intended to verify that ordering is preserved end-to-end, or does it do something else? |
||
var id1 = UUID.randomUUID().toString(); | ||
var id2 = UUID.randomUUID().toString(); | ||
var id3 = UUID.randomUUID().toString(); | ||
var createdAtTime = 1000L; | ||
var increment = new AtomicInteger(0); | ||
Stream.of(id1, id2, id3).forEach(id -> store.save(createContractDefinition(id).createdAt(createdAtTime + increment.getAndIncrement()).build())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use an AtomicLong and have it as one operation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, simplified in here. |
||
|
||
var content = """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
{ | ||
"@context": { | ||
"@vocab": "https://w3id.org/edc/v0.0.1/ns/" | ||
}, | ||
"@type": "QuerySpec", | ||
"sortField": "createdAt", | ||
"sortOrder": "DESC", | ||
"limit": 100, | ||
"offset": 0 | ||
} | ||
"""; | ||
var query = JacksonJsonLd.createObjectMapper() | ||
.readValue(content, JsonObject.class); | ||
|
||
var result = context.baseRequest() | ||
.contentType(JSON) | ||
.body(query) | ||
.post("/v3/contractdefinitions/request") | ||
.then() | ||
.log().ifError() | ||
.statusCode(200) | ||
.body("size()", is(3)) | ||
.extract() | ||
.as(List.class); | ||
|
||
assertThat(result) | ||
.extracting(cd -> ((LinkedHashMap<?, ?>) cd).get(ID)) | ||
.containsExactlyElementsOf(List.of(id3, id2, id1)); | ||
} | ||
|
||
@Test | ||
void shouldCreateAndRetrieve(ManagementEndToEndTestContext context, ContractDefinitionStore store) { | ||
var id = UUID.randomUUID().toString(); | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.