-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
459 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/pro/jazzman/odmiana/bot/commands/HealthcheckCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package pro.jazzman.odmiana.bot.commands; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
import pro.jazzman.odmiana.bot.OdmianaBot; | ||
import pro.jazzman.odmiana.bot.interfaces.Command; | ||
import pro.jazzman.odmiana.bot.interfaces.Privacy; | ||
import pro.jazzman.odmiana.bot.messages.HealthcheckView; | ||
import pro.jazzman.odmiana.configurations.Config; | ||
|
||
import java.util.Objects; | ||
|
||
@Service | ||
public class HealthcheckCommand implements Command { | ||
@Autowired | ||
private Config config; | ||
|
||
@Autowired | ||
private HealthcheckView view; | ||
|
||
@Override | ||
public String getCommand() { | ||
return "/healthcheck"; | ||
} | ||
|
||
@Override | ||
public String getUsage() { | ||
return "/healthcheck"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Application Healthcheck"; | ||
} | ||
|
||
public Privacy privacy() { | ||
return Privacy.OWNER; | ||
} | ||
|
||
@Override | ||
public void handle(OdmianaBot bot, Update update) throws TelegramApiException { | ||
if (Objects.equals(config.getOwnerId(), update.getMessage().getChatId())) { | ||
bot.send( | ||
view.render(), | ||
update | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/pro/jazzman/odmiana/bot/interfaces/Privacy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pro.jazzman.odmiana.bot.interfaces; | ||
|
||
public enum Privacy { | ||
PUBLIC, | ||
OWNER | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/pro/jazzman/odmiana/bot/messages/HealthcheckView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package pro.jazzman.odmiana.bot.messages; | ||
|
||
import org.apache.commons.text.StringSubstitutor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import pro.jazzman.odmiana.entities.Healthcheck; | ||
import pro.jazzman.odmiana.services.HealthcheckService; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Service | ||
public class HealthcheckView { | ||
private final HealthcheckService service; | ||
|
||
@Autowired | ||
public HealthcheckView(HealthcheckService service) { | ||
this.service = service; | ||
} | ||
|
||
private static final String TEMPLATE = """ | ||
Version: *${version}* | ||
Status: *${application.status}* | ||
Database: *${database.status}* | ||
"""; | ||
|
||
public String render() { | ||
Healthcheck healthcheck = service.get(); | ||
|
||
Map<String, String> placeholders = new HashMap<>(); | ||
placeholders.put("version", healthcheck.getVersion()); | ||
placeholders.put("application.status", healthcheck.getStatus()); | ||
placeholders.put("database.status", healthcheck.getDatabase()); | ||
|
||
return StringSubstitutor.replace(TEMPLATE, placeholders); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/pro/jazzman/odmiana/configurations/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/pro/jazzman/odmiana/entities/Healthcheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package pro.jazzman.odmiana.entities; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Healthcheck { | ||
private final String version; | ||
private final String status; | ||
private final String database; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/pro/jazzman/odmiana/services/HealthcheckService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package pro.jazzman.odmiana.services; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import pro.jazzman.odmiana.configurations.Config; | ||
import pro.jazzman.odmiana.configurations.SimpleMongoConfig; | ||
import pro.jazzman.odmiana.entities.Healthcheck; | ||
|
||
@Service | ||
public class HealthcheckService { | ||
private static final String STATUS_FAILED = "FAILED"; | ||
private static final String STATUS_OK = "OK"; | ||
|
||
private final Config config; | ||
private final SimpleMongoConfig mongoConfig; | ||
@Autowired | ||
public HealthcheckService(Config config, SimpleMongoConfig mongoConfig) { | ||
this.config = config; | ||
this.mongoConfig = mongoConfig; | ||
} | ||
|
||
public Healthcheck get() { | ||
return new Healthcheck(config.getVersion(), status(), databaseStatus()); | ||
} | ||
|
||
private String databaseStatus() { | ||
try (MongoClient client = mongoConfig.mongo()) { | ||
return STATUS_OK; | ||
} catch (Exception e) { | ||
return STATUS_FAILED; | ||
} | ||
} | ||
|
||
/** | ||
* Summary status of the application. Only checking database connection for now. | ||
* @return OK if application is alive, FAILED - otherwise | ||
*/ | ||
private String status() { | ||
return STATUS_OK.equals(databaseStatus()) ? STATUS_OK : STATUS_FAILED; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/test/java/pro/jazzman/odmiana/bot/commands/HealthcheckCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package pro.jazzman.odmiana.bot.commands; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.telegram.telegrambots.meta.api.objects.Chat; | ||
import org.telegram.telegrambots.meta.api.objects.Message; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
import pro.jazzman.odmiana.bot.OdmianaBot; | ||
import pro.jazzman.odmiana.bot.interfaces.Privacy; | ||
import pro.jazzman.odmiana.bot.messages.HealthcheckView; | ||
import pro.jazzman.odmiana.configurations.Config; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@DisplayName("Healthcheck command") | ||
class HealthcheckCommandTest { | ||
private static final Long OWNER_ID = 1L; | ||
private static final String MESSAGE_TEXT = """ | ||
Version: *1.2.3* | ||
Status: *OK* | ||
Database: *OK* | ||
"""; | ||
|
||
@Mock private OdmianaBot bot; | ||
@Mock private Config config; | ||
@Mock private HealthcheckView view; | ||
private HealthcheckCommand command; | ||
|
||
@BeforeEach | ||
void init() { | ||
command = new HealthcheckCommand(); | ||
|
||
ReflectionTestUtils.setField(command, "config", config); | ||
ReflectionTestUtils.setField(command, "view", view); | ||
} | ||
|
||
@Test | ||
@DisplayName("Has a correct command") | ||
void getCommand() { | ||
assertThat(command.getCommand()).isEqualTo("/healthcheck"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Has a usage example") | ||
void getUsage() { | ||
assertThat(command.getUsage()).isEqualTo("/healthcheck"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Has a description") | ||
void getDescription() { | ||
assertThat(command.getDescription()).isEqualTo("Application Healthcheck"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Has OWNER privacy level") | ||
void privacy() { | ||
assertThat(command.privacy()).isEqualTo(Privacy.OWNER); | ||
} | ||
|
||
@Test | ||
@DisplayName("Send message on the compatible privacy level") | ||
void handle() throws TelegramApiException { | ||
Chat chat = new Chat(); | ||
chat.setId(OWNER_ID); | ||
Message message = new Message(); | ||
message.setChat(chat); | ||
Update update = new Update(); | ||
update.setMessage(message); | ||
|
||
when(config.getOwnerId()).thenReturn(OWNER_ID); | ||
when(view.render()).thenReturn(MESSAGE_TEXT); | ||
|
||
command.handle(bot, update); | ||
|
||
verify(bot).send(MESSAGE_TEXT, update); | ||
} | ||
|
||
@Test | ||
@DisplayName("Does nothing on incompatible privacy level") | ||
void handleNotAllowedDoNothing() throws TelegramApiException { | ||
Chat chat = new Chat(); | ||
chat.setId(100500L); | ||
Message message = new Message(); | ||
message.setChat(chat); | ||
Update update = new Update(); | ||
update.setMessage(message); | ||
|
||
when(config.getOwnerId()).thenReturn(OWNER_ID); | ||
|
||
command.handle(bot, update); | ||
|
||
verify(bot, never()).send(any(), any()); | ||
} | ||
} |
Oops, something went wrong.