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 22, 2024
1 parent 462468b commit a11c533
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import policies.response.IrsPolicyResponse;
import policies.response.PolicyResponse;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static policies.response.PolicyResponse.toDomain;

@Configuration
@ConfigurationPropertiesScan(basePackages = "org.eclipse.tractusx.traceability.*")
@EnableWebMvc
Expand Down Expand Up @@ -105,8 +108,8 @@ private List<AcceptedPolicy> createIrsAcceptedPolicies() {

// Map the IrsPolicyResponse objects to AcceptedPolicy objects
List<AcceptedPolicy> irsPolicies = irsPolicyResponses.stream().map(response -> {
Policy policy = new Policy(response.payload().policyId(), response.payload().policy().getCreatedOn(), response.validUntil(), response.payload().policy().getPermissions());
return new AcceptedPolicy(policy, response.validUntil());
PolicyResponse policy = new PolicyResponse(response.payload().policyId(), response.payload().policy().createdOn(), response.validUntil(), response.payload().policy().permissions(), null);
return new AcceptedPolicy(toDomain(policy), response.validUntil());
}).toList();

// Return the list of AcceptedPolicy objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ public CreatePolicyResponse createPolicy(RegisterPolicyRequest registerPolicyReq
if(registerPolicyRequest.validUntil().isAfter(Instant.now())){
CreatePolicyResponse policy = policyRepository.createPolicy(registerPolicyRequest);
edcNotificationContractService.updateNotificationContractDefinitions();
return policy; }
throw new PolicyNotValidException("Policy is expired" +registerPolicyRequest);
return policy;
}
throw new PolicyNotValidException("Policy is not valid because of a not accepted validUntil value " +registerPolicyRequest);
}

@Override
Expand All @@ -102,7 +103,7 @@ public void updatePolicy(UpdatePolicyRequest updatePolicyRequest) {
edcNotificationContractService.updateNotificationContractDefinitions();
return;
}
throw new PolicyNotValidException("Policy is expired" +updatePolicyRequest);
throw new PolicyNotValidException("Policy is not valid because of a not accepted validUntil value " +updatePolicyRequest);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPoliciesProvider;
import org.eclipse.tractusx.irs.edc.client.policy.AcceptedPolicy;
import org.eclipse.tractusx.irs.edc.client.policy.Constraint;
Expand All @@ -31,8 +32,11 @@
import org.springframework.stereotype.Service;
import policies.request.RegisterPolicyRequest;
import policies.request.UpdatePolicyRequest;
import policies.response.ConstraintResponse;
import policies.response.ConstraintsResponse;
import policies.response.CreatePolicyResponse;
import policies.response.IrsPolicyResponse;
import policies.response.PermissionResponse;
import policies.response.PolicyResponse;

import java.util.HashMap;
Expand All @@ -42,6 +46,7 @@
import java.util.stream.Stream;

import static org.apache.commons.collections4.ListUtils.emptyIfNull;
import static policies.response.PolicyResponse.toDomain;

@Slf4j
@Service
Expand Down Expand Up @@ -138,19 +143,19 @@ private boolean checkConstraints(IrsPolicyResponse irsPolicy) {
}

private boolean checkConstraint(IrsPolicyResponse irsPolicy, String rightOperand) {
return emptyIfNull(irsPolicy.payload().policy().getPermissions()).stream()
return emptyIfNull(irsPolicy.payload().policy().permissions()).stream()
.flatMap(this::getConstraintsStream)
.anyMatch(constraint -> constraint.getRightOperand().equals(rightOperand));
.anyMatch(constraint -> constraint.rightOperand().equals(rightOperand));
}

private Stream<Constraint> getConstraintsStream(Permission permission) {
Constraints constraint = permission.getConstraint();
private Stream<ConstraintResponse> getConstraintsStream(PermissionResponse permission) {
ConstraintsResponse constraint = permission.constraints();
if (constraint == null) {
return Stream.empty();
}
return Stream.concat(
emptyIfNull(constraint.getAnd()).stream(),
emptyIfNull(constraint.getOr()).stream()
emptyIfNull(constraint.and()).stream(),
emptyIfNull(constraint.or()).stream()
);
}

Expand Down Expand Up @@ -181,8 +186,8 @@ private void updateAcceptedPoliciesProvider() {

// Map the IrsPolicyResponse objects to AcceptedPolicy objects
List<AcceptedPolicy> irsPolicies = irsPolicyResponses.stream().map(response -> {
Policy policy = new Policy(response.payload().policyId(), response.payload().policy().getCreatedOn(), response.validUntil(), response.payload().policy().getPermissions());
return new AcceptedPolicy(policy, response.validUntil());
PolicyResponse policy = new PolicyResponse(response.payload().policyId(), response.payload().policy().createdOn(), response.validUntil(), response.payload().policy().permissions(), null);
return new AcceptedPolicy(toDomain(policy), response.validUntil());
}).toList();
defaultAcceptedPoliciesProvider.addAcceptedPolicies(irsPolicies);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.eclipse.tractusx.irs.edc.client.policy.Permission;
import org.eclipse.tractusx.irs.edc.client.policy.Policy;
import org.eclipse.tractusx.irs.edc.client.policy.PolicyType;
import policies.response.ConstraintResponse;
import policies.response.ConstraintsResponse;
import policies.response.IrsPolicyResponse;
import policies.request.Payload;
import org.eclipse.tractusx.traceability.common.properties.TraceabilityProperties;
Expand All @@ -37,6 +39,10 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import policies.response.OperatorTypeResponse;
import policies.response.PermissionResponse;
import policies.response.PolicyResponse;
import policies.response.PolicyTypeResponse;

import java.time.OffsetDateTime;
import java.util.Collections;
Expand Down Expand Up @@ -86,9 +92,9 @@ void givenPolicyExist_whenCreateIrsPolicyIfMissing_thenDoNotCreateApplicationCon
// given
OffsetDateTime validUntil = OffsetDateTime.parse("2023-07-03T16:01:05.309Z");
OffsetDateTime createdOn = OffsetDateTime.now();
Constraint constraint = new Constraint("leftOperand1", new Operator(OperatorType.EQ), "test");
Constraint constraintSecond = new Constraint("leftOperand2", new Operator(OperatorType.EQ), "test2");
Policy policy = new Policy("1", createdOn, validUntil, List.of(new Permission(PolicyType.USE, new Constraints(List.of(constraint, constraintSecond), List.of()))));
ConstraintResponse constraint = new ConstraintResponse("leftOperand1", OperatorTypeResponse.EQ, "test");
ConstraintResponse constraintSecond = new ConstraintResponse("leftOperand2", 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);
Map<String, List<IrsPolicyResponse>> existingPolicies = Map.of("key", List.of(existingPolicy));
Expand All @@ -111,9 +117,9 @@ void givenOutdatedPolicyExist_whenCreatePolicyBasedOnAppConfig_thenUpdateIt() {
// given
OffsetDateTime validUntil = OffsetDateTime.parse("2023-07-03T16:01:05.309Z");
OffsetDateTime createdOn = OffsetDateTime.now();
Constraint constraint = new Constraint("leftOperand1", new Operator(OperatorType.EQ), "test");
Constraint constraintSecond = new Constraint("leftOperand2", new Operator(OperatorType.EQ), "test2");
Policy policy = new Policy("test", createdOn, validUntil, List.of(new Permission(PolicyType.USE, new Constraints(List.of(constraint, constraintSecond), List.of()))));
ConstraintResponse constraint = new ConstraintResponse("leftOperand1", OperatorTypeResponse.EQ, "test");
ConstraintResponse constraintSecond = new ConstraintResponse("leftOperand2", 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);

final IrsPolicyResponse existingPolicy = new IrsPolicyResponse(validUntil, payload);
Expand All @@ -138,11 +144,11 @@ void test_getPolicyConstraints() {
// GIVEN
OffsetDateTime validUntil = OffsetDateTime.now();
OffsetDateTime createdOn = OffsetDateTime.now();
List<Constraint> andConstraints = List.of(new Constraint("leftOperand", new Operator(OperatorType.EQ), "rightOperand"));
List<Constraint> orConstraints = List.of(new Constraint("leftOperand", new Operator(OperatorType.EQ), "rightOperand"));
Constraints constraints = new Constraints(andConstraints, orConstraints);
List<ConstraintResponse> andConstraints = List.of(new ConstraintResponse("leftOperand", OperatorTypeResponse.EQ, "rightOperand"));
List<ConstraintResponse> orConstraints = List.of(new ConstraintResponse("leftOperand", OperatorTypeResponse.EQ, "rightOperand"));
ConstraintsResponse constraints = new ConstraintsResponse(andConstraints, orConstraints);

Policy policy = new Policy("test", createdOn, validUntil, List.of(new Permission(PolicyType.USE, constraints)));
PolicyResponse policy = new PolicyResponse("test", createdOn, validUntil, List.of(new PermissionResponse(PolicyTypeResponse.USE, constraints)), null);
Payload payload = new Payload(null, "test", policy);

final IrsPolicyResponse existingPolicy = new IrsPolicyResponse(validUntil, payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.tractusx.traceability.testdata.PolicyTestDataFactory.createIrsPolicyResponse;
Expand Down Expand Up @@ -94,7 +95,7 @@ void testHandle() throws CreateEdcAssetException, CreateEdcPolicyDefinitionExcep
IrsPolicyResponse irsPolicyResponse = createIrsPolicyResponse("test", OffsetDateTime.now(), "orLeft", "andLeft", "or", "and");
Map<String, List<IrsPolicyResponse>> policyMap = Map.of("testKey", List.of(irsPolicyResponse));

List<PolicyResponse> policyResponses = IrsPolicyResponse.toResponse(policyMap);
List<PolicyResponse> policyResponses = policyMap.values().stream().flatMap(List::stream).map(entry -> entry.payload().policy()).toList();
when(policyRepository.getNewestPolicyByOwnBpn()).thenReturn(Optional.of(policyResponses.get(0)));

CreateNotificationContractRequest request = new CreateNotificationContractRequest(notificationType, notificationMethod);
Expand Down Expand Up @@ -142,7 +143,7 @@ void givenService_whenPolicyDefinitionServiceThrowsException_thenThrowException(
IrsPolicyResponse irsPolicyResponse = createIrsPolicyResponse("test", OffsetDateTime.now(), "orLeft", "andLeft", "or", "and");
Map<String, List<IrsPolicyResponse>> policyMap = Map.of("testKey", List.of(irsPolicyResponse));

List<PolicyResponse> policyResponses = IrsPolicyResponse.toResponse(policyMap);
List<PolicyResponse> policyResponses = policyMap.values().stream().flatMap(List::stream).map(entry -> entry.payload().policy()).toList();
when(policyRepository.getNewestPolicyByOwnBpn()).thenReturn(Optional.of(policyResponses.get(0)));

CreateNotificationContractRequest request = new CreateNotificationContractRequest(notificationType, notificationMethod);
Expand All @@ -168,7 +169,7 @@ void givenService_whenContractDefinitionServiceThrowsException_thenThrowExceptio
IrsPolicyResponse irsPolicyResponse = createIrsPolicyResponse("test", OffsetDateTime.now(), "orLeft", "andLeft", "or", "and");
Map<String, List<IrsPolicyResponse>> policyMap = Map.of("testKey", List.of(irsPolicyResponse));

List<PolicyResponse> policyResponses = IrsPolicyResponse.toResponse(policyMap);
List<PolicyResponse> policyResponses = policyMap.values().stream().flatMap(List::stream).map(entry -> entry.payload().policy()).toList();
when(policyRepository.getNewestPolicyByOwnBpn()).thenReturn(Optional.of(policyResponses.get(0)));

CreateNotificationContractRequest request = new CreateNotificationContractRequest(notificationType, notificationMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
import org.eclipse.tractusx.irs.edc.client.policy.Permission;
import org.eclipse.tractusx.irs.edc.client.policy.Policy;
import org.eclipse.tractusx.irs.edc.client.policy.PolicyType;
import policies.response.ConstraintResponse;
import policies.response.ConstraintsResponse;
import policies.response.IrsPolicyResponse;
import policies.request.Payload;
import org.jetbrains.annotations.NotNull;
import policies.response.OperatorTypeResponse;
import policies.response.PermissionResponse;
import policies.response.PolicyResponse;
import policies.response.PolicyTypeResponse;

import java.time.OffsetDateTime;
import java.util.List;
Expand All @@ -43,15 +49,15 @@ public static IrsPolicyResponse createIrsPolicyResponse(String policyId, OffsetD
Payload.builder()
.policyId(policyId)
.policy(
Policy.builder()
PolicyResponse.builder()
.policyId(policyId)
.createdOn(createdOn)
.permissions(List.of(
Permission.builder()
.action(PolicyType.USE)
.constraint(Constraints.builder()
.and(List.of(new Constraint(andLeftOperand, new Operator(OperatorType.EQ), andRightOperand)))
.or(List.of(new Constraint(orLeftOperand, new Operator(OperatorType.EQ), orRightOperand)))
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)))
.build())
.build()))
.build())
Expand Down
56 changes: 54 additions & 2 deletions tx-models/src/main/java/policies/response/ConstraintResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,68 @@
********************************************************************************/
package policies.response;

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

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Builder
public record ConstraintResponse(
@Schema(example = "PURPOSE")
@Schema(
implementation = String.class,
example = "string"
)
@JsonAlias({"odrl:leftOperand"})
String leftOperand,

@Schema
OperatorTypeResponse operatorTypeResponse,
@Schema(example = "ID Trace 3.1")
@Schema(
implementation = String.class,
example = "string"
)
@JsonAlias({"odrl:rightOperand"})
String rightOperand) {



public static List<Constraint> toDomain(List<ConstraintResponse> constraintResponse) {
if (constraintResponse == null) {
return null;
}
return constraintResponse.stream()
.map(ConstraintResponse::toDomain)
.collect(Collectors.toList());
}
public static Constraint toDomain(ConstraintResponse constraintResponse){
if (constraintResponse == null) {
return null;
}
Operator operator = new Operator(toDomain(constraintResponse.operatorTypeResponse));

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));
}
}
16 changes: 16 additions & 0 deletions tx-models/src/main/java/policies/response/ConstraintsResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,29 @@
********************************************************************************/
package policies.response;

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

import java.util.List;

@Builder
public record ConstraintsResponse(
@ArraySchema
@JsonAlias({"odrl:and"})
List<ConstraintResponse> and,
@ArraySchema
@JsonAlias({"odrl:or"})
List<ConstraintResponse> or
) {
public static Constraints toDomain(ConstraintsResponse constraintsResponse){
if (constraintsResponse == null) {
return null;
}
return Constraints.builder()
.and(ConstraintResponse.toDomain(constraintsResponse.and))
.or(ConstraintResponse.toDomain(constraintsResponse.or))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,4 @@ public record IrsPolicyResponse(@JsonFormat(shape = JsonFormat.Shape.STRING) Off
}
]
""";


}
Loading

0 comments on commit a11c533

Please sign in to comment.