protected override async Task <Entity> UpdateAsync(IDictionary <string, object> propertyUpdates, IDictionary <string, string> attributeUpdates, IEnumerable <string> addedTags, IEnumerable <string> removedTags, int specificRevision, ApiOptions options, bool forceUpdate)
        {
            var request = new UpdateObjectRequest {
                Id = this.Id, Type = this.Type
            };

            ApiOptions.Apply(request, options);
            request.Revision = specificRevision;
            if (propertyUpdates != null && propertyUpdates.Count > 0)
            {
                propertyUpdates.For(x => request.PropertyUpdates[x.Key] = x.Value);
            }
            if (attributeUpdates != null && attributeUpdates.Count > 0)
            {
                attributeUpdates.For(x => request.AttributeUpdates[x.Key] = x.Value);
            }
            if (addedTags != null)
            {
                request.AddedTags.AddRange(addedTags);
            }
            if (removedTags != null)
            {
                request.RemovedTags.AddRange(removedTags);
            }

            // Check if acls are to be added
            request.AllowClaims.AddRange(this.Acl.Allowed);
            request.DenyClaims.AddRange(this.Acl.Denied);
            request.ResetClaims.AddRange(this.Acl.Reset);

            // Check if an update is needed.
            if (request.PropertyUpdates.Count == 0 &&
                request.AttributeUpdates.Count == 0 &&
                request.AddedTags.Count == 0 &&
                request.RemovedTags.Count == 0 &&
                request.AllowClaims.Count == 0 &&
                request.DenyClaims.Count == 0 &&
                request.ResetClaims.Count == 0 &&
                forceUpdate == false)
            {
                return(null);
            }

            var response = await request.ExecuteAsync();

            if (response.Status.IsSuccessful == false)
            {
                throw response.Status.ToFault();
            }

            // 3. Update the last known state based on the differences
            Debug.Assert(response.Object != null, "If status is successful, then updated object should not be null.");
            return(response.Object);
        }
Beispiel #2
0
        public async Task UpdateObjectAsyncTest()
        {
            // Create an apObject
            var     now = DateTime.Now;
            dynamic obj = new APObject("object");

            obj.intfield     = 1;
            obj.decimalfield = 10.0m;
            obj.datefield    = "2012-12-20";
            obj.stringfield  = "initial";
            obj.Tags.Add("initial");

            var createdResponse = await(new CreateObjectRequest()
            {
                Object = obj
            }).ExecuteAsync();

            Assert.IsNotNull(createdResponse, "Object creation failed.");
            Assert.IsNotNull(createdResponse.Status, "Status is null.");
            Assert.IsTrue(createdResponse.Status.IsSuccessful,
                          createdResponse.Status.Message ?? "Create apObject failed.");
            var created = createdResponse.Object;

            // Update the apObject
            var updateRequest = new UpdateObjectRequest()
            {
                Id   = created.Id,
                Type = created.Type
            };

            updateRequest.PropertyUpdates["intfield"]     = "2";
            updateRequest.PropertyUpdates["decimalfield"] = 20.0m.ToString();
            updateRequest.PropertyUpdates["stringfield"]  = null;
            updateRequest.PropertyUpdates["datefield"]    = "2013-11-20";
            updateRequest.AddedTags.AddRange(new[] { "tag1", "tag2" });
            updateRequest.RemovedTags.AddRange(new[] { "initial" });
            var updatedResponse = await updateRequest.ExecuteAsync();


            Assert.IsNotNull(updatedResponse, "Update apObject response is null.");
            Assert.IsNotNull(updatedResponse.Status, "Update apObject response status is null.");
            Assert.IsNotNull(updatedResponse.Object, "Updated apObject is null.");
            var updated = updatedResponse.Object;

            Assert.IsTrue(updated.Get <string>("intfield") == "2");
            Assert.IsTrue(updated.Get <decimal>("decimalfield") == 20.0m);
            Assert.IsTrue(updated.Get <string>("stringfield") == null);
            Assert.IsTrue(updated.Get <string>("datefield") == "2013-11-20");
            Assert.IsTrue(updated.Tags.Count() == 2);
            Assert.IsTrue(updated.Tags.Intersect(new[] { "tag1", "tag2" }).Count() == 2);
        }
Beispiel #3
0
        public async Task BugId14Test()
        {
            // Ref: https://github.com/appacitive/Gossamer/issues/14
            // Updating a null property with a null value fails
            // Create an apObject
            var     now = DateTime.Now;
            dynamic obj = new APObject("object");

            obj.intfield     = 1;
            obj.decimalfield = 10.0m;
            obj.stringfield  = null;

            var createdResponse = await(new CreateObjectRequest()
            {
                Object = obj
            }).ExecuteAsync();

            Assert.IsNotNull(createdResponse, "Object creation failed.");
            Assert.IsNotNull(createdResponse.Status, "Status is null.");
            Assert.IsTrue(createdResponse.Status.IsSuccessful, createdResponse.Status.Message ?? "Create apObject failed.");
            var created = createdResponse.Object;

            // Update the apObject twice
            for (int i = 0; i < 2; i++)
            {
                var updateRequest = new UpdateObjectRequest()
                {
                    Id   = created.Id,
                    Type = created.Type
                };
                updateRequest.PropertyUpdates["stringfield"] = null;
                var updatedResponse = await updateRequest.ExecuteAsync();

                Assert.IsNotNull(updatedResponse, "Update apObject response is null.");
                Assert.IsNotNull(updatedResponse.Status, "Update apObject response status is null.");
                Assert.IsTrue(updatedResponse.Status.IsSuccessful, updatedResponse.Status.Message ?? "NULL");
                Assert.IsNotNull(updatedResponse.Object, "Updated apObject is null.");
                var updated = updatedResponse.Object;
                Assert.IsTrue(updated["stringfield"] is NullValue);
            }
        }
        protected override async Task<Entity> UpdateAsync(IDictionary<string, object> propertyUpdates, IDictionary<string, string> attributeUpdates, IEnumerable<string> addedTags, IEnumerable<string> removedTags, int specificRevision, ApiOptions options, bool forceUpdate)
        {
            var request = new UpdateObjectRequest {Id = this.Id, Type = this.Type};
            ApiOptions.Apply(request, options);
            request.Revision = specificRevision;
            if (propertyUpdates != null && propertyUpdates.Count > 0)
                propertyUpdates.For(x => request.PropertyUpdates[x.Key] = x.Value);
            if (attributeUpdates != null && attributeUpdates.Count > 0)
                attributeUpdates.For(x => request.AttributeUpdates[x.Key] = x.Value);
            if (addedTags != null)
                request.AddedTags.AddRange(addedTags);
            if (removedTags != null)
                request.RemovedTags.AddRange(removedTags);

            // Check if acls are to be added
            request.AllowClaims.AddRange(this.Acl.Allowed);
            request.DenyClaims.AddRange(this.Acl.Denied);
            request.ResetClaims.AddRange(this.Acl.Reset);

            // Check if an update is needed.
            if (request.PropertyUpdates.Count == 0 &&
                request.AttributeUpdates.Count == 0 &&
                request.AddedTags.Count == 0 &&
                request.RemovedTags.Count == 0 && 
                request.AllowClaims.Count == 0 && 
                request.DenyClaims.Count == 0 && 
                request.ResetClaims.Count == 0  && 
                forceUpdate == false )
                return null;

            var response = await request.ExecuteAsync();
            if (response.Status.IsSuccessful == false)
                throw response.Status.ToFault();

            // 3. Update the last known state based on the differences
            Debug.Assert(response.Object != null, "If status is successful, then updated object should not be null.");
            return response.Object;
        }
        public async Task BugId14Test()
        {
            // Ref: https://github.com/appacitive/Gossamer/issues/14
            // Updating a null property with a null value fails 
            // Create an apObject
            var now = DateTime.Now;
            dynamic obj = new APObject("object");
            obj.intfield = 1;
            obj.decimalfield = 10.0m;
            obj.stringfield = null;

            var createdResponse = await (new CreateObjectRequest()
            {
                Object = obj
            }).ExecuteAsync();
            Assert.IsNotNull(createdResponse, "Object creation failed.");
            Assert.IsNotNull(createdResponse.Status, "Status is null.");
            Assert.IsTrue(createdResponse.Status.IsSuccessful, createdResponse.Status.Message ?? "Create apObject failed.");
            var created = createdResponse.Object;

            // Update the apObject twice
            for (int i = 0; i < 2; i++)
            {
                var updateRequest = new UpdateObjectRequest()
                {
                    Id = created.Id,
                    Type = created.Type
                };
                updateRequest.PropertyUpdates["stringfield"] = null;
                var updatedResponse = await updateRequest.ExecuteAsync();

                Assert.IsNotNull(updatedResponse, "Update apObject response is null.");
                Assert.IsNotNull(updatedResponse.Status, "Update apObject response status is null.");
                Assert.IsTrue(updatedResponse.Status.IsSuccessful, updatedResponse.Status.Message ?? "NULL");
                Assert.IsNotNull(updatedResponse.Object, "Updated apObject is null.");
                var updated = updatedResponse.Object;
                Assert.IsTrue(updated["stringfield"] is NullValue);   
            }
        }
        public async Task UpdateObjectAsyncTest()
        {
            // Create an apObject
            var now = DateTime.Now;
            dynamic obj = new APObject("object");
            obj.intfield = 1;
            obj.decimalfield = 10.0m;
            obj.datefield = "2012-12-20";
            obj.stringfield = "initial";
            obj.Tags.Add("initial");

            var createdResponse = await (new CreateObjectRequest()
                {
                    Object = obj
                }).ExecuteAsync();
            Assert.IsNotNull(createdResponse, "Object creation failed.");
            Assert.IsNotNull(createdResponse.Status, "Status is null.");
            Assert.IsTrue(createdResponse.Status.IsSuccessful,
                          createdResponse.Status.Message ?? "Create apObject failed.");
            var created = createdResponse.Object;

            // Update the apObject
            var updateRequest = new UpdateObjectRequest()
                {
                    Id = created.Id,
                    Type = created.Type
                };
            updateRequest.PropertyUpdates["intfield"] = "2";
            updateRequest.PropertyUpdates["decimalfield"] = 20.0m.ToString();
            updateRequest.PropertyUpdates["stringfield"] = null;
            updateRequest.PropertyUpdates["datefield"] = "2013-11-20";
            updateRequest.AddedTags.AddRange(new[] {"tag1", "tag2"});
            updateRequest.RemovedTags.AddRange(new[] {"initial"});
            var updatedResponse = await updateRequest.ExecuteAsync();


            Assert.IsNotNull(updatedResponse, "Update apObject response is null.");
            Assert.IsNotNull(updatedResponse.Status, "Update apObject response status is null.");
            Assert.IsNotNull(updatedResponse.Object, "Updated apObject is null.");
            var updated = updatedResponse.Object;
            Assert.IsTrue(updated.Get<string>("intfield") == "2");
            Assert.IsTrue(updated.Get<decimal>("decimalfield") == 20.0m);
            Assert.IsTrue(updated.Get<string>("stringfield") == null);
            Assert.IsTrue(updated.Get<string>("datefield") == "2013-11-20");
            Assert.IsTrue(updated.Tags.Count() == 2);
            Assert.IsTrue(updated.Tags.Intersect(new[] {"tag1", "tag2"}).Count() == 2);

        }