Skip to content

Commit

Permalink
chore: fix after subcatalog serialization changes (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood authored Nov 12, 2024
1 parent 92d0be7 commit ff03664
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_CATALOG_TYPE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_CATALOG_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATASET_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATA_SERVICE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DISTRIBUTION_TYPE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DSPACE_PROPERTY_PARTICIPANT_ID_IRI;
import static org.eclipse.edc.jsonld.spi.TypeUtil.nodeType;

/**
* Converts from a DCAT catalog as a {@link JsonObject} in JSON-LD expanded form to a {@link Catalog}.
Expand All @@ -52,8 +51,7 @@ public JsonObjectToCatalogTransformer() {
}

private @Nullable Dataset transformDataset(JsonValue datasetJsonObj, TransformerContext context) {
var clazz = DCAT_CATALOG_TYPE.equals(nodeType(datasetJsonObj.asJsonObject())) ? Catalog.class : Dataset.class;
return context.transform(datasetJsonObj.asJsonObject(), clazz);
return transformObject(datasetJsonObj, Dataset.class, context);
}

private void transformProperties(String key, JsonValue value, Catalog.Builder builder, TransformerContext context) {
Expand All @@ -65,6 +63,8 @@ private void transformProperties(String key, JsonValue value, Catalog.Builder bu
} else {
builder.dataset(transformDataset(value, context));
}
} else if (DCAT_CATALOG_ATTRIBUTE.equalsIgnoreCase(key)) {
transformArrayOrObject(value, Catalog.class, builder::dataset, context);
} else if (DCAT_DATA_SERVICE_ATTRIBUTE.equalsIgnoreCase(key)) {
transformArrayOrObject(value, DataService.class, builder::dataService, context);
} else if (DSPACE_PROPERTY_PARTICIPANT_ID_IRI.equalsIgnoreCase(key)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.eclipse.edc.connector.controlplane.catalog.spi.Catalog;
import org.eclipse.edc.connector.controlplane.catalog.spi.DataService;
import org.eclipse.edc.connector.controlplane.catalog.spi.Dataset;
import org.eclipse.edc.connector.controlplane.catalog.spi.Distribution;
Expand All @@ -32,6 +33,7 @@
import static org.eclipse.edc.catalog.transform.TestInput.getExpanded;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_CATALOG_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_CATALOG_TYPE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATASET_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DATA_SERVICE_ATTRIBUTE;
Expand Down Expand Up @@ -138,6 +140,65 @@ void transform_filledCatalog_returnCatalog() {
verify(context, times(1)).transform(isA(JsonObject.class), eq(DataService.class));
}

@Test
void transform_subCatalog_returnCatalog() {
var datasetJson = getJsonObject("dataset");
var dataServiceJson = getJsonObject("dataService");

var dataset = Dataset.Builder.newInstance()
.offer("offerId", Policy.Builder.newInstance().build())
.distribution(Distribution.Builder.newInstance()
.format("format")
.dataService(DataService.Builder.newInstance().build())
.build())
.build();
var dataService = DataService.Builder.newInstance().build();


var subCatalog = Catalog.Builder.newInstance()
.id(CATALOG_ID + "-sub")
.dataService(dataService)
.dataset(dataset)
.build();

var subCatalogJson = jsonFactory.createObjectBuilder()
.add(ID, CATALOG_ID + "-sub")
.add(TYPE, DCAT_CATALOG_TYPE)
.add(DCAT_DATASET_ATTRIBUTE, datasetJson)
.add(DCAT_DATA_SERVICE_ATTRIBUTE, dataServiceJson)
.build();

when(context.transform(any(JsonObject.class), eq(Dataset.class)))
.thenReturn(dataset);
when(context.transform(any(JsonObject.class), eq(DataService.class)))
.thenReturn(dataService);

when(context.transform(any(JsonObject.class), eq(Catalog.class)))
.thenAnswer(args -> transformer.transform(args.getArgument(0), context));

var catalog = jsonFactory.createObjectBuilder()
.add(ID, CATALOG_ID)
.add(TYPE, DCAT_CATALOG_TYPE)
.add(DCAT_DATASET_ATTRIBUTE, datasetJson)
.add(DCAT_CATALOG_ATTRIBUTE, subCatalogJson)
.add(DCAT_DATA_SERVICE_ATTRIBUTE, dataServiceJson)
.build();

var result = transformer.transform(getExpanded(catalog), context);

assertThat(result).isNotNull();
assertThat(result.getId()).isEqualTo(CATALOG_ID);
assertThat(result.getDatasets()).hasSize(2);
assertThat(result.getDatasets()).contains(dataset);
assertThat(result.getDatasets()).usingRecursiveFieldByFieldElementComparator().contains(subCatalog);
assertThat(result.getDataServices()).hasSize(1);
assertThat(result.getDataServices().get(0)).isEqualTo(dataService);

verify(context, never()).reportProblem(anyString());
verify(context, times(2)).transform(isA(JsonObject.class), eq(Dataset.class));
verify(context, times(2)).transform(isA(JsonObject.class), eq(DataService.class));
}

@Test
void transform_invalidType_reportProblem() {
var catalog = jsonFactory.createObjectBuilder().add(TYPE, "not-a-catalog").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.eclipse.edc.connector.controlplane.catalog.spi.Catalog;
import org.eclipse.edc.connector.controlplane.catalog.spi.Dataset;
import org.eclipse.edc.json.JacksonTypeManager;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.junit.annotations.PostgresqlIntegrationTest;
import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension;
Expand All @@ -30,7 +30,7 @@

import java.io.IOException;

@ComponentTest
@PostgresqlIntegrationTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
public class SqlFederatedCatalogCacheTest extends FederatedCatalogCacheTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.eclipse.edc.connector.controlplane.catalog.spi.Dataset;
import org.eclipse.edc.crawler.spi.TargetNodeDirectory;
import org.eclipse.edc.json.JacksonTypeManager;
import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.junit.annotations.PostgresqlIntegrationTest;
import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.sql.QueryExecutor;
import org.eclipse.edc.sql.testfixtures.PostgresqlStoreSetupExtension;
Expand All @@ -30,7 +30,7 @@

import java.io.IOException;

@ComponentTest
@PostgresqlIntegrationTest
@ExtendWith(PostgresqlStoreSetupExtension.class)
public class SqlTargetNodeDirectoryTest extends TargetNodeDirectoryTestBase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"dcat:distribution": [
],
"id": "normal-asset-a873f2be-295e-4d03-af1c-0e15c1363627"
},
}
],
"dcat:catalog": [
{
"@id": "catalog-asset-5a7482bd-a2a8-4248-a8fa-353cb1d14df7",
"@type": "dcat:Catalog",
Expand Down

0 comments on commit ff03664

Please sign in to comment.