ConfigurationParserImpl { public Configuration parseConfiguration(XMLConfiguration configuration) throws ConfigurationException { ConfigurationImpl configImpl = new ConfigurationImpl(); List<HierarchicalConfiguration> bgpServerNodes = configuration.configurationsAt("BgpServer"); List<HierarchicalConfiguration> httpServerNodes = configuration.configurationsAt("HttpServer"); List<HierarchicalConfiguration> bgpPeerNodes = configuration.configurationsAt("BgpPeers.BgpPeer"); List<HierarchicalConfiguration> extensionNodes = configuration.configurationsAt("Extensions.Extension"); List<HierarchicalConfiguration> routingProcessorNodes = configuration.configurationsAt("RoutingProcessor"); if(bgpServerNodes.size() > 1) throw new ConfigurationException("Duplicate <BgpServer /> element"); else if(bgpServerNodes.size() == 1) configImpl.setBgpServerConfiguration(bgpServerConfigurationParser.parseConfiguration(bgpServerNodes.get(0))); if(httpServerNodes.size() > 1) throw new ConfigurationException("Duplicate <HttpServer /> element"); else if(httpServerNodes.size() == 1) configImpl.setHttpServerConfiguration(httpServerConfigurationParser.parseConfiguration(httpServerNodes.get(0))); if(routingProcessorNodes.size() > 1) throw new ConfigurationException("Duplicate <RoutingProcessor /> element"); else if(routingProcessorNodes.size() == 1) configImpl.setRoutingProcessorConfiguration(routingConfigurationParser.parseConfiguration(routingProcessorNodes.get(0))); for(HierarchicalConfiguration bgpPeerNode : bgpPeerNodes) { configImpl.addPeer(peerConfigurationParser.parseConfiguration(bgpPeerNode)); } extensionsConfigurationParser.parseConfiguration(extensionNodes); return configImpl; }  Configuration parseConfiguration(XMLConfiguration configuration);  }
@Test public void testEmptyConfiguration() throws Exception { Configuration config = parser.parseConfiguration(loadConfiguration("config/Empty-Config.xml")); Assert.assertNotNull(config); Assert.assertNull(config.getBgpServerConfiguration()); Assert.assertNull(config.getHttpServerConfiguration()); }
@Test(expected=ConfigurationException.class) public void testConfigurationDuplicateBgpServerConfiguration() throws Exception { Configuration config = parser.parseConfiguration(loadConfiguration("config/Config-With-DuplicateBgpServer.xml")); Assert.assertNotNull(config); Assert.assertNull(config.getBgpServerConfiguration()); Assert.assertNull(config.getHttpServerConfiguration()); }
@Test public void testConfigurationWithBgpServerConfiguration() throws Exception { Configuration config = parser.parseConfiguration(loadConfiguration("config/Config-With-BgpServer.xml")); Assert.assertNotNull(config); Assert.assertNotNull(config.getBgpServerConfiguration()); Assert.assertEquals(17179, config.getBgpServerConfiguration().getServerConfiguration().getListenAddress().getPort()); Assert.assertEquals(InetAddress.getByName("192.168.4.1"), config.getBgpServerConfiguration().getServerConfiguration().getListenAddress().getAddress()); Assert.assertNull(config.getHttpServerConfiguration()); }
@Test(expected=ConfigurationException.class) public void testConfigurationDuplicateHttpServerConfiguration() throws Exception { Configuration config = parser.parseConfiguration(loadConfiguration("config/Config-With-DuplicateHttpServer.xml")); Assert.assertNotNull(config); Assert.assertNull(config.getBgpServerConfiguration()); Assert.assertNull(config.getHttpServerConfiguration()); }
@Test public void testConfigurationWithHttpServerConfiguration() throws Exception { Configuration config = parser.parseConfiguration(loadConfiguration("config/Config-With-HttpServer.xml")); Assert.assertNotNull(config); Assert.assertNotNull(config.getHttpServerConfiguration()); Assert.assertEquals(8080, config.getHttpServerConfiguration().getServerConfiguration().getListenAddress().getPort()); Assert.assertEquals(InetAddress.getByName("192.168.4.1"), config.getHttpServerConfiguration().getServerConfiguration().getListenAddress().getAddress()); Assert.assertNull(config.getBgpServerConfiguration()); }
@Test public void testConfigurationWithTwoBgpPeers() throws Exception { Configuration config = parser.parseConfiguration(loadConfiguration("config/Config-With-BgpPeers.xml")); PeerConfiguration peerConfig; Assert.assertNull(config.getHttpServerConfiguration()); Assert.assertEquals(2, config.listPeerNames().size()); peerConfig = config.getPeer("foo1"); Assert.assertNotNull(peerConfig); Assert.assertEquals(InetAddress.getByName("192.168.4.1"), peerConfig.getClientConfig().getRemoteAddress().getAddress()); Assert.assertEquals(179, peerConfig.getClientConfig().getRemoteAddress().getPort()); Assert.assertEquals(10, peerConfig.getLocalAS()); Assert.assertEquals(11, peerConfig.getRemoteAS()); Assert.assertEquals("foo1", peerConfig.getPeerName()); peerConfig = config.getPeer("foo2"); Assert.assertNotNull(peerConfig); Assert.assertEquals(InetAddress.getByName("192.168.4.2"), peerConfig.getClientConfig().getRemoteAddress().getAddress()); Assert.assertEquals(179, peerConfig.getClientConfig().getRemoteAddress().getPort()); Assert.assertEquals(12, peerConfig.getLocalAS()); Assert.assertEquals(13, peerConfig.getRemoteAS()); Assert.assertEquals("foo2", peerConfig.getPeerName()); }
@Test public void testConfigurationWithTwoBgpPeersIPv4BGPIdentifiers() throws Exception { Configuration config = parser.parseConfiguration(loadConfiguration("config/Config-With-BgpPeers-IPv4BGPIdentifier.xml")); PeerConfiguration peerConfig; Assert.assertNull(config.getHttpServerConfiguration()); Assert.assertEquals(2, config.listPeerNames().size()); peerConfig = config.getPeer("foo1"); Assert.assertNotNull(peerConfig); Assert.assertEquals(InetAddress.getByName("192.168.4.1"), peerConfig.getClientConfig().getRemoteAddress().getAddress()); Assert.assertEquals(179, peerConfig.getClientConfig().getRemoteAddress().getPort()); Assert.assertEquals(10, peerConfig.getLocalAS()); Assert.assertEquals(11, peerConfig.getRemoteAS()); Assert.assertEquals("foo1", peerConfig.getPeerName()); Assert.assertEquals((192L<<24)|(168L<<16)|(4L<<8)|1, peerConfig.getLocalBgpIdentifier()); Assert.assertEquals((192L<<24)|(168L<<16)|(4L<<8)|2, peerConfig.getRemoteBgpIdentifier()); peerConfig = config.getPeer("foo2"); Assert.assertNotNull(peerConfig); Assert.assertEquals(InetAddress.getByName("192.168.4.2"), peerConfig.getClientConfig().getRemoteAddress().getAddress()); Assert.assertEquals(179, peerConfig.getClientConfig().getRemoteAddress().getPort()); Assert.assertEquals(12, peerConfig.getLocalAS()); Assert.assertEquals(13, peerConfig.getRemoteAS()); Assert.assertEquals("foo2", peerConfig.getPeerName()); Assert.assertEquals((192L<<24)|(168L<<16)|(4<<8)|1, peerConfig.getLocalBgpIdentifier()); Assert.assertEquals((192L<<24)|(168L<<16)|(4<<8)|3, peerConfig.getRemoteBgpIdentifier()); }
@Test(expected=ConfigurationException.class) public void testConfigurationWithTwoBgpPeersDuplicatePeer() throws Exception { @SuppressWarnings("unused") Configuration config = parser.parseConfiguration(loadConfiguration("config/Config-With-BgpPeers-DuplicatePeer.xml")); }