From ef3daff3c569b2e581eab26af81cd5e528e2b4e3 Mon Sep 17 00:00:00 2001 From: Daniel Bevenius Date: Wed, 12 Aug 2015 08:08:50 +0200 Subject: [PATCH] Adding unit test to verify that badges are properly handled. --- .../APNsPushNotificationSenderTest.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/push/sender/src/test/java/org/jboss/aerogear/unifiedpush/message/sender/APNsPushNotificationSenderTest.java b/push/sender/src/test/java/org/jboss/aerogear/unifiedpush/message/sender/APNsPushNotificationSenderTest.java index 4ec557a776..f878f10ce2 100644 --- a/push/sender/src/test/java/org/jboss/aerogear/unifiedpush/message/sender/APNsPushNotificationSenderTest.java +++ b/push/sender/src/test/java/org/jboss/aerogear/unifiedpush/message/sender/APNsPushNotificationSenderTest.java @@ -17,6 +17,11 @@ package org.jboss.aerogear.unifiedpush.message.sender; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -25,12 +30,18 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.List; import java.util.UUID; +import com.notnoop.apns.ApnsService; import org.jboss.aerogear.unifiedpush.api.iOSVariant; import org.jboss.aerogear.unifiedpush.message.UnifiedPushMessage; +import org.jboss.aerogear.unifiedpush.message.cache.AbstractServiceCache.ServiceConstructor; import org.jboss.aerogear.unifiedpush.message.cache.ApnsServiceCache; import org.junit.Test; +import org.mockito.ArgumentCaptor; public class APNsPushNotificationSenderTest { @@ -49,7 +60,69 @@ public void callbackOnError() throws Exception { verify(callback).onError("Error sending payload to APNs server: Invalid hex character: t"); } + @Test + public void noBadge() throws Exception { + final ApnsService apnsService = mock(ApnsService.class); + final APNsPushNotificationSender sender = mockSender(apnsService); + final List tokens = Collections.singletonList("token"); + + sender.sendPushMessage(iosVariant(), tokens, new UnifiedPushMessage(), "123", mockCallback()); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(apnsService).push(eq(tokens), captor.capture(), any(Date.class)); + assertFalse("JSON Message should NOT contain a badge property.", captor.getValue().contains("\"badge\":0")); + } + + @Test + public void badgeZero() throws Exception { + final ApnsService apnsService = mock(ApnsService.class); + final APNsPushNotificationSender sender = mockSender(apnsService); + final List tokens = Collections.singletonList("token"); + final UnifiedPushMessage unifiedPushMessage = new UnifiedPushMessage(); + unifiedPushMessage.getMessage().setBadge(0); + + sender.sendPushMessage(iosVariant(), tokens, unifiedPushMessage, "123", mockCallback()); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(apnsService).push(eq(tokens), captor.capture(), any(Date.class)); + assertTrue("JSON Message should contain a badge property.", captor.getValue().contains("\"badge\":0")); + } + + @Test + public void badgeGreaterThanZero() throws Exception { + final ApnsService apnsService = mock(ApnsService.class); + final APNsPushNotificationSender sender = mockSender(apnsService); + final List tokens = Collections.singletonList("token"); + final UnifiedPushMessage unifiedPushMessage = new UnifiedPushMessage(); + unifiedPushMessage.getMessage().setBadge(9); + + sender.sendPushMessage(iosVariant(), tokens, unifiedPushMessage, "123", mockCallback()); + final ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(apnsService).push(eq(tokens), captor.capture(), any(Date.class)); + assertTrue("JSON Message should contain a badge property with value 9: ", captor.getValue().contains("\"badge\":9")); + } + + private iOSVariant iosVariant() throws Exception { + final iOSVariant iosVariant = mock(iOSVariant.class); + when(iosVariant.getVariantID()).thenReturn(UUID.randomUUID().toString()); + when(iosVariant.getCertificate()).thenReturn(readCertificate()); + when(iosVariant.getPassphrase()).thenReturn("123456"); + return iosVariant; + } + + private NotificationSenderCallback mockCallback() { + return mock(NotificationSenderCallback.class); + } + + private APNsPushNotificationSender mockSender(final ApnsService apnsService) { + final ApnsServiceCache serviceCache = mock(ApnsServiceCache.class); + when(serviceCache.dequeueOrCreateNewService(anyString(), + anyString(), + any(ServiceConstructor.class))).thenReturn(apnsService); + final APNsPushNotificationSender sender = new APNsPushNotificationSender(serviceCache); + return sender; + } /** * The store read by this method was copied from