rdhabalia closed pull request #2828: Support topic name with / in name URL: https://github.com/apache/pulsar/pull/2828
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ServiceUrlProviderTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ServiceUrlProviderTest.java index 4b73b9ef6d..3d57b13e47 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ServiceUrlProviderTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/ServiceUrlProviderTest.java @@ -23,11 +23,16 @@ import org.apache.pulsar.client.impl.ConsumerImpl; import org.apache.pulsar.client.impl.ProducerImpl; import org.apache.pulsar.client.impl.PulsarClientImpl; +import org.apache.pulsar.common.naming.TopicName; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import com.google.common.collect.Sets; + +import java.util.Set; import java.util.concurrent.TimeUnit; public class ServiceUrlProviderTest extends ProducerConsumerBase { @@ -46,6 +51,53 @@ protected void cleanup() throws Exception { super.internalCleanup(); } + @DataProvider(name = "cluster") + public Object[][] clusterProvider() { + return new Object[][] { {Boolean.TRUE}, {Boolean.FALSE} }; + } + + @Test(dataProvider = "cluster") + public void testClientWithSpecialCharTopicName(boolean isCluster) throws Exception { + + final String cluster = "test"; + final String topicLocalName = "`~!@#$%^&*()-_+=[]://{}|\\;:'\"<>,./?-my-topic"; + final String topic = "persistent://my-property/" + (isCluster ? cluster + "/" : "") + "my-ns/" + topicLocalName; + + TopicName topicName = TopicName.get(topic); + Assert.assertEquals(topicName.getTenant(), "my-property"); + Assert.assertEquals(topicName.getNamespacePortion(), "my-ns"); + Assert.assertEquals(topicName.getLocalName(), topicLocalName); + if(isCluster) { + Assert.assertEquals(topicName.getCluster(), "test"); + } + + Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topic).subscriptionName("my-subscriber-name") + .subscribe(); + Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).create(); + Assert.assertEquals(consumer.getTopic(), topic); + Assert.assertEquals(producer.getTopic(), topic); + + + for (int i = 0; i < 10; i++) { + String message = "my-message-" + i; + producer.send(message.getBytes()); + } + + Message<byte[]> msg = null; + Set<String> messageSet = Sets.newHashSet(); + for (int i = 0; i < 10; i++) { + msg = consumer.receive(5, TimeUnit.SECONDS); + String receivedMessage = new String(msg.getData()); + String expectedMessage = "my-message-" + i; + testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage); + } + // Acknowledge the consumption of all messages at once + consumer.acknowledgeCumulative(msg); + + producer.close(); + consumer.close(); + } + @Test public void testCreateClientWithServiceUrlProvider() throws Exception { diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java index bde252c107..83bcc0d995 100644 --- a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java +++ b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java @@ -139,6 +139,15 @@ private TopicName(String completeTopicName) { parts = Splitter.on("/").limit(4).splitToList(rest); + if (parts.size() == 4) { + try { + NamedEntity.checkName(parts.get(2)); + } catch (IllegalArgumentException ie) { + // It happens when topic-local-name has "/" in the name and cluster doesn't present into the + // topic-name + parts = Splitter.on("/").limit(3).splitToList(rest); + } + } if (parts.size() == 3) { // New topic name without cluster name this.tenant = parts.get(0); diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/naming/TopicNameTest.java b/pulsar-common/src/test/java/org/apache/pulsar/common/naming/TopicNameTest.java index fdbbc9f3c9..d8f963ec2e 100644 --- a/pulsar-common/src/test/java/org/apache/pulsar/common/naming/TopicNameTest.java +++ b/pulsar-common/src/test/java/org/apache/pulsar/common/naming/TopicNameTest.java @@ -23,11 +23,34 @@ import static org.testng.Assert.fail; import org.apache.pulsar.common.util.Codec; +import org.testng.Assert; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @Test public class TopicNameTest { + @DataProvider(name = "cluster") + public Object[][] clusterProvider() { + return new Object[][] { {Boolean.TRUE}, {Boolean.FALSE} }; + } + + @Test(dataProvider = "cluster") + public void testClientWithSpecialCharTopicName(boolean isCluster) throws Exception { + + final String cluster = "test"; + final String topicLocalName = "`~!@#$%^&*()-_+=[]://{}|\\;:'\"<>,./?-my-topic"; + final String topic = "persistent://my-property/" + (isCluster ? cluster + "/" : "") + "my-ns/" + topicLocalName; + + TopicName topicName = TopicName.get(topic); + Assert.assertEquals(topicName.getTenant(), "my-property"); + Assert.assertEquals(topicName.getNamespacePortion(), "my-ns"); + Assert.assertEquals(topicName.getLocalName(), topicLocalName); + if(isCluster) { + Assert.assertEquals(topicName.getCluster(), "test"); + } + } + @Test void topic() { try { ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services