From eeaabdaaf1c430b8680da4933a00b9f0733fc35c Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 12 Feb 2023 01:29:37 +0400 Subject: [PATCH 1/5] fixed it --- .../com/elegramapi/simpleit/ItSimpleTest.java | 58 +++++++++++++-- .../java/com/l3r8yj/elegramapi/bot/Bot.java | 2 +- .../com/l3r8yj/elegramapi/bot/BtDefault.java | 71 +++++++++---------- .../elegramapi/request/TRqEnvelope.java | 8 ++- .../l3r8yj/elegramapi/request/TRqPost.java | 10 ++- .../elegramapi/request/TRqWithChatId.java | 15 ++-- .../elegramapi/request/TRqWithOffset.java | 15 ++-- .../elegramapi/request/TRqWithText.java | 15 ++-- .../elegramapi/request/TelegramRequest.java | 16 +++-- .../elegramapi/request/TRqPostTest.java | 51 +++++++++++++ .../elegramapi/request/TRqWithChatIdTest.java | 5 +- .../elegramapi/request/TRqWithOffsetTest.java | 5 +- .../elegramapi/request/TRqWithTextTest.java | 5 +- 13 files changed, 191 insertions(+), 85 deletions(-) create mode 100644 src/test/java/com/l3r8yj/elegramapi/request/TRqPostTest.java diff --git a/src/it/simple-it/src/test/java/com/elegramapi/simpleit/ItSimpleTest.java b/src/it/simple-it/src/test/java/com/elegramapi/simpleit/ItSimpleTest.java index d1fe1a9..6cc3bce 100644 --- a/src/it/simple-it/src/test/java/com/elegramapi/simpleit/ItSimpleTest.java +++ b/src/it/simple-it/src/test/java/com/elegramapi/simpleit/ItSimpleTest.java @@ -24,6 +24,7 @@ package com.elegramapi.simpleit; +import com.jcabi.http.response.JsonResponse; import com.l3r8yj.elegramapi.bot.Bot; import com.l3r8yj.elegramapi.bot.BtDefault; import com.l3r8yj.elegramapi.command.Command; @@ -31,6 +32,8 @@ import javax.ws.rs.core.Response; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; /** @@ -40,17 +43,40 @@ */ final class ItSimpleTest { + /** + * Under test bot. + */ + private Bot bot; + + @BeforeEach + void setUp() { + this.bot = new BtDefault( + "5735860614:AAHsneN3fWj76dfXejtdSmNGLf4kq-bUGgg", + new CmdStart(), + new CmdEcho() + ); + } + + @Test + @Disabled + void runsTheBot() throws Exception { + this.bot.start(); + } + @Test void sendsTheMessage() { + final JsonResponse response = this.bot.sendMessage( + 389_133_054, + "Hi, i'm Ruby!" + ); MatcherAssert.assertThat( - new BtDefault( - "5735860614:AAHsneN3fWj76dfXejtdSmNGLf4kq-bUGgg", - new TestCommand() - ) - .sendMessage(389_133_054, "Hi, i'm Ruby!") - .status(), + response.status(), Matchers.equalTo(Response.Status.OK.getStatusCode()) ); + MatcherAssert.assertThat( + response.back().toString().contains("POST"), + Matchers.equalTo(true) + ); } /** @@ -58,7 +84,7 @@ void sendsTheMessage() { * * @since 0.0.0 */ - static class TestCommand implements Command { + static class CmdStart implements Command { @Override public final void act(final Update update, final Bot bot) { @@ -67,4 +93,22 @@ public final void act(final Update update, final Bot bot) { } } } + + /** + * The echo command. + * + * @since 0.0.0 + */ + static class CmdEcho implements Command { + + @Override + public void act(final Update update, final Bot bot) { + if (!update.message().text().isEmpty()) { + bot.sendMessage( + update.message().chatId(), + update.message().text() + ); + } + } + } } diff --git a/src/main/java/com/l3r8yj/elegramapi/bot/Bot.java b/src/main/java/com/l3r8yj/elegramapi/bot/Bot.java index 2259f06..883d409 100644 --- a/src/main/java/com/l3r8yj/elegramapi/bot/Bot.java +++ b/src/main/java/com/l3r8yj/elegramapi/bot/Bot.java @@ -37,7 +37,7 @@ public interface Bot { * * @throws Exception If something went wrong */ - void run() throws Exception; + void start() throws Exception; /** * Sends the message. diff --git a/src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java b/src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java index b00d181..89fd0ef 100644 --- a/src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java +++ b/src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java @@ -25,7 +25,11 @@ * @todo #60 Design/ Testing. * Write tests for DefaultBot class after closing other issues in DefaultBot class. * */ - +/* +* @todo #106 Fix #handleUpdates. +* This method work unpredictable, +* need to fix work with incoming updates. +* */ package com.l3r8yj.elegramapi.bot; import com.jcabi.http.response.JsonResponse; @@ -35,11 +39,13 @@ import com.l3r8yj.elegramapi.request.TRqPost; import com.l3r8yj.elegramapi.request.TRqSendMessage; import com.l3r8yj.elegramapi.request.TRqWithChatId; -import com.l3r8yj.elegramapi.request.TRqWithOffset; import com.l3r8yj.elegramapi.request.TRqWithText; import com.l3r8yj.elegramapi.update.UpdDefault; import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; @@ -65,6 +71,11 @@ public class BtDefault implements Bot { */ private final String token; + /** + * The processed updates. + */ + private final Set processed; + /** * Ctor. * @@ -74,10 +85,11 @@ public class BtDefault implements Bot { public BtDefault(final String token, final Command... commands) { this.token = token; this.commands = new ListOf<>(commands); + this.processed = Collections.synchronizedSet(new HashSet<>()); } @Override - public final void run() { + public final void start() { try { this.handleUpdates(); } catch (final InterruptedException | IOException ex) { @@ -112,8 +124,8 @@ public final JsonResponse sendMessage(final long chat, final String text) { ex.getMessage() ).toString() ); + throw new IllegalStateException("Can't send a message to user!", ex); } - throw new IllegalStateException("Can't send a message to user!"); } /** @@ -125,7 +137,18 @@ public final JsonResponse sendMessage(final long chat, final String text) { private void handleUpdates() throws InterruptedException, IOException { final BlockingQueue updates = new LinkedBlockingQueue<>(); this.updatingThread(updates).start(); - this.actWithNewUpdates(updates); + while (true) { + final JSONObject upd = updates.take(); + Logger.info(this, this.processed.toString()); + for (final Command command : this.commands) { + if (this.processed.contains(upd.getLong("update_id"))) { + continue; + } + this.processed.add(upd.getLong("update_id")); + command.act(new UpdDefault(upd), this); + } + Thread.sleep(500L); + } } /** @@ -144,22 +167,6 @@ private Thread updatingThread(final BlockingQueue updates) { ); } - /** - * Updates count check. - * - * @param updates The queue - * @throws InterruptedException When thread was interrupted - */ - private void actWithNewUpdates(final BlockingQueue updates) - throws InterruptedException { - while (true) { - for (final Command command : this.commands) { - command.act(new UpdDefault(updates.take()), this); - } - Thread.sleep(500L); - } - } - /** * Check new updates. * @@ -168,7 +175,11 @@ private void actWithNewUpdates(final BlockingQueue updates) private void fillUpdates(final BlockingQueue accum) { final AtomicInteger offset = new AtomicInteger(); try { - final JSONArray updates = new JSONObject(this.getUpdatesBody(offset)) + final JSONArray updates = new JSONObject( + new TRqGetUpdates(this.token) + .response() + .body() + ) .getJSONArray("result"); BtDefault.putUpdatesWithOffset(accum, offset, updates); } catch (final IOException | InterruptedException ex) { @@ -202,20 +213,4 @@ private static void putUpdatesWithOffset( updates.put(new JSONObject(upd.toString())); } } - - /** - * Just update body. - * - * @param offset The offset - * @return The body as string - * @throws IOException When something went wrong - */ - private String getUpdatesBody(final AtomicInteger offset) throws IOException { - return new TRqWithOffset( - new TRqGetUpdates(this.token), - offset.get() - ) - .response() - .body(); - } } diff --git a/src/main/java/com/l3r8yj/elegramapi/request/TRqEnvelope.java b/src/main/java/com/l3r8yj/elegramapi/request/TRqEnvelope.java index e598a4f..4360c64 100644 --- a/src/main/java/com/l3r8yj/elegramapi/request/TRqEnvelope.java +++ b/src/main/java/com/l3r8yj/elegramapi/request/TRqEnvelope.java @@ -25,6 +25,7 @@ package com.l3r8yj.elegramapi.request; import com.jcabi.http.Request; +import com.jcabi.http.RequestURI; import com.jcabi.http.request.JdkRequest; import com.jcabi.http.response.JsonResponse; import java.io.IOException; @@ -121,6 +122,11 @@ public final String plainText() { @Override public final JsonResponse response() throws IOException { - return this.request.fetch().as(JsonResponse.class); + return new JdkRequest(this.uri().get()).fetch().as(JsonResponse.class); + } + + @Override + public final RequestURI uri() { + return this.request.uri(); } } diff --git a/src/main/java/com/l3r8yj/elegramapi/request/TRqPost.java b/src/main/java/com/l3r8yj/elegramapi/request/TRqPost.java index 08e4955..0cc7152 100644 --- a/src/main/java/com/l3r8yj/elegramapi/request/TRqPost.java +++ b/src/main/java/com/l3r8yj/elegramapi/request/TRqPost.java @@ -24,6 +24,8 @@ package com.l3r8yj.elegramapi.request; +import com.jcabi.http.RequestURI; +import com.jcabi.http.request.JdkRequest; import com.jcabi.http.response.JsonResponse; import java.io.IOException; @@ -56,10 +58,14 @@ public String plainText() { @Override public JsonResponse response() throws IOException { - return this.origin.response() - .back() + return new JdkRequest(this.uri().toString()) .method("POST") .fetch() .as(JsonResponse.class); } + + @Override + public RequestURI uri() { + return this.origin.uri(); + } } diff --git a/src/main/java/com/l3r8yj/elegramapi/request/TRqWithChatId.java b/src/main/java/com/l3r8yj/elegramapi/request/TRqWithChatId.java index 6a78ce6..6b3e20c 100644 --- a/src/main/java/com/l3r8yj/elegramapi/request/TRqWithChatId.java +++ b/src/main/java/com/l3r8yj/elegramapi/request/TRqWithChatId.java @@ -24,6 +24,8 @@ package com.l3r8yj.elegramapi.request; +import com.jcabi.http.RequestURI; +import com.jcabi.http.request.JdkRequest; import com.jcabi.http.response.JsonResponse; import java.io.IOException; @@ -58,17 +60,18 @@ public TRqWithChatId(final TelegramRequest origin, final long chat) { @Override public String plainText() { - return this.origin.plainText(); + return this.uri().toString(); } @Override public JsonResponse response() throws IOException { - return this.origin.response() - .back() - .uri() - .queryParam("chat_id", this.chat) - .back() + return new JdkRequest(this.uri().toString()) .fetch() .as(JsonResponse.class); } + + @Override + public RequestURI uri() { + return this.origin.uri().queryParam("chat_id", this.chat); + } } diff --git a/src/main/java/com/l3r8yj/elegramapi/request/TRqWithOffset.java b/src/main/java/com/l3r8yj/elegramapi/request/TRqWithOffset.java index 89645bd..2387445 100644 --- a/src/main/java/com/l3r8yj/elegramapi/request/TRqWithOffset.java +++ b/src/main/java/com/l3r8yj/elegramapi/request/TRqWithOffset.java @@ -24,6 +24,8 @@ package com.l3r8yj.elegramapi.request; +import com.jcabi.http.RequestURI; +import com.jcabi.http.request.JdkRequest; import com.jcabi.http.response.JsonResponse; import java.io.IOException; @@ -58,17 +60,18 @@ public TRqWithOffset(final TelegramRequest origin, final int offset) { @Override public String plainText() { - return this.origin.plainText(); + return this.uri().toString(); } @Override public JsonResponse response() throws IOException { - return this.origin.response() - .back() - .uri() - .queryParam("offset", this.offset) - .back() + return new JdkRequest(this.uri().toString()) .fetch() .as(JsonResponse.class); } + + @Override + public RequestURI uri() { + return this.origin.uri().queryParam("offset", this.offset); + } } diff --git a/src/main/java/com/l3r8yj/elegramapi/request/TRqWithText.java b/src/main/java/com/l3r8yj/elegramapi/request/TRqWithText.java index 3ab89a3..13c8a0d 100644 --- a/src/main/java/com/l3r8yj/elegramapi/request/TRqWithText.java +++ b/src/main/java/com/l3r8yj/elegramapi/request/TRqWithText.java @@ -24,6 +24,8 @@ package com.l3r8yj.elegramapi.request; +import com.jcabi.http.RequestURI; +import com.jcabi.http.request.JdkRequest; import com.jcabi.http.response.JsonResponse; import java.io.IOException; @@ -58,17 +60,18 @@ public TRqWithText(final TelegramRequest origin, final String text) { @Override public String plainText() { - return this.origin.plainText(); + return this.uri().toString(); } @Override public JsonResponse response() throws IOException { - return this.origin.response() - .back() - .uri() - .queryParam("text", this.text) - .back() + return new JdkRequest(this.uri().toString()) .fetch() .as(JsonResponse.class); } + + @Override + public RequestURI uri() { + return this.origin.uri().queryParam("text", this.text); + } } diff --git a/src/main/java/com/l3r8yj/elegramapi/request/TelegramRequest.java b/src/main/java/com/l3r8yj/elegramapi/request/TelegramRequest.java index 87ebf4c..f90e6d2 100644 --- a/src/main/java/com/l3r8yj/elegramapi/request/TelegramRequest.java +++ b/src/main/java/com/l3r8yj/elegramapi/request/TelegramRequest.java @@ -21,15 +21,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -/* - * @todo #94 Change request to send message. - * Need to change this {@link com.l3r8yj.elegramapi.request.TRqWithChatId}, - * {@link com.l3r8yj.elegramapi.request.TRqWithOffset}, - * {@link com.l3r8yj.elegramapi.request.TRqSendMessage} - * to fix multiple sending requests. - */ + package com.l3r8yj.elegramapi.request; +import com.jcabi.http.RequestURI; import com.jcabi.http.response.JsonResponse; import java.io.IOException; @@ -54,4 +49,11 @@ public interface TelegramRequest { * @throws IOException When something went wrong */ JsonResponse response() throws IOException; + + /** + * The uri from request. + * + * @return The uri + */ + RequestURI uri(); } diff --git a/src/test/java/com/l3r8yj/elegramapi/request/TRqPostTest.java b/src/test/java/com/l3r8yj/elegramapi/request/TRqPostTest.java new file mode 100644 index 0000000..af88862 --- /dev/null +++ b/src/test/java/com/l3r8yj/elegramapi/request/TRqPostTest.java @@ -0,0 +1,51 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2022-2023 Ivanchuck Ivan. + * + * 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 com.l3r8yj.elegramapi.request; + +import java.io.IOException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +/** + * Test case for {@link TRqPost}. + * + * @since 0.0.0 + */ +class TRqPostTest { + + @Test + void responsesWithPost() throws IOException { + MatcherAssert.assertThat( + "Is a POST request", + new TRqPost(new TRqGetUpdates("tkn")) + .response() + .back() + .toString() + .contains("POST"), + Matchers.equalTo(true) + ); + } +} diff --git a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithChatIdTest.java b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithChatIdTest.java index 07c4883..1b7d7d0 100644 --- a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithChatIdTest.java +++ b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithChatIdTest.java @@ -24,7 +24,6 @@ package com.l3r8yj.elegramapi.request; -import java.io.IOException; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -37,14 +36,12 @@ final class TRqWithChatIdTest { @Test - void responsesFromRightUri() throws IOException { + void responsesFromRightUri() { MatcherAssert.assertThat( new TRqWithChatId( new TRqSendMessage("tkn"), 34 ) - .response() - .back() .uri() .toString() .contains("chat_id=34"), diff --git a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java index b3ce4df..977d213 100644 --- a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java +++ b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java @@ -24,7 +24,6 @@ package com.l3r8yj.elegramapi.request; -import java.io.IOException; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -37,14 +36,12 @@ final class TRqWithOffsetTest { @Test - void responsesFromRightUri() throws IOException { + void responsesFromRightUri() { MatcherAssert.assertThat( new TRqWithOffset( new TRqSendMessage("tkn"), 32 ) - .response() - .back() .uri() .toString() .contains("offset=32"), diff --git a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithTextTest.java b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithTextTest.java index ade5dc5..970b453 100644 --- a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithTextTest.java +++ b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithTextTest.java @@ -24,7 +24,6 @@ package com.l3r8yj.elegramapi.request; -import java.io.IOException; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; @@ -51,9 +50,9 @@ void setUp() { } @Test - void responsesFromRightUri() throws IOException { + void responsesFromRightUri() { MatcherAssert.assertThat( - this.request.response().back().uri().toString().contains("text="), + this.request.uri().toString().contains("text="), Matchers.equalTo(true) ); } From 00a29f11c4d28e5040c51a350f1e1d4f2535e695 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 12 Feb 2023 01:43:23 +0400 Subject: [PATCH 2/5] refactored --- .../com/elegramapi/simpleit/ItSimpleTest.java | 7 ++-- .../com/l3r8yj/elegramapi/bot/BtDefault.java | 35 ++++++++++++------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/it/simple-it/src/test/java/com/elegramapi/simpleit/ItSimpleTest.java b/src/it/simple-it/src/test/java/com/elegramapi/simpleit/ItSimpleTest.java index 6cc3bce..6d7177a 100644 --- a/src/it/simple-it/src/test/java/com/elegramapi/simpleit/ItSimpleTest.java +++ b/src/it/simple-it/src/test/java/com/elegramapi/simpleit/ItSimpleTest.java @@ -32,6 +32,7 @@ import javax.ws.rs.core.Response; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -59,8 +60,10 @@ void setUp() { @Test @Disabled - void runsTheBot() throws Exception { - this.bot.start(); + void runsTheBot() { + Assertions.assertDoesNotThrow( + () -> this.bot.start() + ); } @Test diff --git a/src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java b/src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java index 89fd0ef..49253ac 100644 --- a/src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java +++ b/src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java @@ -137,15 +137,24 @@ public final JsonResponse sendMessage(final long chat, final String text) { private void handleUpdates() throws InterruptedException, IOException { final BlockingQueue updates = new LinkedBlockingQueue<>(); this.updatingThread(updates).start(); + this.processUpdates(updates); + } + + /** + * Processing updates. + * + * @param updates The queue with updates + * @throws InterruptedException When thread interrupted + */ + private void processUpdates(final BlockingQueue updates) + throws InterruptedException { while (true) { final JSONObject upd = updates.take(); - Logger.info(this, this.processed.toString()); - for (final Command command : this.commands) { - if (this.processed.contains(upd.getLong("update_id"))) { - continue; + if (!this.processed.contains(upd.getLong("update_id"))) { + for (final Command command : this.commands) { + this.processed.add(upd.getLong("update_id")); + command.act(new UpdDefault(upd), this); } - this.processed.add(upd.getLong("update_id")); - command.act(new UpdDefault(upd), this); } Thread.sleep(500L); } @@ -175,13 +184,13 @@ private Thread updatingThread(final BlockingQueue updates) { private void fillUpdates(final BlockingQueue accum) { final AtomicInteger offset = new AtomicInteger(); try { - final JSONArray updates = new JSONObject( - new TRqGetUpdates(this.token) - .response() - .body() - ) - .getJSONArray("result"); - BtDefault.putUpdatesWithOffset(accum, offset, updates); + BtDefault.putUpdatesWithOffset( + accum, + offset, + new JSONObject( + new TRqGetUpdates(this.token).response().body() + ).getJSONArray("result") + ); } catch (final IOException | InterruptedException ex) { Logger.error( this, From a7b1e220e04b7b02d7a863d92ae5530cdbd5860a Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 12 Feb 2023 02:01:00 +0400 Subject: [PATCH 3/5] offset tests --- .../elegramapi/request/TRqWithOffsetTest.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java index 977d213..50be96f 100644 --- a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java +++ b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java @@ -24,8 +24,11 @@ package com.l3r8yj.elegramapi.request; +import java.io.IOException; +import javax.ws.rs.core.Response; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** @@ -35,16 +38,31 @@ */ final class TRqWithOffsetTest { + /** + * Under test request. + */ + private TelegramRequest request; + + @BeforeEach + void setUp() { + this.request = new TRqWithOffset( + new TRqSendMessage("tkn"), + 32 + ); + } + + @Test + void responses() throws IOException { + MatcherAssert.assertThat( + this.request.response().status(), + Matchers.equalTo(Response.Status.NOT_FOUND.getStatusCode()) + ); + } + @Test - void responsesFromRightUri() { + void createsUriWithRightParam() { MatcherAssert.assertThat( - new TRqWithOffset( - new TRqSendMessage("tkn"), - 32 - ) - .uri() - .toString() - .contains("offset=32"), + this.request.uri().toString().contains("offset=32"), Matchers.equalTo(true) ); } From 808cda7fc624373b2861a297da60265a088f7583 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 12 Feb 2023 02:01:22 +0400 Subject: [PATCH 4/5] offset tests --- .../java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java index 50be96f..6e54a57 100644 --- a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java +++ b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithOffsetTest.java @@ -52,7 +52,7 @@ void setUp() { } @Test - void responses() throws IOException { + void responsesWithNotFound() throws IOException { MatcherAssert.assertThat( this.request.response().status(), Matchers.equalTo(Response.Status.NOT_FOUND.getStatusCode()) From 431c83e73da40b9ebb54cf32b5f2ada325367d1d Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 12 Feb 2023 02:09:33 +0400 Subject: [PATCH 5/5] request tests --- .../elegramapi/request/TRqWithChatIdTest.java | 34 ++++++++++++++----- .../elegramapi/request/TRqWithTextTest.java | 12 ++++++- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithChatIdTest.java b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithChatIdTest.java index 1b7d7d0..742b536 100644 --- a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithChatIdTest.java +++ b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithChatIdTest.java @@ -24,8 +24,11 @@ package com.l3r8yj.elegramapi.request; +import java.io.IOException; +import javax.ws.rs.core.Response; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** @@ -35,16 +38,31 @@ */ final class TRqWithChatIdTest { + /** + * Under test request. + */ + private TelegramRequest request; + + @BeforeEach + void setUp() { + this.request = new TRqWithChatId( + new TRqSendMessage("tkn"), + 34 + ); + } + + @Test + void responsesWithNotFound() throws IOException { + MatcherAssert.assertThat( + this.request.response().status(), + Matchers.equalTo(Response.Status.NOT_FOUND.getStatusCode()) + ); + } + @Test - void responsesFromRightUri() { + void createsUriWithRightParam() { MatcherAssert.assertThat( - new TRqWithChatId( - new TRqSendMessage("tkn"), - 34 - ) - .uri() - .toString() - .contains("chat_id=34"), + this.request.uri().toString().contains("chat_id=34"), Matchers.equalTo(true) ); } diff --git a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithTextTest.java b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithTextTest.java index 970b453..7ea6c24 100644 --- a/src/test/java/com/l3r8yj/elegramapi/request/TRqWithTextTest.java +++ b/src/test/java/com/l3r8yj/elegramapi/request/TRqWithTextTest.java @@ -24,6 +24,8 @@ package com.l3r8yj.elegramapi.request; +import java.io.IOException; +import javax.ws.rs.core.Response; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; @@ -50,10 +52,18 @@ void setUp() { } @Test - void responsesFromRightUri() { + void createsUriWithRightParam() { MatcherAssert.assertThat( this.request.uri().toString().contains("text="), Matchers.equalTo(true) ); } + + @Test + void responsesWithNotFound() throws IOException { + MatcherAssert.assertThat( + this.request.response().status(), + Matchers.equalTo(Response.Status.NOT_FOUND.getStatusCode()) + ); + } }