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

FIWARE / contract-management / #32

04 Apr 2025 09:42AM UTC coverage: 1.829% (+0.8%) from 0.982%
#32

Pull #5

wistefan
try different id
Pull Request #5: integrate contract negotiation

17 of 247 new or added lines in 9 files covered. (6.88%)

5 existing lines in 2 files now uncovered.

620 of 33897 relevant lines covered (1.83%)

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/tmforum/handlers/QuoteEventHandler.java
1
package org.fiware.iam.tmforum.handlers;
2

3
import com.fasterxml.jackson.databind.ObjectMapper;
4
import io.micronaut.http.HttpResponse;
5
import jakarta.inject.Singleton;
6
import lombok.RequiredArgsConstructor;
7
import lombok.extern.slf4j.Slf4j;
8
import org.fiware.iam.configuration.GeneralProperties;
9
import org.fiware.iam.dsp.RainbowAdapter;
10
import org.fiware.iam.exception.RainbowException;
11
import org.fiware.iam.exception.TMForumException;
12
import org.fiware.iam.tmforum.OrganizationResolver;
13
import org.fiware.iam.tmforum.TMForumAdapter;
14
import org.fiware.iam.tmforum.productcatalog.model.CharacteristicValueSpecificationVO;
15
import org.fiware.iam.tmforum.productcatalog.model.ProductSpecificationCharacteristicVO;
16
import org.fiware.iam.tmforum.quote.model.*;
17
import org.fiware.rainbow.model.NegotiationRequestVO;
18
import org.fiware.rainbow.model.OfferVO;
19
import org.fiware.rainbow.model.PermissionVO;
20
import reactor.core.publisher.Mono;
21

22
import java.util.List;
23
import java.util.Map;
24
import java.util.UUID;
25

26
import static org.fiware.iam.tmforum.TMForumAdapter.CONSUMER_ROLE;
27

28
/**
29
 * Handle all incoming events in connection to Quote
30
 */
31
@RequiredArgsConstructor
32
@Singleton
NEW
33
@Slf4j
×
34
public class QuoteEventHandler implements EventHandler {
35

36
        private static final String URN_UUID_TEMPLATE = "urn:uuid:%s";
37

38

39
        private static final String CREATE_EVENT = "QuoteCreateEvent";
40
        private static final String DELETE_EVENT = "QuoteDeleteEvent";
41
        private static final String STATE_CHANGE_EVENT = "QuoteStateChangeEvent";
42
        private static final String ATTRIBUTE_CHANGE_EVENT = "QuoteAttributeValueChangeEvent";
43

NEW
44
        private static final List<String> SUPPORTED_EVENT_TYPES = List.of(CREATE_EVENT, DELETE_EVENT, STATE_CHANGE_EVENT, ATTRIBUTE_CHANGE_EVENT);
×
45

46
        private final ObjectMapper objectMapper;
47
        private final GeneralProperties generalProperties;
48
        private final OrganizationResolver organizationResolver;
49
        private final TMForumAdapter tmForumAdapter;
50
        private final RainbowAdapter rainbowAdapter;
51

52

53
        @Override
54
        public boolean isEventTypeSupported(String eventType) {
NEW
55
                return SUPPORTED_EVENT_TYPES.contains(eventType);
×
56
        }
57

58
        @Override
59
        public Mono<HttpResponse<?>> handleEvent(String eventType, Map<String, Object> event) {
NEW
60
                return switch (eventType) {
×
NEW
61
                        case CREATE_EVENT -> handleQuoteCreation(event);
×
NEW
62
                        case STATE_CHANGE_EVENT, ATTRIBUTE_CHANGE_EVENT -> handleQuoteStateChange(event);
×
NEW
63
                        case DELETE_EVENT -> handleQuoteDeletion(event);
×
NEW
64
                        default -> throw new IllegalArgumentException("Even type %s is not supported.".formatted(eventType));
×
65
                };
66
        }
67

68
        private Mono<HttpResponse<?>> handleQuoteCreation(Map<String, Object> event) {
NEW
69
                QuoteCreateEventVO quoteCreateEventVO = objectMapper.convertValue(event, QuoteCreateEventVO.class);
×
NEW
70
                QuoteVO quoteVO = quoteCreateEventVO.getEvent()
×
NEW
71
                                .getQuote();
×
72

NEW
73
                return tmForumAdapter.getConsumerDid(quoteVO)
×
NEW
74
                                .flatMap(id -> rainbowAdapter.createParticipant(id, CONSUMER_ROLE))
×
NEW
75
                                .onErrorMap(t -> new RainbowException("Was not able to create consumer.", t))
×
NEW
76
                                .flatMap(r -> {
×
NEW
77
                                        String offerId = getOfferIdFromQuote(quoteVO);
×
NEW
78
                                        return tmForumAdapter.getOfferingParameters(offerId)
×
NEW
79
                                                        .map(offeringParams -> new NegotiationRequestVO()
×
NEW
80
                                                                        .dspaceColonCallbackAddress("")
×
NEW
81
                                                                        .dspaceColonConsumerPid(String.format(URN_UUID_TEMPLATE, UUID.randomUUID()))
×
NEW
82
                                                                        .dspaceColonOffer(new OfferVO()
×
NEW
83
                                                                                        .atId(offerId)
×
NEW
84
                                                                                        .odrlColonTarget(offeringParams.target())
×
NEW
85
                                                                                        .odrlColonPermission(List.of(new PermissionVO()
×
NEW
86
                                                                                                        .odrlColonAction(offeringParams.action())))));
×
87
                                })
NEW
88
                                .onErrorMap(t -> {
×
NEW
89
                                        throw new RainbowException("Was not able to get the offering parameters.", t);
×
90
                                })
NEW
91
                                .flatMap(negotiationRequestVO ->
×
NEW
92
                                                rainbowAdapter
×
NEW
93
                                                                .createNegotiationRequest(negotiationRequestVO)
×
NEW
94
                                                                .flatMap(id -> tmForumAdapter.updateExternalId(quoteVO, id))
×
NEW
95
                                                                .map(t -> HttpResponse.noContent()));
×
96
        }
97

98

99
        private Mono<HttpResponse<?>> handleQuoteStateChange(Map<String, Object> event) {
NEW
100
                QuoteStateChangeEventVO quoteStateChangeEventVO = objectMapper.convertValue(event, QuoteStateChangeEventVO.class);
×
NEW
101
                QuoteVO quoteVO = quoteStateChangeEventVO.getEvent()
×
NEW
102
                                .getQuote();
×
103

NEW
104
                QuoteStateTypeVO quoteStateTypeVO = quoteVO.getState();
×
NEW
105
                return switch (quoteStateTypeVO) {
×
106
                        case APPROVED ->
NEW
107
                                        rainbowAdapter.updateNegotiationProcessByProviderId(quoteVO.getExternalId(), "dspace:OFFERED")
×
NEW
108
                                                        .map(t -> HttpResponse.noContent());
×
NEW
109
                        case ACCEPTED -> tmForumAdapter.getConsumerDid(quoteVO)
×
NEW
110
                                        .flatMap(consumerDid -> rainbowAdapter
×
NEW
111
                                                        .createAgreementAfterNegotiation(quoteVO.getExternalId(), consumerDid, generalProperties.getDid())
×
NEW
112
                                                        .flatMap(r -> rainbowAdapter.updateNegotiationProcessByProviderId(quoteVO.getExternalId(), "dspace:AGREED")
×
NEW
113
                                                                        .map(t -> HttpResponse.noContent())));
×
NEW
114
                        default -> Mono.just(HttpResponse.badRequest());
×
115
                };
116
        }
117

118
        private Mono<HttpResponse<?>> handleQuoteDeletion(Map<String, Object> event) {
NEW
119
                QuoteDeleteEventVO quoteDeleteEventVO = objectMapper.convertValue(event, QuoteDeleteEventVO.class);
×
NEW
120
                QuoteVO quoteVO = quoteDeleteEventVO.getEvent()
×
NEW
121
                                .getQuote();
×
122

NEW
123
                if (quoteVO.getExternalId() == null && !quoteVO.getExternalId().isEmpty()) {
×
NEW
124
                        return rainbowAdapter.updateNegotiationProcessByProviderId(quoteVO.getExternalId(), "dspace:TERMINATED")
×
NEW
125
                                        .map(t -> HttpResponse.noContent());
×
126
                }
127

NEW
128
                return Mono.just(HttpResponse.noContent());
×
129
        }
130

131
        private String getOfferIdFromQuote(QuoteVO quoteVO) {
NEW
132
                return quoteVO
×
NEW
133
                                .getQuoteItem()
×
NEW
134
                                .stream()
×
NEW
135
                                .filter(qi -> !qi.getState().equals("rejected"))
×
NEW
136
                                .map(QuoteItemVO::getProductOffering)
×
NEW
137
                                .map(org.fiware.iam.tmforum.quote.model.ProductOfferingRefVO::getId)
×
NEW
138
                                .findFirst()
×
NEW
139
                                .orElseThrow(() -> new IllegalArgumentException("The event does not reference an offer."));
×
140
        }
141
}
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