AddressHelper { public static String rewrite(final String address, final ClientConfigProperties config) { if (config != null && config.getAddressRewritePattern() != null && !Strings.isNullOrEmpty(config.getAddressRewriteReplacement())) { return config.getAddressRewritePattern().matcher(address).replaceAll(config.getAddressRewriteReplacement()); } return address; } private  AddressHelper(); static String getTargetAddress(final String endpoint, final String tenantId, final String resourceId, final ClientConfigProperties config); static String rewrite(final String address, final ClientConfigProperties config);  }
@Test public void testAddressRewrite() { final ClientConfigProperties config = createConfig("([a-z_]+)/([\\w-]+) test-vhost/$1/$2"); assertEquals("test-vhost/" + address, AddressHelper.rewrite(address, config)); final String namespaceAddress = "telemetry/iotns.iot"; final ClientConfigProperties namespaceConfig = createConfig("([a-z_]+)/([\\w-]+).([\\w-]+) $2/$1/$2.$3"); assertEquals("iotns/" + namespaceAddress, AddressHelper.rewrite(namespaceAddress, namespaceConfig)); final String eventAddress = "event/iotns.iot"; final ClientConfigProperties altConfig = createConfig(("[a-z_]+/([\\w-]+).[\\w-]+ $1/$0")); assertEquals("iotns/" + eventAddress, AddressHelper.rewrite(eventAddress, altConfig)); }
@Test public void testAddressRewriteWithNonValidSyntaxValue() { assertEquals(address, AddressHelper.rewrite(address, createConfig("TEST"))); }
@Test public void testAddressRewriteForNonMatchingPattern() { assertEquals(address, AddressHelper.rewrite(address, createConfig("* *"))); }
@Test public void testAddressRewriteForNonSetValues() { assertEquals(address, AddressHelper.rewrite(address, createConfig(null))); assertEquals(address, AddressHelper.rewrite(address, createConfig(""))); }