KafkaUtilities { public long getNumTopics(KafkaZkClient zkClient) { Objects.requireNonNull(zkClient, "zkClient must not be null"); try { Seq<String> topics = zkClient.getAllTopicsInCluster(); return topics.length(); } catch (Exception e) { log.error("Could not retrieve number of topics from ZooKeeper: {}", e.getMessage()); return -1L; } }  long getNumTopics(KafkaZkClient zkClient); List<String> getBootstrapServers(KafkaZkClient zkClient, int maxNumServers); boolean createAndVerifyTopic(
      KafkaZkClient zkClient,
      String topic,
      int partitions,
      int replication,
      long retentionMs
  ); VerifyTopicState verifySupportTopic(
      KafkaZkClient zkClient,
      String topic,
      int expPartitions,
      int expReplication
  ); boolean isReadyForMetricsCollection(KafkaServer server); boolean isShuttingDown(KafkaServer server);  }
@Test public void getNumTopicsThrowsIAEWhenKafkaZkClientIsNull() { KafkaUtilities kUtil = new KafkaUtilities(); try { kUtil.getNumTopics(null); fail("IllegalArgumentException expected because zkClient is null"); } catch (NullPointerException e) { } }
@Test public void getNumTopicsReturnsMinusOneOnError() { KafkaUtilities kUtil = new KafkaUtilities(); KafkaZkClient zkClient = mock(KafkaZkClient.class); when(zkClient.getAllTopicsInCluster()).thenThrow(new RuntimeException("exception intentionally thrown by test")); assertThat(kUtil.getNumTopics(zkClient)).isEqualTo(-1L); }
@Test public void getNumTopicsReturnsZeroWhenThereAreNoTopics() { KafkaUtilities kUtil = new KafkaUtilities(); KafkaZkClient zkClient = mock(KafkaZkClient.class); List<String> zeroTopics = new ArrayList<>(); when(zkClient.getAllTopicsInCluster()).thenReturn(JavaConversions.asScalaBuffer(zeroTopics).toList()); assertThat(kUtil.getNumTopics(zkClient)).isEqualTo(0L); }
@Test public void getNumTopicsReturnsCorrectNumber() { KafkaUtilities kUtil = new KafkaUtilities(); KafkaZkClient zkClient = mock(KafkaZkClient.class); List<String> topics = new ArrayList<>(); topics.add("topic1"); topics.add("topic2"); when(zkClient.getAllTopicsInCluster()).thenReturn(JavaConversions.asScalaBuffer(topics).toList()); assertThat(kUtil.getNumTopics(zkClient)).isEqualTo(topics.size()); }