Skip to content

Commit

Permalink
replace providerUrl with counterPartyAddress in CatalogRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Jul 31, 2023
1 parent d980a44 commit df0f984
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,19 @@ public interface CatalogApi {
@Schema(name = "CatalogRequest", example = CatalogRequestSchema.CATALOG_REQUEST_EXAMPLE)
record CatalogRequestSchema(
@Schema(name = TYPE, example = CATALOG_REQUEST_TYPE)
String type,
@Deprecated(since = "0.2.0")
@Schema(deprecated = true, description = "please use counterPartyAddress instead")
String providerUrl,
String counterPartyAddress,
String protocol,
ApiCoreSchema.QuerySpecSchema querySpec) {

public static final String CATALOG_REQUEST_EXAMPLE = """
{
"@context": { "edc": "https://w3id.org/edc/v0.0.1/ns/" },
"@type": "CatalogRequest",
"providerUrl": "http://provider-address",
"counterPartyAddress": "http://provider-address",
"protocol": "dataspace-protocol-http",
"querySpec": {
"offset": 0,
Expand All @@ -82,7 +86,8 @@ record CatalogRequestSchema(
@Schema(name = "DatasetRequest", example = DatasetRequestSchema.DATASET_REQUEST_EXAMPLE)
record DatasetRequestSchema(
@Schema(name = TYPE, example = CATALOG_REQUEST_TYPE)
String providerUrl,
String type,
String counterPartyAddress,
String protocol,
ApiCoreSchema.QuerySpecSchema querySpec) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void requestCatalog(JsonObject requestBody, @Suspended AsyncResponse resp
var request = transformerRegistry.transform(requestBody, CatalogRequest.class)
.orElseThrow(InvalidRequestException::new);

service.requestCatalog(request.getProviderUrl(), request.getProtocol(), request.getQuerySpec())
service.requestCatalog(request.getCounterPartyAddress(), request.getProtocol(), request.getQuerySpec())
.whenComplete((result, throwable) -> {
try {
response.resume(toResponse(result, throwable));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
transformerRegistry.register(new JsonObjectToCatalogRequestTransformer());
transformerRegistry.register(new JsonObjectToCatalogRequestTransformer(context.getMonitor()));
transformerRegistry.register(new JsonObjectToDatasetRequestTransformer());

webService.registerResource(config.getContextAlias(), new CatalogApiController(service, transformerRegistry, validatorRegistry));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,45 @@
import jakarta.json.JsonObject;
import org.eclipse.edc.catalog.spi.CatalogRequest;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

import static java.lang.String.format;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROTOCOL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROVIDER_URL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_QUERY_SPEC;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_TYPE;

public class JsonObjectToCatalogRequestTransformer extends AbstractJsonLdTransformer<JsonObject, CatalogRequest> {

public JsonObjectToCatalogRequestTransformer() {
private final Monitor monitor;

public JsonObjectToCatalogRequestTransformer(Monitor monitor) {
super(JsonObject.class, CatalogRequest.class);
this.monitor = monitor;
}

@Override
public @Nullable CatalogRequest transform(@NotNull JsonObject object, @NotNull TransformerContext context) {
var builder = CatalogRequest.Builder.newInstance();

visitProperties(object, key -> switch (key) {
case CATALOG_REQUEST_PROTOCOL -> v -> builder.protocol(transformString(v, context));
case CATALOG_REQUEST_PROVIDER_URL -> v -> builder.providerUrl(transformString(v, context));
case CATALOG_REQUEST_QUERY_SPEC -> v -> builder.querySpec(transformObject(v, QuerySpec.class, context));
default -> doNothing();
});

return builder.build();
var counterPartyAddress = Optional.of(object)
.map(it -> it.get(CATALOG_REQUEST_COUNTER_PARTY_ADDRESS))
.orElseGet(() -> {
monitor.warning(format("The attribute %s has been deprecated in type %s, please use %s",
CATALOG_REQUEST_PROVIDER_URL, CATALOG_REQUEST_TYPE, CATALOG_REQUEST_COUNTER_PARTY_ADDRESS));
return object.get(CATALOG_REQUEST_PROVIDER_URL);
});

return CatalogRequest.Builder.newInstance()
.protocol(transformString(object.get(CATALOG_REQUEST_PROTOCOL), context))
.querySpec(transformObject(object.get(CATALOG_REQUEST_QUERY_SPEC), QuerySpec.class, context))
.counterPartyAddress(transformString(counterPartyAddress, context))
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

import jakarta.json.JsonObject;
import org.eclipse.edc.api.validation.QuerySpecValidator;
import org.eclipse.edc.validator.jsonobject.JsonLdPath;
import org.eclipse.edc.validator.jsonobject.JsonObjectValidator;
import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue;
import org.eclipse.edc.validator.spi.ValidationResult;
import org.eclipse.edc.validator.spi.Validator;

import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROTOCOL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROVIDER_URL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_QUERY_SPEC;
Expand All @@ -28,9 +31,27 @@ public class CatalogRequestValidator {

public static Validator<JsonObject> instance() {
return JsonObjectValidator.newValidator()
.verify(CATALOG_REQUEST_PROVIDER_URL, MandatoryValue::new)
.verify(MandatoryCounterPartyAddressOrProviderUrl::new)
.verify(CATALOG_REQUEST_PROTOCOL, MandatoryValue::new)
.verifyObject(CATALOG_REQUEST_QUERY_SPEC, QuerySpecValidator::instance)
.build();
}

private record MandatoryCounterPartyAddressOrProviderUrl(JsonLdPath path) implements Validator<JsonObject> {

@Override
public ValidationResult validate(JsonObject input) {
var counterPartyAddress = new MandatoryValue(path.append(CATALOG_REQUEST_COUNTER_PARTY_ADDRESS));
var providerUrl = new MandatoryValue(path.append(CATALOG_REQUEST_PROVIDER_URL));

var validateCounterParty = counterPartyAddress.validate(input);
var validateProviderUrl = providerUrl.validate(input);

if (validateCounterParty.succeeded() || validateProviderUrl.succeeded()) {
return ValidationResult.success();
} else {
return validateCounterParty;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected Object controller() {

@Test
void requestCatalog() {
var request = CatalogRequest.Builder.newInstance().providerUrl("http://url").build();
var request = CatalogRequest.Builder.newInstance().counterPartyAddress("http://url").build();
when(validatorRegistry.validate(any(), any())).thenReturn(ValidationResult.success());
when(transformerRegistry.transform(any(), eq(CatalogRequest.class))).thenReturn(Result.success(request));
when(service.requestCatalog(any(), any(), any())).thenReturn(completedFuture(StatusResult.success("{}".getBytes())));
Expand Down Expand Up @@ -110,7 +110,7 @@ void requestCatalog_shouldReturnBadRequest_whenTransformFails() {

@Test
void requestCatalog_shouldReturnBadGateway_whenServiceFails() {
var request = CatalogRequest.Builder.newInstance().providerUrl("http://url").build();
var request = CatalogRequest.Builder.newInstance().counterPartyAddress("http://url").build();
when(validatorRegistry.validate(any(), any())).thenReturn(ValidationResult.success());
when(transformerRegistry.transform(any(), eq(CatalogRequest.class))).thenReturn(Result.success(request));
when(service.requestCatalog(any(), any(), any())).thenReturn(completedFuture(StatusResult.failure(FATAL_ERROR, "error")));
Expand All @@ -128,7 +128,7 @@ void requestCatalog_shouldReturnBadGateway_whenServiceFails() {

@Test
void requestCatalog_shouldReturnBadGateway_whenServiceThrowsException() {
var request = CatalogRequest.Builder.newInstance().providerUrl("http://url").build();
var request = CatalogRequest.Builder.newInstance().counterPartyAddress("http://url").build();
when(validatorRegistry.validate(any(), any())).thenReturn(ValidationResult.success());
when(transformerRegistry.transform(any(), eq(CatalogRequest.class))).thenReturn(Result.success(request));
when(service.requestCatalog(any(), any(), any())).thenReturn(failedFuture(new EdcException("error")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_ATTRIBUTE;
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat;
import static org.eclipse.edc.junit.extensions.TestServiceExtensionContext.testServiceExtensionContext;
import static org.mockito.Mockito.mock;

class CatalogApiTest {

Expand All @@ -54,7 +55,7 @@ class CatalogApiTest {

@BeforeEach
void setUp() {
transformer.register(new JsonObjectToCatalogRequestTransformer());
transformer.register(new JsonObjectToCatalogRequestTransformer(mock()));
transformer.register(new JsonObjectToDatasetRequestTransformer());
transformer.register(new JsonObjectToQuerySpecTransformer());
}
Expand All @@ -73,7 +74,7 @@ void catalogRequestExample() throws JsonProcessingException {
.satisfies(transformResult -> assertThat(transformResult).isSucceeded()
.satisfies(transformed -> {
assertThat(transformed.getProtocol()).isNotBlank();
assertThat(transformed.getProviderUrl()).isNotBlank();
assertThat(transformed.getCounterPartyAddress()).isNotBlank();
assertThat(transformed.getQuerySpec()).isNotNull();
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,30 @@
import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.catalog.spi.CatalogRequest;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROTOCOL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROVIDER_URL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_QUERY_SPEC;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class JsonObjectToCatalogRequestTransformerTest {

private final JsonObjectToCatalogRequestTransformer transformer = new JsonObjectToCatalogRequestTransformer();
private final TransformerContext context = mock(TransformerContext.class);
private final TransformerContext context = mock();
private final Monitor monitor = mock();
private final JsonObjectToCatalogRequestTransformer transformer = new JsonObjectToCatalogRequestTransformer(monitor);

@Test
void types() {
Expand All @@ -46,6 +50,27 @@ void types() {

@Test
void transform() {
var querySpec = QuerySpec.Builder.newInstance().build();
var querySpecJson = Json.createObjectBuilder().build();
when(context.transform(any(), eq(QuerySpec.class))).thenReturn(querySpec);
var json = Json.createObjectBuilder()
.add(TYPE, CATALOG_REQUEST_TYPE)
.add(CATALOG_REQUEST_PROTOCOL, "protocol")
.add(CATALOG_REQUEST_COUNTER_PARTY_ADDRESS, "http://provider/url")
.add(CATALOG_REQUEST_QUERY_SPEC, querySpecJson)
.build();

var result = transformer.transform(json, context);

assertThat(result).isNotNull();
assertThat(result.getProtocol()).isEqualTo("protocol");
assertThat(result.getCounterPartyAddress()).isEqualTo("http://provider/url");
assertThat(result.getQuerySpec()).isEqualTo(querySpec);
verify(context).transform(querySpecJson, QuerySpec.class);
}

@Test
void transform_shouldUseProviderId_whenCounterPartyAddressIsMissing() {
var querySpec = QuerySpec.Builder.newInstance().build();
var querySpecJson = Json.createObjectBuilder().build();
when(context.transform(any(), eq(QuerySpec.class))).thenReturn(querySpec);
Expand All @@ -60,9 +85,10 @@ void transform() {

assertThat(result).isNotNull();
assertThat(result.getProtocol()).isEqualTo("protocol");
assertThat(result.getProviderUrl()).isEqualTo("http://provider/url");
assertThat(result.getCounterPartyAddress()).isEqualTo("http://provider/url");
assertThat(result.getQuerySpec()).isEqualTo(querySpec);
verify(context).transform(querySpecJson, QuerySpec.class);
verify(monitor).warning(anyString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static jakarta.json.Json.createObjectBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.InstanceOfAssertFactories.list;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_COUNTER_PARTY_ADDRESS;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROTOCOL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_PROVIDER_URL;
import static org.eclipse.edc.catalog.spi.CatalogRequest.CATALOG_REQUEST_QUERY_SPEC;
Expand All @@ -39,6 +40,18 @@ class CatalogRequestValidatorTest {

@Test
void shouldSucceed_whenInputIsValid() {
var input = Json.createObjectBuilder()
.add(CATALOG_REQUEST_COUNTER_PARTY_ADDRESS, value("http://any"))
.add(CATALOG_REQUEST_PROTOCOL, value("protocol"))
.build();

var result = validator.validate(input);

assertThat(result).isSucceeded();
}

@Test
void shouldSucceed_whenDeprecatedProviderUrlIsUsed() {
var input = Json.createObjectBuilder()
.add(CATALOG_REQUEST_PROVIDER_URL, value("http://any"))
.add(CATALOG_REQUEST_PROTOCOL, value("protocol"))
Expand All @@ -57,14 +70,14 @@ void shouldFail_whenMandatoryFieldsAreMissing() {

assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class))
.hasSize(2)
.anySatisfy(v -> assertThat(v.path()).isEqualTo(CATALOG_REQUEST_PROVIDER_URL))
.anySatisfy(v -> assertThat(v.path()).isEqualTo(CATALOG_REQUEST_COUNTER_PARTY_ADDRESS))
.anySatisfy(v -> assertThat(v.path()).isEqualTo(CATALOG_REQUEST_PROTOCOL));
}

@Test
void shouldFail_whenOptionalQuerySpecIsInvalid() {
var input = Json.createObjectBuilder()
.add(CATALOG_REQUEST_PROVIDER_URL, value("http://any"))
.add(CATALOG_REQUEST_COUNTER_PARTY_ADDRESS, value("http://any"))
.add(CATALOG_REQUEST_PROTOCOL, value("protocol"))
.add(CATALOG_REQUEST_QUERY_SPEC, createArrayBuilder().add(createObjectBuilder()
.add(EDC_QUERY_SPEC_SORT_FIELD, value(" "))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ public class CatalogRequest {

public static final String CATALOG_REQUEST_TYPE = EDC_NAMESPACE + "CatalogRequest";
public static final String CATALOG_REQUEST_PROTOCOL = EDC_NAMESPACE + "protocol";
public static final String CATALOG_REQUEST_COUNTER_PARTY_ADDRESS = EDC_NAMESPACE + "counterPartyAddress";
@Deprecated(since = "0.2.0")
public static final String CATALOG_REQUEST_PROVIDER_URL = EDC_NAMESPACE + "providerUrl";
public static final String CATALOG_REQUEST_QUERY_SPEC = EDC_NAMESPACE + "querySpec";

private QuerySpec querySpec;
private String providerUrl;
private String counterPartyAddress;
private String protocol;

private CatalogRequest() {
}

public String getProviderUrl() {
return providerUrl;
public String getCounterPartyAddress() {
return counterPartyAddress;
}

public QuerySpec getQuerySpec() {
Expand Down Expand Up @@ -65,8 +67,8 @@ public Builder querySpec(QuerySpec querySpec) {
return this;
}

public Builder providerUrl(String providerUrl) {
instance.providerUrl = providerUrl;
public Builder counterPartyAddress(String counterPartyAddress) {
instance.counterPartyAddress = counterPartyAddress;
return this;
}

Expand All @@ -76,7 +78,6 @@ public Builder protocol(String protocol) {
}

public CatalogRequest build() {
requireNonNull(instance.providerUrl, "providerUrl");
return instance;
}
}
Expand Down
Loading

0 comments on commit df0f984

Please sign in to comment.