Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add private properties to contract definition entity #3534

Merged
merged 26 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
182aabb
feat: add private properties to contract definition
mohannad-ezzo Sep 22, 2023
4ebadf1
feat: adjust JsonObjectFromContractDefinition transformer
mohannad-ezzo Sep 25, 2023
f261809
feat: adjust JsonObjectToContractDefinition transformer
mohannad-ezzo Sep 25, 2023
b1edd63
adjust sql schemam and ER diagram
mohannad-ezzo Sep 28, 2023
9e430ff
feat create contract definition with private properties
mohannad-ezzo Sep 28, 2023
96ed8b6
Merge branch 'eclipse-edc:main' into feat-contract-def-private-prop
mohannad-ezzo Sep 28, 2023
951cbb7
feat: add possibility to update contract definition with private prop…
mohannad-ezzo Sep 28, 2023
f02e148
Merge branch 'feat-contract-def-private-prop' of https://github.com/m…
mohannad-ezzo Sep 28, 2023
bb47b3d
feat: add possibility to find contract by id with private properties
mohannad-ezzo Sep 28, 2023
e4d604b
feat: retrieve properties when triggering findAll
mohannad-ezzo Sep 29, 2023
9fb44d0
feat: remove not required code
mohannad-ezzo Sep 29, 2023
e66c720
Merge branch 'eclipse-edc:main' into feat-contract-def-private-prop
mohannad-ezzo Oct 9, 2023
c486717
Merge branch 'eclipse-edc:main' into feat-contract-def-private-prop
mohannad-ezzo Oct 13, 2023
3dcaec7
fix: address reviewers comments
mohannad-ezzo Oct 13, 2023
a260e1a
Merge branch 'eclipse-edc:main' into feat-contract-def-private-prop
mohannad-ezzo Oct 13, 2023
09a9e83
fix: add more test cases
mohannad-ezzo Oct 15, 2023
4682385
fix: remove isPrivate from contract definition property table
mohannad-ezzo Oct 15, 2023
d30401f
fix: remove not required import statements
mohannad-ezzo Oct 15, 2023
a997c28
fix: handle checkstyle issues
mohannad-ezzo Oct 16, 2023
b253e75
fix: store private properties as JSON instead of having a separate table
mohannad-ezzo Oct 23, 2023
db7fc6d
Merge branch 'eclipse-edc:main' into feat-contract-def-private-prop
mohannad-ezzo Oct 23, 2023
6629cc5
fix: checkstyle and test cases
mohannad-ezzo Oct 23, 2023
be2076d
fix: one more checktyle in import statements
mohannad-ezzo Oct 24, 2023
ba282ff
fix: additional checkstyle issue - avoid import star
mohannad-ezzo Oct 24, 2023
1ee6dcd
fix: return the old behavior for for findById and FindAll
mohannad-ezzo Oct 24, 2023
c696c85
fix: checkstyle - indentation issue
mohannad-ezzo Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Contributors:
* Microsoft Corporation - initial API and implementation
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - improvements
* SAP SE - add private properties to contract definition
*
*/

Expand All @@ -26,12 +27,14 @@
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.validator.spi.JsonObjectValidatorRegistry;
import org.eclipse.edc.web.spi.WebService;

import java.util.Map;

import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_TYPE;
import static org.eclipse.edc.spi.CoreConstants.JSON_LD;

@Extension(value = ContractDefinitionApiExtension.NAME)
public class ContractDefinitionApiExtension implements ServiceExtension {
Expand All @@ -53,6 +56,9 @@ public class ContractDefinitionApiExtension implements ServiceExtension {
@Inject
JsonObjectValidatorRegistry validatorRegistry;

@Inject
private TypeManager typeManager;

@Override
public String name() {
return NAME;
Expand All @@ -61,7 +67,8 @@ public String name() {
@Override
public void initialize(ServiceExtensionContext context) {
var jsonFactory = Json.createBuilderFactory(Map.of());
transformerRegistry.register(new JsonObjectFromContractDefinitionTransformer(jsonFactory));
var mapper = typeManager.getMapper(JSON_LD);
transformerRegistry.register(new JsonObjectFromContractDefinitionTransformer(jsonFactory, mapper));
transformerRegistry.register(new JsonObjectToContractDefinitionTransformer());

validatorRegistry.register(CONTRACT_DEFINITION_TYPE, ContractDefinitionValidator.instance());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
* SAP SE - add private properties to contract definition
*
*/

package org.eclipse.edc.connector.api.management.contractdefinition.transform;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition;
Expand All @@ -31,11 +33,13 @@
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;

public class JsonObjectFromContractDefinitionTransformer extends AbstractJsonLdTransformer<ContractDefinition, JsonObject> {
private final ObjectMapper mapper;
private final JsonBuilderFactory jsonFactory;

public JsonObjectFromContractDefinitionTransformer(JsonBuilderFactory jsonFactory) {
public JsonObjectFromContractDefinitionTransformer(JsonBuilderFactory jsonFactory, ObjectMapper jsonLdMapper) {
super(ContractDefinition.class, JsonObject.class);
this.jsonFactory = jsonFactory;
this.mapper = jsonLdMapper;
}

@Override
Expand All @@ -44,12 +48,19 @@ public JsonObjectFromContractDefinitionTransformer(JsonBuilderFactory jsonFactor
.map(criterion -> context.transform(criterion, JsonObject.class))
.collect(toJsonArray());

return jsonFactory.createObjectBuilder()
var builder = jsonFactory.createObjectBuilder()
.add(ID, contractDefinition.getId())
.add(TYPE, CONTRACT_DEFINITION_TYPE)
.add(CONTRACT_DEFINITION_ACCESSPOLICY_ID, contractDefinition.getAccessPolicyId())
.add(CONTRACT_DEFINITION_CONTRACTPOLICY_ID, contractDefinition.getContractPolicyId())
.add(CONTRACT_DEFINITION_ASSETS_SELECTOR, criteria)
.build();
.add(CONTRACT_DEFINITION_ASSETS_SELECTOR, criteria);

if (!contractDefinition.getPrivateProperties().isEmpty()) {
var privatePropBuilder = jsonFactory.createObjectBuilder();
transformProperties(contractDefinition.getPrivateProperties(), privatePropBuilder, mapper, context);
builder.add(ContractDefinition.CONTRACT_DEFINITION_PRIVATE_PROPERTIES, privatePropBuilder);
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
* SAP SE - add private properties to contract definition
*
*/

package org.eclipse.edc.connector.api.management.contractdefinition.transform;

import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.spi.query.Criterion;
Expand All @@ -25,7 +27,7 @@
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_ACCESSPOLICY_ID;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_ASSETS_SELECTOR;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_CONTRACTPOLICY_ID;

import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_PRIVATE_PROPERTIES;

public class JsonObjectToContractDefinitionTransformer extends AbstractJsonLdTransformer<JsonObject, ContractDefinition> {

Expand All @@ -36,19 +38,23 @@ public JsonObjectToContractDefinitionTransformer() {
@Override
public @Nullable ContractDefinition transform(@NotNull JsonObject object, @NotNull TransformerContext context) {
var builder = ContractDefinition.Builder.newInstance();

builder.id(nodeId(object));

visitProperties(object, (key, value) -> {
switch (key) {
case CONTRACT_DEFINITION_ACCESSPOLICY_ID -> builder.accessPolicyId(transformString(value, context));
case CONTRACT_DEFINITION_CONTRACTPOLICY_ID -> builder.contractPolicyId(transformString(value, context));
case CONTRACT_DEFINITION_ASSETS_SELECTOR -> builder.assetsSelector(transformArray(value, Criterion.class, context));
default -> { }
}
});

visitProperties(object, (s, jsonValue) -> transformProperties(s, jsonValue, builder, context));
return builderResult(builder::build, context);
}

private void transformProperties(String key, JsonValue jsonValue, ContractDefinition.Builder builder, TransformerContext context) {
switch (key) {
case CONTRACT_DEFINITION_ACCESSPOLICY_ID -> builder.accessPolicyId(transformString(jsonValue, context));
case CONTRACT_DEFINITION_CONTRACTPOLICY_ID -> builder.contractPolicyId(transformString(jsonValue, context));
case CONTRACT_DEFINITION_ASSETS_SELECTOR -> builder.assetsSelector(transformArray(jsonValue, Criterion.class, context));
case CONTRACT_DEFINITION_PRIVATE_PROPERTIES -> {
var props = jsonValue.asJsonArray().getJsonObject(0);
visitProperties(props, (k, val) -> transformProperties(k, val, builder, context));
}
default -> {
builder.privateProperty(key, transformGenericProperty(jsonValue, context));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
* SAP SE - add private properties to contract definition
*
*/

Expand All @@ -29,9 +30,11 @@
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_ACCESSPOLICY_ID;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_ASSETS_SELECTOR;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_CONTRACTPOLICY_ID;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_PRIVATE_PROPERTIES;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.util.JacksonJsonLd.createObjectMapper;
import static org.eclipse.edc.spi.query.Criterion.criterion;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -46,7 +49,7 @@ class JsonObjectFromContractDefinitionTransformerTest {
private final JsonBuilderFactory jsonFactory = Json.createBuilderFactory(Map.of());
private final TransformerContext context = mock(TransformerContext.class);

private final JsonObjectFromContractDefinitionTransformer transformer = new JsonObjectFromContractDefinitionTransformer(jsonFactory);
private final JsonObjectFromContractDefinitionTransformer transformer = new JsonObjectFromContractDefinitionTransformer(jsonFactory, createObjectMapper());

@Test
void transform() {
Expand All @@ -72,4 +75,24 @@ void transform() {
verify(context, never()).reportProblem(anyString());
}


@Test
void transform_withPrivateProperties_simpleTypes() {
var criterionJson = jsonFactory.createObjectBuilder().build();
when(context.transform(isA(Criterion.class), eq(JsonObject.class))).thenReturn(criterionJson);
var criterion = criterion("left", "=", "right");
var contractDefinition = ContractDefinition.Builder.newInstance()
.id("id")
.accessPolicyId("accessPolicyId")
.contractPolicyId("contractPolicyId")
.assetsSelector(List.of(criterion))
.privateProperty("some-key", "some-value")
.build();

var jsonObject = transformer.transform(contractDefinition, context);

assertThat(jsonObject).isNotNull();
assertThat(jsonObject.getJsonObject(CONTRACT_DEFINITION_PRIVATE_PROPERTIES).getJsonString("some-key").getString()).isEqualTo("some-value");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
* SAP SE - add private properties to contract definition
*
*/

package org.eclipse.edc.connector.api.management.contractdefinition.transform;

import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition;
import org.eclipse.edc.jsonld.TitaniumJsonLd;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.query.Criterion;
import org.eclipse.edc.transform.spi.TransformerContext;
import org.junit.jupiter.api.Test;
Expand All @@ -26,8 +30,14 @@
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_ACCESSPOLICY_ID;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_ASSETS_SELECTOR;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_CONTRACTPOLICY_ID;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_PRIVATE_PROPERTIES;
import static org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition.CONTRACT_DEFINITION_TYPE;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.CONTEXT;
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.JsonLdKeywords.VOCAB;
import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;
import static org.eclipse.edc.spi.CoreConstants.EDC_PREFIX;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
Expand All @@ -40,6 +50,8 @@ class JsonObjectToContractDefinitionTransformerTest {
private final JsonObjectToContractDefinitionTransformer transformer = new JsonObjectToContractDefinitionTransformer();
private final TransformerContext context = mock(TransformerContext.class);

private final TitaniumJsonLd jsonLd = new TitaniumJsonLd(mock(Monitor.class));

@Test
void types() {
assertThat(transformer.getInputType()).isEqualTo(JsonObject.class);
Expand Down Expand Up @@ -84,4 +96,35 @@ void transform_whenNoAssetSelectorItShouldBeAnEmptyList() {
verify(context, never()).transform(any(), any());
}


@Test
void transform_withPrivateProperties() {
when(context.transform(any(), eq(Object.class))).thenReturn("test-val");
var jsonObj = createObjectBuilder()
.add(CONTEXT, createContextBuilder().addNull(EDC_PREFIX).build())
.add(ID, "some-contract-definition-id")
.add(TYPE, CONTRACT_DEFINITION_TYPE)
.add(CONTRACT_DEFINITION_ACCESSPOLICY_ID, "accessPolicyId")
.add(CONTRACT_DEFINITION_CONTRACTPOLICY_ID, "contractPolicyId")
.add(CONTRACT_DEFINITION_PRIVATE_PROPERTIES, createArrayBuilder().add(createObjectBuilder().add("test-prop", "test-val").build()).build())
.build();

jsonObj = expand(jsonObj);
var contractDefinition = transformer.transform(jsonObj, context);

assertThat(contractDefinition.getPrivateProperties())
.hasSize(1)
.containsEntry(EDC_NAMESPACE + "test-prop", "test-val");
}

private JsonObject expand(JsonObject jsonObject) {
return jsonLd.expand(jsonObject).orElseThrow(f -> new AssertionError(f.getFailureDetail()));
}

private JsonObjectBuilder createContextBuilder() {
return createObjectBuilder()
.add(VOCAB, EDC_NAMESPACE)
.add(EDC_PREFIX, EDC_NAMESPACE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ entity edc_contract_definitions {
* access_policy: string <<json>>
* contract_policy: string <<json>>
* assets_selector: string <<json>>
* private_properties: string <<json>>
}
@enduml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-- Contributors:
-- Daimler TSS GmbH - Initial SQL Query
-- Microsoft Corporation - refactoring
-- SAP SE - add private properties to contract definition
--

-- table: edc_contract_definitions
Expand All @@ -21,5 +22,6 @@ CREATE TABLE IF NOT EXISTS edc_contract_definitions
access_policy_id VARCHAR NOT NULL,
contract_policy_id VARCHAR NOT NULL,
assets_selector JSON NOT NULL,
private_properties JSON,
PRIMARY KEY (contract_definition_id)
);
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Microsoft Corporation - refactoring, bugfixing
* Fraunhofer Institute for Software and Systems Engineering - added method
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - improvements
* SAP SE - add private properties to contract definition
*
*/

Expand All @@ -37,6 +38,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

Expand All @@ -48,6 +50,9 @@ public class SqlContractDefinitionStore extends AbstractSqlStore implements Cont
public static final TypeReference<List<Criterion>> CRITERION_LIST = new TypeReference<>() {
};

private static final TypeReference<Map<String, Object>> PRIVATE_PROPERTIES_TYPE = new TypeReference<>() {
};

public SqlContractDefinitionStore(DataSourceRegistry dataSourceRegistry, String dataSourceName,
TransactionContext transactionContext, ContractDefinitionStatements statements,
ObjectMapper objectMapper, QueryExecutor queryExecutor) {
Expand Down Expand Up @@ -142,6 +147,7 @@ private ContractDefinition mapResultSet(ResultSet resultSet) throws Exception {
.accessPolicyId(resultSet.getString(statements.getAccessPolicyIdColumn()))
.contractPolicyId(resultSet.getString(statements.getContractPolicyIdColumn()))
.assetsSelector(fromJson(resultSet.getString(statements.getAssetsSelectorColumn()), CRITERION_LIST))
.privateProperties(fromJson(resultSet.getString(statements.getPrivatePropertiesColumn()), PRIVATE_PROPERTIES_TYPE))
.build();
}

Expand All @@ -152,7 +158,8 @@ private void insertInternal(Connection connection, ContractDefinition definition
definition.getAccessPolicyId(),
definition.getContractPolicyId(),
toJson(definition.getAssetsSelector()),
definition.getCreatedAt());
definition.getCreatedAt(),
toJson(definition.getPrivateProperties()));
});
}

Expand All @@ -164,6 +171,7 @@ private void updateInternal(Connection connection, ContractDefinition definition
definition.getContractPolicyId(),
toJson(definition.getAssetsSelector()),
definition.getCreatedAt(),
toJson(definition.getPrivateProperties()),
definition.getId());
}

Expand All @@ -182,5 +190,4 @@ private ContractDefinition findById(Connection connection, String id) {
var sql = statements.getFindByTemplate();
return queryExecutor.single(connection, false, this::mapResultSet, sql, id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public String getInsertTemplate() {
.column(getContractPolicyIdColumn())
.jsonColumn(getAssetsSelectorColumn())
.column(getCreatedAtColumn())
.jsonColumn(getPrivatePropertiesColumn())
.insertInto(getContractDefinitionTable());
}

@Override

public String getCountTemplate() {
return format("SELECT COUNT (%s) FROM %s WHERE %s = ?",
getIdColumn(),
Expand All @@ -60,6 +60,7 @@ public String getUpdateTemplate() {
.column(getContractPolicyIdColumn())
.jsonColumn(getAssetsSelectorColumn())
.column(getCreatedAtColumn())
.jsonColumn(getPrivatePropertiesColumn())
.update(getContractDefinitionTable(), getIdColumn());

}
Expand Down