public void CreateEntityShouldPostToCorrectEndPoint()
        {
            const string collectionName = "collection";
            var entityToPost = new Friend {Name = "name1", Age = 1};
            var restResponseContent = new UsergridGetResponse<Friend> {Entities = new List<Friend> {entityToPost}, Cursor = "cursor"};
            IRestResponse<UsergridGetResponse<Friend>> restResponse = Helpers.SetUpRestResponseWithContent<UsergridGetResponse<Friend>>(HttpStatusCode.OK, restResponseContent);
            _request
                .ExecuteJsonRequest(Arg.Any<string>(), Arg.Any<Method>(), Arg.Any<object>())
                .Returns(restResponse);

            Friend returnedEntity = _entityManager.CreateEntity(collectionName, entityToPost);

            _request.Received(1).ExecuteJsonRequest(
                Arg.Is(string.Format("/{0}", collectionName)),
                Arg.Is(Method.POST),
                Arg.Is(entityToPost));

            Assert.AreEqual(entityToPost.Age, returnedEntity.Age);
            Assert.AreEqual(entityToPost.Name, returnedEntity.Name);
        }
        public void ShouldReturnEntityCorrectly()
        {
            var friend = new Friend { Name = "name", Age = 1 };

            var restResponseContent = new UsergridGetResponse<Friend> { Entities = new List<Friend> { friend }, Cursor = "cursor" };
            IRestResponse<UsergridGetResponse<Friend>> restResponse = Helpers.SetUpRestResponseWithContent<UsergridGetResponse<Friend>>(HttpStatusCode.OK, restResponseContent);

            var request = Substitute.For<IUsergridRequest>();
            request
                .Execute(Arg.Any<string>(), Arg.Any<Method>(), Arg.Any<object>(), Arg.Any<string>())
                .Returns(restResponse);

            const string collectionName = "collection";
            const string entityName = "entity";

            var client = new Client(null, null, request: request);
            var returnedFriend = client.GetEntity<Friend>(collectionName, entityName);

            Assert.IsNotNull(returnedFriend);
            Assert.AreEqual(friend.Name, returnedFriend.Name);
            Assert.AreEqual(friend.Age, returnedFriend.Age);
        }
        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);
        }
        public void GetEntityShouldReturnFirstEntityInListCorrectly()
        {
            var friend1 = new Friend {Name = "name1", Age = 1};
            var friend2 = new Friend {Name = "name2", Age = 2};

            var entities = new List<Friend> {friend1, friend2};
            var restResponseContent = new UsergridGetResponse<Friend> {Entities = entities, Cursor = "cursor"};
            IRestResponse<UsergridGetResponse<Friend>> restResponse = Helpers.SetUpRestResponseWithContent<UsergridGetResponse<Friend>>(HttpStatusCode.OK, restResponseContent);

            _request
                .ExecuteJsonRequest(Arg.Any<string>(), Arg.Any<Method>(), Arg.Any<object>())
                .Returns(restResponse);

            const string collectionName = "collection";
            const string identifier = "identifier";

            Friend returnedFriend = _entityManager.GetEntity<Friend>(collectionName, identifier);

            Assert.IsNotNull(returnedFriend);
            Assert.AreEqual(friend1.Name, returnedFriend.Name);
            Assert.AreEqual(friend1.Age, returnedFriend.Age);
        }
        public void GetEntityShouldReturnEntityCorrectly()
        {
            var friend = new Friend {Name = "name", Age = 1};

            const string restResponseContent = "{\"entities\": [" +
                                               "{" +
                                               "\"uuid\": \"bcb343ba-d6d1-11e2-a295-7b4b45081d3b\"," +
                                               "\"type\": \"friend\"," +
                                               "\"name\": \"name\"," +
                                               "\"age\": 1," +
                                               "\"created\": 1371420707691," +
                                               "\"modified\": 1371420707691," +
                                               "\"metadata\": {" +
                                               "  \"path\": \"/friends/bcb343ba-d6d1-11e2-a295-7b4b45081d3b\"" +
                                               "}," +
                                               "}" +
                                               "]}";
            IRestResponse<UsergridGetResponse<Friend>> restResponse = Helpers.SetUpRestResponseWithContent<UsergridGetResponse<Friend>>(HttpStatusCode.OK, restResponseContent);

            _request
                .ExecuteJsonRequest(Arg.Any<string>(), Arg.Any<Method>(), Arg.Any<object>())
                .Returns(restResponse);

            const string collectionName = "collection";
            const string identifier = "identifier";

            Friend returnedFriend = _entityManager.GetEntity<Friend>(collectionName, identifier);

            Assert.IsNotNull(returnedFriend);
            Assert.AreEqual(friend.Name, returnedFriend.Name);
            Assert.AreEqual(friend.Age, returnedFriend.Age);
        }
        public void GetEntitiesShouldReturnListCorrectly()
        {
            var friend1 = new Friend { Name = "name1", Age = 1 };
            var friend2 = new Friend { Name = "name2", Age = 2 };
            var entities = new List<Friend> { friend1, friend2 };
            var restResponseContent = new UsergridGetResponse<Friend> { Entities = entities, Cursor = "cursor" };
            var restResponse = Helpers.SetUpRestResponseWithContent<UsergridGetResponse<Friend>>(HttpStatusCode.OK, restResponseContent);

            _request
                .ExecuteJsonRequest(Arg.Any<string>(), Arg.Any<Method>(), Arg.Any<object>())
                .Returns(restResponse);

            var friends = _entityManager.GetEntities<Friend>("collection");

            Assert.IsNotNull(friends);
            Assert.AreEqual(entities.Count, friends.Count);
            Assert.AreEqual(friend1.Name, friends[0].Name);
            Assert.AreEqual(friend1.Age, friends[0].Age);
            Assert.AreEqual(friend2.Name, friends[1].Name);
            Assert.AreEqual(friend2.Age, friends[1].Age);
        }