Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from danbev/matzew-BadgeOverride_test
Browse files Browse the repository at this point in the history
Adding unit test to verify that badges are properly handled.
  • Loading branch information
matzew committed Aug 12, 2015
2 parents 12e4eac + ef3daff commit 8a232eb
Showing 1 changed file with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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<String> tokens = Collections.singletonList("token");

sender.sendPushMessage(iosVariant(), tokens, new UnifiedPushMessage(), "123", mockCallback());

final ArgumentCaptor<String> 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<String> tokens = Collections.singletonList("token");
final UnifiedPushMessage unifiedPushMessage = new UnifiedPushMessage();
unifiedPushMessage.getMessage().setBadge(0);

sender.sendPushMessage(iosVariant(), tokens, unifiedPushMessage, "123", mockCallback());

final ArgumentCaptor<String> 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<String> tokens = Collections.singletonList("token");
final UnifiedPushMessage unifiedPushMessage = new UnifiedPushMessage();
unifiedPushMessage.getMessage().setBadge(9);

sender.sendPushMessage(iosVariant(), tokens, unifiedPushMessage, "123", mockCallback());

final ArgumentCaptor<String> 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
Expand Down

0 comments on commit 8a232eb

Please sign in to comment.