Skip to content

Commit

Permalink
chore[1194]: updated response
Browse files Browse the repository at this point in the history
  • Loading branch information
ashanmugavel committed Jul 23, 2024
1 parent a11c533 commit 8705228
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static List<EdcPolicyPermission> mapToPermissions(List<PermissionRespons
return permissions.stream().map(permission -> EdcPolicyPermission.builder()
// https://github.com/eclipse-tractusx/traceability-foss/issues/978 here we need to make sure that the USE is lowercase in the object.
.action(permission.action().name().toLowerCase())
.edcPolicyPermissionConstraints(mapToConstraint(permission.constraints()))
.edcPolicyPermissionConstraints(mapToConstraint(permission.constraint()))
.build()
).toList();
}
Expand All @@ -79,7 +79,7 @@ private static List<EdcPolicyPermissionConstraintExpression> mapToConstraintExpr
.leftOperand(constraint.leftOperand())
.rightOperand(constraint.rightOperand())
.operator(EdcOperator.builder()
.operatorId("odrl:" + constraint.operatorTypeResponse().getCode())
.operatorId("odrl:" + constraint.operator().operatorType().getCode())
.build())
.build())
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import policies.response.ConstraintsResponse;
import policies.response.CreatePolicyResponse;
import policies.response.IrsPolicyResponse;
import policies.response.OperatorResponse;
import policies.response.OperatorTypeResponse;
import policies.response.PermissionResponse;
import policies.response.PolicyResponse;
Expand Down Expand Up @@ -137,16 +138,16 @@ public void createPolicyFromAppConfig() {
Context context = Context.getDefault();
String policyId = UUID.randomUUID().toString();

ConstraintResponse constraint = new ConstraintResponse(traceabilityProperties.getLeftOperand(), OperatorTypeResponse.EQ, traceabilityProperties.getRightOperand());
ConstraintResponse constraintSecond = new ConstraintResponse(traceabilityProperties.getLeftOperandSecond(), OperatorTypeResponse.EQ, traceabilityProperties.getRightOperandSecond());
ConstraintResponse constraint = new ConstraintResponse(traceabilityProperties.getLeftOperand(), new OperatorResponse(OperatorTypeResponse.EQ), traceabilityProperties.getRightOperand());
ConstraintResponse constraintSecond = new ConstraintResponse(traceabilityProperties.getLeftOperandSecond(),new OperatorResponse(OperatorTypeResponse.EQ), traceabilityProperties.getRightOperandSecond());

ConstraintsResponse constraints = ConstraintsResponse.builder()
.and(List.of(constraint, constraintSecond))
.build();

PermissionResponse permission = PermissionResponse.builder()
.action(PolicyTypeResponse.USE)
.constraints(constraints)
.constraint(constraints)
.build();

PolicyResponse policy = new PolicyResponse(policyId, Instant.now().atOffset(ZoneOffset.UTC), validUntil, List.of(permission),null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private boolean checkConstraint(IrsPolicyResponse irsPolicy, String rightOperand
}

private Stream<ConstraintResponse> getConstraintsStream(PermissionResponse permission) {
ConstraintsResponse constraint = permission.constraints();
ConstraintsResponse constraint = permission.constraint();
if (constraint == null) {
return Stream.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import policies.response.OperatorResponse;
import policies.response.OperatorTypeResponse;
import policies.response.PermissionResponse;
import policies.response.PolicyResponse;
Expand Down Expand Up @@ -92,8 +93,8 @@ void givenPolicyExist_whenCreateIrsPolicyIfMissing_thenDoNotCreateApplicationCon
// given
OffsetDateTime validUntil = OffsetDateTime.parse("2023-07-03T16:01:05.309Z");
OffsetDateTime createdOn = OffsetDateTime.now();
ConstraintResponse constraint = new ConstraintResponse("leftOperand1", OperatorTypeResponse.EQ, "test");
ConstraintResponse constraintSecond = new ConstraintResponse("leftOperand2", OperatorTypeResponse.EQ, "test2");
ConstraintResponse constraint = new ConstraintResponse("leftOperand1", new OperatorResponse(OperatorTypeResponse.EQ), "test");
ConstraintResponse constraintSecond = new ConstraintResponse("leftOperand2", new OperatorResponse(OperatorTypeResponse.EQ), "test2");
PolicyResponse policy = new PolicyResponse("1", createdOn, validUntil, List.of(new PermissionResponse(PolicyTypeResponse.USE, new ConstraintsResponse(List.of(constraint, constraintSecond), List.of()))),null);
Payload payload = new Payload(null, "1", policy);
final IrsPolicyResponse existingPolicy = new IrsPolicyResponse(validUntil, payload);
Expand All @@ -117,8 +118,8 @@ void givenOutdatedPolicyExist_whenCreatePolicyBasedOnAppConfig_thenUpdateIt() {
// given
OffsetDateTime validUntil = OffsetDateTime.parse("2023-07-03T16:01:05.309Z");
OffsetDateTime createdOn = OffsetDateTime.now();
ConstraintResponse constraint = new ConstraintResponse("leftOperand1", OperatorTypeResponse.EQ, "test");
ConstraintResponse constraintSecond = new ConstraintResponse("leftOperand2", OperatorTypeResponse.EQ, "test2");
ConstraintResponse constraint = new ConstraintResponse("leftOperand1", new OperatorResponse(OperatorTypeResponse.EQ), "test");
ConstraintResponse constraintSecond = new ConstraintResponse("leftOperand2", new OperatorResponse(OperatorTypeResponse.EQ), "test2");
PolicyResponse policy = new PolicyResponse("test", createdOn, validUntil, List.of(new PermissionResponse(PolicyTypeResponse.USE, new ConstraintsResponse(List.of(constraint, constraintSecond), List.of()))),null);
Payload payload = new Payload(null, "test", policy);

Expand All @@ -144,8 +145,8 @@ void test_getPolicyConstraints() {
// GIVEN
OffsetDateTime validUntil = OffsetDateTime.now();
OffsetDateTime createdOn = OffsetDateTime.now();
List<ConstraintResponse> andConstraints = List.of(new ConstraintResponse("leftOperand", OperatorTypeResponse.EQ, "rightOperand"));
List<ConstraintResponse> orConstraints = List.of(new ConstraintResponse("leftOperand", OperatorTypeResponse.EQ, "rightOperand"));
List<ConstraintResponse> andConstraints = List.of(new ConstraintResponse("leftOperand", new OperatorResponse(OperatorTypeResponse.EQ), "rightOperand"));
List<ConstraintResponse> orConstraints = List.of(new ConstraintResponse("leftOperand", new OperatorResponse(OperatorTypeResponse.EQ), "rightOperand"));
ConstraintsResponse constraints = new ConstraintsResponse(andConstraints, orConstraints);

PolicyResponse policy = new PolicyResponse("test", createdOn, validUntil, List.of(new PermissionResponse(PolicyTypeResponse.USE, constraints)), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import policies.response.IrsPolicyResponse;
import policies.request.Payload;
import org.jetbrains.annotations.NotNull;
import policies.response.OperatorResponse;
import policies.response.OperatorTypeResponse;
import policies.response.PermissionResponse;
import policies.response.PolicyResponse;
Expand All @@ -55,9 +56,9 @@ public static IrsPolicyResponse createIrsPolicyResponse(String policyId, OffsetD
.permissions(List.of(
PermissionResponse.builder()
.action(PolicyTypeResponse.USE)
.constraints(ConstraintsResponse.builder()
.and(List.of(new ConstraintResponse(andLeftOperand,OperatorTypeResponse.EQ, andRightOperand)))
.or(List.of(new ConstraintResponse(orLeftOperand, OperatorTypeResponse.EQ, orRightOperand)))
.constraint(ConstraintsResponse.builder()
.and(List.of(new ConstraintResponse(andLeftOperand, new OperatorResponse(OperatorTypeResponse.EQ), andRightOperand)))
.or(List.of(new ConstraintResponse(orLeftOperand, new OperatorResponse(OperatorTypeResponse.EQ), orRightOperand)))
.build())
.build()))
.build())
Expand Down
17 changes: 4 additions & 13 deletions tx-models/src/main/java/policies/response/ConstraintResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public record ConstraintResponse(
@JsonAlias({"odrl:leftOperand"})
String leftOperand,


@Schema
OperatorTypeResponse operatorTypeResponse,
@JsonAlias({"odrl:operator"})
OperatorResponse operator,
@Schema(
implementation = String.class,
example = "string"
Expand All @@ -63,23 +65,12 @@ public static Constraint toDomain(ConstraintResponse constraintResponse){
if (constraintResponse == null) {
return null;
}
Operator operator = new Operator(toDomain(constraintResponse.operatorTypeResponse));
Operator operator = new Operator(OperatorResponse.toDomain(constraintResponse.operator.operatorType()));

return new Constraint(
constraintResponse.leftOperand(),
operator,
constraintResponse.rightOperand()
);
}

public static OperatorType toDomain(OperatorTypeResponse operatorTypeResponse) {
if (operatorTypeResponse == null) {
return null;
}

return Arrays.stream(OperatorType.values())
.filter(type -> type.getCode().equals(operatorTypeResponse.code))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown code: " + operatorTypeResponse.code));
}
}
30 changes: 30 additions & 0 deletions tx-models/src/main/java/policies/response/OperatorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package policies.response;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import org.eclipse.tractusx.irs.edc.client.policy.OperatorType;

import java.util.Arrays;
@Builder
@Schema
public record OperatorResponse(
@JsonProperty("@id")
@Schema(
implementation = OperatorType.class,
example = "odrl:eq"
)
OperatorTypeResponse operatorType
) {
public static OperatorType toDomain(OperatorTypeResponse operatorTypeResponse) {
if (operatorTypeResponse == null) {
return null;
}

return Arrays.stream(OperatorType.values())
.filter(type -> type.getCode().equals(operatorTypeResponse.code))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unknown code: " + operatorTypeResponse.code));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
package policies.response;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Getter;
import org.eclipse.tractusx.irs.edc.client.policy.OperatorType;

import java.util.NoSuchElementException;
import java.util.stream.Stream;


@JsonSerialize(
Expand Down Expand Up @@ -64,4 +69,20 @@ public enum OperatorTypeResponse {
this.label = label;
}

@JsonCreator
public static OperatorTypeResponse fromValue(String value) {
String operator;
if (value.startsWith("odrl:")) {
operator = value.substring(5);
} else {
operator = value;
}

return (OperatorTypeResponse) Stream.of(values()).filter((operatorType) -> {
return operatorType.code.equals(operator);
}).findFirst().orElseThrow(() -> {
return new NoSuchElementException("Unsupported OperatorType: " + operator);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public record PermissionResponse(

@Schema
@JsonAlias({"odrl:constraint"})
ConstraintsResponse constraints
ConstraintsResponse constraint
) {

public static PolicyType toDomain(PolicyTypeResponse response){
Expand All @@ -62,7 +62,7 @@ public static Permission toDomain(PermissionResponse permissionResponse) {
}
return Permission.builder()
.action(toDomain(permissionResponse.action))
.constraint(ConstraintsResponse.toDomain(permissionResponse.constraints))
.constraint(ConstraintsResponse.toDomain(permissionResponse.constraint))
.build();
}
}

0 comments on commit 8705228

Please sign in to comment.