From e410e5707859eac04eb22f97ff04cab55a77fb6a Mon Sep 17 00:00:00 2001 From: farhin23 Date: Fri, 29 Nov 2024 12:20:33 +0100 Subject: [PATCH] Add tests for federated catalog samples 01 and 02 --- system-tests/build.gradle.kts | 4 + .../common/FederatedCatalogCommon.java | 115 ++++++++++++++++++ .../FederatedCatalog01embeddedTest.java | 63 ++++++++++ .../FederatedCatalog02standaloneTest.java | 62 ++++++++++ 4 files changed, 244 insertions(+) create mode 100644 system-tests/src/test/java/org/eclipse/edc/samples/common/FederatedCatalogCommon.java create mode 100644 system-tests/src/test/java/org/eclipse/edc/samples/federatedCatalog/FederatedCatalog01embeddedTest.java create mode 100644 system-tests/src/test/java/org/eclipse/edc/samples/federatedCatalog/FederatedCatalog02standaloneTest.java diff --git a/system-tests/build.gradle.kts b/system-tests/build.gradle.kts index 03370623..25420dda 100644 --- a/system-tests/build.gradle.kts +++ b/system-tests/build.gradle.kts @@ -9,6 +9,7 @@ * * Contributors: * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation + * Fraunhofer-Gesellschaft - dependencies for Federated Catalog Tests * */ @@ -55,6 +56,9 @@ dependencies { testCompileOnly(project(":transfer:transfer-05-file-transfer-cloud:cloud-transfer-provider")) testCompileOnly(project(":transfer:transfer-05-file-transfer-cloud:cloud-transfer-consumer")) testCompileOnly(project(":transfer:transfer-05-file-transfer-cloud:transfer-file-cloud")) + + testCompileOnly(project(":federated-catalog:fc-01-embedded:fc-connector")) + testCompileOnly(project(":federated-catalog:fc-02-standalone:standalone-fc")) } tasks.compileJava { diff --git a/system-tests/src/test/java/org/eclipse/edc/samples/common/FederatedCatalogCommon.java b/system-tests/src/test/java/org/eclipse/edc/samples/common/FederatedCatalogCommon.java new file mode 100644 index 00000000..d8c1125e --- /dev/null +++ b/system-tests/src/test/java/org/eclipse/edc/samples/common/FederatedCatalogCommon.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2024 Fraunhofer-Gesellschaft + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Fraunhofer-Gesellschaft - initial API and implementation + * + */ + +package org.eclipse.edc.samples.common; + +import com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.HttpStatus; +import io.restassured.http.ContentType; +import org.eclipse.edc.junit.extensions.EmbeddedRuntime; +import org.eclipse.edc.junit.extensions.RuntimeExtension; +import org.eclipse.edc.junit.extensions.RuntimePerClassExtension; + +import java.util.Map; + +import static io.restassured.RestAssured.given; +import static org.eclipse.edc.samples.common.FileTransferCommon.getFileContentFromRelativePath; +import static org.eclipse.edc.samples.common.FileTransferCommon.getFileFromRelativePath; +import static org.eclipse.edc.samples.common.PrerequisitesCommon.API_KEY_HEADER_KEY; +import static org.eclipse.edc.samples.common.PrerequisitesCommon.API_KEY_HEADER_VALUE; +import static org.eclipse.edc.samples.util.TransferUtil.post; +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.Matchers.emptyString; + +public class FederatedCatalogCommon { + private static final String CREATE_ASSET_FILE_PATH = "transfer/transfer-01-negotiation/resources/create-asset.json"; + private static final String V3_ASSETS_PATH = "/v3/assets"; + private static final String ASSET_ID = "@id"; + + private static final String STANDALONE_FC_MODULE_PATH = ":federated-catalog:fc-02-standalone:standalone-fc"; + private static final String FC_CONNECTOR_MODULE_PATH = ":federated-catalog:fc-01-embedded:fc-connector"; + private static final String STANDALONE_FC = "standalone-fc"; + private static final String CONSUMER = "consumer"; + + private static final String EDC_KEYSTORE = "edc.keystore"; + private static final String EDC_KEYSTORE_PASSWORD = "edc.keystore.password"; + private static final String EDC_FS_CONFIG = "edc.fs.config"; + private static final String CERT_PFX_FILE_PATH = "transfer/transfer-00-prerequisites/resources/certs/cert.pfx"; + private static final String KEYSTORE_PASSWORD = "123456"; + + private static final String STANDALONE_FC_CONFIG_PROPERTIES_FILE_PATH = "federated-catalog/fc-02-standalone/standalone-fc/config.properties"; + private static final String FC_CONNECTOR_CONFIG_PROPERTIES_FILE_PATH = "federated-catalog/fc-01-embedded/fc-connector/config.properties"; + + private static final String CRAWLER_EXECUTION_DELAY = "edc.catalog.cache.execution.delay.seconds"; + public static final int CRAWLER_EXECUTION_DELAY_VALUE = 5; + private static final String CRAWLER_EXECUTION_PERIOD = "edc.catalog.cache.execution.period.seconds"; + public static final int CRAWLER_EXECUTION_PERIOD_VALUE = 5; + public static final int TIMEOUT = 5 * CRAWLER_EXECUTION_PERIOD_VALUE; + + public static final String EMBEDDED_FC_CATALOG_API_ENDPOINT = "http://localhost:29195/api/catalog/v1alpha/catalog/query"; + public static final String STANDALONE_FC_CATALOG_API_ENDPOINT = "http://localhost:39195/api/catalog/v1alpha/catalog/query"; + public static final String EMPTY_QUERY_FILE_PATH = "federated-catalog/fc-01-embedded/resources/empty-query.json"; + public static final String TYPE = "[0].@type"; + public static final String CATALOG = "dcat:Catalog"; + public static final String DATASET_ASSET_ID = "[0].'dcat:dataset'.@id"; + + public static RuntimeExtension getFcEmbeddedConnector() { + return getRuntime(FC_CONNECTOR_MODULE_PATH, CONSUMER,FC_CONNECTOR_CONFIG_PROPERTIES_FILE_PATH); + } + + public static RuntimeExtension getStandaloneFc() { + return getRuntime(STANDALONE_FC_MODULE_PATH, STANDALONE_FC,STANDALONE_FC_CONFIG_PROPERTIES_FILE_PATH); + } + + private static RuntimeExtension getRuntime( + String modulePath, + String moduleName, + String configPropertiesFilePath + ) { + return new RuntimePerClassExtension(new EmbeddedRuntime( + moduleName, + Map.of( + EDC_KEYSTORE, getFileFromRelativePath(CERT_PFX_FILE_PATH).getAbsolutePath(), + EDC_KEYSTORE_PASSWORD, KEYSTORE_PASSWORD, + EDC_FS_CONFIG, getFileFromRelativePath(configPropertiesFilePath).getAbsolutePath(), + CRAWLER_EXECUTION_DELAY, Integer.toString(CRAWLER_EXECUTION_DELAY_VALUE), + CRAWLER_EXECUTION_PERIOD, Integer.toString(CRAWLER_EXECUTION_PERIOD_VALUE) + ), + modulePath + )); + } + + public static String createAsset() { + return post(PrerequisitesCommon.PROVIDER_MANAGEMENT_URL + V3_ASSETS_PATH, + getFileContentFromRelativePath(CREATE_ASSET_FILE_PATH), + ASSET_ID); + } + + public static String postAndAssertType(String url, String requestBody, String jsonPath) { + return given() + .headers(API_KEY_HEADER_KEY, API_KEY_HEADER_VALUE) + .contentType(ContentType.JSON) + .body(requestBody) + .when() + .post(url) + .then() + .log().ifError() + .statusCode(HttpStatus.SC_OK) + .body(TYPE, not(emptyString())) + .body(TYPE, is(CATALOG)) + .extract() + .jsonPath() + .get(jsonPath); + } + +} diff --git a/system-tests/src/test/java/org/eclipse/edc/samples/federatedCatalog/FederatedCatalog01embeddedTest.java b/system-tests/src/test/java/org/eclipse/edc/samples/federatedCatalog/FederatedCatalog01embeddedTest.java new file mode 100644 index 00000000..db78a728 --- /dev/null +++ b/system-tests/src/test/java/org/eclipse/edc/samples/federatedCatalog/FederatedCatalog01embeddedTest.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2024 Fraunhofer-Gesellschaft + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Fraunhofer-Gesellschaft - initial API and implementation + * + */ + +package org.eclipse.edc.samples.federatedCatalog; + +import org.assertj.core.api.Assertions; +import org.eclipse.edc.junit.annotations.EndToEndTest; +import org.eclipse.edc.junit.extensions.RuntimeExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.time.Clock; +import java.time.Duration; + +import static org.awaitility.Awaitility.await; +import static org.eclipse.edc.samples.common.FederatedCatalogCommon.*; +import static org.eclipse.edc.samples.common.FileTransferCommon.getFileContentFromRelativePath; +import static org.eclipse.edc.samples.common.NegotiationCommon.createContractDefinition; +import static org.eclipse.edc.samples.common.NegotiationCommon.createPolicy; +import static org.eclipse.edc.samples.common.PrerequisitesCommon.*; + +//@EndToEndTest +public class FederatedCatalog01embeddedTest { + + @RegisterExtension + static RuntimeExtension participantConnector = getProvider(); + + @RegisterExtension + static RuntimeExtension fcConnector = getFcEmbeddedConnector(); + + @Test + void shouldStartConnector() { + Assertions.assertThat(participantConnector.getService(Clock.class)).isNotNull(); + Assertions.assertThat(fcConnector.getService(Clock.class)).isNotNull(); + } + + @Test + void runSampleSteps() { + String assetId = createAsset(); + createPolicy(); + createContractDefinition(); + + await() + .atMost(Duration.ofSeconds(TIMEOUT)) + .pollDelay(Duration.ofSeconds(CRAWLER_EXECUTION_DELAY_VALUE)) + .pollInterval(Duration.ofSeconds(CRAWLER_EXECUTION_PERIOD_VALUE)) + .ignoreExceptions() + .until(() -> postAndAssertType(EMBEDDED_FC_CATALOG_API_ENDPOINT, getFileContentFromRelativePath(EMPTY_QUERY_FILE_PATH), DATASET_ASSET_ID), + id -> id.equals(assetId)); + } + +} diff --git a/system-tests/src/test/java/org/eclipse/edc/samples/federatedCatalog/FederatedCatalog02standaloneTest.java b/system-tests/src/test/java/org/eclipse/edc/samples/federatedCatalog/FederatedCatalog02standaloneTest.java new file mode 100644 index 00000000..0b768f90 --- /dev/null +++ b/system-tests/src/test/java/org/eclipse/edc/samples/federatedCatalog/FederatedCatalog02standaloneTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2024 Fraunhofer-Gesellschaft + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Fraunhofer-Gesellschaft - initial API and implementation + * + */ + +package org.eclipse.edc.samples.federatedCatalog; + +import org.assertj.core.api.Assertions; +import org.eclipse.edc.junit.annotations.EndToEndTest; +import org.eclipse.edc.junit.extensions.RuntimeExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.time.Clock; +import java.time.Duration; + +import static org.awaitility.Awaitility.await; +import static org.eclipse.edc.samples.common.FederatedCatalogCommon.*; +import static org.eclipse.edc.samples.common.FileTransferCommon.getFileContentFromRelativePath; +import static org.eclipse.edc.samples.common.NegotiationCommon.createContractDefinition; +import static org.eclipse.edc.samples.common.NegotiationCommon.createPolicy; +import static org.eclipse.edc.samples.common.PrerequisitesCommon.*; + +@EndToEndTest +public class FederatedCatalog02standaloneTest { + @RegisterExtension + static RuntimeExtension participantConnector = getProvider(); + + @RegisterExtension + static RuntimeExtension standaloneFcRuntime = getStandaloneFc(); + + @Test + void shouldStartRuntimes() { + Assertions.assertThat(participantConnector.getService(Clock.class)).isNotNull(); + Assertions.assertThat(standaloneFcRuntime.getService(Clock.class)).isNotNull(); + } + + @Test + void runSampleSteps() { + String assetId = createAsset(); + createPolicy(); + createContractDefinition(); + + await() + .atMost(Duration.ofSeconds(TIMEOUT)) + .pollDelay(Duration.ofSeconds(CRAWLER_EXECUTION_DELAY_VALUE)) + .pollInterval(Duration.ofSeconds(CRAWLER_EXECUTION_PERIOD_VALUE)) + .ignoreExceptions() + .until(() -> postAndAssertType(STANDALONE_FC_CATALOG_API_ENDPOINT, getFileContentFromRelativePath(EMPTY_QUERY_FILE_PATH), DATASET_ASSET_ID), + id -> id.equals(assetId)); + } + +}