From e5935c66966ca762bb0d73c9975fef8f396137e8 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 19 Apr 2024 22:42:32 +0100 Subject: [PATCH] fix metric saving for running in tests --- .../{EnvironmentUtils.java => AppConfig.java} | 15 ++++++++++++++- .../mcutils/backend/service/MetricService.java | 4 ++-- .../mcutils/backend/service/MojangService.java | 4 ++-- .../mcutils/backend/service/PlayerService.java | 6 +++--- .../mcutils/backend/service/ServerService.java | 4 ++-- 5 files changed, 23 insertions(+), 10 deletions(-) rename src/main/java/xyz/mcutils/backend/common/{EnvironmentUtils.java => AppConfig.java} (55%) diff --git a/src/main/java/xyz/mcutils/backend/common/EnvironmentUtils.java b/src/main/java/xyz/mcutils/backend/common/AppConfig.java similarity index 55% rename from src/main/java/xyz/mcutils/backend/common/EnvironmentUtils.java rename to src/main/java/xyz/mcutils/backend/common/AppConfig.java index f1e32b6..fbae4ff 100644 --- a/src/main/java/xyz/mcutils/backend/common/EnvironmentUtils.java +++ b/src/main/java/xyz/mcutils/backend/common/AppConfig.java @@ -4,7 +4,7 @@ import lombok.Getter; import lombok.experimental.UtilityClass; @UtilityClass -public final class EnvironmentUtils { +public final class AppConfig { /** * Is the app running in a production environment? */ @@ -14,4 +14,17 @@ public final class EnvironmentUtils { String env = System.getenv("ENVIRONMENT"); production = env != null && (env.equals("production")); } + + /** + * Is the app running in a test environment? + */ + @Getter + private static boolean isRunningTest = true; + static { + try { + Class.forName("org.junit.Test"); + } catch (ClassNotFoundException e) { + isRunningTest = false; + } + } } \ No newline at end of file diff --git a/src/main/java/xyz/mcutils/backend/service/MetricService.java b/src/main/java/xyz/mcutils/backend/service/MetricService.java index ab0fef3..d7cc37f 100644 --- a/src/main/java/xyz/mcutils/backend/service/MetricService.java +++ b/src/main/java/xyz/mcutils/backend/service/MetricService.java @@ -6,7 +6,7 @@ import com.influxdb.spring.influx.InfluxDB2AutoConfiguration; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import xyz.mcutils.backend.common.EnvironmentUtils; +import xyz.mcutils.backend.common.AppConfig; import xyz.mcutils.backend.common.Timer; import xyz.mcutils.backend.repository.mongo.MetricsRepository; import xyz.mcutils.backend.service.metric.Metric; @@ -56,7 +56,7 @@ public class MetricService { collectorEnabled.put(metric, metric.isCollector()); } - if (EnvironmentUtils.isProduction()) { + if (!AppConfig.isRunningTest()) { // Load the metrics from Redis loadMetrics(); diff --git a/src/main/java/xyz/mcutils/backend/service/MojangService.java b/src/main/java/xyz/mcutils/backend/service/MojangService.java index 2b8b828..e4b690c 100644 --- a/src/main/java/xyz/mcutils/backend/service/MojangService.java +++ b/src/main/java/xyz/mcutils/backend/service/MojangService.java @@ -11,7 +11,7 @@ import lombok.extern.log4j.Log4j2; import net.jodah.expiringmap.ExpirationPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import xyz.mcutils.backend.common.EnvironmentUtils; +import xyz.mcutils.backend.common.AppConfig; import xyz.mcutils.backend.common.ExpiringSet; import xyz.mcutils.backend.common.WebRequest; import xyz.mcutils.backend.model.cache.CachedEndpointStatus; @@ -186,7 +186,7 @@ public class MojangService { public CachedEndpointStatus getMojangApiStatus() { log.info("Getting Mojang API status"); Optional endpointStatus = mojangEndpointStatusRepository.findById(MOJANG_ENDPOINT_STATUS_KEY); - if (endpointStatus.isPresent() && EnvironmentUtils.isProduction()) { + if (endpointStatus.isPresent() && AppConfig.isProduction()) { log.info("Got cached Mojang API status"); return endpointStatus.get(); } diff --git a/src/main/java/xyz/mcutils/backend/service/PlayerService.java b/src/main/java/xyz/mcutils/backend/service/PlayerService.java index b96f6a0..539e485 100644 --- a/src/main/java/xyz/mcutils/backend/service/PlayerService.java +++ b/src/main/java/xyz/mcutils/backend/service/PlayerService.java @@ -61,7 +61,7 @@ public class PlayerService { } Optional cachedPlayer = playerCacheRepository.findById(uuid); - if (cachedPlayer.isPresent() && EnvironmentUtils.isProduction()) { // Return the cached player if it exists + if (cachedPlayer.isPresent() && AppConfig.isProduction()) { // Return the cached player if it exists log.info("Player {} is cached", id); return cachedPlayer.get(); } @@ -105,7 +105,7 @@ public class PlayerService { log.info("Getting UUID from username: {}", username); String id = username.toUpperCase(); Optional cachedPlayerName = playerNameCacheRepository.findById(id); - if (cachedPlayerName.isPresent() && EnvironmentUtils.isProduction()) { + if (cachedPlayerName.isPresent() && AppConfig.isProduction()) { return cachedPlayerName.get(); } try { @@ -155,7 +155,7 @@ public class PlayerService { Optional cache = playerSkinPartCacheRepository.findById(key); // The skin part is cached - if (cache.isPresent() && EnvironmentUtils.isProduction()) { + if (cache.isPresent() && AppConfig.isProduction()) { log.info("Skin part {} for player {} is cached", name, player.getUniqueId()); return cache.get(); } diff --git a/src/main/java/xyz/mcutils/backend/service/ServerService.java b/src/main/java/xyz/mcutils/backend/service/ServerService.java index 67ff7e7..9aaeecf 100644 --- a/src/main/java/xyz/mcutils/backend/service/ServerService.java +++ b/src/main/java/xyz/mcutils/backend/service/ServerService.java @@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import xyz.mcutils.backend.common.DNSUtils; import xyz.mcutils.backend.common.EnumUtils; -import xyz.mcutils.backend.common.EnvironmentUtils; +import xyz.mcutils.backend.common.AppConfig; import xyz.mcutils.backend.exception.impl.BadRequestException; import xyz.mcutils.backend.exception.impl.ResourceNotFoundException; import xyz.mcutils.backend.model.cache.CachedMinecraftServer; @@ -67,7 +67,7 @@ public class ServerService { // Check if the server is cached Optional cached = serverCacheRepository.findById(key); - if (cached.isPresent() && EnvironmentUtils.isProduction()) { + if (cached.isPresent() && AppConfig.isProduction()) { log.info("Server {}:{} is cached", hostname, port); return cached.get(); }