ConfigParser { public static List<URL> parseConfigurators(String rawConfig) { if (isJsonArray(rawConfig)) { return parseJsonArray(rawConfig); } List<URL> urls = new ArrayList<>(); ConfiguratorConfig configuratorConfig = parseObject(rawConfig); String scope = configuratorConfig.getScope(); List<ConfigItem> items = configuratorConfig.getConfigs(); if (ConfiguratorConfig.SCOPE_APPLICATION.equals(scope)) { items.forEach(item -> urls.addAll(appItemToUrls(item, configuratorConfig))); } else { items.forEach(item -> urls.addAll(serviceItemToUrls(item, configuratorConfig))); } return urls; }  static List<URL> parseConfigurators(String rawConfig);  }
@Test public void parseConfiguratorsServiceNoAppTest() throws Exception { try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoApp.yml")) { List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); Assertions.assertNotNull(urls); Assertions.assertEquals(2, urls.size()); URL url = urls.get(0); Assertions.assertEquals(url.getAddress(), "127.0.0.1:20880"); Assertions.assertEquals(url.getParameter(WEIGHT_KEY, 0), 222); } }
@Test public void parseConfiguratorsServiceGroupVersionTest() throws Exception { try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceGroupVersion.yml")) { List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); Assertions.assertNotNull(urls); Assertions.assertEquals(1, urls.size()); URL url = urls.get(0); Assertions.assertEquals("testgroup", url.getParameter(GROUP_KEY)); Assertions.assertEquals("1.0.0", url.getParameter(VERSION_KEY)); } }
@Test public void parseConfiguratorsServiceMultiAppsTest() throws IOException { try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceMultiApps.yml")) { List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); Assertions.assertNotNull(urls); Assertions.assertEquals(4, urls.size()); URL url = urls.get(0); Assertions.assertEquals("127.0.0.1", url.getAddress()); Assertions.assertEquals(6666, url.getParameter(TIMEOUT_KEY, 0)); Assertions.assertNotNull(url.getParameter(APPLICATION_KEY)); } }
@Test public void parseConfiguratorsServiceNoRuleTest() { Assertions.assertThrows(IllegalStateException.class, () -> { try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceNoRule.yml")) { ConfigParser.parseConfigurators(streamToString(yamlStream)); Assertions.fail(); } }); }
@Test public void parseConfiguratorsAppMultiServicesTest() throws IOException { try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml")) { String yamlFile = streamToString(yamlStream); List<URL> urls = ConfigParser.parseConfigurators(yamlFile); Assertions.assertNotNull(urls); Assertions.assertEquals(4, urls.size()); URL url = urls.get(0); Assertions.assertEquals("127.0.0.1", url.getAddress()); Assertions.assertEquals("service1", url.getServiceInterface()); Assertions.assertEquals(6666, url.getParameter(TIMEOUT_KEY, 0)); Assertions.assertEquals("random", url.getParameter(LOADBALANCE_KEY)); Assertions.assertEquals(url.getParameter(APPLICATION_KEY), "demo-consumer"); } }
@Test public void parseConfiguratorsAppAnyServicesTest() throws IOException { try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml")) { List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); Assertions.assertNotNull(urls); Assertions.assertEquals(2, urls.size()); URL url = urls.get(0); Assertions.assertEquals("127.0.0.1", url.getAddress()); Assertions.assertEquals("*", url.getServiceInterface()); Assertions.assertEquals(6666, url.getParameter(TIMEOUT_KEY, 0)); Assertions.assertEquals("random", url.getParameter(LOADBALANCE_KEY)); Assertions.assertEquals(url.getParameter(APPLICATION_KEY), "demo-consumer"); } }
@Test public void parseConfiguratorsAppNoServiceTest() throws IOException { try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppNoService.yml")) { List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); Assertions.assertNotNull(urls); Assertions.assertEquals(1, urls.size()); URL url = urls.get(0); Assertions.assertEquals("127.0.0.1", url.getAddress()); Assertions.assertEquals("*", url.getServiceInterface()); Assertions.assertEquals(6666, url.getParameter(TIMEOUT_KEY, 0)); Assertions.assertEquals("random", url.getParameter(LOADBALANCE_KEY)); Assertions.assertEquals(url.getParameter(APPLICATION_KEY), "demo-consumer"); } }
@Test public void parseConsumerSpecificProvidersTest() throws IOException { try (InputStream yamlStream = this.getClass().getResourceAsStream("/ConsumerSpecificProviders.yml")) { List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream)); Assertions.assertNotNull(urls); Assertions.assertEquals(1, urls.size()); URL url = urls.get(0); Assertions.assertEquals("127.0.0.1", url.getAddress()); Assertions.assertEquals("*", url.getServiceInterface()); Assertions.assertEquals(6666, url.getParameter(TIMEOUT_KEY, 0)); Assertions.assertEquals("random", url.getParameter(LOADBALANCE_KEY)); Assertions.assertEquals("127.0.0.1:20880", url.getParameter(OVERRIDE_PROVIDERS_KEY)); Assertions.assertEquals(url.getParameter(APPLICATION_KEY), "demo-consumer"); } }
@Test public void parseURLJsonArrayCompatible() { String configData = "[\"override: List<URL> urls = ConfigParser.parseConfigurators(configData); Assertions.assertNotNull(urls); Assertions.assertEquals(1, urls.size()); URL url = urls.get(0); Assertions.assertEquals("0.0.0.0", url.getAddress()); Assertions.assertEquals("com.xx.Service", url.getServiceInterface()); Assertions.assertEquals(6666, url.getParameter(TIMEOUT_KEY, 0)); }