Search in sources :

Example 96 with NewTopic

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

the class TopicAdminTest method shouldNotCreateTopicWhenItAlreadyExists.

@Test
public void shouldNotCreateTopicWhenItAlreadyExists() {
    NewTopic newTopic = TopicAdmin.defineTopic("myTopic").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, "myTopic", Collections.singletonList(topicPartitionInfo), null);
        TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
        assertFalse(admin.createTopic(newTopic));
        assertTrue(admin.createTopics(newTopic).isEmpty());
        assertTrue(admin.createOrFindTopic(newTopic));
        TopicAdmin.TopicCreationResponse response = admin.createOrFindTopics(newTopic);
        assertTrue(response.isCreatedOrExisting(newTopic.name()));
        assertTrue(response.isExisting(newTopic.name()));
        assertFalse(response.isCreated(newTopic.name()));
    }
}
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) Test(org.junit.Test)

Example 97 with NewTopic

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

the class TopicAdminTest method returnEmptyWithTopicAuthorizationFailureOnCreate.

@Test
public void returnEmptyWithTopicAuthorizationFailureOnCreate() {
    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(createTopicResponseWithTopicAuthorizationException(newTopic));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        assertFalse(admin.createTopic(newTopic));
        env.kafkaClient().prepareResponse(createTopicResponseWithTopicAuthorizationException(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 98 with NewTopic

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

the class TopicAdminTest method describeTopicConfigShouldReturnEmptyMapWhenTopicAuthorizationFailure.

@Test
public void describeTopicConfigShouldReturnEmptyMapWhenTopicAuthorizationFailure() {
    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());
        Map<String, Config> results = admin.describeTopicConfigs(newTopic.name());
        assertTrue(results.isEmpty());
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) 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) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 99 with NewTopic

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

the class TopicAdminTest method shouldCreateTopicWithDefaultPartitionsAndReplicationFactorWhenItDoesNotExist.

@Test
public void shouldCreateTopicWithDefaultPartitionsAndReplicationFactorWhenItDoesNotExist() {
    NewTopic newTopic = TopicAdmin.defineTopic("my-topic").defaultPartitions().defaultReplicationFactor().compacted().build();
    for (int numBrokers = 1; numBrokers < 10; ++numBrokers) {
        int expectedReplicas = Math.min(3, numBrokers);
        assertTopicCreation(numBrokers, newTopic, null, null, expectedReplicas, 1);
        assertTopicCreation(numBrokers, newTopic, 30, null, expectedReplicas, 30);
    }
}
Also used : NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test)

Example 100 with NewTopic

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

the class TopicAdminTest method describeTopicConfigShouldReturnEmptyMapWhenClusterAuthorizationFailure.

@Test
public void describeTopicConfigShouldReturnEmptyMapWhenClusterAuthorizationFailure() {
    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(describeConfigsResponseWithClusterAuthorizationException(newTopic));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        Map<String, Config> results = admin.describeTopicConfigs(newTopic.name());
        assertTrue(results.isEmpty());
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) 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) 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