From 9889d25bf3cb93d23b33eddd178a4db3fff1dea0 Mon Sep 17 00:00:00 2001 From: Davide Polonio Date: Fri, 31 Mar 2023 09:53:42 +0200 Subject: [PATCH] feat: add Javadoc, add tests --- .../orm/model/CallbackQueryContext.java | 20 +++- .../callbackquery/ChangeLanguage.java | 18 +++ .../migration/V1_0_0__Create_initial_db.sql | 9 ++ .../CallbackQueryContextIntegrationTest.java | 108 ++++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContextIntegrationTest.java diff --git a/src/main/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContext.java b/src/main/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContext.java index 3c29f91..8cf1ef7 100644 --- a/src/main/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContext.java +++ b/src/main/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContext.java @@ -3,6 +3,7 @@ package com.github.polpetta.mezzotre.orm.model; import com.github.polpetta.types.json.CallbackQueryMetadata; import io.ebean.annotation.DbJson; import io.ebean.annotation.Length; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @@ -31,7 +32,10 @@ public class CallbackQueryContext extends Base { @Length(36) 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) { this.id = id; @@ -50,4 +54,18 @@ public class CallbackQueryContext extends Base { public CallbackQueryMetadata getFields() { return fields; } + + @Override + public String toString() { + return "CallbackQueryContext{" + + "id='" + + id + + '\'' + + ", entryGroup='" + + entryGroup + + '\'' + + ", fields=" + + fields + + '}'; + } } diff --git a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ChangeLanguage.java b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ChangeLanguage.java index c0a0aaf..fa533ac 100644 --- a/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ChangeLanguage.java +++ b/src/main/java/com/github/polpetta/mezzotre/telegram/callbackquery/ChangeLanguage.java @@ -23,6 +23,12 @@ import org.apache.velocity.tools.ToolManager; import org.apache.velocity.util.StringBuilderWriter; import org.slf4j.Logger; +/** + * ChangeLanguage event is related to support locale change for a particular chat + * + * @author Davide Polonio + * @since 1.0 + */ @Singleton public class ChangeLanguage implements Processor { @@ -31,6 +37,12 @@ public class ChangeLanguage implements Processor { private final LocalizedMessageFactory localizedMessageFactory; private final Logger log; + /** + * Additional fields that are related to {@code changeLanguage} event + * + * @author Davide Polonio + * @since 1.0 + */ public enum Field { 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 { English("en-US"), Italian("it-IT"); diff --git a/src/main/resources/db/migration/V1_0_0__Create_initial_db.sql b/src/main/resources/db/migration/V1_0_0__Create_initial_db.sql index 5808186..0146fa6 100644 --- a/src/main/resources/db/migration/V1_0_0__Create_initial_db.sql +++ b/src/main/resources/db/migration/V1_0_0__Create_initial_db.sql @@ -1,4 +1,13 @@ -- 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 ( id bigint generated by default as identity not null, chat_context jsonb not null default '{}'::jsonb not null, diff --git a/src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContextIntegrationTest.java b/src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContextIntegrationTest.java new file mode 100644 index 0000000..2c45563 --- /dev/null +++ b/src/test/java/com/github/polpetta/mezzotre/orm/model/CallbackQueryContextIntegrationTest.java @@ -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()); + } +}