Example #1
0
        public void AndroidNotificationShouldReturnCorrectPayload()
        {
            var notification = new AndroidNotification("notifierName", "notification message");
            var payload = notification.GetPayload();

            Assert.AreEqual("notification message", payload.GetReflectedProperty("data"));
        }
        public void PublishNotificationPostsByBuildingQueryAndPayload()
        {
            IRestResponse restResponse = Helpers.SetUpRestResponse(HttpStatusCode.OK);
            _request.ExecuteJsonRequest(Arg.Any<string>(), Arg.Any<Method>(), Arg.Any<object>())
                .Returns(restResponse);

            var recipients = Substitute.For<INotificationRecipients>();
            recipients.BuildQuery().Returns("query");

            var appleNotification = new AppleNotification("appleNotifierName", "appleTestMessge", "chime");
            var googleNotification = new AndroidNotification("googleNotifierName", "androidTestMessage");

            var deliverAt = DateTime.Now.AddDays(1);
            var expireAt = DateTime.Now.AddDays(2);

            var schedulerSettings = new NotificationSchedulerSettings {DeliverAt = deliverAt, ExpireAt = expireAt};

            //call PublishNotification
            _notificationsManager.PublishNotification(new Notification[] {appleNotification, googleNotification}, recipients, schedulerSettings);

            //assert
            recipients.Received(1).BuildQuery();

            Func<NotificationPayload, bool> validatePayload = p =>
                                                                  {
                                                                      bool isValid = true;
                                                                      isValid &= p.DeliverAt == deliverAt.ToUnixTime();
                                                                      isValid &= p.ExpireAt == expireAt.ToUnixTime();

                                                                      var applePayload = p.Payloads["appleNotifierName"].GetReflectedProperty("aps");
                                                                      isValid &= (string) applePayload.GetReflectedProperty("alert") == "appleTestMessge";
                                                                      isValid &= (string) applePayload.GetReflectedProperty("sound") == "chime";

                                                                      var googlePayload = p.Payloads["googleNotifierName"].GetReflectedProperty("data");
                                                                      isValid &= (string) googlePayload == "androidTestMessage";

                                                                      return isValid;
                                                                  };
            _request.Received(1).ExecuteJsonRequest("query", Method.POST,
                                                    Arg.Is<NotificationPayload>(
                                                        p => validatePayload(p)));
        }
Example #3
0
        public void ShouldPublishNotifications()
        {
            //Set up
            const string appleNotifierName = "apple_notifier";
            const string googleNotifierName = "google_notifier";
            const string username = "******";
            const string appleTestMessge = "test message for Apple";
            const string androidTestMessage = "test message for Android";

            var client = InitializeClientAndLogin(AuthType.Organization);
            CreateAppleNotifier(client,appleNotifierName);            
            CreateAndroidNotifier(client,googleNotifierName);            
            CreateUser(username, client);

            //Setup Notifications
            var appleNotification = new AppleNotification(appleNotifierName, appleTestMessge, "chime");
            var googleNotification = new AndroidNotification(googleNotifierName, androidTestMessage);
            //Setup recipients and scheduling
            INotificationRecipients recipients = new NotificationRecipients().AddUserWithName(username);
            var schedulerSettings = new NotificationSchedulerSettings {DeliverAt = DateTime.Now.AddDays(1)};

            client.PublishNotification(new Notification[] {appleNotification, googleNotification}, recipients, schedulerSettings);

            //Assert
            UsergridCollection<dynamic> entities = client.GetEntities<dynamic>("notifications", query: "order by created desc");
            dynamic notification = entities.FirstOrDefault();

            Assert.IsNotNull(notification);
            Assert.IsNotNull(notification.uuid);
            Assert.AreEqual(appleTestMessge, notification.payloads.apple_notifier.aps.alert.Value);
            Assert.AreEqual("chime", notification.payloads.apple_notifier.aps.sound.Value);
            Assert.AreEqual(androidTestMessage, notification.payloads.google_notifier.data.Value);

            //Cancel notification and assert it is canceled
            client.CancelNotification(notification.uuid.Value);
            dynamic entity = client.GetEntity<dynamic>("notifications", notification.uuid.Value);
            Assert.AreEqual(entity.state.Value, "CANCELED");
        }