Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jul 7, 2023
1 parent 55fc525 commit 5e066bc
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 4 deletions.
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

Check warning on line 63 in src/main/java/io/github/eocqrs/kafka/json/KfJsonFlexible.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/github/eocqrs/kafka/json/KfJsonFlexible.java#L63

Added line #L63 was not covered by tests
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

Check warning on line 82 in src/main/java/io/github/eocqrs/kafka/json/KfJsonFlexible.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/github/eocqrs/kafka/json/KfJsonFlexible.java#L82

Added line #L82 was not covered by tests
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
51 changes: 51 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,51 @@
/*
* 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()
);
}
}
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 5e066bc

Please sign in to comment.