Search in sources :

Example 86 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project kafka by apache.

the class TopicAdminTest method describeShouldReturnTopicDescriptionWhenTopicExists.

@Test
public void describeShouldReturnTopicDescriptionWhenTopicExists() {
    String topicName = "myTopic";
    NewTopic newTopic = TopicAdmin.defineTopic(topicName).partitions(1).compacted().build();
    Cluster cluster = createCluster(1);
    try (MockAdminClient mockAdminClient = new MockAdminClient(cluster.nodes(), cluster.nodeById(0))) {
        TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, cluster.nodeById(0), cluster.nodes(), Collections.emptyList());
        mockAdminClient.addTopic(false, topicName, Collections.singletonList(topicPartitionInfo), null);
        TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
        Map<String, TopicDescription> desc = admin.describeTopics(newTopic.name());
        assertFalse(desc.isEmpty());
        TopicDescription topicDesc = new TopicDescription(topicName, false, Collections.singletonList(topicPartitionInfo));
        assertEquals(desc.get("myTopic"), topicDesc);
    }
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Cluster(org.apache.kafka.common.Cluster) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Test(org.junit.Test)

Example 87 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project kafka by apache.

the class TopicAdminTest method describeTopicConfigShouldReturnTopicConfigWhenTopicExists.

@Test
public void describeTopicConfigShouldReturnTopicConfigWhenTopicExists() {
    String topicName = "myTopic";
    NewTopic newTopic = TopicAdmin.defineTopic(topicName).config(Collections.singletonMap("foo", "bar")).partitions(1).compacted().build();
    Cluster cluster = createCluster(1);
    try (MockAdminClient mockAdminClient = new MockAdminClient(cluster.nodes(), cluster.nodeById(0))) {
        TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, cluster.nodeById(0), cluster.nodes(), Collections.emptyList());
        mockAdminClient.addTopic(false, topicName, Collections.singletonList(topicPartitionInfo), null);
        TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
        Map<String, Config> result = admin.describeTopicConfigs(newTopic.name());
        assertFalse(result.isEmpty());
        assertEquals(1, result.size());
        Config config = result.get("myTopic");
        assertNotNull(config);
        config.entries().forEach(entry -> assertEquals(newTopic.configs().get(entry.name()), entry.value()));
    }
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicConfig(org.apache.kafka.common.config.TopicConfig) Config(org.apache.kafka.clients.admin.Config) Cluster(org.apache.kafka.common.Cluster) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) Test(org.junit.Test)

Example 88 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project kafka by apache.

the class TopicAdminTest method returnEmptyWithClusterAuthorizationFailureOnCreate.

@Test
public void returnEmptyWithClusterAuthorizationFailureOnCreate() {
    final NewTopic newTopic = TopicAdmin.defineTopic("myTopic").partitions(1).compacted().build();
    Cluster cluster = createCluster(1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        env.kafkaClient().prepareResponse(createTopicResponseWithClusterAuthorizationException(newTopic));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        assertFalse(admin.createTopic(newTopic));
        env.kafkaClient().prepareResponse(createTopicResponseWithClusterAuthorizationException(newTopic));
        assertTrue(admin.createOrFindTopics(newTopic).isEmpty());
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) Cluster(org.apache.kafka.common.Cluster) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 89 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project kafka by apache.

the class TopicAdminTest method shouldCreateTopicWithPartitionsWhenItDoesNotExist.

@Test
public void shouldCreateTopicWithPartitionsWhenItDoesNotExist() {
    for (int numBrokers = 1; numBrokers < 10; ++numBrokers) {
        int expectedReplicas = Math.min(3, numBrokers);
        int maxDefaultRf = Math.min(numBrokers, 5);
        for (int numPartitions = 1; numPartitions < 30; ++numPartitions) {
            NewTopic newTopic = TopicAdmin.defineTopic("myTopic").partitions(numPartitions).compacted().build();
            // Try clusters with no default replication factor or default partitions
            assertTopicCreation(numBrokers, newTopic, null, null, expectedReplicas, numPartitions);
            // Try clusters with different default partitions
            for (int defaultPartitions = 1; defaultPartitions < 20; ++defaultPartitions) {
                assertTopicCreation(numBrokers, newTopic, defaultPartitions, null, expectedReplicas, numPartitions);
            }
            // Try clusters with different default replication factors
            for (int defaultRF = 1; defaultRF < maxDefaultRf; ++defaultRF) {
                assertTopicCreation(numBrokers, newTopic, null, defaultRF, defaultRF, numPartitions);
            }
        }
    }
}
Also used : NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test)

Example 90 with NewTopic

use of org.apache.kafka.clients.admin.NewTopic in project kafka by apache.

the class TopicAdminTest method verifyingTopicCleanupPolicyShouldReturnFalseWhenTopicAuthorizationError.

@Test
public void verifyingTopicCleanupPolicyShouldReturnFalseWhenTopicAuthorizationError() {
    final NewTopic newTopic = TopicAdmin.defineTopic("myTopic").partitions(1).compacted().build();
    Cluster cluster = createCluster(1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        env.kafkaClient().prepareResponse(describeConfigsResponseWithTopicAuthorizationException(newTopic));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        boolean result = admin.verifyTopicCleanupPolicyOnlyCompact("myTopic", "worker.topic", "purpose");
        assertFalse(result);
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) Cluster(org.apache.kafka.common.Cluster) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Aggregations

NewTopic (org.apache.kafka.clients.admin.NewTopic)132 Test (org.junit.Test)65 HashMap (java.util.HashMap)37 AdminClient (org.apache.kafka.clients.admin.AdminClient)35 Cluster (org.apache.kafka.common.Cluster)24 ExecutionException (java.util.concurrent.ExecutionException)23 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)20 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)19 Config (org.apache.kafka.clients.admin.Config)16 MockTime (org.apache.kafka.common.utils.MockTime)16 AdminClientUnitTestEnv (org.apache.kafka.clients.admin.AdminClientUnitTestEnv)15 Map (java.util.Map)14 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)13 TopicConfig (org.apache.kafka.common.config.TopicConfig)13 StreamsConfig (org.apache.kafka.streams.StreamsConfig)12 ArrayList (java.util.ArrayList)11 CreateTopicsResult (org.apache.kafka.clients.admin.CreateTopicsResult)11 TimeoutException (org.apache.kafka.common.errors.TimeoutException)11 TopicMetadataAndConfig (org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig)10 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)10