Skip to content

Commit 5e066bc

Browse files
author
h1alexbel
committed
1 parent 55fc525 commit 5e066bc

8 files changed

Lines changed: 200 additions & 4 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ SOFTWARE.
9595
<assert4j-core.version>3.24.2</assert4j-core.version>
9696
<xfake.version>0.1.2</xfake.version>
9797
<xembly.version>0.28.1</xembly.version>
98+
<eokson.version>0.3.2</eokson.version>
9899
<maven-surefire-plugin.version>3.1.2</maven-surefire-plugin.version>
99100
<maven-verifier-plugin.version>1.1</maven-verifier-plugin.version>
100101
<slf4j.version>2.0.7</slf4j.version>
@@ -125,6 +126,11 @@ SOFTWARE.
125126
<artifactId>xfake</artifactId>
126127
<version>${xfake.version}</version>
127128
</dependency>
129+
<dependency>
130+
<groupId>io.github.eo-cqrs</groupId>
131+
<artifactId>eokson</artifactId>
132+
<version>${eokson.version}</version>
133+
</dependency>
128134
<dependency>
129135
<groupId>com.jcabi</groupId>
130136
<artifactId>jcabi-xml</artifactId>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package io.github.eocqrs.kafka.json;
24+
25+
import com.jcabi.xml.XMLDocument;
26+
import io.github.eocqrs.eokson.JsonOf;
27+
import io.github.eocqrs.eokson.JsonXML;
28+
import io.github.eocqrs.kafka.ConsumerSettings;
29+
import io.github.eocqrs.kafka.ProducerSettings;
30+
import io.github.eocqrs.kafka.xml.ConsumerXmlMapParams;
31+
import io.github.eocqrs.kafka.xml.ProducerXmlMapParams;
32+
import lombok.SneakyThrows;
33+
import org.apache.kafka.clients.consumer.KafkaConsumer;
34+
import org.apache.kafka.clients.producer.KafkaProducer;
35+
import org.cactoos.io.ResourceOf;
36+
37+
/**
38+
* Allow creating custom Consumer/Producer from JSON.
39+
*
40+
* @param <K> The key
41+
* @param <X> The value
42+
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
43+
* @since 0.4.6
44+
*/
45+
public final class KfJsonFlexible<K, X>
46+
implements ConsumerSettings<K, X>, ProducerSettings<K, X> {
47+
48+
/**
49+
* JSON name.
50+
*/
51+
private final String name;
52+
53+
/**
54+
* Ctor.
55+
*
56+
* @param nm JSON name
57+
*/
58+
public KfJsonFlexible(final String nm) {
59+
this.name = nm;
60+
}
61+
62+
@Override
63+
@SneakyThrows
64+
public KafkaConsumer<K, X> consumer() {
65+
return new KafkaConsumer<>(
66+
new ConsumerXmlMapParams(
67+
new XMLDocument(
68+
new JsonXML(
69+
new JsonOf(
70+
new ResourceOf(
71+
this.name
72+
).stream()
73+
),
74+
"consumer"
75+
).asString()
76+
)
77+
).value()
78+
);
79+
}
80+
81+
@Override
82+
@SneakyThrows
83+
public KafkaProducer<K, X> producer() {
84+
return new KafkaProducer<>(
85+
new ProducerXmlMapParams(
86+
new XMLDocument(
87+
new JsonXML(
88+
new JsonOf(
89+
new ResourceOf(
90+
this.name
91+
).stream()
92+
),
93+
"producer"
94+
).asString()
95+
)
96+
).value()
97+
);
98+
}
99+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
/**
24+
* JSON.
25+
*
26+
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
27+
* @since 0.4.6
28+
*/
29+
package io.github.eocqrs.kafka.json;

src/main/java/io/github/eocqrs/kafka/xml/ConsumerXmlMapParams.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
3232
* @since 0.0.2
3333
*/
34-
final class ConsumerXmlMapParams extends XmlMapParams {
34+
public final class ConsumerXmlMapParams extends XmlMapParams {
3535

3636
/**
3737
* Ctor.
3838
*
3939
* @param config XML config.
4040
*/
41-
ConsumerXmlMapParams(final XML config) {
41+
public ConsumerXmlMapParams(final XML config) {
4242
super(config, KfCustomer.CONSUMER);
4343
}
4444

src/main/java/io/github/eocqrs/kafka/xml/ProducerXmlMapParams.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
3232
* @since 0.0.2
3333
*/
34-
final class ProducerXmlMapParams extends XmlMapParams {
34+
public final class ProducerXmlMapParams extends XmlMapParams {
3535

3636
/**
3737
* Ctor.
3838
*
3939
* @param config XML config.
4040
*/
41-
ProducerXmlMapParams(final XML config) {
41+
public ProducerXmlMapParams(final XML config) {
4242
super(config, KfCustomer.PRODUCER);
4343
}
4444

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package io.github.eocqrs.kafka.json;
24+
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.Test;
27+
28+
/**
29+
* Test case for {@link KfJsonFlexible}.
30+
*
31+
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
32+
* @since 0.4.6
33+
*/
34+
final class KfJsonFlexibleTest {
35+
36+
@Test
37+
void createsCustomConsumer() {
38+
Assertions.assertDoesNotThrow(
39+
() -> new KfJsonFlexible<String, String>("consumer.json")
40+
.consumer()
41+
);
42+
}
43+
44+
@Test
45+
void createsCustomProducer() {
46+
Assertions.assertDoesNotThrow(
47+
() -> new KfJsonFlexible<String, String>("producer.json")
48+
.producer()
49+
);
50+
}
51+
}

src/test/resources/consumer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"bootstrapServers": "localhost:9092",
3+
"groupId": "1",
4+
"keyDeserializer": "org.apache.kafka.common.serialization.StringDeserializer",
5+
"valueDeserializer": "org.apache.kafka.common.serialization.StringDeserializer"
6+
}

src/test/resources/producer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"bootstrapServers": "localhost:9092",
3+
"keySerializer": "org.apache.kafka.common.serialization.StringSerializer",
4+
"valueSerializer": "org.apache.kafka.common.serialization.StringSerializer"
5+
}

0 commit comments

Comments
 (0)