Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jul 7, 2023
2 parents 292d9ca + b49e487 commit f35f269
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 5 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ btw, your [XML](https://en.wikipedia.org/wiki/XML#:~:text=Extensible%20Markup%20
</producer>
```

Since version `0.4.6` you can create Producer with JSON file:
```java
final Producer<String, String> producer =
new KfProducer<>(
new KfJsonFlexible<String, String>("producer.json") // file with producer config
);
```

Your [JSON](https://en.wikipedia.org/wiki/JSON), located in resources directory, should looks like this:
```json
{
"bootstrapServers": "localhost:9092",
"keySerializer": "org.apache.kafka.common.serialization.StringSerializer",
"valueSerializer": "org.apache.kafka.common.serialization.StringSerializer"
}
```

To send a [message](#messages):
```java
try (final Producer<String, String> producer = ...) {
Expand Down Expand Up @@ -215,6 +232,24 @@ Again, [XML](https://en.wikipedia.org/wiki/XML#:~:text=Extensible%20Markup%20Lan
</consumer>
```

Since version `0.4.6` you can create Consumer with JSON file:
```java
final Consumer<String, String> producer =
new KfConsumer<>(
new KfJsonFlexible<String, String>("consumer.json") // file with producer config
);
```

Your [JSON](https://en.wikipedia.org/wiki/JSON), located in resources directory, should looks like this:
```json
{
"bootstrapServers": "localhost:9092",
"groupId": "1",
"keyDeserializer": "org.apache.kafka.common.serialization.StringDeserializer",
"valueDeserializer": "org.apache.kafka.common.serialization.StringDeserializer"
}
```

Consuming [messages](#messages):
```java
try (
Expand Down Expand Up @@ -426,7 +461,7 @@ Under the hood XML document will looks like this:
**By the version `0.3.5`, eo-kafka support only String values in FkConsumer**.

## Config API
| Kafka Property | eo-kafka API | XML tag
| Kafka Property | eo-kafka API | XML/JSON tag
|-----------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------| ----------------------
| `bootstrap.servers` | [BootstrapServers](https://github.com/eo-cqrs/eo-kafka/blob/master/src/main/java/io/github/eocqrs/kafka/parameters/BootstrapServers.java) | bootstrapServers
| `key.serializer` | [KeySerializer](https://github.com/eo-cqrs/eo-kafka/blob/master/src/main/java/io/github/eocqrs/kafka/parameters/KeySerializer.java) | keySerializer
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ SOFTWARE.
<assert4j-core.version>3.24.2</assert4j-core.version>
<xfake.version>0.1.2</xfake.version>
<xembly.version>0.28.1</xembly.version>
<eokson.version>0.3.2</eokson.version>
<maven-surefire-plugin.version>3.1.2</maven-surefire-plugin.version>
<maven-verifier-plugin.version>1.1</maven-verifier-plugin.version>
<slf4j.version>2.0.7</slf4j.version>
Expand Down Expand Up @@ -125,6 +126,11 @@ SOFTWARE.
<artifactId>xfake</artifactId>
<version>${xfake.version}</version>
</dependency>
<dependency>
<groupId>io.github.eo-cqrs</groupId>
<artifactId>eokson</artifactId>
<version>${eokson.version}</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-xml</artifactId>
Expand Down
99 changes: 99 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/json/KfJsonFlexible.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2023 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.json;

import com.jcabi.xml.XMLDocument;
import io.github.eocqrs.eokson.JsonOf;
import io.github.eocqrs.eokson.JsonXML;
import io.github.eocqrs.kafka.ConsumerSettings;
import io.github.eocqrs.kafka.ProducerSettings;
import io.github.eocqrs.kafka.xml.ConsumerXmlMapParams;
import io.github.eocqrs.kafka.xml.ProducerXmlMapParams;
import lombok.SneakyThrows;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.cactoos.io.ResourceOf;

/**
* Allow creating custom Consumer/Producer from JSON.
*
* @param <K> The key
* @param <X> The value
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.4.6
*/
public final class KfJsonFlexible<K, X>
implements ConsumerSettings<K, X>, ProducerSettings<K, X> {

/**
* JSON name.
*/
private final String name;

/**
* Ctor.
*
* @param nm JSON name
*/
public KfJsonFlexible(final String nm) {
this.name = nm;
}

@Override
@SneakyThrows
public KafkaConsumer<K, X> consumer() {
return new KafkaConsumer<>(
new ConsumerXmlMapParams(
new XMLDocument(
new JsonXML(
new JsonOf(
new ResourceOf(
this.name
).stream()
),
"consumer"
).asString()
)
).value()
);
}

@Override
@SneakyThrows
public KafkaProducer<K, X> producer() {
return new KafkaProducer<>(
new ProducerXmlMapParams(
new XMLDocument(
new JsonXML(
new JsonOf(
new ResourceOf(
this.name
).stream()
),
"producer"
).asString()
)
).value()
);
}
}
29 changes: 29 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/json/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2023 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.
*/

/**
* JSON.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.4.6
*/
package io.github.eocqrs.kafka.json;
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.0.2
*/
final class ConsumerXmlMapParams extends XmlMapParams {
public final class ConsumerXmlMapParams extends XmlMapParams {

/**
* Ctor.
*
* @param config XML config.
*/
ConsumerXmlMapParams(final XML config) {
public ConsumerXmlMapParams(final XML config) {
super(config, KfCustomer.CONSUMER);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.0.2
*/
final class ProducerXmlMapParams extends XmlMapParams {
public final class ProducerXmlMapParams extends XmlMapParams {

/**
* Ctor.
*
* @param config XML config.
*/
ProducerXmlMapParams(final XML config) {
public ProducerXmlMapParams(final XML config) {
super(config, KfCustomer.PRODUCER);
}

Expand Down
69 changes: 69 additions & 0 deletions src/test/java/io/github/eocqrs/kafka/json/KfJsonFlexibleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2023 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.json;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link KfJsonFlexible}.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.4.6
*/
final class KfJsonFlexibleTest {

@Test
void createsCustomConsumer() {
Assertions.assertDoesNotThrow(
() -> new KfJsonFlexible<String, String>("consumer.json")
.consumer()
);
}

@Test
void createsCustomProducer() {
Assertions.assertDoesNotThrow(
() -> new KfJsonFlexible<String, String>("producer.json")
.producer()
);
}

@Test
void throwsOnNonExistingProducerFile() {
Assertions.assertThrows(
Exception.class,
() -> new KfJsonFlexible<String, String>("abc.json")
.producer()
);
}

@Test
void throwsOnNonExistingConsumerFile() {
Assertions.assertThrows(
Exception.class,
() -> new KfJsonFlexible<String, String>("cdf.json")
.consumer()
);
}
}
6 changes: 6 additions & 0 deletions src/test/resources/consumer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"bootstrapServers": "localhost:9092",
"groupId": "1",
"keyDeserializer": "org.apache.kafka.common.serialization.StringDeserializer",
"valueDeserializer": "org.apache.kafka.common.serialization.StringDeserializer"
}
5 changes: 5 additions & 0 deletions src/test/resources/producer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bootstrapServers": "localhost:9092",
"keySerializer": "org.apache.kafka.common.serialization.StringSerializer",
"valueSerializer": "org.apache.kafka.common.serialization.StringSerializer"
}

0 comments on commit f35f269

Please sign in to comment.