diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java b/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java index bf12efa..1483500 100644 --- a/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java +++ b/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java @@ -3,7 +3,6 @@ package de.rpr.ddnsclient.dyndns; import de.rpr.ddnsclient.model.DyndnsAuth; import de.rpr.ddnsclient.model.IPs; import jakarta.enterprise.context.ApplicationScoped; -import org.eclipse.microprofile.config.inject.ConfigProperty; import org.jboss.logging.Logger; import java.io.IOException; @@ -12,16 +11,18 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.text.MessageFormat; -import java.time.Duration; -import java.time.LocalDateTime; -import java.util.HashMap; -import java.util.Map; @ApplicationScoped public class Ddnss implements DynDns { private static final Logger log = Logger.getLogger(Ddnss.class); + private final HttpClientFactory httpClientFactory; + + public Ddnss(HttpClientFactory httpClientFactory) { + this.httpClientFactory = httpClientFactory; + } + @Override public String name() { return "ddnss"; @@ -32,10 +33,10 @@ public class Ddnss implements DynDns { String updateUrl = MessageFormat.format("https://ddnss.de/upd.php?key={0}&host={1}&ip={2}&ip6={3}", auth.token(), hostname, currentIps.v4(), currentIps.v6()); - try (HttpClient http = HttpClient.newHttpClient()) { + try (HttpClient http = httpClientFactory.create()) { HttpRequest request = HttpRequest.newBuilder() - .header("User-Agent", "ddnss-client") + .header("User-Agent", "ddns-client") .GET() .uri(URI.create(updateUrl)) .build(); diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/HttpClientFactory.java b/src/main/java/de/rpr/ddnsclient/dyndns/HttpClientFactory.java new file mode 100644 index 0000000..db34506 --- /dev/null +++ b/src/main/java/de/rpr/ddnsclient/dyndns/HttpClientFactory.java @@ -0,0 +1,13 @@ +package de.rpr.ddnsclient.dyndns; + +import jakarta.enterprise.context.ApplicationScoped; + +import java.net.http.HttpClient; + +@ApplicationScoped +public class HttpClientFactory { + + HttpClient create() { + return HttpClient.newHttpClient(); + } +} diff --git a/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java b/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java new file mode 100644 index 0000000..1ff5779 --- /dev/null +++ b/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java @@ -0,0 +1,61 @@ +package de.rpr.ddnsclient.dyndns; + +import de.rpr.ddnsclient.model.DyndnsAuth; +import de.rpr.ddnsclient.model.IPs; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +class DdnssTest { + + @Mock + HttpClientFactory httpClientFactory; + @Mock + HttpClient httpClient; + @Mock + HttpResponse httpResponse; + + @Test + void should_have_correct_name() { + assertThat(new Ddnss(httpClientFactory).name()).isEqualTo("ddnss"); + } + + @Test + void should_send_correct_request() throws IOException, InterruptedException { + when(httpClientFactory.create()).thenReturn(httpClient); + when(httpClient.send(any(), ArgumentMatchers.>any())).thenReturn(httpResponse); + Ddnss ddnss = new Ddnss(httpClientFactory); + + ddnss.update( + "example.com", + new IPs("ipv4", "ipv6"), + new DyndnsAuth(null, null, "token") + ); + + ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(HttpRequest.class); + verify(httpClient).send(requestCaptor.capture(),any()); + + HttpRequest capturedRequest = requestCaptor.getValue(); + + assertThat(capturedRequest.uri().toString()) + .isEqualTo("https://ddnss.de/upd.php?key=token&host=example.com&ip=ipv4&ip6=ipv6"); + + assertThat(capturedRequest.headers() + .allValues("User-Agent")).containsExactly("ddns-client"); + } + +} \ No newline at end of file diff --git a/src/test/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistryTest.java b/src/test/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistryTest.java index 863a8e5..0cd0163 100644 --- a/src/test/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistryTest.java +++ b/src/test/java/de/rpr/ddnsclient/dyndns/DyndnsProviderRegistryTest.java @@ -1,14 +1,21 @@ package de.rpr.ddnsclient.dyndns; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +@ExtendWith(MockitoExtension.class) class DyndnsProviderRegistryTest { + @Mock + HttpClientFactory httpClientFactory; + @Test void empty_dyndns_list_should_throw_exception() { assertThatExceptionOfType(IllegalStateException.class) @@ -17,13 +24,13 @@ class DyndnsProviderRegistryTest { @Test void existing_dyndns_provider_should_be_returned() { - DyndnsProviderRegistry registry = new DyndnsProviderRegistry(List.of(new Ddnss())); + DyndnsProviderRegistry registry = new DyndnsProviderRegistry(List.of(new Ddnss(httpClientFactory))); assertThat(registry.get("ddnss")).isNotNull(); } @Test void unknown_dyndns_provider_should_throw_exception() { - DyndnsProviderRegistry registry = new DyndnsProviderRegistry(List.of(new Ddnss())); + DyndnsProviderRegistry registry = new DyndnsProviderRegistry(List.of(new Ddnss(httpClientFactory))); assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> registry.get("unknown")); } } \ No newline at end of file