From d8f547d1e78af2ba09b32813d569fb5470758388 Mon Sep 17 00:00:00 2001 From: Ryan Harg Date: Fri, 29 Nov 2024 14:32:33 +0100 Subject: [PATCH] Update tests --- .../java/de/rpr/ddnsclient/dyndns/Ddnss.java | 4 +- .../de/rpr/ddnsclient/dyndns/DuckDNS.java | 2 + .../de/rpr/ddnsclient/lookup/DnsJava.java | 2 +- .../de/rpr/ddnsclient/dyndns/DdnssTest.java | 43 +++++++- .../de/rpr/ddnsclient/dyndns/DuckDNSTest.java | 102 ++++++++++++++++++ 5 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 src/test/java/de/rpr/ddnsclient/dyndns/DuckDNSTest.java diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java b/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java index 2c41687..3ec5e62 100644 --- a/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java +++ b/src/main/java/de/rpr/ddnsclient/dyndns/Ddnss.java @@ -45,7 +45,9 @@ public class Ddnss implements DynDns { if (currentIps.v4().isPresent() && currentIps.v6().isPresent()) { return MessageFormat.format("https://ddnss.de/upd.php?key={0}&host={1}&ip={2}&ip6={3}", token, hostname, currentIps.v4().get(), currentIps.v6().get()); } else if (currentIps.v4().isPresent()) { - return MessageFormat.format("https://ddnss.de/upd.php?key={0}&host={1}&ip={2}", token, hostname, currentIps.v4()); + return MessageFormat.format("https://ddnss.de/upd.php?key={0}&host={1}&ip={2}", token, hostname, currentIps.v4().get()); + } else if (currentIps.v6().isPresent()) { + return MessageFormat.format("https://ddnss.de/upd.php?key={0}&host={1}&ip6={2}", token, hostname, currentIps.v6().get()); } throw new RuntimeException("No ips to update!"); } diff --git a/src/main/java/de/rpr/ddnsclient/dyndns/DuckDNS.java b/src/main/java/de/rpr/ddnsclient/dyndns/DuckDNS.java index 875e565..b8c8658 100644 --- a/src/main/java/de/rpr/ddnsclient/dyndns/DuckDNS.java +++ b/src/main/java/de/rpr/ddnsclient/dyndns/DuckDNS.java @@ -49,6 +49,8 @@ public class DuckDNS implements DynDns { return MessageFormat.format("https://www.duckdns.org/update?domains={0}&token={1}&ip={2}&ipv6={3}", hostname, token, currentIps.v4().get(), currentIps.v6().get()); } else if (currentIps.v4().isPresent()) { return MessageFormat.format("https://www.duckdns.org/update?domains={0}&token={1}&ip={2}", hostname, token, currentIps.v4().get()); + } else if (currentIps.v6().isPresent()) { + return MessageFormat.format("https://www.duckdns.org/update?domains={0}&token={1}&ipv6={2}", hostname, token, currentIps.v6().get()); } throw new RuntimeException("No ips to update!"); } diff --git a/src/main/java/de/rpr/ddnsclient/lookup/DnsJava.java b/src/main/java/de/rpr/ddnsclient/lookup/DnsJava.java index 1f1e19c..485fc73 100644 --- a/src/main/java/de/rpr/ddnsclient/lookup/DnsJava.java +++ b/src/main/java/de/rpr/ddnsclient/lookup/DnsJava.java @@ -36,7 +36,7 @@ public class DnsJava implements DnsResolver { Record[] records = lookup.run(); if (records == null || records.length == 0) { - log.infof("No record for type %s found", type.name()); + log.infof("Hostname %s has no record for type %s found", hostname, type.name()); return Optional.empty(); } diff --git a/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java b/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java index 1ff5779..6ea0323 100644 --- a/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java +++ b/src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java @@ -47,7 +47,7 @@ class DdnssTest { ); ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(HttpRequest.class); - verify(httpClient).send(requestCaptor.capture(),any()); + verify(httpClient).send(requestCaptor.capture(), any()); HttpRequest capturedRequest = requestCaptor.getValue(); @@ -58,4 +58,45 @@ class DdnssTest { .allValues("User-Agent")).containsExactly("ddns-client"); } + @Test + void should_send_correct_request_if_only_ipv4_is_present() throws IOException, InterruptedException { + when(httpClientFactory.create()).thenReturn(httpClient); + when(httpClient.send(any(), ArgumentMatchers.>any())).thenReturn(httpResponse); + Ddnss dynDns = new Ddnss(httpClientFactory); + + dynDns.update( + "example.com", + new IPs("ipv4", null), + 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"); + } + + @Test + void should_send_correct_request_if_only_ipv6_is_present() throws IOException, InterruptedException { + when(httpClientFactory.create()).thenReturn(httpClient); + when(httpClient.send(any(), ArgumentMatchers.>any())).thenReturn(httpResponse); + Ddnss dynDns = new Ddnss(httpClientFactory); + + dynDns.update( + "example.com", + new IPs(null, "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&ip6=ipv6"); + } } \ No newline at end of file diff --git a/src/test/java/de/rpr/ddnsclient/dyndns/DuckDNSTest.java b/src/test/java/de/rpr/ddnsclient/dyndns/DuckDNSTest.java new file mode 100644 index 0000000..0c361a4 --- /dev/null +++ b/src/test/java/de/rpr/ddnsclient/dyndns/DuckDNSTest.java @@ -0,0 +1,102 @@ +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 DuckDNSTest { + + @Mock + HttpClientFactory httpClientFactory; + @Mock + HttpClient httpClient; + @Mock + HttpResponse httpResponse; + + @Test + void should_have_correct_name() { + assertThat(new DuckDNS(httpClientFactory).name()).isEqualTo("duckdns"); + } + + @Test + void should_send_correct_request_if_ipv4_and_ipv6_are_present() throws IOException, InterruptedException { + when(httpClientFactory.create()).thenReturn(httpClient); + when(httpClient.send(any(), ArgumentMatchers.>any())).thenReturn(httpResponse); + DuckDNS dynDns = new DuckDNS(httpClientFactory); + + dynDns.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://www.duckdns.org/update?domains=example.com&token=token&ip=ipv4&ipv6=ipv6"); + + assertThat(capturedRequest.headers() + .allValues("User-Agent")).containsExactly("ddns-client"); + } + + @Test + void should_send_correct_request_if_only_ipv4_is_present() throws IOException, InterruptedException { + when(httpClientFactory.create()).thenReturn(httpClient); + when(httpClient.send(any(), ArgumentMatchers.>any())).thenReturn(httpResponse); + DuckDNS dynDns = new DuckDNS(httpClientFactory); + + dynDns.update( + "example.com", + new IPs("ipv4", null), + 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://www.duckdns.org/update?domains=example.com&token=token&ip=ipv4"); + } + + @Test + void should_send_correct_request_if_only_ipv6_is_present() throws IOException, InterruptedException { + when(httpClientFactory.create()).thenReturn(httpClient); + when(httpClient.send(any(), ArgumentMatchers.>any())).thenReturn(httpResponse); + DuckDNS dynDns = new DuckDNS(httpClientFactory); + + dynDns.update( + "example.com", + new IPs(null, "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://www.duckdns.org/update?domains=example.com&token=token&ipv6=ipv6"); + } +} \ No newline at end of file