package net.windwards.dnsfrontend;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.Assert;
import net.windwards.dnsfrontend.api.ConfigurationException;
import net.windwards.dnsfrontend.api.NoSuchDomainException;
import org.junit.Test;
import org.xbill.DNS.Name;
import java.io.IOException;
import java.io.StringReader;
public class TestConfigurationImpl {
private ConfigurationImpl mkConfiguration(String confdata) throws IOException {
JsonFactory factory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(factory);
StringReader stream = new StringReader(confdata);
return mapper.readValue(stream, ConfigurationImpl.class);
}
@Test
public void somethingShouldBeRequired() throws Exception {
String confdata = "{ }";
ConfigurationImpl conf = this.mkConfiguration(confdata);
try {
conf.initialize();
Assert.fail("Some config parameter should be required");
} catch (ConfigurationException ignored) {}
}
@Test
public void domainLookup() throws Exception {
String confdata = "{ \"domains\": { " +
"\"example.com\": { \"ttl\": 1800, \"moniker\": \"foo\" }," +
"\"windwards.net\": { \"ttl\": 1799, \"moniker\": \"bar\" } " +
"} }";
ConfigurationImpl conf = this.mkConfiguration(confdata);
conf.initialize();
Assert.assertEquals("foo", conf.monikerFromName(new Name("example.com.")));
Assert.assertEquals(new Name("host.example.com."), conf.qualifiedDomainName("host", "foo"));
Assert.assertEquals(1799, conf.getTtl("bar"));
}
@Test
public void failOnUnknownDomain() throws Exception {
String confdata = "{ \"domains\": { " +
"\"example.com\": { \"ttl\": 1800, \"moniker\": \"foo\" }," +
"\"windwards.net\": { \"ttl\": 1799, \"moniker\": \"bar\" } " +
"} }";
ConfigurationImpl conf = this.mkConfiguration(confdata);
conf.initialize();
try {
conf.monikerFromName(new Name("blah.com."));
Assert.fail("Fail on unknown domain");
} catch (NoSuchDomainException ignored) {}
}
}