Restructuring and add tests for DDNSS update
All checks were successful
ci/woodpecker/push/build Pipeline was successful
All checks were successful
ci/woodpecker/push/build Pipeline was successful
This commit is contained in:
parent
fcac4fb75b
commit
4826805f22
4 changed files with 91 additions and 9 deletions
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
61
src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java
Normal file
61
src/test/java/de/rpr/ddnsclient/dyndns/DdnssTest.java
Normal 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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue