Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: Enhancing code maintainability by reducing code smells. #1336

Open
wants to merge 32 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d2ffe8b
Add default constructor to WriteAccessAllowed (#1273)
aNNiMON Jan 22, 2024
ac32664
Update AllowedUpdates (#1276)
bryan-zhu-dev Jan 22, 2024
47be445
fix ReactionType validations (#1303)
fullmoonrising Jan 28, 2024
b0d3989
Update Using-Replies.md (#1282)
ChelouteVR Jan 28, 2024
870d1e5
Update MaybeInaccessibleMessage.java (#1310)
Chase22 Feb 2, 2024
6b50d68
Fixed incorrect Chat#profileBackgroundCustomEmojiId field type. Chang…
link1107 Feb 4, 2024
d615fbf
Missing fields and constructor update for API 7.0 (#1322)
rbondar Feb 17, 2024
ed75ff9
Add missing methods from API 6.6 (#1323)
aNNiMON Feb 17, 2024
0e96778
API 7.1
rubenlagus Feb 17, 2024
e344f9c
API 7.1
rubenlagus Feb 17, 2024
6406b1f
Test cases for ASDC assignment-1
Yukta026 Mar 1, 2024
11b5569
Implementation code smell - 1 - Refactored code for Magic number usin…
Yukta026 Mar 23, 2024
e80a519
Implementation code smell - 2 - Refactored code for Complex Method us…
Yukta026 Mar 23, 2024
3e8d8c7
Implementation code smell - 3 - Refactored code for Long Statement us…
Yukta026 Mar 23, 2024
aafebe6
Implementation code smell - 4 - Refactored test code for Magic number…
Yukta026 Mar 23, 2024
4344b84
Implementation code smell - 5 - Refactored test code for Empty Catch …
Yukta026 Mar 23, 2024
b9c94fd
Implementation code smell - 6 - Refactored test code for Long Identif…
Yukta026 Mar 23, 2024
3d3099b
Design code smell - 1 - Refactored code for Insufficient Modularizati…
Yukta026 Mar 23, 2024
848266c
Design code smell - 2 - Refactored code for Unnecessary Abstraction u…
Yukta026 Mar 23, 2024
240fb70
Design code smell - 3 - Refactored code for Cyclically-dependent Modu…
Yukta026 Mar 23, 2024
2b9aea9
Fix: merge conflict for story file fixed
Yukta026 Mar 26, 2024
5fba3b2
Removed/renamed files for design smell-1
Yukta026 Mar 29, 2024
e70155a
Reset code changed in implementation smell-1
Yukta026 Mar 29, 2024
1b6598a
Removed unnecessary comments from refactored code for implementation …
Yukta026 Mar 29, 2024
423ceca
Made necessary changes for refactoring implementation smell-3
Yukta026 Mar 29, 2024
3729e7b
Reset code changed for refactoring implementation smell-4
Yukta026 Mar 29, 2024
fb128ba
Removed unncessary comments
Yukta026 Mar 29, 2024
f4cef77
Removed unnecessary comments
Yukta026 Mar 29, 2024
3c98bfa
Removed unnecessary comments
Yukta026 Mar 29, 2024
5b97c30
Removed unnecessary comments
Yukta026 Mar 29, 2024
6451919
Code reset for story and chat files
Yukta026 Mar 29, 2024
1278586
Design code smell - 4 - Refactored code for unexploited encapsulation…
Yukta026 Mar 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>6.9.7.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -135,6 +140,11 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
Expand Down Expand Up @@ -218,6 +228,25 @@
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions telegrambots-abilities/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
</properties>

<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ public final class Ability {
private final List<Reply> replies;
private final List<Predicate<Update>> flags;


@SafeVarargs
private Ability(String name, String info, Locality locality, Privacy privacy, int argNum, boolean statsEnabled, Consumer<MessageContext> action, Consumer<MessageContext> postAction, List<Reply> replies, Predicate<Update>... flags) {
checkArgument(isValidCommandName(name), "Method name can only contain alpha-numeric characters and underscores," +
" cannot be longer than 31 characters, empty or null", name);
String ERROR_MESSAGE_PREFIX = "Method name can only contain alpha-numeric characters and underscores," ;
String ERROR_MESSAGE_SUFFIX = " cannot be longer than 31 characters, empty or null";
checkArgument(isValidCommandName(name), "%s%s", ERROR_MESSAGE_PREFIX, ERROR_MESSAGE_SUFFIX, name);

this.name = name;
this.info = info;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ private void checkLength() {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("user", user)
.add("chatId", chatId)
.add("arguments", arguments)
.add("update", update)
.toString();
.add("user", user)
.add("chatId", chatId)
.add("arguments", arguments)
.add("update", update)
.toString();
}

@Override
Expand All @@ -122,13 +122,13 @@ public boolean equals(Object o) {

MessageContext that = (MessageContext) o;
return Objects.equal(user, that.user) &&
Objects.equal(chatId, that.chatId) &&
Arrays.equals(arguments, that.arguments) &&
Objects.equal(update, that.update);
Objects.equal(chatId, that.chatId) &&
Arrays.equals(arguments, that.arguments) &&
Objects.equal(update, that.update);
}

@Override
public int hashCode() {
return Objects.hashCode(user, chatId, Arrays.hashCode(arguments), update);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto;
import org.telegram.telegrambots.meta.api.methods.send.*;
import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet;
import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet;
import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile;
import org.telegram.telegrambots.meta.api.objects.File;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.User;
Expand Down Expand Up @@ -41,21 +38,6 @@ public <T extends Serializable, Method extends BotApiMethod<T>> T execute(Method
return bot.execute(method);
}

@Override
public Boolean addStickerToSet(AddStickerToSet addStickerToSet) throws TelegramApiException {
return bot.execute(addStickerToSet);
}

@Override
public Boolean createNewStickerSet(CreateNewStickerSet createNewStickerSet) throws TelegramApiException {
return bot.execute(createNewStickerSet);
}

@Override
public File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException {
return bot.execute(uploadStickerFile);
}

@Override
public Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException {
return bot.execute(setChatPhoto);
Expand Down Expand Up @@ -136,4 +118,4 @@ public Message sendVoice(SendVoice sendVoice) throws TelegramApiException {
public Message sendVideoNote(SendVideoNote sendVideoNote) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.telegram.abilitybots.api.sender;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet;
import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet;
import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile;
import org.telegram.telegrambots.meta.api.objects.File;
import org.telegram.telegrambots.bots.DefaultAbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;

import java.io.Serializable;
public class DefaultSticker implements StickerMessage {
private static final String TAG = MessageSender.class.getName();

private DefaultAbsSender bot;

@Override
public <T extends Serializable, Method extends BotApiMethod<T>, Callback extends SentCallback<T>> void executeAsync(Method method, Callback callback) throws TelegramApiException {

}

@Override
public <T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method) throws TelegramApiException {
return null;
}

@Override
public Boolean addStickerToSet(AddStickerToSet addStickerToSet) throws TelegramApiException {
return bot.execute(addStickerToSet);
}

@Override
public Boolean createNewStickerSet(CreateNewStickerSet createNewStickerSet) throws TelegramApiException {
return bot.execute(createNewStickerSet);
}

@Override
public File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException {
return bot.execute(uploadStickerFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto;
import org.telegram.telegrambots.meta.api.methods.send.*;
import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet;
import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet;
import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile;
import org.telegram.telegrambots.meta.api.objects.File;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.User;
Expand All @@ -28,12 +25,6 @@ public interface MessageSender {

<T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method) throws TelegramApiException;

Boolean addStickerToSet(AddStickerToSet addStickerToSet) throws TelegramApiException;

Boolean createNewStickerSet(CreateNewStickerSet createNewStickerSet) throws TelegramApiException;

File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException;

Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException;

java.io.File downloadFile(String path) throws TelegramApiException;
Expand Down Expand Up @@ -65,4 +56,4 @@ public interface MessageSender {
Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException;

Message sendSticker(SendSticker sendSticker) throws TelegramApiException;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.telegram.abilitybots.api.sender;


import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet;
import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet;
import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile;
import org.telegram.telegrambots.meta.api.objects.File;
import org.telegram.telegrambots.bots.DefaultAbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.updateshandlers.SentCallback;

import java.io.Serializable;

/**
* A sender interface that replicates {@link DefaultAbsSender} methods.
*
* @author Abbas Abou Daya
*/
public interface StickerMessage {

<T extends Serializable, Method extends BotApiMethod<T>, Callback extends SentCallback<T>> void executeAsync(Method method, Callback callback) throws TelegramApiException;

<T extends Serializable, Method extends BotApiMethod<T>> T execute(Method method) throws TelegramApiException;
Boolean addStickerToSet(AddStickerToSet addStickerToSet) throws TelegramApiException;

Boolean createNewStickerSet(CreateNewStickerSet createNewStickerSet) throws TelegramApiException;

File uploadStickerFile(UploadStickerFile uploadStickerFile) throws TelegramApiException;
}

Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ public static boolean isValidCommand(String command){
* @param commandName the command name to be checked for validity
* @return whether the command name is valid
*/


public static boolean isValidCommandName(String commandName){
if (commandName == null || commandName.length() > 31) return false;
return commandName.matches("[A-Za-z_0-9]+");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,3 @@ void abilityBuilderSetStatsEnabledFalseTest() {
assertFalse(statsDisabledAbility.statsEnabled());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.telegram.telegrambots.extensions.bots.commandbot.commands;

import org.junit.jupiter.api.Test;
import org.telegram.telegrambots.extensions.bots.timedbot.TimedSendLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;
import static org.mockito.Mockito.mock;


public class TimedSendLongPollingBotTest
{
@Test
public void testMessageProcessing()
{
TimedSendLongPollingBot testbot = new TimedSendLongPollingBot()
{
@Override
public String getBotUsername() {
return null;
}

@Override
public void onUpdateReceived(Update update)
{

}
@Override
public void sendMessageCallback(Long chatId, Object messageRequest)
{

}
};

Long chatId = 2L;
Object messageRequest = mock(Object.class);

testbot.sendTimed(chatId,messageRequest);
testbot.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;

import java.io.Serializable;

Expand All @@ -13,7 +14,15 @@
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public abstract class BotApiMethod<T extends Serializable> extends PartialBotApiMethod<T> {
protected static final String METHOD_FIELD = "method";
public class BotApiMethod<T extends Serializable> extends PartialBotApiMethod<T> {

@Override
public T deserializeResponse(String answer) throws TelegramApiRequestException {
return null;
}

@Override
public String getMethod() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public abstract class PartialBotApiMethod<T extends Serializable> implements Val
@JsonIgnore
protected static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

protected static final String METHOD_FIELD = "method";

/**
* Deserialize a json answer to the response type to a method
* @param answer Json answer received
Expand Down Expand Up @@ -62,6 +64,7 @@ private T deserializeResponseInternal(String answer, JavaType type) throws Teleg
* Getter for method path (that is the same as method name)
* @return Method path
*/
@JsonProperty(BotApiMethod.METHOD_FIELD)

@JsonProperty(METHOD_FIELD)
public abstract String getMethod();
}
Loading