Skip to content

Commit

Permalink
MM-1280 - Added new geo reporting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislavin committed Feb 7, 2017
1 parent e05a246 commit 3b6d7c5
Show file tree
Hide file tree
Showing 51 changed files with 1,867 additions and 1,038 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.infobip.mobile.messaging.Message;
import org.infobip.mobile.messaging.MobileMessaging;
import org.infobip.mobile.messaging.api.shaded.google.gson.GsonBuilder;
import org.infobip.mobile.messaging.storage.MessageStore;
import org.json.JSONException;

Expand Down Expand Up @@ -159,12 +160,8 @@ private String getFullMessageText(int groupPosition) {
text += "\nfrom: " + m.getFrom();
text += "\nreceivedTimestamp: " + m.getReceivedTimestamp();
text += "\nseenTimestamp: " + m.getSeenTimestamp();
if (m.getInternalData() != null) {
try {
text += "\ninternalData: " + m.getInternalData().toString(1);
} catch (JSONException e) {
e.printStackTrace();
}
if (m.getGeo() != null) {
text += "\ngeo: " + new GsonBuilder().setPrettyPrinting().create().toJson(m.getGeo());
}
if (m.getCustomPayload() != null) {
try {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.infobip.mobile.messaging;

import android.os.Bundle;

import junit.framework.TestCase;

import org.infobip.mobile.messaging.dal.bundle.FCMMessageMapper;

/**
* @author sslavin
* @since 29/12/2016.
*/

public class FCMMessageMapperTest extends TestCase {

public void test_message_should_be_possible_to_construct_message_from_bundle() throws Exception {
Bundle bundle = new Bundle();
bundle.putString("gcm.notification.messageId", "SomeMessageId");
bundle.putString("gcm.notification.title", "SomeMessageTitle");
bundle.putString("gcm.notification.body", "SomeMessageBody");
bundle.putString("gcm.notification.sound", "SomeMessageSound");
bundle.putString("gcm.notification.sound2", "SomeMessageSound");
bundle.putString("gcm.notification.vibrate", "true");
bundle.putString("gcm.notification.icon", "SomeMessageIcon");
bundle.putString("gcm.notification.silent", "false");
bundle.putString("gcm.notification.category", "SomeMessageCategory");
bundle.putString("from", "SomeMessageFrom");
bundle.putLong("received_timestamp", 1234L);
bundle.putLong("seen_timestamp", 5678L);
bundle.putString("internalData", "{}");
bundle.putString("customPayload", "{}");
bundle.putString("destination", "SomeDestination");
bundle.putString("status", "SUCCESS");
bundle.putString("status_message", "SomeStatusMessage");

Message message = FCMMessageMapper.fromCloudBundle(bundle);

assertEquals("SomeMessageId", message.getMessageId());
assertEquals("SomeMessageTitle", message.getTitle());
assertEquals("SomeMessageBody", message.getBody());
assertEquals("SomeMessageSound", message.getSound());
assertEquals(true, message.isVibrate());
assertEquals("SomeMessageIcon", message.getIcon());
assertEquals(false, message.isSilent());
assertEquals("SomeMessageCategory", message.getCategory());
assertEquals("SomeMessageFrom", message.getFrom());
assertEquals(1234L, message.getReceivedTimestamp());
assertEquals(5678L, message.getSeenTimestamp());
assertEquals(0, message.getCustomPayload().length());
assertTrue(message.getGeo() == null || message.getGeo().getAreasList() == null);
assertEquals("SomeDestination", message.getDestination());
assertEquals(Message.Status.SUCCESS, message.getStatus());
assertEquals("SomeStatusMessage", message.getStatusMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import junit.framework.TestCase;

import org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer;
import org.infobip.mobile.messaging.dal.bundle.BundleMessageMapper;
import org.infobip.mobile.messaging.dal.bundle.FCMMessageMapper;
import org.infobip.mobile.messaging.geo.Area;
import org.infobip.mobile.messaging.geo.Geo;
import org.skyscreamer.jsonassert.JSONAssert;

import java.util.ArrayList;
import java.util.Date;

import static org.junit.Assert.assertNotEquals;
Expand All @@ -17,11 +19,11 @@
* @author mstipanov
* @since 30.03.2016.
*/
public class MessageTest extends TestCase {
public class FCMMessageTest extends TestCase {

private class GeoTest extends Geo {
GeoTest() {
super(null, null, null);
super(null, null, null, null, null, null, new ArrayList<Area>(), null);
}

Date getExpiry() {
Expand All @@ -31,11 +33,11 @@ Date getExpiry() {

public void test_toBundle_success() throws Exception {

Message message = Message.createFrom(new Bundle());
Message message = new Message();
message.setTitle("lala");
message.setFrom("from");

Bundle plainBundle = BundleMessageMapper.toBundle(message);
Bundle plainBundle = FCMMessageMapper.toCloudBundle(message);
assertEquals(plainBundle.getString("gcm.notification.title"), "lala");
assertEquals(plainBundle.getString("from"), "from");
}
Expand All @@ -51,7 +53,7 @@ public void test_fromBundle_success() throws Exception {
plainBundle.putString("gcm.notification.icon", "some icon");
plainBundle.putString("gcm.notification.silent", "false");

Message message = Message.createFrom(plainBundle);
Message message = FCMMessageMapper.fromCloudBundle(plainBundle);

assertEquals(message.getMessageId(), "1234");
assertEquals(message.getSound(), "some sound");
Expand All @@ -73,7 +75,7 @@ public void test_customData() throws Exception {
Bundle bundle = new Bundle();
bundle.putString("customPayload", customPayload);

Message message = Message.createFrom(bundle);
Message message = FCMMessageMapper.fromCloudBundle(bundle);

JSONAssert.assertEquals(customPayload, message.getCustomPayload(), true);
}
Expand All @@ -97,7 +99,7 @@ public void test_customDataWithGcmKeys() throws Exception {
bundle.putString("android.support.content.wakelockid", "11");
bundle.putString("collapse_key", "org.infobip.mobile.messaging.showcase.dev");

Message message = Message.createFrom(bundle);
Message message = FCMMessageMapper.fromCloudBundle(bundle);

JSONAssert.assertEquals(customPayload, message.getCustomPayload(), true);
}
Expand All @@ -123,7 +125,7 @@ public void test_silentMessage_fromJson_withSilentData() throws Exception {
bundle.putString("internalData", internalData);
bundle.putString("gcm.notification.silent", "true");

Message message = Message.createFrom(bundle);
Message message = FCMMessageMapper.fromCloudBundle(bundle);

assertEquals("silentTitle", message.getTitle());
assertEquals("silentBody", message.getBody());
Expand All @@ -143,7 +145,7 @@ public void test_silentMessage_fromJson_withoutSilentData() throws Exception {
bundle.putString("gcm.notification.body", "notSilentBody");
bundle.putString("gcm.notification.sound", "notSilentSound");

Message message = Message.createFrom(bundle);
Message message = FCMMessageMapper.fromCloudBundle(bundle);

assertNotEquals("notSilentTitle", message.getTitle());
assertNotEquals("notSilentBody", message.getBody());
Expand Down Expand Up @@ -174,7 +176,7 @@ public void test_normalMessage_fromJson_withNormalData() throws Exception {
bundle.putString("gcm.notification.category", "notSilentCategory");
bundle.putString("internalData", internalData);

Message message = Message.createFrom(bundle);
Message message = FCMMessageMapper.fromCloudBundle(bundle);

assertEquals("notSilentTitle", message.getTitle());
assertEquals("notSilentBody", message.getBody());
Expand Down Expand Up @@ -202,7 +204,7 @@ public void test_normalMessage_fromJson_withoutNormalData() throws Exception {
bundle.putString("collapse_key", "org.infobip.mobile.messaging.showcase.dev");
bundle.putString("internalData", internalData);

Message message = Message.createFrom(bundle);
Message message = FCMMessageMapper.fromCloudBundle(bundle);

assertNotEquals("silentTitle", message.getTitle());
assertNotEquals("silentBody", message.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import org.infobip.mobile.messaging.api.support.http.serialization.JsonSerializer;
import org.infobip.mobile.messaging.geo.Area;
import org.infobip.mobile.messaging.geo.Geo;
import org.infobip.mobile.messaging.geo.GeoEvent;
import org.infobip.mobile.messaging.geo.GeoEventSetting;
import org.infobip.mobile.messaging.util.PreferenceHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand All @@ -22,10 +23,10 @@ public class GeoEventsTest extends InstrumentationTestCase {

private class GeoTest extends Geo {
public GeoTest() {
super(null, null, null);
super(null, null, null, null, null, null, new ArrayList<Area>(), null);
}

List<GeoEvent> getEventFilters() {
List<GeoEventSetting> getEventFilters() {
return getEvents();
}
}
Expand Down Expand Up @@ -61,14 +62,14 @@ private void setLastNotificationTimeForArea(Area area, long timeMs) {

public void test_handle_geo_events() throws InterruptedException {
GeoTest geo = new JsonSerializer().deserialize(internalData(), GeoTest.class);
List<Area> geofenceAreasList = geo.getAreasList();

int entryCnt = 0;

// TODO: Implement with reasonable test time, otherwise it takes forever to run
for (int i = 0; i < 5; i++) {

List<GeoEvent> events = geo.getEventFilters();
for (GeoEvent event : events) {
List<GeoEventSetting> events = geo.getEventFilters();
for (GeoEventSetting event : events) {

for (Area area : geo.getAreasList()) {

Expand All @@ -81,11 +82,11 @@ public void test_handle_geo_events() throws InterruptedException {
boolean isTimeoutExpired = timeIntervalBetweenEvents > TimeUnit.MINUTES.toMillis(event.getTimeoutInMinutes());

int eventLimit = event.getLimit();
boolean isLimitBreached = eventLimit != GeoEvent.UNLIMITED_RECURRING && timesTriggered >= eventLimit;
boolean isLimitBreached = eventLimit != GeoEventSetting.UNLIMITED_RECURRING && timesTriggered >= eventLimit;

if (isTimeoutExpired && !isLimitBreached) {

if (eventLimit != GeoEvent.UNLIMITED_RECURRING) {
if (eventLimit != GeoEventSetting.UNLIMITED_RECURRING) {
++timesTriggered;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected void setUp() throws Exception {
captor = ArgumentCaptor.forClass(Intent.class);
receiver = Mockito.mock(BroadcastReceiver.class);
getInstrumentation().getContext().registerReceiver(receiver, new IntentFilter(Event.MESSAGES_SENT.getKey()));
getInstrumentation().getContext().deleteDatabase("mm_infobip_database.db");
}

@Override
Expand Down Expand Up @@ -113,15 +114,15 @@ public void test_sendMultipleMessages() {
assertEquals("myDestination", messages.get(0).getDestination());
assertEquals("myText", messages.get(0).getBody());
assertEquals("string", messages.get(0).getCustomPayload().opt("myStringKey"));
assertEquals(1, messages.get(0).getCustomPayload().opt("myNumberKey"));
assertEquals(1.0, messages.get(0).getCustomPayload().optDouble("myNumberKey"), 0.01);
assertEquals(true, messages.get(0).getCustomPayload().opt("myBooleanKey"));
assertEquals("myMessageId2", messages.get(1).getMessageId());
assertEquals(Message.Status.SUCCESS, messages.get(1).getStatus());
assertEquals("Message sent", messages.get(1).getStatusMessage());
assertEquals("myDestination2", messages.get(1).getDestination());
assertEquals("myText2", messages.get(1).getBody());
assertEquals("string2", messages.get(1).getCustomPayload().opt("myStringKey"));
assertEquals(2, messages.get(1).getCustomPayload().opt("myNumberKey"));
assertEquals(2.0, messages.get(1).getCustomPayload().optDouble("myNumberKey"), 0.01);
assertEquals(false, messages.get(1).getCustomPayload().opt("myBooleanKey"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,8 @@ public void test_shouldCountAllObjectsInDatabase() {

assertEquals(100, databaseHelper.countAll(SomethingInDatabase.class));
}

public void test_shouldMigrateTable() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void test_shouldMigrateAllMessagesToSqlite() throws Exception {
0,
null,
null,
null,
"SomeDestination" + i,
Message.Status.SUCCESS,
"SomeStatusMessage" + i
Expand Down Expand Up @@ -83,7 +82,6 @@ public void test_shouldMigrateAllMessagesToSqlite() throws Exception {
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).getInternalData());
Assert.assertEquals(null, map.get(id).getCustomPayload());
Assert.assertEquals(null, map.get(id).getGeo());
Assert.assertEquals("SomeDestination" + i, map.get(id).getDestination());
Expand Down
Loading

0 comments on commit 3b6d7c5

Please sign in to comment.