Skip to content

Commit

Permalink
refactor: remove DTOs from management-api (#3309)
Browse files Browse the repository at this point in the history
* refactor: get rid of DTOs on management api

* PR remark
  • Loading branch information
ndr-brt authored Jul 18, 2023
1 parent 84c76af commit 25ef946
Show file tree
Hide file tree
Showing 108 changed files with 557 additions and 3,133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static java.util.stream.Collectors.toList;
import static org.eclipse.edc.spi.query.Criterion.CRITERION_OPERAND_LEFT;
import static org.eclipse.edc.spi.query.Criterion.CRITERION_OPERAND_RIGHT;
import static org.eclipse.edc.spi.query.Criterion.CRITERION_OPERATOR;
Expand All @@ -32,21 +33,20 @@ public JsonObjectToCriterionTransformer() {
}

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

visitProperties(input, key -> {
switch (key) {
case CRITERION_OPERAND_LEFT:
return v -> builder.operandLeft(transformGenericProperty(v, context));
case CRITERION_OPERAND_RIGHT:
return v -> builder.operandRight(transformGenericProperty(v, context));
case CRITERION_OPERATOR:
return v -> builder.operator(transformString(v, context));
default:
return doNothing();
}
});
builder.operandLeft(transformString(object.get(CRITERION_OPERAND_LEFT), context));

var operator = transformString(object.get(CRITERION_OPERATOR), context);
builder.operator(operator);

var operandRight = object.get(CRITERION_OPERAND_RIGHT);
if ("in".equals(operator)) {
builder.operandRight(operandRight.asJsonArray().stream().map(this::nodeValue).collect(toList()));
} else {
builder.operandRight(transformString(operandRight, context));
}

return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.edc.core.transform.transformer.TestInput.getExpanded;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
Expand All @@ -45,14 +47,34 @@ void transform() {
var json = Json.createObjectBuilder()
.add(JsonLdKeywords.TYPE, CRITERION_TYPE)
.add(CRITERION_OPERAND_LEFT, "foo")
.add(CRITERION_OPERAND_RIGHT, "bar")
.add(CRITERION_OPERATOR, "=")
.add(CRITERION_OPERAND_RIGHT, "bar")
.build();

var crit = transformer.transform(getExpanded(json), context);
assertThat(crit).isNotNull();
assertThat(crit.getOperator()).isEqualTo("=");
assertThat(crit.getOperandLeft()).isEqualTo("foo");
assertThat(crit.getOperandRight()).isEqualTo("bar");
var result = transformer.transform(getExpanded(json), context);

assertThat(result).isNotNull();
assertThat(result.getOperandLeft()).isEqualTo("foo");
assertThat(result.getOperator()).isEqualTo("=");
assertThat(result.getOperandRight()).isEqualTo("bar");
}

@Test
void transform_shouldConsiderRightAsList_whenOperatorIsIn() {
var context = mock(TransformerContext.class);
when(context.transform(any(JsonValue.class), eq(Object.class))).thenAnswer(a -> genericTypeTransformer.transform(a.getArgument(0), context));
var json = Json.createObjectBuilder()
.add(JsonLdKeywords.TYPE, CRITERION_TYPE)
.add(CRITERION_OPERAND_LEFT, "foo")
.add(CRITERION_OPERATOR, "in")
.add(CRITERION_OPERAND_RIGHT, Json.createArrayBuilder().add("bar").add("baz"))
.build();

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

assertThat(result).isNotNull();
assertThat(result.getOperandLeft()).isEqualTo("foo");
assertThat(result.getOperator()).isEqualTo("in");
assertThat(result.getOperandRight()).isInstanceOf(List.class).asList().containsExactly("bar", "baz");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,20 @@
package org.eclipse.edc.api;

import jakarta.json.Json;
import org.eclipse.edc.api.transformer.CriterionDtoToCriterionTransformer;
import org.eclipse.edc.api.transformer.CriterionToCriterionDtoTransformer;
import org.eclipse.edc.api.transformer.DataAddressDtoToDataAddressTransformer;
import org.eclipse.edc.api.transformer.DataAddressToDataAddressDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromCallbackAddressTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromCriterionDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromDataAddressDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromIdResponseDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectFromIdResponseTransformer;
import org.eclipse.edc.api.transformer.JsonObjectToCallbackAddressTransformer;
import org.eclipse.edc.api.transformer.JsonObjectToCriterionDtoTransformer;
import org.eclipse.edc.api.transformer.JsonObjectToQuerySpecDtoTransformer;
import org.eclipse.edc.api.transformer.QuerySpecDtoToQuerySpecTransformer;
import org.eclipse.edc.api.validation.QuerySpecDtoValidator;
import org.eclipse.edc.api.validation.QuerySpecValidator;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.types.TypeManager;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;
import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry;

import java.util.Map;

import static org.eclipse.edc.api.model.QuerySpecDto.EDC_QUERY_SPEC_TYPE;
import static org.eclipse.edc.spi.CoreConstants.JSON_LD;
import static org.eclipse.edc.spi.query.QuerySpec.EDC_QUERY_SPEC_TYPE;

@Extension(value = ApiCoreExtension.NAME)
public class ApiCoreExtension implements ServiceExtension {
Expand All @@ -49,9 +38,6 @@ public class ApiCoreExtension implements ServiceExtension {
@Inject
private TypeTransformerRegistry transformerRegistry;

@Inject
private TypeManager typeManager;

@Inject
private JsonObjectValidatorRegistry validatorRegistry;

Expand All @@ -62,24 +48,12 @@ public String name() {

@Override
public void initialize(ServiceExtensionContext context) {
transformerRegistry.register(new QuerySpecDtoToQuerySpecTransformer());
transformerRegistry.register(new CriterionToCriterionDtoTransformer());
transformerRegistry.register(new CriterionDtoToCriterionTransformer());
transformerRegistry.register(new DataAddressDtoToDataAddressTransformer());
transformerRegistry.register(new DataAddressToDataAddressDtoTransformer());

var mapper = typeManager.getMapper(JSON_LD);
var jsonFactory = Json.createBuilderFactory(Map.of());

transformerRegistry.register(new JsonObjectFromCallbackAddressTransformer(jsonFactory));
transformerRegistry.register(new JsonObjectFromCriterionDtoTransformer(jsonFactory, mapper));
transformerRegistry.register(new JsonObjectFromDataAddressDtoTransformer(jsonFactory));
transformerRegistry.register(new JsonObjectFromIdResponseDtoTransformer(jsonFactory));

transformerRegistry.register(new JsonObjectFromIdResponseTransformer(jsonFactory));
transformerRegistry.register(new JsonObjectToCallbackAddressTransformer());
transformerRegistry.register(new JsonObjectToCriterionDtoTransformer());
transformerRegistry.register(new JsonObjectToQuerySpecDtoTransformer());

validatorRegistry.register(EDC_QUERY_SPEC_TYPE, QuerySpecDtoValidator.instance());
validatorRegistry.register(EDC_QUERY_SPEC_TYPE, QuerySpecValidator.instance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

import java.util.List;

import static org.eclipse.edc.api.model.CriterionDto.CRITERION_TYPE;
import static org.eclipse.edc.api.model.QuerySpecDto.EDC_QUERY_SPEC_TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.spi.query.Criterion.CRITERION_TYPE;
import static org.eclipse.edc.spi.query.QuerySpec.EDC_QUERY_SPEC_TYPE;

public interface ApiCoreSchema {

@Schema(example = CriterionSchema.CRITERION_EXAMPLE)
@Schema(name = "Criterion", example = CriterionSchema.CRITERION_EXAMPLE)
record CriterionSchema(
@Schema(name = TYPE, example = CRITERION_TYPE)
String type,
Expand All @@ -45,7 +45,7 @@ record CriterionSchema(
""";
}

@Schema(example = QuerySpecSchema.QUERY_SPEC_EXAMPLE)
@Schema(name = "QuerySpec", example = QuerySpecSchema.QUERY_SPEC_EXAMPLE)
record QuerySpecSchema(
@Schema(name = TYPE, example = EDC_QUERY_SPEC_TYPE)
String type,
Expand All @@ -68,7 +68,7 @@ record QuerySpecSchema(
""";
}

@Schema(example = IdResponseSchema.ID_RESPONSE_EXAMPLE)
@Schema(name = "IdResponse", example = IdResponseSchema.ID_RESPONSE_EXAMPLE)
record IdResponseSchema(
@Schema(name = ID)
String id,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 25ef946

Please sign in to comment.