• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FIWARE / contract-management / #63

22 Aug 2025 07:03AM UTC coverage: 1.651% (-0.003%) from 1.654%
#63

Pull #11

web-flow
Merge branch 'main' into policy-support
Pull Request #11: Policy support

0 of 54 new or added lines in 2 files covered. (0.0%)

24 existing lines in 1 file now uncovered.

559 of 33861 relevant lines covered (1.65%)

0.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/fiware/iam/PAPAdapter.java
1
package org.fiware.iam;
2

3
import io.micronaut.http.HttpResponse;
4
import jakarta.inject.Singleton;
5
import lombok.RequiredArgsConstructor;
6
import lombok.extern.slf4j.Slf4j;
7
import org.fiware.iam.odrl.pap.api.DefaultApiClient;
8
import reactor.core.publisher.Mono;
9

10
import java.util.List;
11
import java.util.Map;
12
import java.util.Optional;
13

14
/**
15
 * Responsible for handling connections with the PAP.
16
 */
17
@Singleton
18
@RequiredArgsConstructor
19
@Slf4j
×
20
public class PAPAdapter {
21

22
        private static final String TYPE_KEY = "@type";
23
        private static final String ID_KEY = "@id";
24
        private static final String UID_KEY = "odrl:uid";
25
        private static final String LOGICAL_CONSTRAINT_TYPE = "odrl:LogicalConstraint";
26
        private static final String PARTY_COLLECTION_TYPE = "odrl:PartyCollection";
27
        private static final String AND_KEY = "odrl:and";
28
        private static final String REFINEMENT_KEY = "odrl:refinement";
29
        private static final String ASSIGNEE_KEY = "odrl:assignee";
30
        private static final String PERMISSION_KEY = "odrl:permission";
31
        private static final String LEFT_OPERAND_KEY = "odrl:leftOperand";
32
        private static final String RIGHT_OPERAND_KEY = "odrl:rightOperand";
33
        private static final String OPERATOR_KEY = "odrl:operator";
34
        private static final String EQ_OPERATOR = "odrl:eq";
35
        private static final String VC_CURRENT_PARTY_OPERAND = "vc:currentParty";
36

37
        private static final String ID_TEMPLATE = "%s-%s";
38

39
        private final DefaultApiClient papClient;
40

41
        /**
42
         * Creates the given policy for the given customer (added as assignee) in the ODRL-PAP. Since this becomes a concrete instantiation of the policy,
43
         * its ID will be updated to include the product-order it originates from
44
         */
45
        public Mono<Boolean> createPolicy(String customer, String orderId, Map<String, Object> policy) {
NEW
46
                policy = updatePolicyId(orderId, policy);
×
NEW
47
                return papClient.createPolicy(addAssignee(customer, policy)).map(HttpResponse::code).map(code -> code >= 200 && code < 300);
×
48
        }
49

50
        public Mono<Boolean> deletePolicy(String orderId, Map<String, Object> policy) {
NEW
51
                String fullId = buildFullId(orderId, policy);
×
NEW
52
                return papClient.deletePolicyByUid(fullId).map(HttpResponse::code).map(code -> code >= 200 && code < 300);
×
53
        }
54

55
        private String buildFullId(String orderId, Map<String, Object> policy) {
NEW
56
                return String.format(ID_TEMPLATE, getPolicyId(policy), orderId);
×
57
        }
58

59
        private Map<String, Object> updatePolicyId(String orderId, Map<String, Object> policy) {
NEW
60
                policy.put(UID_KEY, buildFullId(orderId, policy));
×
NEW
61
                return policy;
×
62
        }
63

64
        private String getPolicyId(Map<String, Object> policy) {
NEW
65
                if (policy.containsKey(UID_KEY) && policy.get(UID_KEY) instanceof String idString) {
×
NEW
66
                        return idString;
×
67
                } else {
NEW
68
                        throw new IllegalArgumentException("The provided policy does not contain an odrl:uid.");
×
69
                }
70
        }
71

72
        private Map<String, Object> addAssignee(String customer, Map<String, Object> policy) {
NEW
73
                Map<String, Object> permission = getPermission(policy);
×
NEW
74
                Optional<Map.Entry<String, Object>> optionalAssignee = permission.entrySet()
×
NEW
75
                                .stream()
×
NEW
76
                                .filter(e -> e.getKey().equals(ASSIGNEE_KEY))
×
NEW
77
                                .findFirst();
×
NEW
78
                if (optionalAssignee.isEmpty()) {
×
NEW
79
                        permission.put(ASSIGNEE_KEY, customer);
×
80
                } else {
NEW
81
                        permission.put(ASSIGNEE_KEY, addToAssignees(customer, optionalAssignee.get()));
×
82
                }
NEW
83
                policy.put(PERMISSION_KEY, permission);
×
NEW
84
                return policy;
×
85
        }
86

87
        private Map<String, Object> getPermission(Map<String, Object> policy) {
NEW
88
                Object permissionObject = policy.get(PERMISSION_KEY);
×
NEW
89
                if (permissionObject instanceof Map permissionMap) {
×
NEW
90
                        return permissionMap;
×
91
                }
NEW
92
                throw new IllegalArgumentException("The policy needs to contain a permission.");
×
93
        }
94

95
        private Map<String, Object> addToAssignees(String customer, Map.Entry<String, Object> assignee) {
NEW
96
                Object assigneeValue = assignee.getValue();
×
NEW
97
                Map<String, Object> customerConstraint = getIdConstraint(customer);
×
NEW
98
                Map<String, Object> idConstraint = Map.of();
×
NEW
99
                if (assigneeValue instanceof String assigneeId) {
×
NEW
100
                        idConstraint = getIdConstraint(assigneeId);
×
NEW
101
                } else if (assigneeValue instanceof Map valueMap) {
×
NEW
102
                        idConstraint = getOriginalConstraint(valueMap);
×
103
                }
NEW
104
                return Map.of(TYPE_KEY, PARTY_COLLECTION_TYPE, REFINEMENT_KEY, getAndConstraint(customerConstraint, idConstraint));
×
105
        }
106

107
        private Map<String, Object> getOriginalConstraint(Map originalMap) {
NEW
108
                if (originalMap.containsKey(ID_KEY) && originalMap.get(ID_KEY) instanceof String idString) {
×
NEW
109
                        return getIdConstraint(idString);
×
NEW
110
                } else if (originalMap.containsKey(ID_KEY) && originalMap.get(ID_KEY) instanceof Map<?, ?> idMap) {
×
NEW
111
                        if (idMap.containsKey(ID_KEY) && idMap.get(ID_KEY) instanceof String idString) {
×
NEW
112
                                return getIdConstraint(idString);
×
113
                        }
NEW
114
                } else if (originalMap.containsKey(REFINEMENT_KEY) && originalMap.get(REFINEMENT_KEY) instanceof Map refinementMap) {
×
NEW
115
                        return refinementMap;
×
116
                }
NEW
117
                throw new IllegalArgumentException("The policy does not contain a valid assignee.");
×
118
        }
119

120
        private Map<String, Object> getIdConstraint(String id) {
NEW
121
                return Map.of(LEFT_OPERAND_KEY, VC_CURRENT_PARTY_OPERAND, OPERATOR_KEY, EQ_OPERATOR, RIGHT_OPERAND_KEY, id);
×
122
        }
123

124
        private Map<String, Object> getAndConstraint(Map<String, Object> customerConstraint, Map<String, Object> originalConstraint) {
NEW
125
                return Map.of(TYPE_KEY, LOGICAL_CONSTRAINT_TYPE, AND_KEY, List.of(customerConstraint, originalConstraint));
×
126
        }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc