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

FIWARE / contract-management / #59

20 Aug 2025 12:23PM UTC coverage: 1.652% (-0.002%) from 1.654%
#59

push

wistefan
support policy creation

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

559 of 33838 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 LOGICAL_CONSTRAINT_TYPE = "odrl:LogicalConstraint";
25
        private static final String PARTY_COLLECTION_TYPE = "odrl:PartyCollection";
26
        private static final String AND_KEY = "odrl:and";
27
        private static final String REFINEMENT_KEY = "odrl:refinement";
28
        private static final String ASSIGNEE_KEY = "odrl:assignee";
29
        private static final String PERMISSION_KEY = "odrl:permission";
30
        private static final String LEFT_OPERAND_KEY = "odrl:leftOperand";
31
        private static final String RIGHT_OPERAND_KEY = "odrl:rightOperand";
32
        private static final String OPERATOR_KEY = "odrl:operator";
33
        private static final String EQ_OPERATOR = "odrl:eq";
34
        private static final String VC_CURRENT_PARTY_OPERAND = "vc:currentParty";
35

36
        private final DefaultApiClient papClient;
37

38
        public Mono<Boolean> createPolicy(String customer, Map<String, Object> policy) {
NEW
39
                return papClient.createPolicy(addAssignee(customer, policy)).map(HttpResponse::code).map(code -> code >= 200 && code < 300);
×
40
        }
41

42
        private Map<String, Object> addAssignee(String customer, Map<String, Object> policy) {
NEW
43
                Map<String, Object> permission = getPermission(policy);
×
NEW
44
                Optional<Map.Entry<String, Object>> optionalAssignee = permission.entrySet()
×
NEW
45
                                .stream()
×
NEW
46
                                .filter(e -> e.getKey().equals(ASSIGNEE_KEY))
×
NEW
47
                                .findFirst();
×
NEW
48
                if (optionalAssignee.isEmpty()) {
×
NEW
49
                        permission.put(ASSIGNEE_KEY, customer);
×
50
                } else {
NEW
51
                        permission.put(ASSIGNEE_KEY, addToAssignees(customer, optionalAssignee.get()));
×
52
                }
NEW
53
                policy.put(PERMISSION_KEY, permission);
×
NEW
54
                return policy;
×
55
        }
56

57
        private Map<String, Object> getPermission(Map<String, Object> policy) {
NEW
58
                Object permissionObject = policy.get(PERMISSION_KEY);
×
NEW
59
                if (permissionObject instanceof Map permissionMap) {
×
NEW
60
                        return permissionMap;
×
61
                }
NEW
62
                throw new IllegalArgumentException("The policy needs to contain a permission.");
×
63
        }
64

65
        private Map<String, Object> addToAssignees(String customer, Map.Entry<String, Object> assignee) {
NEW
66
                Object assigneeValue = assignee.getValue();
×
NEW
67
                Map<String, Object> customerConstraint = getIdConstraint(customer);
×
NEW
68
                Map<String, Object> idConstraint = Map.of();
×
NEW
69
                if (assigneeValue instanceof String assigneeId) {
×
NEW
70
                        idConstraint = getIdConstraint(assigneeId);
×
NEW
71
                } else if (assigneeValue instanceof Map valueMap) {
×
NEW
72
                        idConstraint = getOriginalConstraint(valueMap);
×
73
                }
NEW
74
                return Map.of(TYPE_KEY, PARTY_COLLECTION_TYPE, REFINEMENT_KEY, getAndConstraint(customerConstraint, idConstraint));
×
75
        }
76

77
        private Map<String, Object> getOriginalConstraint(Map originalMap) {
NEW
78
                if (originalMap.containsKey(ID_KEY) && originalMap.get(ID_KEY) instanceof String idString) {
×
NEW
79
                        return getIdConstraint(idString);
×
NEW
80
                } else if (originalMap.containsKey(ID_KEY) && originalMap.get(ID_KEY) instanceof Map<?, ?> idMap) {
×
NEW
81
                        if (idMap.containsKey(ID_KEY) && idMap.get(ID_KEY) instanceof String idString) {
×
NEW
82
                                return getIdConstraint(idString);
×
83
                        }
NEW
84
                } else if (originalMap.containsKey(REFINEMENT_KEY) && originalMap.get(REFINEMENT_KEY) instanceof Map refinementMap) {
×
NEW
85
                        return refinementMap;
×
86
                }
NEW
87
                throw new IllegalArgumentException("The policy does not contain a valid assignee.");
×
88
        }
89

90
        private Map<String, Object> getIdConstraint(String id) {
NEW
91
                return Map.of(LEFT_OPERAND_KEY, VC_CURRENT_PARTY_OPERAND, OPERATOR_KEY, EQ_OPERATOR, RIGHT_OPERAND_KEY, id);
×
92
        }
93

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

© 2026 Coveralls, Inc