Search in sources :

Example 6 with JUnitDNSServer

use of org.opennms.core.test.dns.annotations.JUnitDNSServer in project opennms by OpenNMS.

the class DnsRequisitionUrlConnectionIT method dwoUrlAsResource.

@Test
@JUnitDNSServer(port = 9153, zones = { @DNSZone(name = "example.com", entries = { @DNSEntry(hostname = "www", address = "72.14.204.99") }) })
public void dwoUrlAsResource() throws IOException, JAXBException {
    Resource resource = new UrlResource(TEST_URL);
    Assert.assertEquals(TEST_URL, resource.getURL().toString());
    Requisition req = null;
    Assert.assertNotNull(resource);
    InputStream resourceStream = resource.getInputStream();
    JAXBContext context = JAXBContext.newInstance(Requisition.class);
    Unmarshaller um = context.createUnmarshaller();
    um.setSchema(null);
    req = (Requisition) um.unmarshal(resourceStream);
    Assert.assertEquals("should have 2 A records: 1 for example.com, and 1 for www.example.com", 2, req.getNodeCount());
    resourceStream.close();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) InputStream(java.io.InputStream) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) Test(org.junit.Test)

Example 7 with JUnitDNSServer

use of org.opennms.core.test.dns.annotations.JUnitDNSServer in project opennms by OpenNMS.

the class DnsRequisitionUrlConnectionIT method dwoConnect.

/**
     * Just test that connection doesn't get implementation without fixing this test
     * @throws IOException
     */
@Test
@JUnitDNSServer(port = 9153, zones = { @DNSZone(name = "example.com", entries = { @DNSEntry(hostname = "www", address = "72.14.204.99") }) })
public void dwoConnect() throws IOException {
    URLConnection c = new DnsRequisitionUrlConnection(new URL(TEST_URL));
    c.connect();
}
Also used : URLConnection(java.net.URLConnection) URL(java.net.URL) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) Test(org.junit.Test)

Example 8 with JUnitDNSServer

use of org.opennms.core.test.dns.annotations.JUnitDNSServer in project opennms by OpenNMS.

the class DnsRequisitionUrlConnectionIT method dwoGetInputStream.

@Test
@JUnitDNSServer(port = 9153, zones = { @DNSZone(name = "example.com", entries = { @DNSEntry(hostname = "www", address = "72.14.204.99") }) })
public void dwoGetInputStream() throws IOException {
    URLConnection c = new DnsRequisitionUrlConnection(new URL(TEST_URL));
    InputStream s = c.getInputStream();
    Assert.assertNotNull(s);
    s.close();
}
Also used : InputStream(java.io.InputStream) URLConnection(java.net.URLConnection) URL(java.net.URL) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) Test(org.junit.Test)

Example 9 with JUnitDNSServer

use of org.opennms.core.test.dns.annotations.JUnitDNSServer in project opennms by OpenNMS.

the class JUnitDNSServerExecutionListener method beforeTestMethod.

/** {@inheritDoc} */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
    super.beforeTestMethod(testContext);
    final JUnitDNSServer config = findTestAnnotation(JUnitDNSServer.class, testContext);
    if (config == null) {
        return;
    }
    LOG.info("initializing DNS on port {}", config.port());
    m_server = new DNSServer();
    m_server.addPort(config.port());
    for (final DNSZone dnsZone : config.zones()) {
        String name = dnsZone.name();
        if (!name.endsWith(".")) {
            name = name + ".";
        }
        final Name zoneName = Name.fromString(name, Name.root);
        LOG.debug("zoneName = {}", zoneName);
        final Zone zone = new Zone(zoneName, new Record[] { new SOARecord(zoneName, DClass.IN, DEFAULT_TTL, zoneName, Name.fromString("admin." + name), 1, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver1.opendns.com.")), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver2.opendns.com.")), new ARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v4address())), new AAAARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v6address())) });
        LOG.debug("zone = {}", zone);
        for (final DNSEntry entry : dnsZone.entries()) {
            LOG.debug("adding entry: {}", entry);
            String hostname = entry.hostname();
            final Name recordName = Name.fromString(hostname, zoneName);
            LOG.debug("name = {}", recordName);
            if (entry.ipv6()) {
                zone.addRecord(new AAAARecord(recordName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(entry.address())));
            } else {
                zone.addRecord(new ARecord(recordName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(entry.address())));
            }
        }
        m_server.addZone(zone);
    }
    LOG.debug("starting DNS server");
    m_server.start();
}
Also used : DNSEntry(org.opennms.core.test.dns.annotations.DNSEntry) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) SOARecord(org.xbill.DNS.SOARecord) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) AAAARecord(org.xbill.DNS.AAAARecord) Zone(org.xbill.DNS.Zone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) NSRecord(org.xbill.DNS.NSRecord) SOARecord(org.xbill.DNS.SOARecord) Name(org.xbill.DNS.Name)

Example 10 with JUnitDNSServer

use of org.opennms.core.test.dns.annotations.JUnitDNSServer in project opennms by OpenNMS.

the class DnsRequisitionUrlConnectionIT method dwoUrlAsResourceUsingComplexMatchingExpression.

@Test
@JUnitDNSServer(port = 9153, zones = { @DNSZone(name = "example.com", entries = { @DNSEntry(hostname = "www", address = "72.14.204.99") }) })
public void dwoUrlAsResourceUsingComplexMatchingExpression() throws IOException, JAXBException {
    String urlString = "dns://localhost:9153/example.com/?expression=(%3Fi)^WWW.EXAM.*";
    Resource resource = new UrlResource(urlString);
    Assert.assertEquals(urlString, resource.getURL().toString());
    Requisition req = null;
    Assert.assertNotNull(resource);
    InputStream resourceStream = resource.getInputStream();
    JAXBContext context = JAXBContext.newInstance(Requisition.class);
    Unmarshaller um = context.createUnmarshaller();
    um.setSchema(null);
    req = (Requisition) um.unmarshal(resourceStream);
    Assert.assertEquals(1, req.getNodeCount());
    resourceStream.close();
}
Also used : UrlResource(org.springframework.core.io.UrlResource) InputStream(java.io.InputStream) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Requisition(org.opennms.netmgt.provision.persist.requisition.Requisition) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) Test(org.junit.Test)

Aggregations

JUnitDNSServer (org.opennms.core.test.dns.annotations.JUnitDNSServer)11 Test (org.junit.Test)10 InputStream (java.io.InputStream)5 Requisition (org.opennms.netmgt.provision.persist.requisition.Requisition)5 UrlResource (org.springframework.core.io.UrlResource)5 JAXBContext (javax.xml.bind.JAXBContext)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 Resource (org.springframework.core.io.Resource)4 URL (java.net.URL)2 URLConnection (java.net.URLConnection)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 DNSEntry (org.opennms.core.test.dns.annotations.DNSEntry)1 DNSZone (org.opennms.core.test.dns.annotations.DNSZone)1 OnmsAssetRequisition (org.opennms.netmgt.provision.persist.OnmsAssetRequisition)1 OnmsIpInterfaceRequisition (org.opennms.netmgt.provision.persist.OnmsIpInterfaceRequisition)1 OnmsMonitoredServiceRequisition (org.opennms.netmgt.provision.persist.OnmsMonitoredServiceRequisition)1 OnmsNodeCategoryRequisition (org.opennms.netmgt.provision.persist.OnmsNodeCategoryRequisition)1 OnmsNodeRequisition (org.opennms.netmgt.provision.persist.OnmsNodeRequisition)1 OnmsServiceCategoryRequisition (org.opennms.netmgt.provision.persist.OnmsServiceCategoryRequisition)1 AAAARecord (org.xbill.DNS.AAAARecord)1