From 9c4610f34b19b17174cb2354e93bd9de13a0d1f9 Mon Sep 17 00:00:00 2001 From: Thomas Nijman <25709868+thomasrutger@users.noreply.github.com> Date: Thu, 21 Nov 2024 15:11:19 +0100 Subject: [PATCH] fix: add missing JsonObjectToCriterionTransformer (#267) * fix: add missing JsonObjectToCriterionTransformer * test(e2e): verify catalog with QuerySpec.filterExpression --- .../cache/FederatedCatalogCacheExtension.java | 2 ++ .../eclipse/edc/end2end/CatalogApiClient.java | 4 +-- .../edc/end2end/FederatedCatalogTest.java | 27 ++++++++++++++----- .../eclipse/edc/end2end/TestFunctions.java | 21 ++++++++++----- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/core/federated-catalog-core/src/main/java/org/eclipse/edc/catalog/cache/FederatedCatalogCacheExtension.java b/core/federated-catalog-core/src/main/java/org/eclipse/edc/catalog/cache/FederatedCatalogCacheExtension.java index 2085fd0e..741a2daa 100644 --- a/core/federated-catalog-core/src/main/java/org/eclipse/edc/catalog/cache/FederatedCatalogCacheExtension.java +++ b/core/federated-catalog-core/src/main/java/org/eclipse/edc/catalog/cache/FederatedCatalogCacheExtension.java @@ -50,6 +50,7 @@ import org.eclipse.edc.spi.system.health.HealthCheckService; import org.eclipse.edc.spi.types.TypeManager; import org.eclipse.edc.transform.spi.TypeTransformerRegistry; +import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToCriterionTransformer; import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToQuerySpecTransformer; import org.eclipse.edc.transform.transformer.edc.to.JsonValueToGenericTypeTransformer; @@ -156,6 +157,7 @@ private void registerTransformers(ServiceExtensionContext context) { transformerRegistry.register(new JsonObjectToDataServiceTransformer()); transformerRegistry.register(new JsonObjectToDistributionTransformer()); transformerRegistry.register(new JsonObjectToQuerySpecTransformer()); + transformerRegistry.register(new JsonObjectToCriterionTransformer()); var jsonFactory = Json.createBuilderFactory(Map.of()); var mapper = context.getService(TypeManager.class).getMapper(JSON_LD); diff --git a/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/CatalogApiClient.java b/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/CatalogApiClient.java index 829fafbe..3db8a358 100644 --- a/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/CatalogApiClient.java +++ b/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/CatalogApiClient.java @@ -67,8 +67,8 @@ Result postContractDefinition(JsonObject definition) { return postObjectWithId(createPostRequest(definition, managementBaseUrl + "/v3/contractdefinitions")); } - List getContractOffers() { - var rq = createPostRequest(TestFunctions.createEmptyQuery(), catalog("/v1alpha/catalog/query")); + List getCatalogs(JsonObject querySpec) { + var rq = createPostRequest(querySpec, catalog("/v1alpha/catalog/query")); try (var response = getClient().newCall(rq).execute()) { if (response.isSuccessful()) { diff --git a/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/FederatedCatalogTest.java b/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/FederatedCatalogTest.java index 087313c6..29b10a67 100644 --- a/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/FederatedCatalogTest.java +++ b/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/FederatedCatalogTest.java @@ -23,6 +23,7 @@ import org.eclipse.edc.catalog.transform.JsonObjectToDataServiceTransformer; import org.eclipse.edc.catalog.transform.JsonObjectToDatasetTransformer; import org.eclipse.edc.catalog.transform.JsonObjectToDistributionTransformer; +import org.eclipse.edc.connector.controlplane.catalog.spi.Catalog; import org.eclipse.edc.connector.controlplane.transform.odrl.from.JsonObjectFromPolicyTransformer; import org.eclipse.edc.connector.core.agent.NoOpParticipantIdMapper; import org.eclipse.edc.crawler.spi.TargetNode; @@ -182,19 +183,31 @@ void crawl_whenOfferAvailable_shouldContainOffer(TestInfo testInfo) { .pollInterval(ofSeconds(1)) .atMost(TIMEOUT) .untilAsserted(() -> { - var catalogs = apiClient.getContractOffers(); - assertThat(catalogs).hasSizeGreaterThanOrEqualTo(1); - assertThat(catalogs).anySatisfy(catalog -> assertThat(catalog.getDatasets()) - .anySatisfy(dataset -> { - assertThat(dataset.getOffers()).hasSizeGreaterThanOrEqualTo(1); - assertThat(dataset.getOffers().keySet()).anyMatch(key -> key.contains(assetIdBase64)); - })); + // With empty query + var emptyQuery = TestFunctions.createEmptyQuery(); + var catalogs = apiClient.getCatalogs(emptyQuery); + assertCatalogContainsOffer(assetIdBase64, catalogs); + + // With query containing a filter expression for existing asset. + var queryWithExistingAssetId = TestFunctions.createQuerySpecWithFilterExpressionForAssetId(id); + catalogs = apiClient.getCatalogs(queryWithExistingAssetId); + + assertCatalogContainsOffer(assetIdBase64, catalogs); }); } private String getError(Result r) { return ofNullable(r.getFailureDetail()).orElse("No error"); } + + private void assertCatalogContainsOffer(String assetIdBase64, List catalogs) { + assertThat(catalogs).hasSizeGreaterThanOrEqualTo(1); + assertThat(catalogs).anySatisfy(catalog -> assertThat(catalog.getDatasets()) + .anySatisfy(dataset -> { + assertThat(dataset.getOffers()).hasSizeGreaterThanOrEqualTo(1); + assertThat(dataset.getOffers().keySet()).anyMatch(key -> key.contains(assetIdBase64)); + })); + } } diff --git a/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/TestFunctions.java b/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/TestFunctions.java index 0ce3123c..2f039b1b 100644 --- a/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/TestFunctions.java +++ b/system-tests/end2end-test/e2e-junit-runner/src/test/java/org/eclipse/edc/end2end/TestFunctions.java @@ -35,6 +35,7 @@ import static org.eclipse.edc.junit.testfixtures.TestUtils.getResourceFileContentAsString; import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE; import static org.eclipse.edc.spi.constants.CoreConstants.EDC_PREFIX; +import static org.eclipse.edc.spi.query.QuerySpec.EDC_QUERY_SPEC_FILTER_EXPRESSION; public class TestFunctions { @@ -60,7 +61,7 @@ public static JsonObject createContractDef(String id, String accessPolicyId, Str .add(ID, id) .add(CONTRACT_DEFINITION_ACCESSPOLICY_ID, accessPolicyId) .add(CONTRACT_DEFINITION_CONTRACTPOLICY_ID, contractPolicyId) - .add(CONTRACT_DEFINITION_ASSETS_SELECTOR, createCriterionBuilder(assetId).build()) + .add(CONTRACT_DEFINITION_ASSETS_SELECTOR, createCriterionBuilder(Asset.PROPERTY_ID, "=", assetId).build()) .build(); } @@ -70,13 +71,21 @@ public static JsonObject createEmptyQuery() { .build(); } - private static JsonArrayBuilder createCriterionBuilder(String assetId) { + public static JsonObject createQuerySpecWithFilterExpressionForAssetId(String id) { + return Json.createObjectBuilder() + .add(TYPE, QuerySpec.EDC_QUERY_SPEC_TYPE) + .add(EDC_QUERY_SPEC_FILTER_EXPRESSION, createCriterionBuilder("datasets.id", "=", id)) + .build(); + } + + + private static JsonArrayBuilder createCriterionBuilder(String operandLeft, String operator, String operandRight) { return Json.createArrayBuilder() .add(Json.createObjectBuilder() - .add(TYPE, EDC_NAMESPACE + "CriterionDto") - .add(EDC_NAMESPACE + "operandLeft", Asset.PROPERTY_ID) - .add(EDC_NAMESPACE + "operator", "=") - .add(EDC_NAMESPACE + "operandRight", assetId) + .add(TYPE, EDC_NAMESPACE + "Criterion") + .add(EDC_NAMESPACE + "operandLeft", operandLeft) + .add(EDC_NAMESPACE + "operator", operator) + .add(EDC_NAMESPACE + "operandRight", operandRight) ); }