Skip to content

Commit

Permalink
fix: make /catalog/query handle null input (#234)
Browse files Browse the repository at this point in the history
* fix: make /catalog/query handle null input

* dependencies
  • Loading branch information
ndr-brt authored Aug 1, 2024
1 parent ba6f3fd commit 79dbdd4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
32 changes: 16 additions & 16 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ maven/mavencentral/com.fasterxml.jackson.module/jackson-module-jakarta-xmlbind-a
maven/mavencentral/com.fasterxml.jackson.module/jackson-module-jakarta-xmlbind-annotations/2.17.2, Apache-2.0, approved, #13668
maven/mavencentral/com.fasterxml.jackson/jackson-bom/2.16.2, Apache-2.0, approved, #11852
maven/mavencentral/com.fasterxml.jackson/jackson-bom/2.17.2, Apache-2.0, approved, #14162
maven/mavencentral/com.github.docker-java/docker-java-api/3.3.6, Apache-2.0, approved, #10346
maven/mavencentral/com.github.docker-java/docker-java-transport-zerodep/3.3.6, Apache-2.0 AND (Apache-2.0 AND BSD-3-Clause), approved, #15251
maven/mavencentral/com.github.docker-java/docker-java-transport/3.3.6, Apache-2.0, approved, #7942
maven/mavencentral/com.github.docker-java/docker-java-api/3.4.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.github.docker-java/docker-java-transport-zerodep/3.4.0, Apache-2.0 AND (Apache-2.0 AND BSD-3-Clause), approved, #15745
maven/mavencentral/com.github.docker-java/docker-java-transport/3.4.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.google.code.findbugs/jsr305/3.0.2, CC-BY-2.5, approved, #15220
maven/mavencentral/com.google.code.gson/gson/2.10.1, Apache-2.0, approved, #6159
maven/mavencentral/com.google.crypto.tink/tink/1.13.0, Apache-2.0, approved, #14502
Expand Down Expand Up @@ -278,7 +278,7 @@ maven/mavencentral/org.eclipse.jetty/jetty-servlet/11.0.22, EPL-2.0 OR Apache-2.
maven/mavencentral/org.eclipse.jetty/jetty-util/11.0.22, EPL-2.0 OR Apache-2.0, approved, rt.jetty
maven/mavencentral/org.eclipse.jetty/jetty-webapp/11.0.22, EPL-2.0 OR Apache-2.0, approved, rt.jetty
maven/mavencentral/org.eclipse.jetty/jetty-xml/11.0.22, EPL-2.0 OR Apache-2.0, approved, rt.jetty
maven/mavencentral/org.eclipse.parsson/parsson/1.1.6, EPL-2.0, approved, ee4j.parsson
maven/mavencentral/org.eclipse.parsson/parsson/1.1.7, EPL-2.0, approved, ee4j.parsson
maven/mavencentral/org.glassfish.hk2.external/aopalliance-repackaged/3.0.6, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.glassfish
maven/mavencentral/org.glassfish.hk2/hk2-api/3.0.6, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.glassfish
maven/mavencentral/org.glassfish.hk2/hk2-locator/3.0.6, EPL-2.0 OR GPL-2.0-only with Classpath-exception-2.0, approved, ee4j.glassfish
Expand All @@ -297,10 +297,10 @@ maven/mavencentral/org.glassfish/jakarta.json/2.0.1, EPL-2.0 OR GPL-2.0-only wit
maven/mavencentral/org.hamcrest/hamcrest-core/1.3, BSD-2-Clause, approved, CQ11429
maven/mavencentral/org.hamcrest/hamcrest/2.1, BSD-3-Clause, approved, clearlydefined
maven/mavencentral/org.hamcrest/hamcrest/2.2, BSD-3-Clause, approved, clearlydefined
maven/mavencentral/org.jacoco/org.jacoco.agent/0.8.9, EPL-2.0, approved, CQ23285
maven/mavencentral/org.jacoco/org.jacoco.ant/0.8.9, EPL-2.0, approved, #1068
maven/mavencentral/org.jacoco/org.jacoco.core/0.8.9, EPL-2.0, approved, CQ23283
maven/mavencentral/org.jacoco/org.jacoco.report/0.8.9, EPL-2.0 AND Apache-2.0, approved, CQ23284
maven/mavencentral/org.jacoco/org.jacoco.agent/0.8.11, EPL-2.0, approved, CQ23285
maven/mavencentral/org.jacoco/org.jacoco.ant/0.8.11, EPL-2.0, approved, #1068
maven/mavencentral/org.jacoco/org.jacoco.core/0.8.11, EPL-2.0, approved, CQ23283
maven/mavencentral/org.jacoco/org.jacoco.report/0.8.11, EPL-2.0 AND Apache-2.0, approved, CQ23284
maven/mavencentral/org.javassist/javassist/3.28.0-GA, Apache-2.0 OR LGPL-2.1-or-later OR MPL-1.1, approved, #327
maven/mavencentral/org.javassist/javassist/3.30.2-GA, Apache-2.0 AND LGPL-2.1-or-later AND MPL-1.1, approved, #12108
maven/mavencentral/org.jetbrains.kotlin/kotlin-stdlib-common/1.9.10, Apache-2.0, approved, #14186
Expand All @@ -325,11 +325,11 @@ maven/mavencentral/org.mockito/mockito-core/5.12.0, MIT AND (Apache-2.0 AND MIT)
maven/mavencentral/org.mockito/mockito-core/5.2.0, MIT AND (Apache-2.0 AND MIT) AND Apache-2.0, approved, #7401
maven/mavencentral/org.objenesis/objenesis/3.3, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.opentest4j/opentest4j/1.3.0, Apache-2.0, approved, #9713
maven/mavencentral/org.ow2.asm/asm-commons/9.5, BSD-3-Clause, approved, #7553
maven/mavencentral/org.ow2.asm/asm-commons/9.6, BSD-3-Clause, approved, #10775
maven/mavencentral/org.ow2.asm/asm-commons/9.7, BSD-3-Clause, approved, #14075
maven/mavencentral/org.ow2.asm/asm-tree/9.5, BSD-3-Clause, approved, #7555
maven/mavencentral/org.ow2.asm/asm-tree/9.6, BSD-3-Clause, approved, #10773
maven/mavencentral/org.ow2.asm/asm-tree/9.7, BSD-3-Clause, approved, #14073
maven/mavencentral/org.ow2.asm/asm/9.5, BSD-3-Clause, approved, #7554
maven/mavencentral/org.ow2.asm/asm/9.6, BSD-3-Clause, approved, #10776
maven/mavencentral/org.ow2.asm/asm/9.7, BSD-3-Clause, approved, #14076
maven/mavencentral/org.postgresql/postgresql/42.7.3, BSD-2-Clause AND Apache-2.0, approved, #11681
maven/mavencentral/org.reflections/reflections/0.10.2, Apache-2.0 AND WTFPL, approved, clearlydefined
Expand All @@ -338,10 +338,10 @@ maven/mavencentral/org.slf4j/slf4j-api/1.7.25, MIT, approved, CQ13368
maven/mavencentral/org.slf4j/slf4j-api/1.7.30, MIT, approved, CQ13368
maven/mavencentral/org.slf4j/slf4j-api/1.7.36, MIT, approved, CQ13368
maven/mavencentral/org.slf4j/slf4j-api/2.0.9, MIT, approved, #5915
maven/mavencentral/org.testcontainers/database-commons/1.19.8, Apache-2.0, approved, #10345
maven/mavencentral/org.testcontainers/jdbc/1.19.8, Apache-2.0, approved, #10348
maven/mavencentral/org.testcontainers/junit-jupiter/1.19.8, MIT, approved, #10344
maven/mavencentral/org.testcontainers/postgresql/1.19.8, MIT, approved, #10350
maven/mavencentral/org.testcontainers/testcontainers/1.19.8, MIT, approved, #15203
maven/mavencentral/org.testcontainers/database-commons/1.20.0, MIT, approved, clearlydefined
maven/mavencentral/org.testcontainers/jdbc/1.20.0, MIT, approved, clearlydefined
maven/mavencentral/org.testcontainers/junit-jupiter/1.20.0, MIT, approved, clearlydefined
maven/mavencentral/org.testcontainers/postgresql/1.20.0, MIT, approved, clearlydefined
maven/mavencentral/org.testcontainers/testcontainers/1.20.0, MIT, approved, #15747
maven/mavencentral/org.xmlresolver/xmlresolver/5.2.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.yaml/snakeyaml/2.2, Apache-2.0 AND (Apache-2.0 OR BSD-3-Clause OR EPL-1.0 OR GPL-2.0-or-later OR LGPL-2.1-or-later), approved, #10232
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ public FederatedCatalogApiController(QueryService queryService, TypeTransformerR
@Override
@POST
public JsonArray getCachedCatalog(JsonObject catalogQuery, @DefaultValue("false") @QueryParam("flatten") boolean flatten) {
var querySpec = transformerRegistry.transform(catalogQuery, QuerySpec.class)
.orElseThrow(InvalidRequestException::new);
var querySpec = catalogQuery == null
? QuerySpec.Builder.newInstance().build()
: transformerRegistry.transform(catalogQuery, QuerySpec.class)
.orElseThrow(InvalidRequestException::new);

var catalogs = queryService.getCatalog(querySpec)
.orElseThrow(ServiceResultHandler.exceptionMapper(Catalog.class));


return catalogs.stream()
.map(c -> flatten ? FederatedCatalogUtil.flatten(c) : c)
.map(c -> transformerRegistry.transform(c, JsonObject.class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import io.restassured.specification.RequestSpecification;
import jakarta.json.Json;
import org.eclipse.edc.catalog.cache.query.QueryServiceImpl;
import org.eclipse.edc.catalog.store.InMemoryFederatedCatalogCache;
import org.eclipse.edc.catalog.spi.QueryService;
import org.eclipse.edc.connector.controlplane.catalog.spi.Dataset;
import org.eclipse.edc.connector.core.agent.NoOpParticipantIdMapper;
import org.eclipse.edc.jsonld.util.JacksonJsonLd;
Expand All @@ -26,17 +25,19 @@
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.query.QuerySpec;
import org.eclipse.edc.spi.result.ServiceResult;
import org.eclipse.edc.transform.TypeTransformerRegistryImpl;
import org.eclipse.edc.transform.transformer.edc.to.JsonObjectToQuerySpecTransformer;
import org.eclipse.edc.web.jersey.testfixtures.RestControllerTestBase;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;
import static java.util.Collections.emptyList;
import static java.util.stream.IntStream.range;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.catalog.test.TestUtil.buildCatalog;
Expand All @@ -45,22 +46,24 @@
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ApiTest
class FederatedCatalogApiControllerTest extends RestControllerTestBase {
private static final String PATH = "/v1alpha/catalog/query";
private final InMemoryFederatedCatalogCache store = mock();
private final QueryService service = mock();

@Test
void queryApi_whenEmptyResult() {
when(store.query(any())).thenReturn(Collections.emptyList());
when(service.getCatalog(any())).thenReturn(ServiceResult.success(emptyList()));

baseRequest()
.contentType(JSON)
.body("{}")
.post(PATH)
.then()
.log().ifError()
.log().ifValidationFails()
.statusCode(200)
.contentType(JSON)
.body("size()", is(0));
Expand All @@ -69,22 +72,22 @@ void queryApi_whenEmptyResult() {
@Test
void queryApi_whenResultsReturned() {
var catalogs = range(0, 3).mapToObj(i -> createCatalog("catalog-" + i)).toList();
when(store.query(any())).thenReturn(catalogs);
when(service.getCatalog(any())).thenReturn(ServiceResult.success(catalogs));

baseRequest()
.contentType(JSON)
.body("{}")
.post(PATH)
.then()
.log().ifError()
.log().ifValidationFails()
.statusCode(200)
.contentType(JSON)
.body("size()", is(3));
}

@Test
void queryApi_whenQueryUnsuccessful() {
when(store.query(any())).thenThrow(new RuntimeException("test exception"));
when(service.getCatalog(any())).thenThrow(new RuntimeException("test exception"));

baseRequest()
.contentType(JSON)
Expand All @@ -104,14 +107,14 @@ void queryApi_whenFlattened() {
.dataset(Dataset.Builder.newInstance().id("sub2-normal-asset-" + i).build())
.build())
.build()).toList();
when(store.query(any())).thenReturn(catalogs);
when(service.getCatalog(any())).thenReturn(ServiceResult.success(catalogs));

var response = baseRequest()
.contentType(JSON)
.body("{}")
.post(PATH + "?flatten=true")
.then()
.log().ifError()
.log().ifValidationFails()
.statusCode(200)
.contentType(JSON)
.body("size()", is(2))
Expand All @@ -125,7 +128,22 @@ void queryApi_whenFlattened() {

List<String> types2 = jp.get("[1].'http://www.w3.org/ns/dcat#dataset'.'@type'");
assertThat(types2).containsOnly("http://www.w3.org/ns/dcat#Dataset");
}

@Test
void queryApi_shouldUseEmptyQuerySpec_whenRequestBodyIsNull() {
when(service.getCatalog(any())).thenReturn(ServiceResult.success(emptyList()));

baseRequest()
.contentType(JSON)
.post(PATH)
.then()
.log().ifValidationFails()
.statusCode(200)
.contentType(JSON)
.body("size()", is(0));

verify(service).getCatalog(QuerySpec.none());
}

@Override
Expand All @@ -138,7 +156,7 @@ protected Object controller() {
typeTransformerRegistry.register(new JsonObjectFromDistributionTransformer(factory));
typeTransformerRegistry.register(new JsonObjectFromDataServiceTransformer(factory));
typeTransformerRegistry.register(new JsonObjectToQuerySpecTransformer());
return new FederatedCatalogApiController(new QueryServiceImpl(store), typeTransformerRegistry);
return new FederatedCatalogApiController(service, typeTransformerRegistry);
}

private RequestSpecification baseRequest() {
Expand Down

0 comments on commit 79dbdd4

Please sign in to comment.