Skip to content
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

chore(test): adopt EDC test runtime model #198

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
maven/mavencentral/com.apicatalog/carbon-did/0.3.0, Apache-2.0, approved, clearlydefined

Check warning on line 1 in DEPENDENCIES

View workflow job for this annotation

GitHub Actions / check / Dash-Verify-Licenses

Restricted Dependencies found

Some dependencies are marked 'restricted' - please review them
maven/mavencentral/com.apicatalog/copper-multibase/0.5.0, Apache-2.0, approved, #14501
maven/mavencentral/com.apicatalog/copper-multicodec/0.1.1, Apache-2.0, approved, #14500
maven/mavencentral/com.apicatalog/iron-verifiable-credentials/0.14.0, Apache-2.0, approved, clearlydefined
Expand Down Expand Up @@ -37,7 +37,7 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
5 changes: 3 additions & 2 deletions extensions/api/federated-catalog-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,52 @@

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;
import org.eclipse.edc.catalog.transform.JsonObjectToDistributionTransformer;
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
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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());
}
}
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Loading