From aac6d672f1f26f12323dbd792b0305e3ff8accd3 Mon Sep 17 00:00:00 2001 From: Paul Latzelsperger Date: Tue, 11 Jun 2024 16:52:17 +0200 Subject: [PATCH 1/2] chore(test): adopt EDC test runtime model --- .../cache/FederatedCatalogCacheExtension.java | 2 +- .../federated-catalog-api/build.gradle.kts | 5 +- .../FederatedCatalogApiControllerTest.java | 77 +++++++++---------- gradle/libs.versions.toml | 1 + ...ataplaneInstanceRegistrationExtension.java | 3 +- 5 files changed, 41 insertions(+), 47 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 78d34451..ebe76e7b 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 @@ -109,7 +109,7 @@ public void initialize(ServiceExtensionContext context) { int numCrawlers = context.getSetting(NUM_CRAWLER_SETTING, DEFAULT_NUMBER_OF_CRAWLERS); // by default only uses FC nodes that are not "self" - nodeFilter = ofNullable(nodeFilter).orElse(node -> !node.name().equals(context.getConnectorId())); + nodeFilter = ofNullable(nodeFilter).orElse(node -> !node.name().equals(context.getRuntimeId())); executionManager = ExecutionManager.Builder.newInstance() .monitor(context.getMonitor().withPrefix("ExecutionManager")) diff --git a/extensions/api/federated-catalog-api/build.gradle.kts b/extensions/api/federated-catalog-api/build.gradle.kts index 0e212441..4fc50c1e 100644 --- a/extensions/api/federated-catalog-api/build.gradle.kts +++ b/extensions/api/federated-catalog-api/build.gradle.kts @@ -39,8 +39,9 @@ dependencies { testImplementation(libs.edc.iam.mock) testImplementation(libs.edc.json.ld.lib) testImplementation(libs.edc.dsp.transform.catalog) - testImplementation(libs.edc.dsp.transform.catalog) - + testImplementation(testFixtures(libs.edc.core.jersey)) + testImplementation(libs.edc.lib.transform) + testImplementation(libs.edc.lib.query) } edcBuild { diff --git a/extensions/api/federated-catalog-api/src/test/java/org/eclipse/edc/catalog/api/query/FederatedCatalogApiControllerTest.java b/extensions/api/federated-catalog-api/src/test/java/org/eclipse/edc/catalog/api/query/FederatedCatalogApiControllerTest.java index 9f8cca3a..529ae7b0 100644 --- a/extensions/api/federated-catalog-api/src/test/java/org/eclipse/edc/catalog/api/query/FederatedCatalogApiControllerTest.java +++ b/extensions/api/federated-catalog-api/src/test/java/org/eclipse/edc/catalog/api/query/FederatedCatalogApiControllerTest.java @@ -16,10 +16,12 @@ import io.restassured.specification.RequestSpecification; import jakarta.json.Json; +import org.eclipse.edc.catalog.cache.query.CacheQueryAdapterImpl; +import org.eclipse.edc.catalog.cache.query.CacheQueryAdapterRegistryImpl; +import org.eclipse.edc.catalog.cache.query.QueryEngineImpl; import org.eclipse.edc.catalog.spi.CacheQueryAdapter; -import org.eclipse.edc.catalog.spi.CacheQueryAdapterRegistry; -import org.eclipse.edc.catalog.spi.FederatedCacheStore; import org.eclipse.edc.catalog.spi.model.FederatedCatalogCacheQuery; +import org.eclipse.edc.catalog.store.InMemoryFederatedCacheStore; import org.eclipse.edc.catalog.transform.JsonObjectToCatalogTransformer; import org.eclipse.edc.catalog.transform.JsonObjectToDataServiceTransformer; import org.eclipse.edc.catalog.transform.JsonObjectToDatasetTransformer; @@ -27,45 +29,39 @@ import org.eclipse.edc.connector.core.agent.NoOpParticipantIdMapper; import org.eclipse.edc.jsonld.util.JacksonJsonLd; import org.eclipse.edc.junit.annotations.ApiTest; -import org.eclipse.edc.junit.extensions.EdcExtension; import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromCatalogTransformer; import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromDataServiceTransformer; import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromDatasetTransformer; import org.eclipse.edc.protocol.dsp.catalog.transform.from.JsonObjectFromDistributionTransformer; -import org.eclipse.edc.spi.system.ServiceExtension; -import org.eclipse.edc.spi.system.ServiceExtensionContext; -import org.eclipse.edc.transform.spi.TypeTransformerRegistry; +import org.eclipse.edc.query.CriterionOperatorRegistryImpl; +import org.eclipse.edc.transform.TypeTransformerRegistryImpl; +import org.eclipse.edc.util.concurrency.LockManager; +import org.eclipse.edc.web.jersey.testfixtures.RestControllerTestBase; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import java.util.Map; +import java.util.concurrent.locks.ReentrantReadWriteLock; import static io.restassured.RestAssured.given; import static io.restassured.http.ContentType.JSON; import static java.util.stream.IntStream.range; import static org.eclipse.edc.catalog.test.TestUtil.createCatalog; -import static org.eclipse.edc.util.io.Ports.getFreePort; import static org.hamcrest.CoreMatchers.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ApiTest -@ExtendWith(EdcExtension.class) -class FederatedCatalogApiControllerTest { - private static final String BASE_PATH = "/api/catalog"; - private final int port = getFreePort(); +class FederatedCatalogApiControllerTest extends RestControllerTestBase { + private final CacheQueryAdapterRegistryImpl cacheQueryAdapterRegistry = new CacheQueryAdapterRegistryImpl(); + private InMemoryFederatedCacheStore store; @BeforeEach - void setUp(EdcExtension extension) { - extension.setConfiguration(Map.of( - "web.http.path", "/api", - "web.http.port", String.valueOf(getFreePort()), - "web.http.catalog.port", String.valueOf(port), - "web.http.catalog.path", BASE_PATH - )); - extension.registerSystemExtension(ServiceExtension.class, new TransformerRegistrarExtension()); + void setup() { + store = new InMemoryFederatedCacheStore(new LockManager(new ReentrantReadWriteLock()), CriterionOperatorRegistryImpl.ofDefaults()); + var adapter = new CacheQueryAdapterImpl(store); + cacheQueryAdapterRegistry.register(adapter); } @Test @@ -81,7 +77,7 @@ void queryApi_whenEmptyResult() { } @Test - void queryApi_whenResultsReturned(FederatedCacheStore store) { + void queryApi_whenResultsReturned() { range(0, 3).mapToObj(i -> createCatalog("some-offer-" + i)).forEach(store::save); baseRequest() @@ -95,11 +91,11 @@ void queryApi_whenResultsReturned(FederatedCacheStore store) { } @Test - void queryApi_whenQueryUnsuccessful(CacheQueryAdapterRegistry adapterRegistry) { + void queryApi_whenQueryUnsuccessful() { var adapter = mock(CacheQueryAdapter.class); when(adapter.executeQuery(any())).thenThrow(new RuntimeException("test exception")); when(adapter.canExecute(any())).thenReturn(true); - adapterRegistry.register(adapter); + cacheQueryAdapterRegistry.register(adapter); baseRequest() .contentType(JSON) @@ -109,28 +105,25 @@ void queryApi_whenQueryUnsuccessful(CacheQueryAdapterRegistry adapterRegistry) { .statusCode(500); } + @Override + protected Object controller() { + var typeTransformerRegistry = new TypeTransformerRegistryImpl(); + var factory = Json.createBuilderFactory(Map.of()); + var mapper = JacksonJsonLd.createObjectMapper(); + typeTransformerRegistry.register(new JsonObjectToCatalogTransformer()); + typeTransformerRegistry.register(new JsonObjectFromDatasetTransformer(factory, mapper)); + typeTransformerRegistry.register(new JsonObjectFromDistributionTransformer(factory)); + typeTransformerRegistry.register(new JsonObjectFromDataServiceTransformer(factory)); + typeTransformerRegistry.register(new JsonObjectFromCatalogTransformer(factory, mapper, new NoOpParticipantIdMapper())); + typeTransformerRegistry.register(new JsonObjectToDatasetTransformer()); + typeTransformerRegistry.register(new JsonObjectToDataServiceTransformer()); + typeTransformerRegistry.register(new JsonObjectToDistributionTransformer()); + return new FederatedCatalogApiController(new QueryEngineImpl(cacheQueryAdapterRegistry), typeTransformerRegistry); + } + private RequestSpecification baseRequest() { return given() .baseUri("http://localhost:" + port) - .basePath(BASE_PATH) .when(); } - - public static class TransformerRegistrarExtension implements ServiceExtension { - - @Override - public void initialize(ServiceExtensionContext context) { - var typeTransformerRegistry = context.getService(TypeTransformerRegistry.class); - var factory = Json.createBuilderFactory(Map.of()); - var mapper = JacksonJsonLd.createObjectMapper(); - typeTransformerRegistry.register(new JsonObjectToCatalogTransformer()); - typeTransformerRegistry.register(new JsonObjectFromDatasetTransformer(factory, mapper)); - typeTransformerRegistry.register(new JsonObjectFromDistributionTransformer(factory)); - typeTransformerRegistry.register(new JsonObjectFromDataServiceTransformer(factory)); - typeTransformerRegistry.register(new JsonObjectFromCatalogTransformer(factory, mapper, new NoOpParticipantIdMapper())); - typeTransformerRegistry.register(new JsonObjectToDatasetTransformer()); - typeTransformerRegistry.register(new JsonObjectToDataServiceTransformer()); - typeTransformerRegistry.register(new JsonObjectToDistributionTransformer()); - } - } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 69e826a5..e707bddd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -50,6 +50,7 @@ edc-lib-boot = { module = "org.eclipse.edc:boot-lib", version.ref = "edc" } edc-lib-providers-jersey = { module = "org.eclipse.edc:jersey-providers-lib", version.ref = "edc" } edc-lib-transform = { module = "org.eclipse.edc:transform-lib", version.ref = "edc" } edc-lib-util = { module = "org.eclipse.edc:util-lib", version.ref = "edc" } +edc-lib-query = { module = "org.eclipse.edc:query-lib", version.ref = "edc" } edc-lib-jerseyproviders = { module = "org.eclipse.edc:jersey-providers-lib", version.ref = "edc" } # protocol modules diff --git a/system-tests/end2end-test/connector-runtime/src/main/java/org/eclipse/edc/federatedcatalog/end2end/DataplaneInstanceRegistrationExtension.java b/system-tests/end2end-test/connector-runtime/src/main/java/org/eclipse/edc/federatedcatalog/end2end/DataplaneInstanceRegistrationExtension.java index b5faf732..3d9305d2 100644 --- a/system-tests/end2end-test/connector-runtime/src/main/java/org/eclipse/edc/federatedcatalog/end2end/DataplaneInstanceRegistrationExtension.java +++ b/system-tests/end2end-test/connector-runtime/src/main/java/org/eclipse/edc/federatedcatalog/end2end/DataplaneInstanceRegistrationExtension.java @@ -20,7 +20,6 @@ import org.eclipse.edc.connector.dataplane.selector.spi.store.DataPlaneInstanceStore; import org.eclipse.edc.runtime.metamodel.annotation.Inject; import org.eclipse.edc.runtime.metamodel.annotation.Provider; -import org.eclipse.edc.spi.EdcException; import org.eclipse.edc.spi.system.ServiceExtension; import org.eclipse.edc.spi.system.ServiceExtensionContext; @@ -37,7 +36,7 @@ public void initialize(ServiceExtensionContext context) { .allowedSourceType("test-src-type") .url("http://test.local") .build(); - dataPlaneInstanceStore.create(dpi).orElseThrow(f -> new EdcException(f.getFailureDetail())); + dataPlaneInstanceStore.save(dpi); } @Provider From 968bc3f19aa32441668b5bc342192b6125db7e7b Mon Sep 17 00:00:00 2001 From: Paul Latzelsperger Date: Tue, 11 Jun 2024 18:01:37 +0200 Subject: [PATCH 2/2] DEPENDENCIES --- DEPENDENCIES | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DEPENDENCIES b/DEPENDENCIES index 76106f9c..2cea3d8a 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -37,7 +37,7 @@ maven/mavencentral/com.google.guava/failureaccess/1.0.2, Apache-2.0, approved, C maven/mavencentral/com.google.guava/guava/33.2.0-jre, Apache-2.0 AND CC0-1.0 AND (Apache-2.0 AND CC-PDDC), approved, #14607 maven/mavencentral/com.google.guava/listenablefuture/9999.0-empty-to-avoid-conflict-with-guava, Apache-2.0, approved, CQ22657 maven/mavencentral/com.google.protobuf/protobuf-java/3.25.1, BSD-3-Clause, approved, clearlydefined -maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.39.3, Apache-2.0, approved, #14830 +maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.40, , restricted, clearlydefined maven/mavencentral/com.puppycrawl.tools/checkstyle/10.17.0, LGPL-2.1-or-later AND (Apache-2.0 AND LGPL-2.1-or-later) AND Apache-2.0, approved, #15077 maven/mavencentral/com.squareup.okhttp3/okhttp-dnsoverhttps/4.12.0, Apache-2.0, approved, #11159 maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #11156