Search in sources :

Example 41 with NewTopic

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

the class TopicAdminTest method describeTopicConfigShouldReturnEmptyMapWhenUnsupportedVersionFailure.

@Test
public void describeTopicConfigShouldReturnEmptyMapWhenUnsupportedVersionFailure() {
    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(describeConfigsResponseWithUnsupportedVersion(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 42 with NewTopic

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

the class TopicAdminTest method verifyingTopicCleanupPolicyShouldReturnFalseWhenBrokerVersionIsUnsupported.

@Test
public void verifyingTopicCleanupPolicyShouldReturnFalseWhenBrokerVersionIsUnsupported() {
    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(describeConfigsResponseWithUnsupportedVersion(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)

Example 43 with NewTopic

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

the class TopicAdminTest method verifyingTopicCleanupPolicyShouldReturnFalseWhenClusterAuthorizationError.

@Test
public void verifyingTopicCleanupPolicyShouldReturnFalseWhenClusterAuthorizationError() {
    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());
        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)

Example 44 with NewTopic

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

the class TopicCreationTest method topicCreationWithOneGroup.

@Test
public void topicCreationWithOneGroup() {
    short fooReplicas = 3;
    int partitions = 5;
    sourceProps = defaultConnectorPropsWithTopicCreation();
    sourceProps.put(TOPIC_CREATION_GROUPS_CONFIG, String.join(",", FOO_GROUP));
    sourceProps.put(DEFAULT_TOPIC_CREATION_PREFIX + PARTITIONS_CONFIG, String.valueOf(partitions));
    sourceProps.put(TOPIC_CREATION_PREFIX + FOO_GROUP + "." + INCLUDE_REGEX_CONFIG, FOO_REGEX);
    sourceProps.put(TOPIC_CREATION_PREFIX + FOO_GROUP + "." + EXCLUDE_REGEX_CONFIG, BAR_REGEX);
    sourceProps.put(TOPIC_CREATION_PREFIX + FOO_GROUP + "." + REPLICATION_FACTOR_CONFIG, String.valueOf(fooReplicas));
    Map<String, String> topicProps = new HashMap<>();
    topicProps.put(CLEANUP_POLICY_CONFIG, CLEANUP_POLICY_COMPACT);
    topicProps.forEach((k, v) -> sourceProps.put(TOPIC_CREATION_PREFIX + FOO_GROUP + "." + k, v));
    // verify config creation
    sourceConfig = new SourceConnectorConfig(MOCK_PLUGINS, sourceProps, true);
    assertTrue(sourceConfig.usesTopicCreation());
    assertEquals(DEFAULT_REPLICATION_FACTOR, (short) sourceConfig.topicCreationReplicationFactor(DEFAULT_TOPIC_CREATION_GROUP));
    assertEquals(partitions, (int) sourceConfig.topicCreationPartitions(DEFAULT_TOPIC_CREATION_GROUP));
    assertThat(sourceConfig.topicCreationInclude(DEFAULT_TOPIC_CREATION_GROUP), is(Collections.singletonList(".*")));
    assertThat(sourceConfig.topicCreationExclude(DEFAULT_TOPIC_CREATION_GROUP), is(Collections.emptyList()));
    assertThat(sourceConfig.topicCreationOtherConfigs(DEFAULT_TOPIC_CREATION_GROUP), is(Collections.emptyMap()));
    // verify topic creation group is instantiated correctly
    Map<String, TopicCreationGroup> groups = TopicCreationGroup.configuredGroups(sourceConfig);
    assertEquals(2, groups.size());
    assertThat(groups.keySet(), hasItems(DEFAULT_TOPIC_CREATION_GROUP, FOO_GROUP));
    // verify topic creation
    TopicCreation topicCreation = TopicCreation.newTopicCreation(workerConfig, groups);
    TopicCreationGroup defaultGroup = topicCreation.defaultTopicGroup();
    // Default group will match all topics besides empty string
    assertTrue(defaultGroup.matches(" "));
    assertTrue(defaultGroup.matches(FOO_TOPIC));
    assertTrue(defaultGroup.matches(BAR_TOPIC));
    assertEquals(DEFAULT_TOPIC_CREATION_GROUP, defaultGroup.name());
    TopicCreationGroup fooGroup = groups.get(FOO_GROUP);
    assertFalse(fooGroup.matches(" "));
    assertTrue(fooGroup.matches(FOO_TOPIC));
    assertFalse(fooGroup.matches(BAR_TOPIC));
    assertEquals(FOO_GROUP, fooGroup.name());
    assertTrue(topicCreation.isTopicCreationEnabled());
    assertTrue(topicCreation.isTopicCreationRequired(FOO_TOPIC));
    assertEquals(1, topicCreation.topicGroups().size());
    assertThat(topicCreation.topicGroups().keySet(), hasItems(FOO_GROUP));
    assertEquals(fooGroup, topicCreation.findFirstGroup(FOO_TOPIC));
    topicCreation.addTopic(FOO_TOPIC);
    assertFalse(topicCreation.isTopicCreationRequired(FOO_TOPIC));
    // verify new topic properties
    NewTopic defaultTopicSpec = topicCreation.findFirstGroup(BAR_TOPIC).newTopic(BAR_TOPIC);
    assertEquals(BAR_TOPIC, defaultTopicSpec.name());
    assertEquals(DEFAULT_REPLICATION_FACTOR, defaultTopicSpec.replicationFactor());
    assertEquals(partitions, defaultTopicSpec.numPartitions());
    assertThat(defaultTopicSpec.configs(), is(Collections.emptyMap()));
    NewTopic fooTopicSpec = topicCreation.findFirstGroup(FOO_TOPIC).newTopic(FOO_TOPIC);
    assertEquals(FOO_TOPIC, fooTopicSpec.name());
    assertEquals(fooReplicas, fooTopicSpec.replicationFactor());
    assertEquals(partitions, fooTopicSpec.numPartitions());
    assertThat(fooTopicSpec.configs(), is(topicProps));
}
Also used : SourceConnectorConfig(org.apache.kafka.connect.runtime.SourceConnectorConfig) HashMap(java.util.HashMap) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test)

Example 45 with NewTopic

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

the class TopicCreationTest method topicCreationWithDefaultGroupAndCustomProps.

@Test
public void topicCreationWithDefaultGroupAndCustomProps() {
    short replicas = 3;
    int partitions = 5;
    long retentionMs = TimeUnit.DAYS.toMillis(30);
    String compressionType = "lz4";
    Map<String, String> topicProps = new HashMap<>();
    topicProps.put(COMPRESSION_TYPE_CONFIG, compressionType);
    topicProps.put(RETENTION_MS_CONFIG, String.valueOf(retentionMs));
    sourceProps = defaultConnectorPropsWithTopicCreation();
    sourceProps.put(DEFAULT_TOPIC_CREATION_PREFIX + REPLICATION_FACTOR_CONFIG, String.valueOf(replicas));
    sourceProps.put(DEFAULT_TOPIC_CREATION_PREFIX + PARTITIONS_CONFIG, String.valueOf(partitions));
    topicProps.forEach((k, v) -> sourceProps.put(DEFAULT_TOPIC_CREATION_PREFIX + k, v));
    // Setting here but they should be ignored for the default group
    sourceProps.put(TOPIC_CREATION_PREFIX + DEFAULT_TOPIC_CREATION_GROUP + "." + INCLUDE_REGEX_CONFIG, FOO_REGEX);
    sourceProps.put(TOPIC_CREATION_PREFIX + DEFAULT_TOPIC_CREATION_GROUP + "." + EXCLUDE_REGEX_CONFIG, BAR_REGEX);
    // verify config creation
    sourceConfig = new SourceConnectorConfig(MOCK_PLUGINS, sourceProps, true);
    assertTrue(sourceConfig.usesTopicCreation());
    assertEquals(replicas, (short) sourceConfig.topicCreationReplicationFactor(DEFAULT_TOPIC_CREATION_GROUP));
    assertEquals(partitions, (int) sourceConfig.topicCreationPartitions(DEFAULT_TOPIC_CREATION_GROUP));
    assertThat(sourceConfig.topicCreationInclude(DEFAULT_TOPIC_CREATION_GROUP), is(Collections.singletonList(".*")));
    assertThat(sourceConfig.topicCreationExclude(DEFAULT_TOPIC_CREATION_GROUP), is(Collections.emptyList()));
    assertThat(sourceConfig.topicCreationOtherConfigs(DEFAULT_TOPIC_CREATION_GROUP), is(topicProps));
    // verify topic creation group is instantiated correctly
    Map<String, TopicCreationGroup> groups = TopicCreationGroup.configuredGroups(sourceConfig);
    assertEquals(1, groups.size());
    assertThat(groups.keySet(), hasItem(DEFAULT_TOPIC_CREATION_GROUP));
    // verify topic creation
    TopicCreation topicCreation = TopicCreation.newTopicCreation(workerConfig, groups);
    TopicCreationGroup group = topicCreation.defaultTopicGroup();
    // Default group will match all topics besides empty string
    assertTrue(group.matches(" "));
    assertTrue(group.matches(FOO_TOPIC));
    assertEquals(DEFAULT_TOPIC_CREATION_GROUP, group.name());
    assertTrue(topicCreation.isTopicCreationEnabled());
    assertTrue(topicCreation.isTopicCreationRequired(FOO_TOPIC));
    assertThat(topicCreation.topicGroups(), is(Collections.emptyMap()));
    assertEquals(topicCreation.defaultTopicGroup(), topicCreation.findFirstGroup(FOO_TOPIC));
    topicCreation.addTopic(FOO_TOPIC);
    assertFalse(topicCreation.isTopicCreationRequired(FOO_TOPIC));
    // verify new topic properties
    NewTopic topicSpec = topicCreation.findFirstGroup(FOO_TOPIC).newTopic(FOO_TOPIC);
    assertEquals(FOO_TOPIC, topicSpec.name());
    assertEquals(replicas, topicSpec.replicationFactor());
    assertEquals(partitions, topicSpec.numPartitions());
    assertThat(topicSpec.configs(), is(topicProps));
}
Also used : SourceConnectorConfig(org.apache.kafka.connect.runtime.SourceConnectorConfig) HashMap(java.util.HashMap) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test)

Aggregations

NewTopic (org.apache.kafka.clients.admin.NewTopic)115 Test (org.junit.Test)59 HashMap (java.util.HashMap)34 AdminClient (org.apache.kafka.clients.admin.AdminClient)26 Cluster (org.apache.kafka.common.Cluster)24 ExecutionException (java.util.concurrent.ExecutionException)21 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)19 TopicExistsException (org.apache.kafka.common.errors.TopicExistsException)19 MockTime (org.apache.kafka.common.utils.MockTime)16 AdminClientUnitTestEnv (org.apache.kafka.clients.admin.AdminClientUnitTestEnv)15 Config (org.apache.kafka.clients.admin.Config)15 Map (java.util.Map)14 TopicConfig (org.apache.kafka.common.config.TopicConfig)13 StreamsConfig (org.apache.kafka.streams.StreamsConfig)12 ArrayList (java.util.ArrayList)11 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)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 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)10