Search in sources :

Example 1 with SQLiteMessageStore

use of org.infobip.mobile.messaging.storage.SQLiteMessageStore in project mobile-messaging-sdk-android by infobip.

the class SharedPreferencesMigrationTest method test_shouldMigrateAllMessagesToSqlite.

@Test
public void test_shouldMigrateAllMessagesToSqlite() throws Exception {
    UUID uuid = UUID.randomUUID();
    int numberOfMessages = 100;
    JsonObject attachment = new JsonObject();
    attachment.addProperty("url", "http://www.some-content.com.ru.hr");
    JsonArray attachments = new JsonArray();
    attachments.add(attachment);
    JsonObject internalData = new JsonObject();
    internalData.add("atts", attachments);
    internalData.add("silent", new JsonObject());
    for (int i = 0; i < numberOfMessages; i++) {
        sharedPreferencesMessageStore.save(context, new Message(i + uuid.toString(), "SomeTitle" + i, "SomeBody" + i, "SomeSound" + i, true, "SomeIcon" + i, false, "SomeCategory" + i, "SomeFrom" + i, 0, 0, 0, null, internalData.toString(), "SomeDestination" + i, Message.Status.SUCCESS, "SomeStatusMessage" + i, "http://www.some-content.com.ru.hr"));
    }
    MessageStore store = new SQLiteMessageStore();
    List<Message> messages = store.findAll(context);
    HashMap<String, Message> map = new HashMap<>();
    for (Message m : messages) map.put(m.getMessageId(), m);
    // we are not removing messages from shared prefs
    // in case user still uses SharedPreferencesMessageStore
    Assert.assertEquals(numberOfMessages, sharedPreferencesMessageStore.findAll(context).size());
    Assert.assertEquals(numberOfMessages, map.size());
    for (int i = 0; i < numberOfMessages; i++) {
        String id = i + uuid.toString();
        Assert.assertEquals(id, map.get(id).getMessageId());
        Assert.assertEquals("SomeTitle" + i, map.get(id).getTitle());
        Assert.assertEquals("SomeBody" + i, map.get(id).getBody());
        Assert.assertEquals("SomeSound" + i, map.get(id).getSound());
        Assert.assertEquals(true, map.get(id).isVibrate());
        Assert.assertEquals("SomeIcon" + i, map.get(id).getIcon());
        Assert.assertEquals(false, map.get(id).isSilent());
        Assert.assertEquals("SomeCategory" + i, map.get(id).getCategory());
        Assert.assertEquals("SomeFrom" + i, map.get(id).getFrom());
        Assert.assertEquals(0, map.get(id).getReceivedTimestamp());
        Assert.assertEquals(0, map.get(id).getSeenTimestamp());
        Assert.assertEquals(null, map.get(id).getCustomPayload());
        JSONAssert.assertEquals(internalData.toString(), map.get(id).getInternalData(), false);
        Assert.assertEquals("SomeDestination" + i, map.get(id).getDestination());
        Assert.assertEquals(Message.Status.SUCCESS, map.get(id).getStatus());
        Assert.assertEquals("SomeStatusMessage" + i, map.get(id).getStatusMessage());
        Assert.assertEquals("http://www.some-content.com.ru.hr", map.get(id).getContentUrl());
    }
}
Also used : JsonArray(org.infobip.mobile.messaging.api.shaded.google.gson.JsonArray) SharedPreferencesMessageStore(org.infobip.mobile.messaging.storage.SharedPreferencesMessageStore) MessageStore(org.infobip.mobile.messaging.storage.MessageStore) SQLiteMessageStore(org.infobip.mobile.messaging.storage.SQLiteMessageStore) Message(org.infobip.mobile.messaging.Message) SQLiteMessageStore(org.infobip.mobile.messaging.storage.SQLiteMessageStore) HashMap(java.util.HashMap) JsonObject(org.infobip.mobile.messaging.api.shaded.google.gson.JsonObject) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with SQLiteMessageStore

use of org.infobip.mobile.messaging.storage.SQLiteMessageStore in project mobile-messaging-sdk-android by infobip.

the class SqliteMessageMigrationTest method test_shouldAddSendDateTimeToInternalData.

@Test
public void test_shouldAddSendDateTimeToInternalData() throws Exception {
    // Create SQLiteOpenHelper directly to perform raw operations on database
    context.deleteDatabase(DatabaseHelperImpl.DATABASE_NAME);
    SQLiteOpenHelper sqLiteOpenHelper = new SQLiteOpenHelper(context, DatabaseHelperImpl.DATABASE_NAME, null, DatabaseHelperImpl.VER_2017_MAY_15) {

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_CREATE_MAY_MESSAGES_TABLE);
            db.execSQL(SQL_CREATE_GEO_MESSAGES_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    };
    SQLiteDatabase db = sqLiteOpenHelper.getWritableDatabase();
    // Save new data to database
    ContentValues contentValues = new ContentValues();
    contentValues.put("id", "SomeMessageId");
    contentValues.put("title", "SomeMessageTitle");
    contentValues.put("body", "SomeMessageBody");
    contentValues.put("sound", "SomeMessageSound");
    contentValues.put("vibrate", 1);
    contentValues.put("icon", "SomeMessageIcon");
    contentValues.put("silent", 1);
    contentValues.put("category", "SomeMessageCategory");
    contentValues.put("_from", "SomeMessageFrom");
    contentValues.put("received_timestamp", 1234L);
    contentValues.put("seen_timestamp", 5678L);
    contentValues.put("internal_data", "{\"key1\":\"value1\"}");
    contentValues.put("custom_payload", "{\"key2\":\"value2\"}");
    contentValues.put("destination", "SomeMessageDestination");
    contentValues.put("status", "ERROR");
    contentValues.put("status_message", "SomeMessageStatusMessage");
    contentValues.put("content_url", "SomeMessageContentUrl");
    db.insertWithOnConflict(DatabaseContract.Tables.MESSAGES, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
    db.close();
    sqLiteOpenHelper.close();
    // Check that sent timestamp was added and other fields are the same
    SQLiteMessageStore messageStore = new SQLiteMessageStore();
    List<Message> messages = messageStore.findAll(context);
    assertEquals(1, messages.size());
    assertEquals("SomeMessageId", messages.get(0).getMessageId());
    assertEquals("SomeMessageTitle", messages.get(0).getTitle());
    assertEquals("SomeMessageBody", messages.get(0).getBody());
    assertEquals("SomeMessageSound", messages.get(0).getSound());
    assertEquals(true, messages.get(0).isVibrate());
    assertEquals("SomeMessageIcon", messages.get(0).getIcon());
    assertEquals(true, messages.get(0).isSilent());
    assertEquals("SomeMessageCategory", messages.get(0).getCategory());
    assertEquals("SomeMessageFrom", messages.get(0).getFrom());
    assertEquals(1234L, messages.get(0).getReceivedTimestamp());
    assertEquals(5678L, messages.get(0).getSeenTimestamp());
    assertEquals(1234L, messages.get(0).getSentTimestamp());
    JSONAssert.assertEquals("{\"key1\" : \"value1\", \"sendDateTime\":1234}", messages.get(0).getInternalData(), true);
    JSONAssert.assertEquals("{\"key2\" : \"value2\"}", messages.get(0).getCustomPayload(), true);
    assertEquals("SomeMessageDestination", messages.get(0).getDestination());
    assertEquals(Message.Status.ERROR, messages.get(0).getStatus());
    assertEquals("SomeMessageStatusMessage", messages.get(0).getStatusMessage());
    assertEquals("SomeMessageContentUrl", messages.get(0).getContentUrl());
}
Also used : SQLiteOpenHelper(android.database.sqlite.SQLiteOpenHelper) ContentValues(android.content.ContentValues) Message(org.infobip.mobile.messaging.Message) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteMessageStore(org.infobip.mobile.messaging.storage.SQLiteMessageStore) Test(org.junit.Test)

Aggregations

Message (org.infobip.mobile.messaging.Message)2 SQLiteMessageStore (org.infobip.mobile.messaging.storage.SQLiteMessageStore)2 Test (org.junit.Test)2 ContentValues (android.content.ContentValues)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 JsonArray (org.infobip.mobile.messaging.api.shaded.google.gson.JsonArray)1 JsonObject (org.infobip.mobile.messaging.api.shaded.google.gson.JsonObject)1 MessageStore (org.infobip.mobile.messaging.storage.MessageStore)1 SharedPreferencesMessageStore (org.infobip.mobile.messaging.storage.SharedPreferencesMessageStore)1