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(api, spi, storage): policy profile implementation #4332

Merged
merged 14 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ maven/mavencentral/io.prometheus/simpleclient_tracer_otel/0.16.0, Apache-2.0, ap
maven/mavencentral/io.prometheus/simpleclient_tracer_otel_agent/0.16.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.rest-assured/json-path/5.5.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.rest-assured/rest-assured-common/5.5.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.rest-assured/rest-assured/5.5.0, Apache-2.0, restricted, clearlydefined
maven/mavencentral/io.rest-assured/rest-assured/5.5.0, Apache-2.0, approved, #15676
maven/mavencentral/io.rest-assured/xml-path/5.5.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.setl/rdf-urdna/1.1, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.swagger.core.v3/swagger-annotations-jakarta/2.2.22, Apache-2.0, approved, #5947
Expand Down Expand Up @@ -188,6 +188,7 @@ maven/mavencentral/net.bytebuddy/byte-buddy-agent/1.14.15, Apache-2.0, approved,
maven/mavencentral/net.bytebuddy/byte-buddy/1.14.1, Apache-2.0 AND BSD-3-Clause, approved, #7163
maven/mavencentral/net.bytebuddy/byte-buddy/1.14.15, Apache-2.0 AND BSD-3-Clause, approved, #7163
maven/mavencentral/net.bytebuddy/byte-buddy/1.14.16, Apache-2.0 AND BSD-3-Clause, approved, #7163
maven/mavencentral/net.bytebuddy/byte-buddy/1.14.18, Apache-2.0 AND BSD-3-Clause, approved, #7163
maven/mavencentral/net.java.dev.jna/jna/5.13.0, Apache-2.0 AND LGPL-2.1-or-later, approved, #15196
maven/mavencentral/net.javacrumbs.json-unit/json-unit-core/2.36.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/net.minidev/accessors-smart/2.4.7, Apache-2.0, approved, #7515
Expand Down Expand Up @@ -228,6 +229,7 @@ maven/mavencentral/org.apache.velocity/velocity-engine-scripting/2.3, Apache-2.0
maven/mavencentral/org.apache.xbean/xbean-reflect/3.7, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.apiguardian/apiguardian-api/1.1.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/org.assertj/assertj-core/3.26.0, Apache-2.0, approved, #14886
maven/mavencentral/org.assertj/assertj-core/3.26.3, Apache-2.0, approved, #14886
maven/mavencentral/org.awaitility/awaitility/4.2.1, Apache-2.0, approved, #14178
maven/mavencentral/org.bouncycastle/bcpkix-jdk18on/1.72, MIT, approved, #3789
maven/mavencentral/org.bouncycastle/bcpkix-jdk18on/1.78.1, MIT, approved, #14434
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2024 ZF Friedrichshafen AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* ZF Friedrichshafen AG - initial implementation
*
*/

package org.eclipse.edc.validator.jsonobject.validators;

import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import org.eclipse.edc.validator.jsonobject.JsonLdPath;
import org.eclipse.edc.validator.spi.ValidationResult;
import org.eclipse.edc.validator.spi.Validator;

import java.util.Optional;
import java.util.function.Function;

import static java.lang.String.format;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID;
import static org.eclipse.edc.validator.spi.Violation.violation;

/**
* Verifies that if an array is present the key:value pairs are of type @id:string with optional constraint on min size.
*/
public class OptionalIdArray implements Validator<JsonObject> {
private final JsonLdPath path;
private final Integer min;

public OptionalIdArray(JsonLdPath path) {
this(path, null);
}

public OptionalIdArray(JsonLdPath path, Integer min) {
this.path = path;
this.min = min;
}

public static Function<JsonLdPath, Validator<JsonObject>> min(Integer min) {
return path -> new OptionalIdArray(path, min);
}

@Override
public ValidationResult validate(JsonObject input) {
ValidationResult sizeResult;

sizeResult = Optional.ofNullable(input.getJsonArray(path.last()))
.map(this::validateMin)
.orElse(ValidationResult.success());

if (sizeResult.failed()) {
return sizeResult;
}

return Optional.ofNullable(input.getJsonArray(path.last()))
.map(this::validateType)
.orElse(ValidationResult.success());
}

private ValidationResult validateMin(JsonArray array) {
if (min == null || (array.size() >= min)) {
return ValidationResult.success();
}
return ValidationResult.failure(violation(format("array '%s' should at least contains '%s' elements", path, min), path.toString()));
}

private ValidationResult validateType(JsonArray array) {
for (var value : array) {
try {
var id = value.asJsonObject().getJsonString(ID);
if (id == null || id.getString().isBlank()) {
return ValidationResult.failure(violation(format("contents of array '%s' should not be blank or empty", path), path.toString()));
}
} catch (ClassCastException e) {
return ValidationResult.failure(violation(format("contents of array '%s' should be of type string", path), path.toString()));
}
}
return ValidationResult.success();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import org.eclipse.edc.validator.jsonobject.validators.MandatoryArray;
import org.eclipse.edc.validator.jsonobject.validators.MandatoryObject;
import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue;
import org.eclipse.edc.validator.jsonobject.validators.OptionalIdArray;
import org.eclipse.edc.validator.jsonobject.validators.OptionalIdNotBlank;
import org.eclipse.edc.validator.spi.ValidationFailure;
import org.eclipse.edc.validator.spi.Violation;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static jakarta.json.Json.createArrayBuilder;
Expand Down Expand Up @@ -217,6 +219,77 @@ void shouldFail_whenInputIsNull() {
});
}

@Nested
class TestOptionalIdArray {
@Test
void shouldFail_ValidateOptionalIdArrayMinSize() {
var input = createObjectBuilder()
.add("arrayProperty", createArrayBuilder()
.add(createObjectBuilder().add(ID, "value1"))
.add(createObjectBuilder().add(ID, "value2"))
);

var result = JsonObjectValidator.newValidator()
.verify("arrayProperty", OptionalIdArray.min(3))
.build()
.validate(input.build());

assertThat(result).isFailed().satisfies(failure -> {
assertThat(failure.getViolations()).anySatisfy(violation -> {
assertThat(violation.path()).contains("arrayProperty");
});
});
}

@Test
void shouldSucceed_ValidateOptionalIdArrayNoValue() {
var input = createObjectBuilder();

var result = JsonObjectValidator.newValidator()
.verify("arrayProperty", OptionalIdArray::new)
.build()
.validate(input.build());

assertThat(result).isSucceeded();
}

@Test
void shouldFail_ValidateOptionalIdArrayWrongType() {
var input = createObjectBuilder()
.add("arrayProperty", createArrayBuilder()
.add(createObjectBuilder().add("subProperty", createObjectBuilder()))
);

var result = JsonObjectValidator.newValidator()
.verify("arrayProperty", OptionalIdArray::new)
.build()
.validate(input.build());

assertThat(result).isFailed().satisfies(failure -> {
assertThat(failure.getViolations()).anySatisfy(violation -> {
assertThat(violation.path()).contains("arrayProperty");
});
});
}

@Test
void shouldSucceed_ValidateOptionalIdArray() {
var input = createObjectBuilder()
.add("arrayProperty", createArrayBuilder()
.add(createObjectBuilder().add(ID, "value1"))
.add(createObjectBuilder().add(ID, "value2"))
);

var result = JsonObjectValidator.newValidator()
.verify("arrayProperty", OptionalIdArray::new)
.build()
.validate(input.build());

assertThat(result).isSucceeded();
}
}


private JsonArrayBuilder value(String value) {
return createArrayBuilder().add(createObjectBuilder().add(VALUE, value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import jakarta.json.JsonObject;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer;
import org.eclipse.edc.policy.model.Duty;
import org.eclipse.edc.policy.model.Permission;
Expand All @@ -27,8 +28,11 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static java.util.stream.Collectors.toList;
import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNEE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNER_ATTRIBUTE;
Expand All @@ -37,6 +41,7 @@
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_AGREEMENT;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_OFFER;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROFILE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROHIBITION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_TARGET_ATTRIBUTE;

Expand Down Expand Up @@ -91,10 +96,17 @@ public JsonObjectToPolicyTransformer(ParticipantIdMapper participantIdMapper) {
case ODRL_TARGET_ATTRIBUTE -> v -> builder.target(transformString(v, context));
case ODRL_ASSIGNER_ATTRIBUTE -> v -> builder.assigner(participantIdMapper.fromIri(transformString(v, context)));
case ODRL_ASSIGNEE_ATTRIBUTE -> v -> builder.assignee(participantIdMapper.fromIri(transformString(v, context)));
case ODRL_PROFILE_ATTRIBUTE -> v -> builder.profiles(transformProfile(v));
default -> v -> builder.extensibleProperty(key, transformGenericProperty(v, context));
});

return builderResult(builder::build, context);
}

private List<String> transformProfile(JsonValue value) {
return value.asJsonArray().stream()
.map(this::nodeId)
.filter(Objects::nonNull)
.collect(toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import static org.assertj.core.api.Assertions.assertThat;
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.PropertyAndTypeNames.ODRL_ASSIGNEE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_ASSIGNER_ATTRIBUTE;
Expand All @@ -48,6 +49,7 @@
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_AGREEMENT;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_OFFER;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROFILE_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROHIBITION_ATTRIBUTE;
import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_TARGET_ATTRIBUTE;
import static org.eclipse.edc.policy.model.PolicyType.CONTRACT;
Expand Down Expand Up @@ -185,6 +187,21 @@ void shouldGetTypeFromContext_whenSet() {
assertThat(result.getType()).isEqualTo(CONTRACT);
}

@Test
void transform_withProfile() {

var policy = jsonFactory.createObjectBuilder()
.add(TYPE, ODRL_POLICY_TYPE_SET)
.add(ODRL_PROFILE_ATTRIBUTE, jsonFactory.createObjectBuilder().add(ID, "http://example.com/odrl:profile:01"))
.build();

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

assertThat(result).isNotNull();
assertThat(result.getProfiles().get(0)).isEqualTo("http://example.com/odrl:profile:01");
verify(context, never()).reportProblem(anyString());
}

private static class PolicyTypeArguments implements ArgumentsProvider {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{
"version": "3.1.0-alpha",
"urlPath": "/v3.1alpha",
"lastUpdated": "2024-07-09T09:17:00Z",
"lastUpdated": "2024-07-09T17:23:00Z",
"maturity": "alpha"
}
]
Loading
Loading