Skip to content

Commit 5b962e3

Browse files
authored
Merge pull request #42 from VaryaGet/main
Add bot envelope
2 parents 5575483 + bac016d commit 5b962e3

File tree

5 files changed

+271
-7
lines changed

5 files changed

+271
-7
lines changed

core/pom.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
~ SOFTWARE.
2323
-->
24-
<project xmlns="http://maven.apache.org/POM/4.0.0"
25-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
24+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
25+
xmlns="http://maven.apache.org/POM/4.0.0"
2626
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2727
<modelVersion>4.0.0</modelVersion>
2828
<parent>
@@ -47,6 +47,12 @@
4747
<version>1.3</version>
4848
<scope>test</scope>
4949
</dependency>
50+
<dependency>
51+
<groupId>org.cactoos</groupId>
52+
<artifactId>cactoos</artifactId>
53+
<version>0.57.0</version>
54+
<scope>compile</scope>
55+
</dependency>
5056
</dependencies>
5157
<build>
5258
<sourceDirectory>src</sourceDirectory>
@@ -62,11 +68,6 @@
6268
<artifactId>qulice-maven-plugin</artifactId>
6369
<version>0.24.0</version>
6470
<configuration>
65-
<excludes>
66-
<exclude>
67-
pmd:/src/test/java/com/github/artemget/teleroute/.*
68-
</exclude>
69-
</excludes>
7071
<license>file:${basedir}/LICENSE</license>
7172
</configuration>
7273
<executions>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024-2026. Artem Getmanskii
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
package io.github.artemget.teleroute.bot;
27+
28+
import io.github.artemget.teleroute.update.Wrap;
29+
30+
/**
31+
* Bot. Entrance to your bot construction.
32+
*
33+
* @param <U> Update
34+
* @since 2.0.0
35+
*/
36+
public interface Bot<U> {
37+
/**
38+
* Start handle update.
39+
*
40+
* @param update Update wrap
41+
*/
42+
void handle(Wrap<U> update) throws Exception;
43+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024-2026. Artem Getmanskii
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
package io.github.artemget.teleroute.bot;
27+
28+
import io.github.artemget.teleroute.route.Route;
29+
import io.github.artemget.teleroute.route.RouteDfs;
30+
import io.github.artemget.teleroute.update.Wrap;
31+
import org.cactoos.proc.CheckedProc;
32+
import org.cactoos.proc.UncheckedProc;
33+
import org.cactoos.scalar.Unchecked;
34+
35+
/**
36+
* Bot envelope helps create bot signature.
37+
*
38+
* @param <U> Update
39+
* @param <C> Client
40+
* @since 2.0.0
41+
*/
42+
public final class BotEnvelope<U, C> implements Bot<U> {
43+
/**
44+
* Client.
45+
*/
46+
private final C client;
47+
48+
/**
49+
* Route.
50+
*/
51+
private final Route<U, C> route;
52+
53+
/**
54+
* Secondary constructor.
55+
*
56+
* @param client Client
57+
* @param routes Routes
58+
*/
59+
@SafeVarargs
60+
public BotEnvelope(final C client, final Route<U, C>... routes) {
61+
this(client, new RouteDfs<>(routes));
62+
}
63+
64+
/**
65+
* Main constructor.
66+
*
67+
* @param client Client
68+
* @param route Route
69+
*/
70+
public BotEnvelope(final C client, final Route<U, C> route) {
71+
this.client = client;
72+
this.route = route;
73+
}
74+
75+
@Override
76+
public void handle(final Wrap<U> update) throws Exception {
77+
new CheckedProc<>(
78+
(Wrap<U> u) -> this.route.route(update)
79+
.map(
80+
cmd -> new Unchecked<>(() -> cmd.execute(update.src())).value()
81+
).ifPresent(send -> new UncheckedProc<>(send::send).exec(this.client)),
82+
ex -> new Exception("Failed to handle update", ex)
83+
).exec(update);
84+
}
85+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024-2026. Artem Getmanskii
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
package io.github.artemget.teleroute.bot;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024-2026. Artem Getmanskii
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
package io.github.artemget.teleroute.bot;
27+
28+
import io.github.artemget.teleroute.command.FkCmd;
29+
import io.github.artemget.teleroute.command.FkCmdErr;
30+
import io.github.artemget.teleroute.route.RouteEnd;
31+
import io.github.artemget.teleroute.send.FkClient;
32+
import io.github.artemget.teleroute.send.FkSend;
33+
import io.github.artemget.teleroute.send.FkSendErr;
34+
import io.github.artemget.teleroute.update.FkWrap;
35+
import org.hamcrest.MatcherAssert;
36+
import org.hamcrest.Matchers;
37+
import org.junit.jupiter.api.Assertions;
38+
import org.junit.jupiter.api.Test;
39+
40+
/**
41+
* Test case {@link BotEnvelope}.
42+
*
43+
* @since 2.0.0
44+
*/
45+
final class BotEnvelopeTest {
46+
47+
@Test
48+
void handlesUpdateWhenRouteReturnsCommand() throws Exception {
49+
final FkClient client = new FkClient();
50+
new BotEnvelope<>(client,
51+
new RouteEnd<>(
52+
new FkCmd(
53+
new FkSend("response")
54+
)
55+
)
56+
).handle(new FkWrap());
57+
MatcherAssert.assertThat(
58+
"Did not send response",
59+
client.sent(),
60+
Matchers.equalTo(java.util.Collections.singletonList("response"))
61+
);
62+
}
63+
64+
@Test
65+
void handlesUpdateWhenRouteReturnsEmpty() throws Exception {
66+
final FkClient client = new FkClient();
67+
new BotEnvelope<>(client,
68+
new RouteEnd<>(
69+
new FkCmd()))
70+
.handle(new FkWrap());
71+
MatcherAssert.assertThat(
72+
"Sent response when route returned empty",
73+
client.sent().isEmpty(),
74+
Matchers.is(true)
75+
);
76+
}
77+
78+
@Test
79+
void throwsExceptionWhenCommandExecutionFails() {
80+
final BotEnvelope<String, FkClient> bot =
81+
new BotEnvelope<>(new FkClient(),
82+
new RouteEnd<>(
83+
new FkCmdErr()
84+
));
85+
Assertions.assertThrows(
86+
Exception.class,
87+
() -> bot.handle(new FkWrap()),
88+
"Expected Exception when command execution fails"
89+
);
90+
}
91+
92+
@Test
93+
void throwsExceptionWhenSendFails() {
94+
final BotEnvelope<String, FkClient> bot =
95+
new BotEnvelope<>(
96+
new FkClient(),
97+
new RouteEnd<>(
98+
new FkCmd(
99+
new FkSendErr()
100+
)
101+
)
102+
);
103+
Assertions.assertThrows(
104+
Exception.class,
105+
() -> bot.handle(new FkWrap()),
106+
"Expected Exception when send fails"
107+
);
108+
}
109+
}

0 commit comments

Comments
 (0)