diff --git a/src/main/java/de/fabianonline/telegram_backup/models/Message.java b/src/main/java/de/fabianonline/telegram_backup/models/Message.java index 942e861..88ee6a0 100644 --- a/src/main/java/de/fabianonline/telegram_backup/models/Message.java +++ b/src/main/java/de/fabianonline/telegram_backup/models/Message.java @@ -8,18 +8,24 @@ public class Message { protected static String tableName = "messages"; private JsonObject json; private String message = null; - + private Integer id = null; + public Message(String json) { this.json = new JsonParser().parse(json).getAsJsonObject(); } - + public static Message get(int id) { String json = Database.getInstance().queryString("SELECT json FROM " + tableName + " WHERE id=" + id); return new Message(json); } - + public String getMessage() { - if (message != null) return message; - return message = json.getAsJsonPrimitive("message").getAsString(); + if (message==null) message=json.getAsJsonPrimitive("message").getAsString(); + return message; + } + + public int getId() { + if (id==null) id=json.getAsJsonPrimitive("id").getAsInt(); + return id; } } diff --git a/src/test/java/de/fabianonline/telegram_backup/models/MessageParsingTest.java b/src/test/java/de/fabianonline/telegram_backup/models/MessageParsingTest.java new file mode 100644 index 0000000..75ce618 --- /dev/null +++ b/src/test/java/de/fabianonline/telegram_backup/models/MessageParsingTest.java @@ -0,0 +1,22 @@ +import static org.junit.Assert.*; +import org.junit.Test; +import de.fabianonline.telegram_backup.models.Message; +import de.fabianonline.telegram_backup.test.models.TestJSON; + +public class MessageParsingTest { + @Test + public void testGetMessage() { + assertEquals( + "Hey cool, i will test it at weekend", + new Message(TestJSON.layer53message).getMessage()); + assertEquals( + "Nur mal so als Info, was wir bisher haben.", + new Message(TestJSON.layer51message).getMessage()); + } + + @Test + public void testGetId() { + assertEquals(15200, new Message(TestJSON.layer51message).getId()); + assertEquals(39402, new Message(TestJSON.layer53message).getId()); + } +} diff --git a/src/test/java/de/fabianonline/telegram_backup/models/TestJSON.java b/src/test/java/de/fabianonline/telegram_backup/models/TestJSON.java new file mode 100644 index 0000000..e02be65 --- /dev/null +++ b/src/test/java/de/fabianonline/telegram_backup/models/TestJSON.java @@ -0,0 +1,23 @@ +package de.fabianonline.telegram_backup.test.models; + +public class TestJSON { + public static String layer53message = "{" + + "\"flags\": 256," + + "\"out\": false," + + "\"mentioned\": false," + + "\"mediaUnread\": false," + + "\"silent\": false," + + "\"post\": false," + + "\"fromId\": 159899813," + + "\"toId\": {" + + "\"chatId\": 128405771," + + "\"_constructor\": \"peerChat#bad0e5bb\"" + + "}," + + "\"date\": 1487145903," + + "\"message\": \"Hey cool, i will test it at weekend\"," + + "\"_constructor\": \"message#c09be45f\"," + + "\"id\": 39402" + + "}"; + + public static String layer51message = "{\"flags\":256,\"unread\":false,\"out\":false,\"mentioned\":false,\"mediaUnread\":false,\"fromId\":2088876634,\"toId\":{\"chatId\":123851650,\"_constructor\":\"peerChat#bad0e5bb\"},\"date\":1468447000,\"message\":\"Nur mal so als Info, was wir bisher haben.\",\"_constructor\":\"message#c992e15c\",\"id\":15200}"; +}