Restructuring and unit tests
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
13ec4b769f
commit
b49d62780d
3 changed files with 75 additions and 13 deletions
|
@ -4,9 +4,16 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import de.rpr.ddnsclient.model.Config;
|
import de.rpr.ddnsclient.model.Config;
|
||||||
import jakarta.enterprise.context.ApplicationScoped;
|
import jakarta.enterprise.context.ApplicationScoped;
|
||||||
import jakarta.ws.rs.Produces;
|
import jakarta.ws.rs.Produces;
|
||||||
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
|
|
||||||
|
import javax.naming.Context;
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
import javax.naming.directory.DirContext;
|
||||||
|
import javax.naming.directory.InitialDirContext;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
public class AppConfig {
|
public class AppConfig {
|
||||||
|
|
||||||
|
@ -17,4 +24,14 @@ public class AppConfig {
|
||||||
return objectMapper.readValue(configFile, Config.class);
|
return objectMapper.readValue(configFile, Config.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Produces
|
||||||
|
@ApplicationScoped
|
||||||
|
public DirContext dirContext(
|
||||||
|
@ConfigProperty(name = "ddnsclient.dns.resolver", defaultValue = "9.9.9.9") String dnsServer)
|
||||||
|
throws NamingException {
|
||||||
|
Properties env = new Properties();
|
||||||
|
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
|
||||||
|
env.setProperty(Context.PROVIDER_URL, MessageFormat.format("dns://{0}", dnsServer));
|
||||||
|
return new InitialDirContext(env);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,13 @@
|
||||||
package de.rpr.ddnsclient.lookup;
|
package de.rpr.ddnsclient.lookup;
|
||||||
|
|
||||||
|
|
||||||
import de.rpr.ddnsclient.dyndns.Ddnss;
|
|
||||||
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 javax.naming.Context;
|
|
||||||
import javax.naming.NamingException;
|
import javax.naming.NamingException;
|
||||||
import javax.naming.directory.Attributes;
|
import javax.naming.directory.Attributes;
|
||||||
import javax.naming.directory.DirContext;
|
import javax.naming.directory.DirContext;
|
||||||
import javax.naming.directory.InitialDirContext;
|
|
||||||
import java.text.MessageFormat;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
@ApplicationScoped
|
@ApplicationScoped
|
||||||
public class DnsResolver {
|
public class DnsResolver {
|
||||||
|
@ -21,11 +15,10 @@ public class DnsResolver {
|
||||||
private static final Logger log = Logger.getLogger(DnsResolver.class);
|
private static final Logger log = Logger.getLogger(DnsResolver.class);
|
||||||
|
|
||||||
public static final int MAX_RETRIES = 5;
|
public static final int MAX_RETRIES = 5;
|
||||||
private final Properties env = new Properties();
|
private final DirContext dirContext;
|
||||||
|
|
||||||
public DnsResolver(@ConfigProperty(name = "ddnsclient.dns.resolver", defaultValue = "9.9.9.9") String dnsServer) {
|
DnsResolver(DirContext dirContext) {
|
||||||
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
|
this.dirContext = dirContext;
|
||||||
env.setProperty(Context.PROVIDER_URL, MessageFormat.format("dns://{0}", dnsServer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IPs resolve(String hostname) {
|
public IPs resolve(String hostname) {
|
||||||
|
@ -34,16 +27,15 @@ public class DnsResolver {
|
||||||
|
|
||||||
int resolveCounter = 0;
|
int resolveCounter = 0;
|
||||||
try {
|
try {
|
||||||
DirContext context = new InitialDirContext((env));
|
|
||||||
|
|
||||||
Attributes result;
|
Attributes result;
|
||||||
do {
|
do {
|
||||||
if (resolveCounter == MAX_RETRIES) {
|
if (resolveCounter == MAX_RETRIES) {
|
||||||
throw new RuntimeException("Couldn't resolve " + hostname);
|
throw new IllegalStateException("Couldn't resolve all attributes");
|
||||||
}
|
}
|
||||||
resolveCounter++;
|
resolveCounter++;
|
||||||
log.tracef("Try #%d", resolveCounter);
|
log.tracef("Try #%d", resolveCounter);
|
||||||
result = context.getAttributes(hostname, new String[]{"A", "AAAA"});
|
result = dirContext.getAttributes(hostname, new String[]{"A", "AAAA"});
|
||||||
} while (result.size() != 2);
|
} while (result.size() != 2);
|
||||||
|
|
||||||
return new IPs(
|
return new IPs(
|
||||||
|
|
53
src/test/java/de/rpr/ddnsclient/lookup/DnsResolverTest.java
Normal file
53
src/test/java/de/rpr/ddnsclient/lookup/DnsResolverTest.java
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
package de.rpr.ddnsclient.lookup;
|
||||||
|
|
||||||
|
import de.rpr.ddnsclient.model.IPs;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import javax.naming.NamingException;
|
||||||
|
import javax.naming.directory.BasicAttributes;
|
||||||
|
import javax.naming.directory.DirContext;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class DnsResolverTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
DirContext dirContext;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_throw_exception_if_resolving_failes_repeatedly() throws NamingException {
|
||||||
|
|
||||||
|
when(dirContext.getAttributes(anyString(), any())).thenReturn(new BasicAttributes());
|
||||||
|
|
||||||
|
DnsResolver dnsResolver = new DnsResolver(dirContext);
|
||||||
|
|
||||||
|
assertThatExceptionOfType(IllegalStateException.class)
|
||||||
|
.isThrownBy(() -> dnsResolver.resolve("example.com"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void should_return_ips() throws NamingException {
|
||||||
|
|
||||||
|
BasicAttributes attributes = new BasicAttributes() {{
|
||||||
|
put("A", "ipv4");
|
||||||
|
put("AAAA", "ipv6");
|
||||||
|
}};
|
||||||
|
when(dirContext.getAttributes(anyString(), any())).thenReturn(attributes);
|
||||||
|
|
||||||
|
DnsResolver dnsResolver = new DnsResolver(dirContext);
|
||||||
|
|
||||||
|
IPs result = dnsResolver.resolve("example.com");
|
||||||
|
|
||||||
|
assertThat(result.v4()).isEqualTo("ipv4");
|
||||||
|
assertThat(result.v6()).isEqualTo("ipv6");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue