Beispiel #1
0
        public void ReducePatch__Given_ArrayPropertyNotEqual__Then_PatchEntireArray(TestJsonObject obj)
        {
            // Assemble
            var original = JObject.FromObject(obj);

            obj.OtherChildren.First().String = "Updated Value";
            var update  = JObject.FromObject(obj);
            var subject = new JsonDiff();

            // Act
            var result = (JObject)subject.ReducePatch(original, update);

            // Assert
            Assert.NotNull(result);

            // All values should be included in array patch
            Assert.Equal(obj.OtherChildren.Count, result[nameof(obj.OtherChildren)].Count());
            var otherChildObj  = obj.OtherChildren.First();
            var otherChildJson = result[nameof(obj.OtherChildren)][0];

            Assert.Equal(otherChildObj.String, otherChildJson["String"].Value <string>());
            Assert.Equal(otherChildObj.Guid, otherChildJson["Guid"].Value <Guid>());
            Assert.Equal(otherChildObj.Int, otherChildJson["Int"].Value <int>());
            Assert.Equal(otherChildObj.Decimal, otherChildJson["Decimal"].Value <decimal>());
            Assert.Equal(otherChildObj.Double, otherChildJson["Double"].Value <double>());
            Assert.Equal(otherChildObj.DateTime, otherChildJson["DateTime"].Value <DateTime>());
            // Other properties should not need patching
            Assert.Single(result);
        }
Beispiel #2
0
        public void ReducePatch__Given_AllEqual__Then_Null(TestJsonObject obj)
        {
            // Assemble
            var original = JObject.FromObject(obj);
            var update   = JObject.FromObject(obj);
            var subject  = new JsonDiff();

            // Act
            var result = subject.ReducePatch(original, update);

            // Assert
            Assert.Null(result);
        }
Beispiel #3
0
        public void ReducePatch__Given_SinglePropertyNotEqual__Then_PatchProperty(TestJsonObject obj)
        {
            // Assemble
            var original = JObject.FromObject(obj);

            obj.String = "Updated Value";
            var update  = JObject.FromObject(obj);
            var subject = new JsonDiff();

            // Act
            var result = (JObject)subject.ReducePatch(original, update);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(obj.String, result["String"].Value <string>());
            // Other properties should not need patching
            Assert.Single(result);
        }
Beispiel #4
0
        private Resource BuildPatch(object model)
        {
            var originalResource = ModelRegistry.GetResource(model);

            var modelRtlns    = ModelRegistry.GetRelationshipValues(model) ?? new Dictionary <string, Relationship>();
            var originalRtlns = originalResource.Relationships ?? new Dictionary <string, Relationship>();
            var patchRtlns    = modelRtlns.Where(modelRltn =>
            {
                originalRtlns.TryGetValue(modelRltn.Key, out var ogRtln);
                // JToken.DeepEquals does not work here
                return(!JsonComparer.Equals(ogRtln?.Data, modelRltn.Value?.Data));
            }).ToDictionary(x => x.Key, x => x.Value);

            var modelAttrs    = ModelRegistry.GetAttributeValues(model) ?? new JObject();
            var originalAttrs = originalResource.Attributes ?? new JObject();

            var modelMetas    = ModelRegistry.GetMetaValues(model) ?? new JObject();
            var originalMetas = originalResource.Meta ?? new JObject();
            // Links are not patched

            var patchAttrs = (JObject)JsonDiff.ReducePatch(originalAttrs, modelAttrs);
            var patchMetas = (JObject)JsonDiff.ReducePatch(originalMetas, modelMetas);

            if (patchAttrs == null &&
                !patchRtlns.Any() &&
                patchMetas == null)
            {
                // Nothing to update
                return(null);
            }
            return(new Resource
            {
                Id = originalResource.Id,
                Type = originalResource.Type,
                Attributes = patchAttrs,
                Relationships = patchRtlns.Any() ? patchRtlns : null,
                Meta = patchMetas
            });
        }