feat: add Javadoc, add tests
continuous-integration/drone/push Build is passing Details

pull/2/head
Davide Polonio 2023-03-31 09:53:42 +02:00
parent 3705840989
commit 9889d25bf3
4 changed files with 154 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package com.github.polpetta.mezzotre.orm.model;
import com.github.polpetta.types.json.CallbackQueryMetadata; import com.github.polpetta.types.json.CallbackQueryMetadata;
import io.ebean.annotation.DbJson; import io.ebean.annotation.DbJson;
import io.ebean.annotation.Length; import io.ebean.annotation.Length;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
@ -31,7 +32,10 @@ public class CallbackQueryContext extends Base {
@Length(36) @Length(36)
private final String entryGroup; private final String entryGroup;
@DbJson @NotNull private final CallbackQueryMetadata fields; @DbJson
@Column(columnDefinition = "json not null default '{}'::json")
@NotNull
private final CallbackQueryMetadata fields;
public CallbackQueryContext(String id, String entryGroup, CallbackQueryMetadata fields) { public CallbackQueryContext(String id, String entryGroup, CallbackQueryMetadata fields) {
this.id = id; this.id = id;
@ -50,4 +54,18 @@ public class CallbackQueryContext extends Base {
public CallbackQueryMetadata getFields() { public CallbackQueryMetadata getFields() {
return fields; return fields;
} }
@Override
public String toString() {
return "CallbackQueryContext{"
+ "id='"
+ id
+ '\''
+ ", entryGroup='"
+ entryGroup
+ '\''
+ ", fields="
+ fields
+ '}';
}
} }

View File

@ -23,6 +23,12 @@ import org.apache.velocity.tools.ToolManager;
import org.apache.velocity.util.StringBuilderWriter; import org.apache.velocity.util.StringBuilderWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
/**
* ChangeLanguage event is related to support locale change for a particular chat
*
* @author Davide Polonio
* @since 1.0
*/
@Singleton @Singleton
public class ChangeLanguage implements Processor { public class ChangeLanguage implements Processor {
@ -31,6 +37,12 @@ public class ChangeLanguage implements Processor {
private final LocalizedMessageFactory localizedMessageFactory; private final LocalizedMessageFactory localizedMessageFactory;
private final Logger log; private final Logger log;
/**
* Additional fields that are related to {@code changeLanguage} event
*
* @author Davide Polonio
* @since 1.0
*/
public enum Field { public enum Field {
NewLanguage("newLanguage"); NewLanguage("newLanguage");
@ -45,6 +57,12 @@ public class ChangeLanguage implements Processor {
} }
} }
/**
* Possible values for the additional {@link Field} of {@code changeLanguage} event
*
* @author Davide Polonio
* @since 1.0
*/
public enum Language { public enum Language {
English("en-US"), English("en-US"),
Italian("it-IT"); Italian("it-IT");

View File

@ -1,4 +1,13 @@
-- apply changes -- apply changes
create table callback_query_context (
id varchar(36) not null,
entry_group varchar(36) not null,
fields json not null,
entry_created timestamptz not null,
entry_modified timestamptz not null,
constraint pk_callback_query_context primary key (id)
);
create table telegram_chat ( create table telegram_chat (
id bigint generated by default as identity not null, id bigint generated by default as identity not null,
chat_context jsonb not null default '{}'::jsonb not null, chat_context jsonb not null default '{}'::jsonb not null,

View File

@ -0,0 +1,108 @@
package com.github.polpetta.mezzotre.orm.model;
import static org.junit.jupiter.api.Assertions.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.polpetta.mezzotre.helper.Loader;
import com.github.polpetta.mezzotre.helper.TestConfig;
import com.github.polpetta.mezzotre.orm.model.query.QCallbackQueryContext;
import com.github.polpetta.mezzotre.util.Clock;
import com.github.polpetta.mezzotre.util.UUIDGenerator;
import com.github.polpetta.types.json.CallbackQueryMetadata;
import io.ebean.Database;
import io.ebean.SqlRow;
import java.sql.Timestamp;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Tag("slow")
@Tag("database")
@Testcontainers
class CallbackQueryContextIntegrationTest {
private static ObjectMapper objectMapper;
private static UUIDGenerator uuidGenerator;
@Container
private final PostgreSQLContainer<?> postgresServer =
new PostgreSQLContainer<>(TestConfig.POSTGRES_DOCKER_IMAGE);
private Database database;
@BeforeAll
static void beforeAll() {
objectMapper = new ObjectMapper();
uuidGenerator = new UUIDGenerator();
}
@BeforeEach
void setUp() throws Exception {
database =
Loader.connectToDatabase(Loader.loadDefaultEbeanConfigWithPostgresSettings(postgresServer));
}
@Test
void shouldInsertEntryIntoDatabase() throws Exception {
final String uuidContext = uuidGenerator.generateAsString();
final String uuidGroup = uuidGenerator.generateAsString();
final CallbackQueryMetadata callbackQueryMetadata =
new CallbackQueryMetadata.CallbackQueryMetadataBuilder()
.withEvent("eventExample")
.withTelegramChatId(1234L)
.withMessageId(4242L)
.build();
final CallbackQueryContext callbackQueryContext =
new CallbackQueryContext(uuidContext, uuidGroup, callbackQueryMetadata);
callbackQueryContext.save();
final String query = "select * from callback_query_context where id = ?";
final SqlRow savedCallbackQueryContext =
database.sqlQuery(query).setParameter(uuidContext).findOne();
assertNotNull(savedCallbackQueryContext);
assertEquals(uuidGroup, savedCallbackQueryContext.getString("entry_group"));
assertEquals(uuidContext, savedCallbackQueryContext.getString("id"));
assertEquals(
objectMapper.writeValueAsString(callbackQueryMetadata),
savedCallbackQueryContext.getString("fields"));
}
@Test
void shouldRetrieveEntryInDatabase() throws Exception {
final Timestamp timestampFromUnixEpoch = Clock.getTimestampFromUnixEpoch(1L);
final String uuidId = uuidGenerator.generateAsString();
final String uuidEntryGroup = uuidGenerator.generateAsString();
final CallbackQueryMetadata callbackQueryMetadata =
new CallbackQueryMetadata.CallbackQueryMetadataBuilder()
.withEvent("eventExample")
.withTelegramChatId(12324L)
.withMessageId(42342L)
.build();
final String insertQuery = "insert into callback_query_context values (?, ?, ?::json, ?, ?)";
final int affectedRows =
database
.sqlUpdate(insertQuery)
.setParameter(uuidId)
.setParameter(uuidEntryGroup)
.setParameter(objectMapper.writeValueAsString(callbackQueryMetadata))
.setParameter(timestampFromUnixEpoch)
.setParameter(timestampFromUnixEpoch)
.execute();
assertEquals(1, affectedRows);
final CallbackQueryContext got = new QCallbackQueryContext().id.eq(uuidId).findOne();
assertNotNull(got);
final CallbackQueryMetadata gotFields = got.getFields();
assertNotNull(gotFields);
assertEquals("eventExample", gotFields.getEvent());
assertEquals(12324L, Double.valueOf(gotFields.getTelegramChatId()).longValue());
assertEquals(42342L, Double.valueOf(gotFields.getMessageId()).longValue());
}
}