diff --git a/src/main/groovy/offtopic/OfftopicClient.groovy b/src/main/groovy/offtopic/OfftopicClient.groovy index aa55edb..fcc4048 100644 --- a/src/main/groovy/offtopic/OfftopicClient.groovy +++ b/src/main/groovy/offtopic/OfftopicClient.groovy @@ -23,7 +23,7 @@ class OfftopicClient { } public void createSubscribersFor(String topicsPattern) { - topicsPattern.split("\\+").each { topic -> + topicsFrom(topicsPattern).each { topic -> if (topic.length() == 0) { return } @@ -57,4 +57,30 @@ class OfftopicClient { subscriber.shutdown() } } + + public ArrayList topicsFrom(String topicsPattern) { + ArrayList topics = new ArrayList() + topicsPattern.split("\\+").each { topic -> + if (topic.length() == 0) { + return + } + if (topic.indexOf('*') >= 0) { + topics.addAll(lookupTopicsFor(topicsPattern)) + } + else { + topics.add(topic) + } + } + return topics + } + + private ArrayList lookupTopicsFor(String topicPattern) { + ArrayList topics = new ArrayList() + KafkaService.fetchTopics().each { topic -> + if (topic =~ topicPattern) { + topics.add(topic) + } + } + return topics + } } diff --git a/src/main/groovy/offtopic/curator/CuratorPool.groovy b/src/main/groovy/offtopic/curator/CuratorPool.groovy index bbc7b2b..ae094f8 100644 --- a/src/main/groovy/offtopic/curator/CuratorPool.groovy +++ b/src/main/groovy/offtopic/curator/CuratorPool.groovy @@ -18,7 +18,6 @@ class CuratorPool extends GenericObjectPool{ private CuratorPool(String zookeepers) { super(new CuratorClientObjectFactory(zookeepers)) - println "CREATING WITH ${zookeepers}" } public static void withCurator(Closure closure) { diff --git a/src/test/groovy/offtopic/OfftopicClientSpec.groovy b/src/test/groovy/offtopic/OfftopicClientSpec.groovy index 3b6ce5d..f3990b4 100644 --- a/src/test/groovy/offtopic/OfftopicClientSpec.groovy +++ b/src/test/groovy/offtopic/OfftopicClientSpec.groovy @@ -1,6 +1,6 @@ package offtopic -import spock.lang.Specification +import spock.lang.* class OfftopicClientSpec extends Specification { def "initialization should create a clientId"() { @@ -43,3 +43,56 @@ class OfftopicClientCreateSubscribersSpec extends Specification { this.client.subscribers.size() == 1 } } + +class OfftopicClientTopicParsingSpec extends Specification { + def client = null + def setup() { + this.client = new OfftopicClient() + } + + def "a single topic"() { + when: + def topics = this.client.topicsFrom('topic') + + then: + topics.size() == 1 + } + + def "plus-delimited topics"() { + when: + def topics = this.client.topicsFrom('topic+some.topic') + + then: + topics.size() == 2 + } +} + +class OfftopicClientTopicLookupSpec extends Specification { + def client + + def setup() { + // Mocking out KafkaService so we don't actually have to hit Zookeeper + GroovyMock(KafkaService, global: true) + this.client = new OfftopicClient() + } + + def "looking up topics that don't exist"() { + when: + // Call fetchTopics() once and return an empty Array + 1 * KafkaService.fetchTopics() >> [] + def topics = this.client.lookupTopicsFor('spock.*') + + then: + topics.size() == 0 + } + + def "looking up topics against topics that exist"() { + when: + // Call fetchTopics() once and return an empty Array + 1 * KafkaService.fetchTopics() >> ['foo', 'bar', 'spock.rocks'] + def topics = this.client.lookupTopicsFor('spock.*') + + then: + topics.size() == 1 + } +}