use of org.infobip.mobile.messaging.api.geo.EventReport in project mobile-messaging-sdk-android by infobip.
the class MobileApiGeoTest method create_eventReport_success.
@Test
public void create_eventReport_success() throws Exception {
String jsonResponse = "{" + " 'finishedCampaignIds':['id1', 'id2', 'id3']," + " 'suspendedCampaignIds':['id4', 'id5', 'id6']," + " 'messageIds':{'messageId1':'newMessageId1', 'messageId2':'newMessageId2', 'messageId3':'newMessageId3'}" + "}";
Set<EventReport> events = new HashSet<EventReport>() {
{
add(new EventReport(EventType.entry, "myAreaId1", "campaignId1", "messageId1", "sdkMessageId1", 1L));
add(new EventReport(EventType.exit, "myAreaId2", "campaignId1", "messageId1", "sdkMessageId2", 2L));
add(new EventReport(EventType.dwell, "myAreaId3", "campaignId2", "messageId2", "sdkMessageId3", 3L));
}
};
Set<MessagePayload> messages = new HashSet<MessagePayload>() {
{
add(new MessagePayload("messageId1", null, null, null, null, null, null, null, null));
add(new MessagePayload("messageId2", null, null, null, null, null, null, null, null));
}
};
debugServer.respondWith(NanoHTTPD.Response.Status.OK, jsonResponse);
EventReportResponse response = mobileApiGeo.report(new EventReportBody(messages, events, "SomeDeviceInstanceId"));
// inspect http context
assertThat(debugServer.getUri()).isEqualTo("/mobile/4/geo/event");
assertThat(debugServer.getRequestCount()).isEqualTo(1);
assertThat(debugServer.getRequestMethod()).isEqualTo(NanoHTTPD.Method.POST);
assertThat(debugServer.getQueryParametersCount()).isEqualTo(0);
assertThat(response.getFinishedCampaignIds().size()).isEqualTo(3);
assertThat(response.getFinishedCampaignIds()).contains("id1");
assertThat(response.getFinishedCampaignIds()).contains("id2");
assertThat(response.getFinishedCampaignIds()).contains("id3");
assertThat(response.getSuspendedCampaignIds().size()).isEqualTo(3);
assertThat(response.getSuspendedCampaignIds()).contains("id4");
assertThat(response.getSuspendedCampaignIds()).contains("id5");
assertThat(response.getSuspendedCampaignIds()).contains("id6");
assertThat(response.getMessageIds().get("messageId1")).isEqualTo("newMessageId1");
assertThat(response.getMessageIds().get("messageId2")).isEqualTo("newMessageId2");
assertThat(response.getMessageIds().get("messageId3")).isEqualTo("newMessageId3");
JSONAssert.assertEquals(debugServer.getBody(), "{" + "\"platformType\":\"GCM\"," + "\"deviceApplicationInstanceId\":\"SomeDeviceInstanceId\"," + "\"messages\": [" + "{\n" + " \"messageId\": \"messageId1\",\n" + " \"title\": null,\n" + " \"body\": null,\n" + " \"sound\": null,\n" + " \"vibrate\": null,\n" + " \"category\": null,\n" + " \"silent\": null,\n" + " \"customPayload\": null,\n" + " \"internalData\": null\n" + " },\n" + " {\n" + " \"messageId\": \"messageId2\",\n" + " \"title\": null,\n" + " \"body\": null,\n" + " \"sound\": null,\n" + " \"vibrate\": null,\n" + " \"category\": null,\n" + " \"silent\": null,\n" + " \"customPayload\": null,\n" + " \"internalData\": null\n" + " }" + "]," + "\"reports\": [" + "{" + "\"event\":\"entry\"," + "\"geoAreaId\":\"myAreaId1\"," + "\"campaignId\":\"campaignId1\"," + "\"messageId\":\"messageId1\"," + "\"sdkMessageId\":\"sdkMessageId1\"," + "\"timestampDelta\":1" + "}," + "{" + "\"event\":\"exit\"," + "\"geoAreaId\":\"myAreaId2\"," + "\"campaignId\":\"campaignId1\"," + "\"messageId\":\"messageId1\"," + "\"sdkMessageId\":\"sdkMessageId2\"," + "\"timestampDelta\":2" + "}," + "{" + "\"event\":\"dwell\"," + "\"geoAreaId\":\"myAreaId3\"," + "\"campaignId\":\"campaignId2\"," + "\"messageId\":\"messageId2\"," + "\"sdkMessageId\":\"sdkMessageId3\"," + "\"timestampDelta\":3" + "}" + "]" + "}", JSONCompareMode.LENIENT);
}
use of org.infobip.mobile.messaging.api.geo.EventReport in project mobile-messaging-sdk-android by infobip.
the class GeoReporter method prepareEventReportBody.
/**
* Creates event report request body based on provided geofencing report.
*
* @param geoMessageStore message store for geo
* @param geoReports map that contains original signaling messages as keys and related geo reports as values.
* @return request body for geo reporting.
*/
@NonNull
private static EventReportBody prepareEventReportBody(Context context, MessageStore geoMessageStore, @NonNull GeoReport[] geoReports) {
Set<MessagePayload> messagePayloads = new HashSet<>();
Set<EventReport> eventReports = new HashSet<>();
List<Message> messages = geoMessageStore.findAll(context);
for (GeoReport r : geoReports) {
Message m = GeoReportHelper.getSignalingMessageForReport(messages, r);
if (m == null) {
MobileMessagingLogger.e("Cannot find signaling message for id: " + r.getSignalingMessageId());
continue;
}
messagePayloads.add(new MessagePayload(m.getMessageId(), m.getTitle(), m.getBody(), m.getSound(), m.isVibrate(), m.getCategory(), m.isSilent(), m.getCustomPayload() != null ? m.getCustomPayload().toString() : null, InternalDataMapper.createInternalDataBasedOnMessageContents(m)));
Long timestampDelta = Time.now() - r.getTimestampOccurred();
Long timestampDeltaSeconds = TimeUnit.MILLISECONDS.toSeconds(timestampDelta);
eventReports.add(new EventReport(EventType.valueOf(r.getEvent().name()), r.getArea().getId(), r.getCampaignId(), r.getSignalingMessageId(), r.getMessageId(), timestampDeltaSeconds));
}
String deviceInstanceId = MobileMessagingCore.getInstance(context).getPushRegistrationId();
return new EventReportBody(messagePayloads, eventReports, deviceInstanceId);
}
use of org.infobip.mobile.messaging.api.geo.EventReport in project mobile-messaging-sdk-android by infobip.
the class GeoReporterTest method test_success.
@Test
public void test_success() throws Exception {
// Given
Area area1 = createArea("areaId1");
Area area2 = createArea("areaId2");
Area area3 = createArea("areaId3");
createMessage(context, "signalingMessageId1", "campaignId1", true, area1, area2);
createMessage(context, "signalingMessageId2", "campaignId3", true, area3);
createReport(context, "signalingMessageId1", "campaignId1", "messageId1", true, area1);
createReport(context, "signalingMessageId1", "campaignId1", "messageId2", true, area2);
createReport(context, "signalingMessageId2", "campaignId3", "messageId3", true, area3);
// When
geoReporter.synchronize();
// Then
// Examine what is reported back via broadcast intent
Mockito.verify(geoBroadcaster, Mockito.after(5000).atLeastOnce()).geoReported(geoReportCaptor.capture());
List<GeoReport> broadcastedReports = geoReportCaptor.getValue();
assertEquals(3, broadcastedReports.size());
for (GeoReport r : broadcastedReports) {
switch(r.getMessageId()) {
case "messageId1":
assertEquals(GeoEventType.entry, r.getEvent());
assertEquals("campaignId1", r.getCampaignId());
assertEquals("signalingMessageId1", r.getSignalingMessageId());
assertEquals(r.getMessageId(), "messageId1");
assertEquals(r.getArea().getId(), "areaId1");
break;
case "messageId2":
assertEquals(GeoEventType.entry, r.getEvent());
assertEquals("campaignId1", r.getCampaignId());
assertEquals("signalingMessageId1", r.getSignalingMessageId());
assertEquals("messageId2", r.getMessageId());
assertEquals("areaId2", r.getArea().getId());
break;
case "messageId3":
assertEquals(GeoEventType.entry, r.getEvent());
assertEquals("campaignId3", r.getCampaignId());
assertEquals("signalingMessageId2", r.getSignalingMessageId());
assertEquals("messageId3", r.getMessageId());
assertEquals("areaId3", r.getArea().getId());
break;
default:
assertTrue("Unexpected message id " + r.getMessageId(), false);
break;
}
}
// Examine HTTP request body
final EventReportBody body = reportBodyCaptor.getValue();
assertEquals(2, body.getMessages().size());
Set<String> messageIds = new HashSet<String>() {
{
List<MessagePayload> ps = new ArrayList<>(body.getMessages());
add(ps.get(0).getMessageId());
add(ps.get(1).getMessageId());
}
};
assertTrue(messageIds.contains("signalingMessageId1"));
assertTrue(messageIds.contains("signalingMessageId2"));
assertEquals(3, body.getReports().size());
EventReport[] reports = body.getReports().toArray(new EventReport[body.getReports().size()]);
assertNotSame(reports[0].getTimestampDelta(), reports[1].getTimestampDelta());
assertNotSame(reports[0].getTimestampDelta(), reports[2].getTimestampDelta());
assertNotSame(reports[1].getTimestampDelta(), reports[2].getTimestampDelta());
assertEquals(3, reports.length);
for (EventReport report : reports) {
switch(report.getSdkMessageId()) {
case "messageId1":
assertEquals(EventType.entry, report.getEvent());
assertEquals("areaId1", report.getGeoAreaId());
assertEquals("signalingMessageId1", report.getMessageId());
assertEquals("campaignId1", report.getCampaignId());
assertNotNull(report.getTimestampDelta());
break;
case "messageId2":
assertEquals(EventType.entry, report.getEvent());
assertEquals("areaId2", report.getGeoAreaId());
assertEquals("signalingMessageId1", report.getMessageId());
assertEquals("campaignId1", report.getCampaignId());
assertNotNull(report.getTimestampDelta());
break;
case "messageId3":
assertEquals(EventType.entry, report.getEvent());
assertEquals("areaId3", report.getGeoAreaId());
assertEquals("signalingMessageId2", report.getMessageId());
assertEquals("campaignId3", report.getCampaignId());
assertNotNull(report.getTimestampDelta());
break;
default:
assertTrue("Unexpected message id " + report.getMessageId(), false);
break;
}
}
}
Aggregations