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

FIWARE / contract-management / #65

22 Aug 2025 11:38AM UTC coverage: 1.651% (-0.003%) from 1.654%
#65

Pull #11

wistefan
fix did
Pull Request #11: Policy support

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

24 existing lines in 1 file now uncovered.

559 of 33863 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
                return papClient.createPolicy(addAssignee(customer, updatePolicyId(orderId, policy))).map(HttpResponse::code).map(code -> code >= 200 && code < 300);
×
47
        }
48

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

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

58
        private Map<String, Object> updatePolicyId(String orderId, Map<String, Object> policy) {
NEW
59
                policy.put(UID_KEY, buildFullId(orderId, policy));
×
NEW
60
                log.info("Added the uid: {}", 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
                log.info("The final policy: {}", policy);
×
NEW
85
                return policy;
×
86
        }
87

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

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

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

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

125
        private Map<String, Object> getAndConstraint(Map<String, Object> customerConstraint, Map<String, Object> originalConstraint) {
NEW
126
                return Map.of(TYPE_KEY, LOGICAL_CONSTRAINT_TYPE, AND_KEY, List.of(customerConstraint, originalConstraint));
×
127
        }
128
}
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