-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
254 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/main/java/io/github/eocqrs/kafka/json/KfJsonFlexible.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/main/java/io/github/eocqrs/kafka/json/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/java/io/github/eocqrs/kafka/json/KfJsonFlexibleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |