Skip to content

Commit

Permalink
fix: add missing JsonObjectToCriterionTransformer (#267)
Browse files Browse the repository at this point in the history
* fix: add missing JsonObjectToCriterionTransformer

* test(e2e): verify catalog with QuerySpec.filterExpression
  • Loading branch information
thomasrutger authored Nov 21, 2024
1 parent b815d90 commit 9c4610f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Result<String> postContractDefinition(JsonObject definition) {
return postObjectWithId(createPostRequest(definition, managementBaseUrl + "/v3/contractdefinitions"));
}

List<Catalog> getContractOffers() {
var rq = createPostRequest(TestFunctions.createEmptyQuery(), catalog("/v1alpha/catalog/query"));
List<Catalog> getCatalogs(JsonObject querySpec) {
var rq = createPostRequest(querySpec, catalog("/v1alpha/catalog/query"));

try (var response = getClient().newCall(rq).execute()) {
if (response.isSuccessful()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> r) {
return ofNullable(r.getFailureDetail()).orElse("No error");
}

private void assertCatalogContainsOffer(String assetIdBase64, List<Catalog> 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));
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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();
}

Expand All @@ -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)
);
}

Expand Down

0 comments on commit 9c4610f

Please sign in to comment.