Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Feb 11, 2023
2 parents 0656828 + 431c83e commit ccbfbf3
Show file tree
Hide file tree
Showing 13 changed files with 262 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@

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;
import com.l3r8yj.elegramapi.update.Update;
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;

/**
Expand All @@ -40,25 +44,50 @@
*/
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() {
Assertions.assertDoesNotThrow(
() -> 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)
);
}

/**
* The test command.
*
* @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) {
Expand All @@ -67,4 +96,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()
);
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/l3r8yj/elegramapi/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface Bot {
*
* @throws Exception If something went wrong
*/
void run() throws Exception;
void start() throws Exception;

/**
* Sends the message.
Expand Down
84 changes: 44 additions & 40 deletions src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -65,6 +71,11 @@ public class BtDefault implements Bot {
*/
private final String token;

/**
* The processed updates.
*/
private final Set<Long> processed;

/**
* Ctor.
*
Expand All @@ -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) {
Expand Down Expand Up @@ -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!");
}

/**
Expand All @@ -125,7 +137,27 @@ public final JsonResponse sendMessage(final long chat, final String text) {
private void handleUpdates() throws InterruptedException, IOException {
final BlockingQueue<JSONObject> updates = new LinkedBlockingQueue<>();
this.updatingThread(updates).start();
this.actWithNewUpdates(updates);
this.processUpdates(updates);
}

/**
* Processing updates.
*
* @param updates The queue with updates
* @throws InterruptedException When thread interrupted
*/
private void processUpdates(final BlockingQueue<JSONObject> updates)
throws InterruptedException {
while (true) {
final JSONObject upd = updates.take();
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);
}
}
Thread.sleep(500L);
}
}

/**
Expand All @@ -144,22 +176,6 @@ private Thread updatingThread(final BlockingQueue<JSONObject> updates) {
);
}

/**
* Updates count check.
*
* @param updates The queue
* @throws InterruptedException When thread was interrupted
*/
private void actWithNewUpdates(final BlockingQueue<JSONObject> updates)
throws InterruptedException {
while (true) {
for (final Command command : this.commands) {
command.act(new UpdDefault(updates.take()), this);
}
Thread.sleep(500L);
}
}

/**
* Check new updates.
*
Expand All @@ -168,9 +184,13 @@ private void actWithNewUpdates(final BlockingQueue<JSONObject> updates)
private void fillUpdates(final BlockingQueue<JSONObject> accum) {
final AtomicInteger offset = new AtomicInteger();
try {
final JSONArray updates = new JSONObject(this.getUpdatesBody(offset))
.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,
Expand Down Expand Up @@ -202,20 +222,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();
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/l3r8yj/elegramapi/request/TRqEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/l3r8yj/elegramapi/request/TRqPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}
15 changes: 9 additions & 6 deletions src/main/java/com/l3r8yj/elegramapi/request/TRqWithChatId.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
15 changes: 9 additions & 6 deletions src/main/java/com/l3r8yj/elegramapi/request/TRqWithOffset.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Loading

2 comments on commit ccbfbf3

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on ccbfbf3 Feb 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 94-926f09b4 disappeared from src/main/java/com/l3r8yj/elegramapi/request/TelegramRequest.java), that's why I closed #106. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on ccbfbf3 Feb 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 106-92a1f24f discovered in src/main/java/com/l3r8yj/elegramapi/bot/BtDefault.java) and submitted as #115. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.