public UsergridCollection <T> GetEntities <T>(string collectionName, int limit = 10, string query = null) { _pageSizes.Remove(typeof(T)); _pageSizes.Add(typeof(T), limit); string url = string.Format("/{0}?limit={1}", collectionName, limit); if (query != null) { url += "&query=" + query; } IRestResponse response = Request.ExecuteJsonRequest(url, Method.GET); if (response.StatusCode == HttpStatusCode.NotFound) { return(new UsergridCollection <T>()); } ValidateResponse(response); var getResponse = JsonConvert.DeserializeObject <UsergridGetResponse <T> >(response.Content); var collection = new UsergridCollection <T>(getResponse.Entities); _cursorStates.Remove(typeof(T)); if (getResponse.Cursor != null) { collection.HasNext = true; collection.HasPrevious = false; _cursorStates.Add(typeof(T), new Stack <string>(new[] { null, null, getResponse.Cursor })); } return(collection); }
public void ShouldCrudPocoEntity() { const string collectionName = "friends"; var friend = new Friend { Name = "EntityName", Age = 25 }; DeleteEntityIfExists <Friend>(_client, collectionName, friend.Name); // Create a new entity _client.CreateEntity(collectionName, friend); // Get it back var friendFromUsergrid = _client.GetEntity <Friend>(collectionName, friend.Name); // Assert that the entity returned is correct Assert.IsNotNull(friendFromUsergrid); Assert.AreEqual(friend.Name, friendFromUsergrid.Name); Assert.AreEqual(friend.Age, friendFromUsergrid.Age); // Get it back with query string query = "select * where name = '" + friend.Name + "'"; UsergridCollection <Friend> friends = _client.GetEntities <Friend>(collectionName, query: query); // Assert the collection is correct Assert.IsNotNull(friends); Assert.AreEqual(1, friends.Count); Assert.IsFalse(friends.HasNext); Assert.IsFalse(friends.HasPrevious); friendFromUsergrid = friends[0]; Assert.IsNotNull(friendFromUsergrid); Assert.AreEqual(friend.Name, friendFromUsergrid.Name); Assert.AreEqual(friend.Age, friendFromUsergrid.Age); // Update the entity Friend friendToUpdate = friendFromUsergrid; friendToUpdate.Age = 30; _client.UpdateEntity(collectionName, friendToUpdate.Name, friendToUpdate); // Get it back friendFromUsergrid = _client.GetEntity <Friend>(collectionName, friendToUpdate.Name); // Assert that entity is updated Assert.AreEqual(friendToUpdate.Age, friendFromUsergrid.Age); // Delete the entity _client.DeleteEntity(collectionName, friend.Name); // Get it back friendFromUsergrid = _client.GetEntity <Friend>(collectionName, friend.Name); // Assert that it doesn't exist Assert.IsNull(friendFromUsergrid); }
public void GetUserFeedShouldDelegateToEntityManagerWithCorrectEndpoint() { var usergridActivities = new UsergridCollection<UsergridActivity>(); _entityManager.GetEntities<UsergridActivity>("/users/userIdentifier/feed").Returns(usergridActivities); UsergridCollection<UsergridActivity> returnedActivities = _client.GetUserFeed<UsergridActivity>("userIdentifier"); _entityManager.Received(1).GetEntities<UsergridActivity>("/users/userIdentifier/feed"); Assert.AreEqual(usergridActivities, returnedActivities); }
public void GetUserFeedShouldDelegateToEntityManagerWithCorrectEndpoint() { var usergridActivities = new UsergridCollection <UsergridActivity>(); _entityManager.GetEntities <UsergridActivity>("/users/userIdentifier/feed").Returns(usergridActivities); UsergridCollection <UsergridActivity> returnedActivities = _client.GetUserFeed <UsergridActivity>("userIdentifier"); _entityManager.Received(1).GetEntities <UsergridActivity>("/users/userIdentifier/feed"); Assert.AreEqual(usergridActivities, returnedActivities); }
public UsergridCollection <T> GetPreviousEntities <T>(string collectionName, string query = null) { if (!_cursorStates.ContainsKey(typeof(T))) { var error = new UsergridError { Error = "cursor_not_initialized", Description = "Call GetEntities method to initialize the cursor" }; throw new UsergridException(error); } Stack <string> stack = _cursorStates[typeof(T)]; stack.Pop(); stack.Pop(); string cursor = stack.Peek(); int limit = _pageSizes[typeof(T)]; if (cursor == null) { return(GetEntities <T>(collectionName, limit)); } string url = string.Format("/{0}?cursor={1}&limit={2}", collectionName, cursor, limit); if (query != null) { url += "&query=" + query; } IRestResponse response = Request.ExecuteJsonRequest(url, Method.GET); if (response.StatusCode == HttpStatusCode.NotFound) { return(new UsergridCollection <T>()); } ValidateResponse(response); var getResponse = JsonConvert.DeserializeObject <UsergridGetResponse <T> >(response.Content); var collection = new UsergridCollection <T>(getResponse.Entities) { HasNext = true, HasPrevious = true }; stack.Push(getResponse.Cursor); return(collection); }
public UsergridCollection <T> GetNextEntities <T>(string collectionName, string query = null) { if (!_cursorStates.ContainsKey(typeof(T))) { return(new UsergridCollection <T>()); } Stack <string> stack = _cursorStates[typeof(T)]; string cursor = stack.Peek(); if (cursor == null) { return(new UsergridCollection <T> { HasNext = false, HasPrevious = true }); } int limit = _pageSizes[typeof(T)]; string url = string.Format("/{0}?cursor={1}&limit={2}", collectionName, cursor, limit); if (query != null) { url += "&query=" + query; } IRestResponse response = Request.ExecuteJsonRequest(url, Method.GET); if (response.StatusCode == HttpStatusCode.NotFound) { return(new UsergridCollection <T>()); } ValidateResponse(response); var getResponse = JsonConvert.DeserializeObject <UsergridGetResponse <T> >(response.Content); var collection = new UsergridCollection <T>(getResponse.Entities); if (getResponse.Cursor != null) { collection.HasNext = true; stack.Push(getResponse.Cursor); } else { stack.Push(null); } collection.HasPrevious = true; return(collection); }
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"); }
public void GetPreviousEntitiesShouldGetToCorrectEndPointWithCorrectCursorState() { var friend1 = new Friend { Name = "name1", Age = 1 }; var friend2 = new Friend { Name = "name2", Age = 2 }; var friend3 = new Friend { Name = "name3", Age = 3 }; var friend4 = new Friend { Name = "name4", Age = 4 }; var friend5 = new Friend { Name = "name5", Age = 5 }; var friend6 = new Friend { Name = "name6", Age = 6 }; var entities1 = new List <Friend> { friend1, friend2 }; var entities2 = new List <Friend> { friend3, friend4 }; var entities3 = new List <Friend> { friend5, friend6 }; var restResponseContent1 = new UsergridGetResponse <Friend> { Entities = entities1, Cursor = "cursor1" }; var restResponse1 = Helpers.SetUpRestResponseWithContent <UsergridGetResponse <Friend> >(HttpStatusCode.OK, restResponseContent1); var restResponseContent2 = new UsergridGetResponse <Friend> { Entities = entities2, Cursor = "cursor2" }; var restResponse2 = Helpers.SetUpRestResponseWithContent <UsergridGetResponse <Friend> >(HttpStatusCode.OK, restResponseContent2); var restResponseContent3 = new UsergridGetResponse <Friend> { Entities = entities3 }; var restResponse3 = Helpers.SetUpRestResponseWithContent <UsergridGetResponse <Friend> >(HttpStatusCode.OK, restResponseContent3); _request .ExecuteJsonRequest("/collection?limit=10", Method.GET, Arg.Is <object>(x => x == null)) .Returns(restResponse1); _request .ExecuteJsonRequest("/collection?cursor=cursor1&limit=10", Method.GET, Arg.Is <object>(x => x == null)) .Returns(restResponse2); _request .ExecuteJsonRequest("/collection?cursor=cursor2&limit=10", Method.GET, Arg.Is <object>(x => x == null)) .Returns(restResponse3); _request .ExecuteJsonRequest("/collection?cursor=cursor1&limit=10", Method.GET, Arg.Is <object>(x => x == null)) .Returns(restResponse2); _request .ExecuteJsonRequest("/collection?&limit=10", Method.GET, Arg.Is <object>(x => x == null)) .Returns(restResponse1); UsergridCollection <Friend> list1 = _entityManager.GetEntities <Friend>("collection"); UsergridCollection <Friend> list2 = _entityManager.GetNextEntities <Friend>("collection"); UsergridCollection <Friend> list3 = _entityManager.GetNextEntities <Friend>("collection"); UsergridCollection <Friend> list4 = _entityManager.GetPreviousEntities <Friend>("collection"); UsergridCollection <Friend> list5 = _entityManager.GetPreviousEntities <Friend>("collection"); Assert.AreEqual(entities1[0].Name, list1[0].Name); Assert.AreEqual(entities1[1].Age, list1[1].Age); Assert.IsTrue(list1.HasNext); Assert.IsFalse(list1.HasPrevious); Assert.AreEqual(entities2[0].Name, list2[0].Name); Assert.AreEqual(entities2[1].Age, list2[1].Age); Assert.IsTrue(list2.HasNext); Assert.IsTrue(list2.HasPrevious); Assert.AreEqual(entities3[0].Name, list3[0].Name); Assert.AreEqual(entities3[1].Age, list3[1].Age); Assert.IsFalse(list3.HasNext); Assert.IsTrue(list3.HasPrevious); Assert.AreEqual(entities2[0].Name, list4[0].Name); Assert.AreEqual(entities2[1].Age, list4[1].Age); Assert.IsTrue(list4.HasNext); Assert.IsTrue(list4.HasPrevious); Assert.AreEqual(entities1[0].Name, list5[0].Name); Assert.AreEqual(entities1[1].Age, list5[1].Age); Assert.IsTrue(list5.HasNext); Assert.IsFalse(list5.HasPrevious); }