Search in sources :

Example 21 with AdminClientUnitTestEnv

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

the class TopicAdminTest method throwsWithTopicAuthorizationFailureOnDescribe.

@Test
public void throwsWithTopicAuthorizationFailureOnDescribe() {
    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(describeTopicResponseWithTopicAuthorizationException(newTopic));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        Exception e = assertThrows(ConnectException.class, () -> admin.describeTopics(newTopic.name()));
        assertTrue(e.getCause() instanceof TopicAuthorizationException);
    }
}
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) ClusterAuthorizationException(org.apache.kafka.common.errors.ClusterAuthorizationException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ConfigException(org.apache.kafka.common.config.ConfigException) ExecutionException(java.util.concurrent.ExecutionException) ConnectException(org.apache.kafka.connect.errors.ConnectException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Test(org.junit.Test)

Example 22 with AdminClientUnitTestEnv

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

the class TopicAdminTest method endOffsetsShouldReturnOffsetsForOnePartition.

@Test
public void endOffsetsShouldReturnOffsetsForOnePartition() {
    String topicName = "myTopic";
    TopicPartition tp1 = new TopicPartition(topicName, 0);
    Set<TopicPartition> tps = Collections.singleton(tp1);
    long offset = 1000L;
    Cluster cluster = createCluster(1, topicName, 1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.NONE));
        env.kafkaClient().prepareResponse(listOffsetsResult(tp1, offset));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        Map<TopicPartition, Long> offsets = admin.endOffsets(tps);
        assertEquals(1, offsets.size());
        assertEquals(Long.valueOf(offset), offsets.get(tp1));
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) TopicPartition(org.apache.kafka.common.TopicPartition) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 23 with AdminClientUnitTestEnv

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

the class TopicAdminTest method describeTopicConfigShouldReturnEmptyMapWhenNoTopicsAreSpecified.

@Test
public void describeTopicConfigShouldReturnEmptyMapWhenNoTopicsAreSpecified() {
    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();
        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 24 with AdminClientUnitTestEnv

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

the class TopicAdminTest method endOffsetsShouldFailWhenAnyTopicPartitionHasError.

@Test
public void endOffsetsShouldFailWhenAnyTopicPartitionHasError() {
    String topicName = "myTopic";
    TopicPartition tp1 = new TopicPartition(topicName, 0);
    Set<TopicPartition> tps = Collections.singleton(tp1);
    long offset = 1000;
    Cluster cluster = createCluster(1, topicName, 1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.NONE));
        env.kafkaClient().prepareResponse(listOffsetsResultWithClusterAuthorizationException(tp1, null));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        ConnectException e = assertThrows(ConnectException.class, () -> admin.endOffsets(tps));
        assertTrue(e.getMessage().contains("Not authorized to get the end offsets"));
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) TopicPartition(org.apache.kafka.common.TopicPartition) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) ConnectException(org.apache.kafka.connect.errors.ConnectException) Test(org.junit.Test)

Aggregations

AdminClientUnitTestEnv (org.apache.kafka.clients.admin.AdminClientUnitTestEnv)24 Cluster (org.apache.kafka.common.Cluster)23 Test (org.junit.Test)23 MockTime (org.apache.kafka.common.utils.MockTime)21 NewTopic (org.apache.kafka.clients.admin.NewTopic)15 TopicPartition (org.apache.kafka.common.TopicPartition)9 ConnectException (org.apache.kafka.connect.errors.ConnectException)6 Config (org.apache.kafka.clients.admin.Config)4 TopicConfig (org.apache.kafka.common.config.TopicConfig)4 TimeoutException (org.apache.kafka.common.errors.TimeoutException)4 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)4 ExecutionException (java.util.concurrent.ExecutionException)3 ConfigException (org.apache.kafka.common.config.ConfigException)3 ClusterAuthorizationException (org.apache.kafka.common.errors.ClusterAuthorizationException)3 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)3 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 ListOffsetsOptions (org.apache.kafka.clients.admin.ListOffsetsOptions)1 Uuid (org.apache.kafka.common.Uuid)1 MetadataResponseData (org.apache.kafka.common.message.MetadataResponseData)1