Skip to content

Commit 0a90d87

Browse files
committed
Merge branch '__rultor'
2 parents 2f0eb59 + 5e71c5e commit 0a90d87

6 files changed

Lines changed: 299 additions & 15 deletions

File tree

src/main/java/io/github/eocqrs/kafka/fake/FkProducer.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,13 @@ public Future<RecordMetadata> send(
8080
final K key,
8181
final Data<X> message
8282
) throws Exception {
83-
final boolean exists = this.broker.data(
84-
"broker/topics/topic[name = '%s']/name/text()"
85-
.formatted(message.topic())
86-
).stream()
87-
.anyMatch(s ->
88-
s.equals(message.topic())
89-
);
90-
if (!exists) {
91-
throw new IllegalArgumentException(
92-
"topic %s does not exits!"
93-
.formatted(
94-
message.topic()
95-
)
96-
);
97-
}
83+
new ThrowsOnFalse(
84+
new TopicExists(message.topic(), this.broker),
85+
"topic %s does not exists!"
86+
.formatted(
87+
message.topic()
88+
)
89+
).value();
9890
this.broker.with(new DatasetDirs<>(key, message).value());
9991
final RecordMetadata metadata = new RecordMetadata(
10092
new TopicPartition(
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.fake;
24+
25+
import org.cactoos.Scalar;
26+
27+
/**
28+
* Throwing exception on a False logical statement.
29+
*
30+
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
31+
* @since 0.3.5
32+
*/
33+
public final class ThrowsOnFalse implements Scalar<Boolean> {
34+
35+
/**
36+
* Logical statement.
37+
*/
38+
private final Scalar<Boolean> scalar;
39+
/**
40+
* Error message.
41+
*/
42+
private final String message;
43+
44+
/**
45+
* Ctor.
46+
*
47+
* @param sclr Boolean scalar
48+
* @param msg Error Message
49+
*/
50+
public ThrowsOnFalse(final Scalar<Boolean> sclr, final String msg) {
51+
this.scalar = sclr;
52+
this.message = msg;
53+
}
54+
55+
@Override
56+
public Boolean value() throws Exception {
57+
if (!this.scalar.value()) {
58+
throw new IllegalArgumentException(
59+
this.message
60+
);
61+
}
62+
return true;
63+
}
64+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.fake;
24+
25+
import org.cactoos.Scalar;
26+
27+
/**
28+
* Topic Exists or not.
29+
*
30+
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
31+
* @since 0.3.5
32+
*/
33+
public final class TopicExists implements Scalar<Boolean> {
34+
35+
/**
36+
* Topic to check.
37+
*/
38+
private final String topic;
39+
/**
40+
* Broker.
41+
*/
42+
private final FkBroker broker;
43+
44+
/**
45+
* Ctor.
46+
*
47+
* @param tpc Topic to check
48+
* @param brkr Broker
49+
*/
50+
public TopicExists(final String tpc, final FkBroker brkr) {
51+
this.topic = tpc;
52+
this.broker = brkr;
53+
}
54+
55+
@Override
56+
public Boolean value() throws Exception {
57+
return this.broker.data(
58+
"broker/topics/topic[name = '%s']/name/text()"
59+
.formatted(this.topic)
60+
).stream()
61+
.anyMatch(s ->
62+
s.equals(this.topic)
63+
);
64+
}
65+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.fake;
24+
25+
import org.hamcrest.MatcherAssert;
26+
import org.hamcrest.Matchers;
27+
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.api.Test;
29+
30+
/**
31+
* Test case for {@link ThrowsOnFalse}.
32+
*
33+
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
34+
* @since 0.3.5
35+
*/
36+
final class ThrowsOnFalseTest {
37+
38+
@Test
39+
void throwsOnFalse() {
40+
final String msg = "test message";
41+
final String message = Assertions.assertThrows(IllegalArgumentException.class,
42+
() -> new ThrowsOnFalse(
43+
() -> false, msg
44+
).value()
45+
).getMessage();
46+
MatcherAssert.assertThat(
47+
"Exception message in right format",
48+
message,
49+
Matchers.equalTo(msg)
50+
);
51+
}
52+
53+
@Test
54+
void returnsTrueOnTrue() throws Exception {
55+
MatcherAssert.assertThat(
56+
"Returns true on true statement",
57+
new ThrowsOnFalse(() -> true, "test").value(),
58+
Matchers.equalTo(true)
59+
);
60+
}
61+
62+
@Test
63+
void doesNotThrowOnTrue() {
64+
Assertions.assertDoesNotThrow(
65+
() -> new ThrowsOnFalse(
66+
() -> true, "msg"
67+
).value()
68+
);
69+
}
70+
}

src/test/java/io/github/eocqrs/kafka/fake/TopicDirsTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
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+
123
package io.github.eocqrs.kafka.fake;
224

325
import org.hamcrest.MatcherAssert;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.fake;
24+
25+
import io.github.eocqrs.xfake.InFile;
26+
import io.github.eocqrs.xfake.Synchronized;
27+
import org.hamcrest.MatcherAssert;
28+
import org.hamcrest.Matchers;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
32+
/**
33+
* Test case for {@link TopicExists}.
34+
*
35+
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
36+
* @since 0.3.5
37+
*/
38+
final class TopicExistsTest {
39+
40+
private FkBroker broker;
41+
42+
@BeforeEach
43+
void setUp() throws Exception {
44+
this.broker = new InXml(
45+
new Synchronized(
46+
new InFile(
47+
"topic-exists-test",
48+
"<broker/>"
49+
)
50+
)
51+
).with(new TopicDirs("1.test").value());
52+
}
53+
54+
@Test
55+
void returnsTrueOnExistingTopic() throws Exception {
56+
MatcherAssert.assertThat(
57+
"Topic is present",
58+
new TopicExists("1.test", this.broker).value(),
59+
Matchers.equalTo(true)
60+
);
61+
}
62+
63+
@Test
64+
void returnsFalseOnUnknownTopic() throws Exception {
65+
MatcherAssert.assertThat(
66+
"Topic is not present",
67+
new TopicExists("unknown.test", this.broker).value(),
68+
Matchers.equalTo(false)
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)