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 10, 2023
2 parents a38f729 + 2e31139 commit 5fe9022
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 20 deletions.
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.addLombokGeneratedAnnotation = true
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ SOFTWARE.
<maven-gpg-plugin.version>3.1.0</maven-gpg-plugin.version>
<mockito-core.version>5.4.0</mockito-core.version>
<mockito-junit-jupiter.version>5.4.0</mockito-junit-jupiter.version>
<snakeyaml.version>2.0</snakeyaml.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -140,6 +141,11 @@ SOFTWARE.
<artifactId>xembly</artifactId>
<version>${xembly.version}</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-immutable</artifactId>
Expand Down
43 changes: 23 additions & 20 deletions src/main/java/io/github/eocqrs/kafka/xml/XmlMapParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.cactoos.Scalar;
import org.cactoos.io.ResourceOf;

import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -72,7 +73,7 @@ protected XmlMapParams(final XML config, final KfCustomer cust) {
* A ctor that takes an Input and converts it to XMLDocument.
*
* @param resource Resource with settings.
* @param cust Customer type.
* @param cust Customer type.
* @throws Exception When something went wrong.
*/
protected XmlMapParams(final Input resource, final KfCustomer cust)
Expand All @@ -94,25 +95,27 @@ protected XmlMapParams(final String name, final KfCustomer cust)

@Override
public final Map<String, Object> value() throws Exception {
return new XMLDocument(this.configuration.toString())
.nodes("//%s/*".formatted(this.customer))
.stream()
.map(Object::toString)
.map(XMLDocument::new)
.map(xml -> xml.nodes("//*").get(0).node().getNodeName())
.collect(
Collectors.toMap(
name ->
XmlMapParams.CAPITALS
.matcher(name)
.replaceAll(".$1")
.toLowerCase(Locale.ROOT),
name ->
new TextXpath(
this.configuration,
"//%s".formatted(name)
).toString()
return Collections.unmodifiableMap(
new XMLDocument(this.configuration.toString())
.nodes("//%s/*".formatted(this.customer))
.stream()
.map(Object::toString)
.map(XMLDocument::new)
.map(xml -> xml.nodes("//*").get(0).node().getNodeName())
.collect(
Collectors.toMap(
name ->
XmlMapParams.CAPITALS
.matcher(name)
.replaceAll(".$1")
.toLowerCase(Locale.ROOT),
name ->
new TextXpath(
this.configuration,
"//%s".formatted(name)
).toString()
)
)
);
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.yaml;

import io.github.eocqrs.kafka.ConsumerSettings;
import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.consumer.KafkaConsumer;

/**
* Yaml configuration for consumer.
*
* @param <K> The key type.
* @param <X> The value type.
*/
@RequiredArgsConstructor
public final class KfYamlConsumerSettings<K, X>
implements ConsumerSettings<K, X> {

/**
* YAML params.
*/
private final YamlMapParams<K, X> params;

@Override
public KafkaConsumer<K, X> consumer() {
return new KafkaConsumer<>(this.params.value());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.yaml;

import io.github.eocqrs.kafka.ProducerSettings;
import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.producer.KafkaProducer;

/**
* Yaml configuration for producer.
*
* @param <K> The key type.
* @param <X> The value type.
*/
@RequiredArgsConstructor
public final class KfYamlProducerSettings<K, X>
implements ProducerSettings<K, X> {

/**
* YAML params.
*/
private final YamlMapParams<K, X> params;

@Override
public KafkaProducer<K, X> producer() {
return new KafkaProducer<>(this.params.value());
}
}
92 changes: 92 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/yaml/YamlMapParams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.yaml;

import lombok.RequiredArgsConstructor;
import org.cactoos.Input;
import org.cactoos.Scalar;
import org.cactoos.io.ResourceOf;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* This class converts a YAML source from
* kebab case keys into kafka specific keys Map.
*
* @param <K> The key type.
* @param <X> The value type.
*/
@RequiredArgsConstructor
public class YamlMapParams<K, X> implements Scalar<Map<String, Object>> {

/**
* The config value.
*/
private final Map<String, Object> value;

/**
* Filename ctor.
*
* @param name Config name
* @throws Exception When something went wrong
*/
public YamlMapParams(final String name) throws Exception {
this(new ResourceOf(name));
}

/**
* Input ctor.
*
* @param input Input source
* @throws Exception When something went wrong
*/
public YamlMapParams(final Input input) throws Exception {
this(input.stream());
}

/**
* Primary ctor.
*
* @param stream Input source
*/
public YamlMapParams(final InputStream stream) {
this.value = new Yaml().load(stream);
}

@Override
public final Map<String, Object> value() {
final Map<String, Object> accum = new HashMap<>(0);
this.value
.forEach(
(key, val) -> accum.put(
key.replace('-', '.'),
val
)
);
return Collections.unmodifiableMap(accum);
}
}
26 changes: 26 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/yaml/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*/

/**
* YAML.
*/
package io.github.eocqrs.kafka.yaml;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.yaml;

import io.github.eocqrs.kafka.consumer.KfConsumer;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

/**
* Test case for {@link KfYamlProducerSettings}.
*/
class KfYamlConsumerSettingsTest {

@Test
void createsConsumerFromYamlConfiguration() {
assertDoesNotThrow(
() ->
new KfConsumer<>(
new KfYamlConsumerSettings<>(
new YamlMapParams<String, String>("consumer.yaml")
)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.yaml;

import io.github.eocqrs.kafka.producer.KfProducer;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

/**
* Test case for {@link KfYamlConsumerSettings}.
*/
class KfYamlProducerSettingsTest {

@Test
void createsProducerFromYaml() {
assertDoesNotThrow(
() -> new KfProducer<>(
new KfYamlProducerSettings<>(
new YamlMapParams<String, String>(
"producer.yaml"
)
)
)
);
}
}
Loading

0 comments on commit 5fe9022

Please sign in to comment.