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);
}
}
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()));
}
}
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());
}
}
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);
}
}
}
}
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);
}
}
Aggregations