From fcac4fb75bd012c25858af0426677c47df230da1 Mon Sep 17 00:00:00 2001 From: Ryan Harg Date: Wed, 27 Nov 2024 14:11:56 +0100 Subject: [PATCH] Rework dyndns provider registry and add tests --- src/main/java/de/rpr/ddnsclient/Updater.java | 11 ++++--- .../rpr/ddnsclient/dyndns/DynDnsRouter.java | 27 ----------------- .../dyndns/DyndnsProviderRegistry.java | 30 +++++++++++++++++++ .../java/de/rpr/ddnsclient/UpdaterTest.java | 20 ++++++------- .../dyndns/DyndnsProviderRegistryTest.java | 29 ++++++++++++++++++ 5 files changed, 74 insertions(+), 43 deletions(-) delete mode 100644 src/main/java/de/rpr/ddnsclient/dyndns/DynDnsRouter.java create mode 100644 src/main/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistry.java create mode 100644 src/test/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistryTest.java diff --git a/src/main/java/de/rpr/ddnsclient/Updater.java b/src/main/java/de/rpr/ddnsclient/Updater.java index 3967875..1334f28 100644 --- a/src/main/java/de/rpr/ddnsclient/Updater.java +++ b/src/main/java/de/rpr/ddnsclient/Updater.java @@ -1,7 +1,7 @@ package de.rpr.ddnsclient; import de.rpr.ddnsclient.dyndns.DynDns; -import de.rpr.ddnsclient.dyndns.DynDnsRouter; +import de.rpr.ddnsclient.dyndns.DyndnsProviderRegistry; import de.rpr.ddnsclient.lookup.DnsResolver; import de.rpr.ddnsclient.lookup.PublicIpLookup; import de.rpr.ddnsclient.model.Config; @@ -16,14 +16,13 @@ import java.time.Duration; import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; -import java.util.Optional; @ApplicationScoped public class Updater { private static final Logger log = Logger.getLogger(Updater.class); - private final DynDnsRouter dynDnsRouter; + private final DyndnsProviderRegistry dyndnsProviderRegistry; private final PublicIpLookup publicIpLookup; private final DnsResolver dnsResolver; private final Duration backoff; @@ -33,12 +32,12 @@ public class Updater { private final Map updateMap = new HashMap<>(); @Inject - public Updater(DynDnsRouter dynDnsRouter, + public Updater(DyndnsProviderRegistry dyndnsProviderRegistry, PublicIpLookup publicIpLookup, DnsResolver dnsResolver, Config config, @ConfigProperty(name = "ddnsclient.backoff-duration", defaultValue = "300s") Duration backoff) { - this.dynDnsRouter = dynDnsRouter; + this.dyndnsProviderRegistry = dyndnsProviderRegistry; this.publicIpLookup = publicIpLookup; this.dnsResolver = dnsResolver; this.config = config; @@ -70,7 +69,7 @@ public class Updater { if (!publicIps.equals(registeredIps)) { log.tracef("IPs changed, updating..."); - DynDns dynDns = dynDnsRouter.get(cfg.provider()); + DynDns dynDns = dyndnsProviderRegistry.get(cfg.provider()); dynDns.update(cfg.hostname(), publicIps, new DyndnsAuth(null, null, cfg.token())); updateMap.put(cfg.hostname(), LocalDateTime.now()); } else { diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/DynDnsRouter.java b/src/main/java/de/rpr/ddnsclient/dyndns/DynDnsRouter.java deleted file mode 100644 index a7ebdea..0000000 --- a/src/main/java/de/rpr/ddnsclient/dyndns/DynDnsRouter.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.rpr.ddnsclient.dyndns; - -import de.rpr.ddnsclient.model.DyndnsAuth; -import io.quarkus.arc.All; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.inject.Inject; - -import java.util.List; - -@ApplicationScoped -public class DynDnsRouter { - - private final List dynDnsList; - - @SuppressWarnings("CdiInjectionPointsInspection") - @Inject - public DynDnsRouter(@All List dynDnsList) { - this.dynDnsList = dynDnsList; - } - - public DynDns get(String name) { - return dynDnsList.stream() - .filter(it -> it.name().equals(name)) - .findFirst() - .orElseThrow(() -> new IllegalStateException("Unknown DynDns provider")); - } -} diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistry.java b/src/main/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistry.java new file mode 100644 index 0000000..d70b8b2 --- /dev/null +++ b/src/main/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistry.java @@ -0,0 +1,30 @@ +package de.rpr.ddnsclient.dyndns; + +import io.quarkus.arc.All; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@ApplicationScoped +public class DyndnsProviderRegistry { + + private final Map dynDnsMap; + + @SuppressWarnings("CdiInjectionPointsInspection") + public DyndnsProviderRegistry(@All List dynDnsList) { + if (dynDnsList == null || dynDnsList.isEmpty()) { + throw new IllegalStateException("No dyndns services implemented1"); + } + this.dynDnsMap = dynDnsList.stream().collect(Collectors.toMap(DynDns::name, dynDns -> dynDns)); + } + + public DynDns get(String name) { + if (!dynDnsMap.containsKey(name)) { + throw new IllegalStateException("Unknown DynDns provider"); + } + return dynDnsMap.get(name); + } +} diff --git a/src/test/java/de/rpr/ddnsclient/UpdaterTest.java b/src/test/java/de/rpr/ddnsclient/UpdaterTest.java index 3df0d6c..1cb737c 100644 --- a/src/test/java/de/rpr/ddnsclient/UpdaterTest.java +++ b/src/test/java/de/rpr/ddnsclient/UpdaterTest.java @@ -2,7 +2,7 @@ package de.rpr.ddnsclient; import de.rpr.ddnsclient.dyndns.Ddnss; import de.rpr.ddnsclient.dyndns.DynDns; -import de.rpr.ddnsclient.dyndns.DynDnsRouter; +import de.rpr.ddnsclient.dyndns.DyndnsProviderRegistry; import de.rpr.ddnsclient.lookup.DnsResolver; import de.rpr.ddnsclient.lookup.PublicIpLookup; import de.rpr.ddnsclient.model.Config; @@ -25,7 +25,7 @@ import static org.mockito.Mockito.*; class UpdaterTest { @Mock - DynDnsRouter dynDnsRouter; + DyndnsProviderRegistry dyndnsProviderRegistry; @Mock PublicIpLookup publicIpLookup; @Mock @@ -40,7 +40,7 @@ class UpdaterTest { @Test void should_throw_exception_if_config_is_empty() { Config config = new Config(); - Updater updater = new Updater(dynDnsRouter, publicIpLookup, dnsResolver, config, backoff); + Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, config, backoff); Assertions.assertThrows(IllegalStateException.class, updater::run); } @@ -56,7 +56,7 @@ class UpdaterTest { add(new Value("ddnss", "host2.example.org", null, null, "token")); }}; - Updater updater = new Updater(dynDnsRouter, publicIpLookup, dnsResolver, multipleConfigEntries, backoff); + Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, multipleConfigEntries, backoff); updater.run(); ArgumentCaptor hostnameCaptor = ArgumentCaptor.forClass(String.class); @@ -74,10 +74,10 @@ class UpdaterTest { when(publicIpLookup.get()).thenReturn(ips); when(dnsResolver.resolve("example.org")).thenReturn(ips); - Updater updater = new Updater(dynDnsRouter, publicIpLookup, dnsResolver, ddnssConfig, backoff); + Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, ddnssConfig, backoff); updater.run(); - verify(dynDnsRouter, never()).get(any()); + verify(dyndnsProviderRegistry, never()).get(any()); } @@ -86,13 +86,13 @@ class UpdaterTest { DynDns ddnss = mock(Ddnss.class); - when(dynDnsRouter.get("ddnss")).thenReturn(ddnss); + when(dyndnsProviderRegistry.get("ddnss")).thenReturn(ddnss); IPs publicIps = new IPs("ipv4", "ipv6"); when(publicIpLookup.get()).thenReturn(publicIps); when(dnsResolver.resolve("example.org")).thenReturn(new IPs("registered_ipv4", "registered_ipv6")); - Updater updater = new Updater(dynDnsRouter, publicIpLookup, dnsResolver, ddnssConfig, backoff); + Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, ddnssConfig, backoff); updater.run(); verify(ddnss).update( @@ -107,13 +107,13 @@ class UpdaterTest { DynDns ddnss = mock(Ddnss.class); - when(dynDnsRouter.get("ddnss")).thenReturn(ddnss); + when(dyndnsProviderRegistry.get("ddnss")).thenReturn(ddnss); IPs publicIps = new IPs("ipv4", "ipv6"); when(publicIpLookup.get()).thenReturn(publicIps); when(dnsResolver.resolve("example.org")).thenReturn(new IPs("registered_ipv4", "registered_ipv6")); - Updater updater = new Updater(dynDnsRouter, publicIpLookup, dnsResolver, ddnssConfig, backoff); + Updater updater = new Updater(dyndnsProviderRegistry, publicIpLookup, dnsResolver, ddnssConfig, backoff); updater.run(); updater.run(); diff --git a/src/test/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistryTest.java b/src/test/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistryTest.java new file mode 100644 index 0000000..863a8e5 --- /dev/null +++ b/src/test/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistryTest.java @@ -0,0 +1,29 @@ +package de.rpr.ddnsclient.dyndns; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +class DyndnsProviderRegistryTest { + + @Test + void empty_dyndns_list_should_throw_exception() { + assertThatExceptionOfType(IllegalStateException.class) + .isThrownBy(() -> new DyndnsProviderRegistry(List.of())); + } + + @Test + void existing_dyndns_provider_should_be_returned() { + DyndnsProviderRegistry registry = new DyndnsProviderRegistry(List.of(new Ddnss())); + assertThat(registry.get("ddnss")).isNotNull(); + } + + @Test + void unknown_dyndns_provider_should_throw_exception() { + DyndnsProviderRegistry registry = new DyndnsProviderRegistry(List.of(new Ddnss())); + assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> registry.get("unknown")); + } +} \ No newline at end of file