Restructuring and add tests for DDNSS update
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
Ryan Harg 2024-11-27 14:34:40 +01:00
parent fcac4fb75b
commit 4826805f22
4 changed files with 91 additions and 9 deletions

View file

@ -3,7 +3,6 @@ package de.rpr.ddnsclient.dyndns;
import de.rpr.ddnsclient.model.DyndnsAuth; import de.rpr.ddnsclient.model.DyndnsAuth;
import de.rpr.ddnsclient.model.IPs; import de.rpr.ddnsclient.model.IPs;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import java.io.IOException; import java.io.IOException;
@ -12,16 +11,18 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
@ApplicationScoped @ApplicationScoped
public class Ddnss implements DynDns { public class Ddnss implements DynDns {
private static final Logger log = Logger.getLogger(Ddnss.class); private static final Logger log = Logger.getLogger(Ddnss.class);
private final HttpClientFactory httpClientFactory;
public Ddnss(HttpClientFactory httpClientFactory) {
this.httpClientFactory = httpClientFactory;
}
@Override @Override
public String name() { public String name() {
return "ddnss"; 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}", String updateUrl = MessageFormat.format("https://ddnss.de/upd.php?key={0}&host={1}&ip={2}&ip6={3}",
auth.token(), hostname, currentIps.v4(), currentIps.v6()); auth.token(), hostname, currentIps.v4(), currentIps.v6());
try (HttpClient http = HttpClient.newHttpClient()) { try (HttpClient http = httpClientFactory.create()) {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.header("User-Agent", "ddnss-client") .header("User-Agent", "ddns-client")
.GET() .GET()
.uri(URI.create(updateUrl)) .uri(URI.create(updateUrl))
.build(); .build();

View file

@ -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();
}
}

View file

@ -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<String> 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.<HttpResponse.BodyHandler<String>>any())).thenReturn(httpResponse);
Ddnss ddnss = new Ddnss(httpClientFactory);
ddnss.update(
"example.com",
new IPs("ipv4", "ipv6"),
new DyndnsAuth(null, null, "token")
);
ArgumentCaptor<HttpRequest> 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");
}
}

View file

@ -1,14 +1,21 @@
package de.rpr.ddnsclient.dyndns; package de.rpr.ddnsclient.dyndns;
import org.junit.jupiter.api.Test; 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 java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@ExtendWith(MockitoExtension.class)
class DyndnsProviderRegistryTest { class DyndnsProviderRegistryTest {
@Mock
HttpClientFactory httpClientFactory;
@Test @Test
void empty_dyndns_list_should_throw_exception() { void empty_dyndns_list_should_throw_exception() {
assertThatExceptionOfType(IllegalStateException.class) assertThatExceptionOfType(IllegalStateException.class)
@ -17,13 +24,13 @@ class DyndnsProviderRegistryTest {
@Test @Test
void existing_dyndns_provider_should_be_returned() { 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(); assertThat(registry.get("ddnss")).isNotNull();
} }
@Test @Test
void unknown_dyndns_provider_should_throw_exception() { 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")); assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> registry.get("unknown"));
} }
} }