Skip to content

Commit

Permalink
#285, create, list topics
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed May 4, 2023
1 parent d78f949 commit 5464d25
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/it/producer-consumer-api/src/test/java/EntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.github.eocqrs.kafka.Consumer;
import io.github.eocqrs.kafka.Dataized;
import io.github.eocqrs.kafka.Producer;
import io.github.eocqrs.kafka.admin.CreateTopics;
import io.github.eocqrs.kafka.consumer.KfConsumer;
import io.github.eocqrs.kafka.consumer.settings.KfConsumerParams;
import io.github.eocqrs.kafka.data.KfData;
Expand Down Expand Up @@ -81,12 +82,6 @@

import static org.assertj.core.api.Assertions.*;

/**
* @todo #81 Tests to produce-consume data.
* Write a test which will be check how consumer
* reads data from producer.
*/

/**
* @todo #236:30m/DEV Enable tests
*/
Expand All @@ -103,7 +98,6 @@ final class EntryTest {
private static final KafkaContainer KAFKA = new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka:7.3.0")
)
.withEnv("auto.create.topics.enable", "true")
.withEnv("KAFKA_CREATE_TOPICS", "TEST-TOPIC")
.withReuse(true)
.withLogConsumer(
Expand All @@ -113,7 +107,7 @@ final class EntryTest {
)
)
)
.withExternalZookeeper("localhost:2181");
.withEmbeddedZookeeper();

private static String servers;

Expand Down Expand Up @@ -202,6 +196,10 @@ void createsProducerAndSendsMessage() throws Exception {
final AdminClient admin = AdminClient.create(
ImmutableMap.of(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, EntryTest.servers)
);
new CreateTopics(
admin,
new NewTopic("TEST-TOPIC", 1, (short) 1)
).value().get(30L, TimeUnit.SECONDS);
final KafkaProducer<String, String> producer = new KafkaProducer<>(
ImmutableMap.of(
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
Expand All @@ -224,12 +222,6 @@ void createsProducerAndSendsMessage() throws Exception {
new StringDeserializer(),
new StringDeserializer()
);
final Collection<NewTopic> topics =
Collections.singletonList(
new NewTopic("TEST-TOPIC", 1, (short) 1)
);
admin.createTopics(topics)
.all().get(30L, TimeUnit.SECONDS);
consumer.subscribe(Collections.singletonList("TEST-TOPIC"));
producer.send(new ProducerRecord<>("TEST-TOPIC", "testcontainers", "rulezzz")).get();
Unreliables.retryUntilTrue(
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/admin/CreateTopics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2022 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.admin;

import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.KafkaFuture;
import org.cactoos.Scalar;
import org.cactoos.list.ListOf;

import java.util.List;

/**
* Create topics.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.1.3
*/
public final class CreateTopics implements Scalar<KafkaFuture<Void>> {

/**
* Admin.
*/
private final AdminClient admin;
/**
* Topics to create.
*/
private final List<NewTopic> topics;

/**
* Ctor.
*
* @param admn Kafka Admin
* @param tpcs Topics to create
* @see AdminClient
* @see NewTopic
*/
public CreateTopics(final AdminClient admn, final NewTopic... tpcs) {
this.admin = admn;
this.topics = new ListOf<>(tpcs);
}

@Override
public KafkaFuture<Void> value() throws Exception {
return this.admin.createTopics(this.topics)
.all();
}
}
68 changes: 68 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/admin/ListTopics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2022 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.admin;

import org.apache.kafka.clients.admin.AdminClient;
import org.cactoos.Scalar;

import java.util.List;

/**
* List all topics.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.1.3
*/
public final class ListTopics implements Scalar<List<String>> {

/**
* Admin.
*/
private final AdminClient admin;

/**
* Ctor.
*
* @param admn Kafka Admin
* @see AdminClient
*/
public ListTopics(final AdminClient admn) {
this.admin = admn;
}

@Override
public List<String> value() throws Exception {
return this.admin.listTopics()
.listings()
.get()
.stream()
.map(listing ->
"id: %s, name: %s, internal: %s"
.formatted(
listing.topicId().toString(),
listing.name(),
listing.isInternal()
)
).toList();
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/admin/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2022 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* Kafka Admin interactions.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.1.3
*/
package io.github.eocqrs.kafka.admin;

0 comments on commit 5464d25

Please sign in to comment.