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

FIWARE / contract-management / #69

30 Oct 2025 07:38AM UTC coverage: 1.686% (+0.04%) from 1.651%
#69

Pull #12

wistefan
improve organization creation and cleanup
Pull Request #12: Support for central marketplace and policy creation

117 of 1238 new or added lines in 31 files covered. (9.45%)

5 existing lines in 2 files now uncovered.

587 of 34807 relevant lines covered (1.69%)

0.02 hits per line

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

2.33
/src/main/java/org/fiware/iam/tmforum/OrganizationResolver.java
1
package org.fiware.iam.tmforum;
2

3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import io.micronaut.context.annotation.Requires;
5
import io.micronaut.http.HttpResponse;
6
import io.micronaut.http.HttpStatus;
7
import jakarta.inject.Singleton;
8
import lombok.RequiredArgsConstructor;
9
import lombok.extern.slf4j.Slf4j;
10
import org.fiware.iam.configuration.GeneralProperties;
11
import org.fiware.iam.domain.ContractManagement;
12
import org.fiware.iam.exception.TMForumException;
13
import org.fiware.iam.tmforum.party.api.OrganizationApiClient;
14
import org.fiware.iam.tmforum.party.model.CharacteristicVO;
15
import org.fiware.iam.tmforum.party.model.ExternalReferenceVO;
16
import reactor.core.publisher.Mono;
17

18
import java.util.List;
19
import java.util.Optional;
20

21
@Requires(condition = GeneralProperties.TmForumCondition.class)
22
@Singleton
23
@Slf4j
1✔
24
@RequiredArgsConstructor
25
public class OrganizationResolver {
26

27
    public static final String PROVIDER_ROLE = "provider";
28

29
    private static final String PARTY_CHARACTERISTIC_DID = "did";
30
    private static final String FIELD_NAME_CONTRACT_MANAGEMENT = "contractManagement";
31
    private static final String EXTERNAL_REFERENCE_IDM_ID = "idm_id";
32
    private static final String DID = "did";
33

34
    private final GeneralProperties generalProperties;
35
    private final ObjectMapper objectMapper;
36
    private final OrganizationApiClient apiClient;
37

38
    //TODO Cache me if you can
39
    public Mono<String> getDID(String organizationId) {
NEW
40
        return apiClient.retrieveOrganization(organizationId, null)
×
NEW
41
                .filter(response -> response.getStatus().equals(HttpStatus.OK))
×
NEW
42
                .map(HttpResponse::body)
×
NEW
43
                .map(ovo -> {
×
NEW
44
                            String did = getDidFromExternalReference(ovo.getExternalReference())
×
NEW
45
                                    .or(() -> getDidFromPartyCharacteristics(ovo.getPartyCharacteristic()))
×
NEW
46
                                    .orElseThrow(() -> new TMForumException("Could not find organizations DID (%s) in response.".formatted(organizationId)));
×
NEW
47
                            log.debug("Did is {}", did);
×
NEW
48
                            return did;
×
49
                        }
50
                );
51
    }
52

53
    public Mono<ContractManagement> getContractManagement(String organizationId) {
NEW
54
        return getDID(organizationId)
×
NEW
55
                .flatMap(did -> {
×
NEW
56
                    if (did.equals(generalProperties.getDid())) {
×
NEW
57
                        return Mono.just(new ContractManagement(true));
×
58
                    } else {
NEW
59
                        return apiClient.retrieveOrganization(organizationId, null)
×
NEW
60
                                .filter(response -> response.getStatus().equals(HttpStatus.OK))
×
NEW
61
                                .map(HttpResponse::body)
×
NEW
62
                                .map(ovo ->
×
NEW
63
                                        ovo.getPartyCharacteristic()
×
NEW
64
                                                .stream()
×
NEW
65
                                                .filter(pc -> pc.getName().equals(FIELD_NAME_CONTRACT_MANAGEMENT))
×
NEW
66
                                                .map(CharacteristicVO::getValue)
×
NEW
67
                                                .map(pcv -> objectMapper.convertValue(pcv, ContractManagement.class))
×
NEW
68
                                                .findAny()
×
NEW
69
                                                .orElse(new ContractManagement(true))
×
70
                                );
71
                    }
72
                });
73
    }
74

75
    private Optional<String> getDidFromPartyCharacteristics(List<CharacteristicVO> characteristicVOS) {
NEW
76
        if (characteristicVOS == null) {
×
NEW
77
            return Optional.empty();
×
78
        }
NEW
79
        return characteristicVOS.stream()
×
NEW
80
                .filter(entry -> PARTY_CHARACTERISTIC_DID.equals(entry.getName()))
×
NEW
81
                .map(CharacteristicVO::getValue)
×
NEW
82
                .filter(e -> e instanceof String)
×
NEW
83
                .map(e -> (String) e)
×
NEW
84
                .filter(this::isDid)
×
NEW
85
                .findAny();
×
86
    }
87

88
    private Optional<String> getDidFromExternalReference(List<ExternalReferenceVO> externalReferenceVOList) {
NEW
89
        if (externalReferenceVOList == null) {
×
NEW
90
            return Optional.empty();
×
91
        }
NEW
92
        return externalReferenceVOList.stream()
×
NEW
93
                .filter(ervo -> ervo.getExternalReferenceType().equals(EXTERNAL_REFERENCE_IDM_ID))
×
NEW
94
                .map(ExternalReferenceVO::getName)
×
NEW
95
                .filter(this::isDid)
×
NEW
96
                .findFirst();
×
97
    }
98

99
    private boolean isDid(String id) {
NEW
100
        String[] idParts = id.split(":");
×
NEW
101
        return idParts.length >= 3 && idParts[0].equals(DID);
×
102
    }
103
}
104

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