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

FIWARE / contract-management / #18

27 Sep 2023 05:51AM UTC coverage: 1.629% (-0.001%) from 1.63%
#18

Pull #2

pulledtim
Handle Subscription already present
Pull Request #2: Handle Subscription already present

5 of 5 new or added lines in 1 file covered. (100.0%)

241 of 14792 relevant lines covered (1.63%)

0.02 hits per line

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

21.05
/src/main/java/org/fiware/iam/tmforum/NotificationSubscriber.java
1
package org.fiware.iam.tmforum;
2

3
import io.micronaut.context.annotation.Value;
4
import io.micronaut.context.event.ApplicationEventListener;
5
import io.micronaut.http.HttpResponse;
6
import io.micronaut.http.HttpStatus;
7
import io.micronaut.http.client.exceptions.HttpClientException;
8
import io.micronaut.http.client.exceptions.HttpClientResponseException;
9
import io.micronaut.runtime.server.event.ServerStartupEvent;
10
import io.micronaut.scheduling.TaskScheduler;
11
import jakarta.inject.Singleton;
12
import lombok.RequiredArgsConstructor;
13
import lombok.extern.slf4j.Slf4j;
14
import org.fiware.iam.tmforum.product.client.api.EventsSubscriptionApiClient;
15
import org.fiware.iam.tmforum.product.client.model.EventSubscriptionInputVO;
16
import org.fiware.iam.tmforum.product.client.model.EventSubscriptionVO;
17

18
import java.time.Duration;
19

20
import static org.fiware.iam.tmforum.product.server.api.NotificationListenersClientSideApi.PATH_LISTEN_TO_PRODUCT_ORDER_CREATE_EVENT;
21

22
@Singleton
23
@RequiredArgsConstructor
1✔
24
@Slf4j
1✔
25
public class NotificationSubscriber implements ApplicationEventListener<ServerStartupEvent> {
26

27
    private static final String QUERY_PRODUCT_ORDER_CREATED = "eventType=ProductOrderCreateEvent";
28

29
    private final EventsSubscriptionApiClient eventsSubscriptionApi;
30

31
    private final TaskScheduler taskScheduler;
32

33
    @Value("${general.basepath:}")
34
    private String controllerPath;
35

36
    @Value("${micronaut.server.port:8080}")
37
    private String servicePort;
38

39
    @Value("${general.name:contract-management}")
40
    private String serviceUrl;
41

42
    /**
43
     * Register a subscription at the TM Forum API service after startup. Using the
44
     * {@link io.micronaut.scheduling.annotation.Scheduled} annotation resulted in start
45
     * failure like https://stackoverflow.com/q/77075901/4341660
46
     *
47
     * @param event
48
     */
49
    @Override
50
    public void onApplicationEvent(ServerStartupEvent event) {
51
        //Using fixed rate since we don't have an option to check if the subscription (still) exists
52
        taskScheduler.scheduleAtFixedRate(Duration.ofSeconds(10), Duration.ofSeconds(30), () -> {
1✔
53
            try {
54
                String callbackUrl = String.format("http://%s:%s%s%s", serviceUrl, servicePort, controllerPath, PATH_LISTEN_TO_PRODUCT_ORDER_CREATE_EVENT);
×
55
                log.info("Attempting to register subscription with callback {}", callbackUrl);
×
56

57
                EventSubscriptionInputVO subscription = new EventSubscriptionInputVO()
×
58
                        .callback(callbackUrl)
×
59
                        .query(QUERY_PRODUCT_ORDER_CREATED);
×
60
                HttpResponse<EventSubscriptionVO> eventSubscriptionVOHttpResponse = eventsSubscriptionApi.registerListener(subscription);
×
61
                log.info("Got reply {} and status {}", eventSubscriptionVOHttpResponse.body(), eventSubscriptionVOHttpResponse.getStatus());
×
62
            } catch (HttpClientResponseException e) {
×
63
                if (e.getStatus() == HttpStatus.CONFLICT) {
×
64
                    log.debug("Subscription for query {}, was already present in TM Forum API service", QUERY_PRODUCT_ORDER_CREATED);
×
65
                } else {
66
                    log.error("Could not create subscription in TM Forum API. Response was {}", e.getResponse(), e);
×
67
                }
68
            } catch (HttpClientException e) {
×
69
                log.error("Could not create subscription in TM Forum API", e);
×
70
            }
×
71
        });
×
72
    }
1✔
73
}
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