Search in sources :

Example 1 with MoMessageDelivery

use of org.infobip.mobile.messaging.api.messages.MoMessageDelivery in project mobile-messaging-sdk-android by infobip.

the class MoMessageSenderTest method shouldOnlySaveNonRetriableMessagesToStore.

@Test
public void shouldOnlySaveNonRetriableMessagesToStore() {
    // Given
    MobileMessagingCore.setMessageStoreClass(context, SQLiteMessageStore.class);
    Message givenMessage1 = givenMessage("someMessageId1");
    Message givenMessage2 = givenMessage("someMessageId2");
    final MoMessageDelivery givenDelivery1 = givenDelivery(givenMessage1.getMessageId());
    final MoMessageDelivery givenDelivery2 = givenDelivery(givenMessage2.getMessageId());
    given(apiMock.sendMO(any(MoMessagesBody.class))).willReturn(new MoMessagesResponse(new MoMessageDelivery[] { givenDelivery1 })).willReturn(new MoMessagesResponse(new MoMessageDelivery[] { givenDelivery2 }));
    // When
    moMessageSender.send(null, givenMessage1);
    moMessageSender.sendWithRetry(givenMessage2);
    // Then
    verify(apiMock, after(200).atLeast(1)).sendMO(any(MoMessagesBody.class));
    verify(messageStoreWrapperMock, times(1)).upsert(messageCaptor.capture());
    List<Message> storedMessages = getAllMessages(messageCaptor.getAllValues());
    assertEquals(1, storedMessages.size());
    assertEquals(givenMessage1.getMessageId(), storedMessages.get(0).getMessageId());
}
Also used : MoMessage(org.infobip.mobile.messaging.api.messages.MoMessage) Message(org.infobip.mobile.messaging.Message) MoMessageDelivery(org.infobip.mobile.messaging.api.messages.MoMessageDelivery) MoMessagesBody(org.infobip.mobile.messaging.api.messages.MoMessagesBody) MoMessagesResponse(org.infobip.mobile.messaging.api.messages.MoMessagesResponse) Test(org.junit.Test)

Example 2 with MoMessageDelivery

use of org.infobip.mobile.messaging.api.messages.MoMessageDelivery in project mobile-messaging-sdk-android by infobip.

the class MoMessageSenderTest method shouldSendMultipleMessages.

@Test
public void shouldSendMultipleMessages() throws Exception {
    // Given
    final MoMessageDelivery givenMessage1 = new MoMessageDelivery() {

        {
            setStatus("Message not sent");
            setStatusCode(1);
            setMessageId("myMessageId");
            setDestination("myDestination");
            setText("myText");
            setCustomPayload(new HashMap<String, Object>() {

                {
                    put("myStringKey", "string");
                    put("myBooleanKey", true);
                    put("myNumberKey", 1);
                }
            });
        }
    };
    final MoMessageDelivery givenMessage2 = new MoMessageDelivery() {

        {
            setStatus("Message sent");
            setStatusCode(0);
            setMessageId("myMessageId2");
            setDestination("myDestination2");
            setText("myText2");
            setCustomPayload(new HashMap<String, Object>() {

                {
                    put("myStringKey", "string2");
                    put("myBooleanKey", false);
                    put("myNumberKey", 2);
                }
            });
        }
    };
    MoMessagesResponse givenResponse = new MoMessagesResponse() {

        {
            setMessages(new MoMessageDelivery[] { givenMessage1, givenMessage2 });
        }
    };
    given(apiMock.sendMO(any(MoMessagesBody.class))).willReturn(givenResponse);
    // When
    moMessageSender.send(null, givenMessage(givenMessage1.getMessageId()), givenMessage(givenMessage2.getMessageId()));
    // Then
    verify(broadcaster, after(1000).atLeastOnce()).messagesSent(captor.capture());
    List<Message> messages = captor.getValue();
    assertEquals("myMessageId", messages.get(0).getMessageId());
    assertEquals(Message.Status.ERROR, messages.get(0).getStatus());
    assertEquals("Message not sent", messages.get(0).getStatusMessage());
    assertEquals("myDestination", messages.get(0).getDestination());
    assertEquals("myText", messages.get(0).getBody());
    assertEquals("string", messages.get(0).getCustomPayload().opt("myStringKey"));
    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.0, messages.get(1).getCustomPayload().optDouble("myNumberKey"), 0.01);
    assertEquals(false, messages.get(1).getCustomPayload().opt("myBooleanKey"));
}
Also used : MoMessage(org.infobip.mobile.messaging.api.messages.MoMessage) Message(org.infobip.mobile.messaging.Message) MoMessageDelivery(org.infobip.mobile.messaging.api.messages.MoMessageDelivery) MoMessagesBody(org.infobip.mobile.messaging.api.messages.MoMessagesBody) MoMessagesResponse(org.infobip.mobile.messaging.api.messages.MoMessagesResponse) Test(org.junit.Test)

Example 3 with MoMessageDelivery

use of org.infobip.mobile.messaging.api.messages.MoMessageDelivery in project mobile-messaging-sdk-android by infobip.

the class MoMessageMapper method messages.

static Message[] messages(MoMessagesResponse response) {
    if (response == null || response.getMessages() == null || response.getMessages().length == 0) {
        return new Message[0];
    }
    List<Message> messages = new ArrayList<>(response.getMessages().length);
    for (MoMessageDelivery delivery : response.getMessages()) {
        Message message = new Message();
        message.setMessageId(delivery.getMessageId());
        message.setDestination(delivery.getDestination());
        message.setBody(delivery.getText());
        message.setStatusMessage(delivery.getStatus());
        message.setReceivedTimestamp(Time.now());
        message.setSeenTimestamp(Time.now());
        message.setCustomPayload(delivery.getCustomPayload() != null ? new JSONObject(delivery.getCustomPayload()) : null);
        Message.Status status = Message.Status.UNKNOWN;
        int statusCode = delivery.getStatusCode();
        if (statusCode < Message.Status.values().length) {
            status = Message.Status.values()[statusCode];
        } else {
            MobileMessagingLogger.e(delivery.getMessageId() + ":Unexpected status code: " + statusCode);
        }
        message.setStatus(status);
        messages.add(message);
    }
    return messages.toArray(new Message[messages.size()]);
}
Also used : MoMessage(org.infobip.mobile.messaging.api.messages.MoMessage) Message(org.infobip.mobile.messaging.Message) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) MoMessageDelivery(org.infobip.mobile.messaging.api.messages.MoMessageDelivery)

Aggregations

Message (org.infobip.mobile.messaging.Message)3 MoMessage (org.infobip.mobile.messaging.api.messages.MoMessage)3 MoMessageDelivery (org.infobip.mobile.messaging.api.messages.MoMessageDelivery)3 MoMessagesBody (org.infobip.mobile.messaging.api.messages.MoMessagesBody)2 MoMessagesResponse (org.infobip.mobile.messaging.api.messages.MoMessagesResponse)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 JSONObject (org.json.JSONObject)1