diff --git a/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubClient.java b/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubClient.java index 79ffab1d75..0aed885de4 100644 --- a/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubClient.java +++ b/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubClient.java @@ -22,6 +22,7 @@ public class XtmHubClient { private final XtmHubConfig config; private final HttpClientFactory httpClientFactory; + private static final String platformIdentifier = "openaev"; public XtmHubConnectivityStatus refreshRegistrationStatus( String platformId, String platformVersion, String token) { @@ -57,12 +58,13 @@ mutation RefreshPlatformRegistrationConnectivityStatus($input: RefreshPlatformRe "input": { "platformId": "%s", "platformVersion": "%s", - "token": "%s" + "token": "%s", + "platformIdentifier": "%s" } } } """, - platformId, platformVersion, token); + platformId, platformVersion, token, platformIdentifier); JsonElement element = JsonParser.parseString(mutationBody); return new StringEntity(element.toString()); diff --git a/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubConnectivityStatus.java b/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubConnectivityStatus.java index 8ecad44976..788b89f6d8 100644 --- a/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubConnectivityStatus.java +++ b/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubConnectivityStatus.java @@ -2,7 +2,8 @@ public enum XtmHubConnectivityStatus { ACTIVE("active"), - INACTIVE("inactive"); + INACTIVE("inactive"), + NOT_FOUND("not_found"); public final String label; diff --git a/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubService.java b/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubService.java index 9620b75514..1ae1efee27 100644 --- a/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubService.java +++ b/openaev-api/src/main/java/io/openaev/xtmhub/XtmHubService.java @@ -41,12 +41,16 @@ public PlatformSettings unregister() { public PlatformSettings refreshConnectivity() { PlatformSettings settings = platformSettingsService.findSettings(); - if (!isRegisteredWithXtmHub(settings)) { return settings; } ConnectivityCheckResult checkResult = checkConnectivityStatus(settings); + if (checkResult.status == XtmHubConnectivityStatus.NOT_FOUND) { + log.warn("Platform was not found on XTM Hub"); + return platformSettingsService.deleteXTMHubRegistration(); + } + handleConnectivityLossNotification(settings, checkResult); return updateRegistrationStatus(settings, checkResult); @@ -61,10 +65,9 @@ private ConnectivityCheckResult checkConnectivityStatus(PlatformSettings setting xtmHubClient.refreshRegistrationStatus( settings.getPlatformId(), settings.getPlatformVersion(), settings.getXtmHubToken()); - boolean isActive = status == XtmHubConnectivityStatus.ACTIVE; LocalDateTime lastCheck = parseLastConnectivityCheck(settings); - return new ConnectivityCheckResult(isActive, lastCheck); + return new ConnectivityCheckResult(status, lastCheck); } private LocalDateTime parseLastConnectivityCheck(PlatformSettings settings) { @@ -83,7 +86,7 @@ private void handleConnectivityLossNotification( private boolean shouldSendConnectivityLossEmail( PlatformSettings settings, ConnectivityCheckResult checkResult) { - return !checkResult.isActive() + return checkResult.status() != XtmHubConnectivityStatus.ACTIVE && hasConnectivityBeenLostForTooLong(checkResult.lastCheck()) && isEmailNotificationEnabled(settings); } @@ -101,12 +104,14 @@ private PlatformSettings updateRegistrationStatus( PlatformSettings settings, ConnectivityCheckResult checkResult) { XtmHubRegistrationStatus newStatus = - checkResult.isActive() + checkResult.status() == XtmHubConnectivityStatus.ACTIVE ? XtmHubRegistrationStatus.REGISTERED : XtmHubRegistrationStatus.LOST_CONNECTIVITY; LocalDateTime updatedLastCheck = - checkResult.isActive() ? LocalDateTime.now() : checkResult.lastCheck(); + checkResult.status() == XtmHubConnectivityStatus.ACTIVE + ? LocalDateTime.now() + : checkResult.lastCheck(); boolean shouldKeepEmailNotificationEnabled = !shouldSendConnectivityLossEmail(settings, checkResult); @@ -126,5 +131,6 @@ private LocalDateTime parseRegistrationDate(PlatformSettings settings) { } /** Encapsulates the result of a connectivity check */ - private record ConnectivityCheckResult(boolean isActive, LocalDateTime lastCheck) {} + private record ConnectivityCheckResult( + XtmHubConnectivityStatus status, LocalDateTime lastCheck) {} } diff --git a/openaev-api/src/test/java/io/openaev/xtmhub/XtmHubServiceTest.java b/openaev-api/src/test/java/io/openaev/xtmhub/XtmHubServiceTest.java index 9da4823fc9..1e14472fa3 100644 --- a/openaev-api/src/test/java/io/openaev/xtmhub/XtmHubServiceTest.java +++ b/openaev-api/src/test/java/io/openaev/xtmhub/XtmHubServiceTest.java @@ -76,6 +76,30 @@ void refreshConnectivity_WhenTokenIsNull_ShouldReturnSettingsUnchanged() { verifyNoInteractions(xtmHubEmailService); } + @Test + @DisplayName("Should remove XTM Hub registration when platform is not found in the hub") + void refreshConnectivity_WhenPlatformIsNotFound_ShouldRemoveRegistration() { + // Given + String token = "valid-token"; + String platformId = "platform-123"; + String platformVersion = "1.0.0"; + + mockSettings.setXtmHubToken(token); + mockSettings.setPlatformId(platformId); + mockSettings.setPlatformVersion(platformVersion); + + when(platformSettingsService.findSettings()).thenReturn(mockSettings); + when(xtmHubClient.refreshRegistrationStatus(platformId, platformVersion, token)) + .thenReturn(XtmHubConnectivityStatus.NOT_FOUND); + + // When + xtmHubService.refreshConnectivity(); + + // Then + verify(platformSettingsService).deleteXTMHubRegistration(); + verifyNoInteractions(xtmHubEmailService); + } + @Test @DisplayName("Should update registration as REGISTERED when connectivity is ACTIVE") void refreshConnectivity_WhenConnectivityIsActive_ShouldUpdateAsRegistered() {