diff --git a/booklore-api/src/main/java/org/booklore/crons/CronService.java b/booklore-api/src/main/java/org/booklore/crons/CronService.java index 6c1b50c17..96b30cb9a 100644 --- a/booklore-api/src/main/java/org/booklore/crons/CronService.java +++ b/booklore-api/src/main/java/org/booklore/crons/CronService.java @@ -42,8 +42,7 @@ public class CronService { @Scheduled(fixedDelay = 24, timeUnit = TimeUnit.HOURS, initialDelay = 24) public void sendTelemetryData() { - AppSettings settings = appSettingService.getAppSettings(); - if (settings != null && settings.isTelemetryEnabled()) { + if (isTelemetryEnabled()) { String url = appProperties.getTelemetry().getBaseUrl() + "/api/v1/ingest"; BookloreTelemetry telemetry = telemetryService.collectTelemetry(); if (postData(url, telemetry)) { @@ -54,11 +53,13 @@ public class CronService { @Scheduled(fixedDelay = 24, timeUnit = TimeUnit.HOURS, initialDelay = 12) public void sendPing() { - String url = appProperties.getTelemetry().getBaseUrl() + "/api/v1/heartbeat"; - InstallationPing ping = telemetryService.getInstallationPing(); - if (ping != null && postData(url, ping)) { - appSettingService.saveSetting(LAST_PING_KEY, Instant.now().toString()); - appSettingService.saveSetting(LAST_PING_APP_VERSION_KEY, ping.getAppVersion()); + if (isTelemetryEnabled()) { + String url = appProperties.getTelemetry().getBaseUrl() + "/api/v1/heartbeat"; + InstallationPing ping = telemetryService.getInstallationPing(); + if (ping != null && postData(url, ping)) { + appSettingService.saveSetting(LAST_PING_KEY, Instant.now().toString()); + appSettingService.saveSetting(LAST_PING_APP_VERSION_KEY, ping.getAppVersion()); + } } } @@ -76,6 +77,11 @@ public class CronService { } } + private void isTelemetryEnabled() { + AppSettings settings = appSettingService.getAppSettings(); + return settings != null && settings.isTelemetryEnabled(); + } + private void checkAndRunTelemetry() { AppSettings settings = appSettingService.getAppSettings(); if (settings == null || !settings.isTelemetryEnabled()) { diff --git a/booklore-api/src/test/java/org/booklore/crons/CronServiceTest.java b/booklore-api/src/test/java/org/booklore/crons/CronServiceTest.java index 0dbfa5700..c76d6aba4 100644 --- a/booklore-api/src/test/java/org/booklore/crons/CronServiceTest.java +++ b/booklore-api/src/test/java/org/booklore/crons/CronServiceTest.java @@ -84,7 +84,19 @@ class CronServiceTest { } @Test - void sendPing_postSuccess_savesSettings() { + void sendPing_telemetryDisabled_doesNotSend() { + AppSettings settings = mock(AppSettings.class); + when(appSettingService.getAppSettings()).thenReturn(settings); + when(settings.isTelemetryEnabled()).thenReturn(false); + cronService.sendPing(); + verifyNoInteractions(restClient); + } + + @Test + void sendPing_telemetryEnabled_postSuccess_savesSettings() { + AppSettings settings = mock(AppSettings.class); + when(appSettingService.getAppSettings()).thenReturn(settings); + when(settings.isTelemetryEnabled()).thenReturn(true); when(appProperties.getTelemetry().getBaseUrl()).thenReturn("http://telemetry"); InstallationPing ping = InstallationPing.builder().appVersion("1.0.0").build(); when(telemetryService.getInstallationPing()).thenReturn(ping); @@ -99,7 +111,10 @@ class CronServiceTest { } @Test - void sendPing_postFails_doesNotSaveSettings() { + void sendPing_telemetryEnabled_postFails_doesNotSaveSettings() { + AppSettings settings = mock(AppSettings.class); + when(appSettingService.getAppSettings()).thenReturn(settings); + when(settings.isTelemetryEnabled()).thenReturn(true); when(appProperties.getTelemetry().getBaseUrl()).thenReturn("http://telemetry"); InstallationPing ping = InstallationPing.builder().appVersion("1.0.0").build(); when(telemetryService.getInstallationPing()).thenReturn(ping);