package net.ripe.db.whois.common.rpsl.attrs;

import net.ripe.db.whois.common.ip.Ipv4Resource;
import net.ripe.db.whois.common.ip.Ipv6Resource;
import org.junit.Test;

import static net.ripe.db.whois.common.domain.CIString.ciString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

public class NServerTest {
    @Test(expected = AttributeParseException.class)
    public void empty() {
        NServer.parse("");
    }

    @Test(expected = AttributeParseException.class)
    public void hostname_invalid() {
        NServer.parse("$");
    }

    @Test
    public void hostname_only() {
        final NServer nServer = NServer.parse("dns.comcor.ru");
        assertThat(nServer.getHostname(), is(ciString("dns.comcor.ru")));
        assertNull(nServer.getIpInterval());
        assertThat(nServer.toString(), is("dns.comcor.ru"));
    }

    @Test
    public void hostname_trailing_dot() {
        final NServer nServer = NServer.parse("dns.comcor.ru.");
        assertThat(nServer.getHostname(), is(ciString("dns.comcor.ru")));
        assertNull(nServer.getIpInterval());
        assertThat(nServer.toString(), is("dns.comcor.ru"));
    }

    @Test
    public void hostname_and_ipv4() {
        final NServer nServer = NServer.parse("dns.comcor.ru 194.0.0.0");
        assertThat(nServer.getHostname(), is(ciString("dns.comcor.ru")));
        assertThat((Ipv4Resource) nServer.getIpInterval(), is(Ipv4Resource.parse("194.0.0.0")));
        assertThat(nServer.toString(), is("dns.comcor.ru 194.0.0.0"));
    }

    @Test(expected = AttributeParseException.class)
    public void hostname_and_ipv4_range_24() {
        NServer.parse("dns.comcor.ru 194.0.0.0/24");
    }

    @Test
    public void hostname_and_ipv4_range_32() {
        final NServer nServer = NServer.parse("dns.comcor.ru 194.0.0.0/32");
        assertThat(nServer.getHostname(), is(ciString("dns.comcor.ru")));
        assertThat((Ipv4Resource) nServer.getIpInterval(), is(Ipv4Resource.parse("194.0.0.0")));
        assertThat(nServer.toString(), is("dns.comcor.ru 194.0.0.0"));
    }

    @Test
    public void hostname_trailing_dot_and_ipv4() {
        final NServer nServer = NServer.parse("dns.comcor.ru. 194.0.0.0");
        assertThat(nServer.getHostname(), is(ciString("dns.comcor.ru")));
        assertThat((Ipv4Resource) nServer.getIpInterval(), is(Ipv4Resource.parse("194.0.0.0")));
        assertThat(nServer.toString(), is("dns.comcor.ru 194.0.0.0"));
    }

    @Test(expected = AttributeParseException.class)
    public void hostname_and_ipv4_list() {
        NServer.parse("dns.comcor.ru 194.0.0.0 194.0.0.0");
    }

    @Test
    public void ipv4_only() {
        final NServer nServer = NServer.parse("194.0.0.0");
        assertThat(nServer.getHostname(), is(ciString("194.0.0.0")));
        assertNull(nServer.getIpInterval());
        assertThat(nServer.toString(), is("194.0.0.0"));
    }

    @Test
    public void hostname_and_ipv6() {
        final NServer nServer = NServer.parse("dns.comcor.ru f::1");
        assertThat(nServer.getHostname(), is(ciString("dns.comcor.ru")));
        assertThat((Ipv6Resource) nServer.getIpInterval(), is(Ipv6Resource.parse("f::1")));
        assertThat(nServer.toString(), is("dns.comcor.ru f::1"));
    }

    @Test(expected = AttributeParseException.class)
    public void hostname_and_invalid_ip() {
        NServer.parse("dns.comcor.ru dns.comcor.ru");
    }
}

@@ -133,24 +133,25 @@
 v6Resource;%0A
+%0A
 import org.j
@@ -267,16 +267,60 @@
 ers.is;%0A
+import static org.junit.Assert.assertFalse;%0A
 import s
@@ -401,40 +401,902 @@
 at;%0A
-%0Apublic class NServerTest %7B%0A    
+import static org.junit.Assert.assertTrue;%0A%0Apublic class NServerTest %7B%0A    %0A%09private static final NServer nServer1_1 = NServer.parse(%22dns.comcor.ru 194.0.0.0%22);%0A%09private static final NServer nServer1_2 = NServer.parse(%22dns.comcor.ru 194.0.0.0%22);%0A%09private static final NServer nServer2 = NServer.parse(%22dns.somedomain.com 200.0.0.0%22);%0A%09%0A%09@Test%0A%09public void testEquals() %7B%0A%09%09assertTrue(%22Equivalent NServer objects should be equal%22, nServer1_1.equals(nServer1_2) && nServer1_2.equals(nServer1_1));%0A%09%09assertFalse(%22Differing NServer objects should be unequal%22, nServer1_1.equals(nServer2) %7C%7C nServer2.equals(nServer1_2));%0A%09%7D%0A%09%0A%09@Test%0A%09public void testHashCode() %7B%0A%09%09assertTrue(%22Equivalent NServers should have matching hashcodes%22, nServer1_1.hashCode()==nServer1_2.hashCode());%0A%09%09assertFalse(%22Differing NServers should have different hashcodes%22, nServer1_1.hashCode()==nServer2.hashCode());%0A%09%7D%0A%09%0A%09%0A%09
 @Tes

Add basic tests for new code in NServer