public void SetProperty(IDeserializerNode targetNode, PropertySpec property, object propertyValue)
 {
     if (targetNode.Operation == DeserializerNodeOperation.Default)
     {
         throw new InvalidOperationException("Invalid deserializer node operation default");
     }
     if ((targetNode.Operation == DeserializerNodeOperation.Post &&
          property.AccessMode.HasFlag(HttpMethod.Post)) ||
         (targetNode.Operation == DeserializerNodeOperation.Patch &&
          property.AccessMode.HasFlag(HttpMethod.Put)))
     {
         property.SetValue(targetNode.Value, propertyValue, targetNode.Context);
     }
     else
     {
         var propPath = string.IsNullOrEmpty(targetNode.ExpandPath)
             ? property.Name
             : targetNode.ExpandPath + "." + property.Name;
         throw new ResourceValidationException(
                   $"Property {property.Name} of resource {targetNode.ValueType.Name} is not writable.",
                   propPath,
                   targetNode.ValueType.Name,
                   null);
     }
 }
        public void SetProperty(IDeserializerNode target, PropertySpec property, object propertyValue)
        {
            if (!property.IsWritable)
            {
                throw new InvalidOperationException("Unable to set property.");
            }

            if (typeof(IClientRepository).IsAssignableFrom(property.PropertyType))
            {
                var repoImplementationType = this.clientRepositoryImplementationMap.GetOrAdd(property.PropertyType,
                                                                                             t =>
                                                                                             t.Assembly.GetTypes()
                                                                                             .First(
                                                                                                 x =>
                                                                                                 !x.IsInterface &&
                                                                                                 x.IsClass &&
                                                                                                 t
                                                                                                 .IsAssignableFrom
                                                                                                     (x)));

                var    listProxyValue = propertyValue as LazyCollectionProxy;
                object repo;
                if (listProxyValue != null)
                {
                    repo = Activator.CreateInstance(repoImplementationType,
                                                    this.client,
                                                    listProxyValue.Uri,
                                                    null,
                                                    target.Value);
                }
                else
                {
                    repo = Activator.CreateInstance(repoImplementationType,
                                                    this.client,
                                                    target.Uri + "/" + NameUtils.ConvertCamelCaseToUri(property.Name),
                                                    propertyValue,
                                                    target.Value);
                }
                property.SetValue(target.Value, repo);
                return;
            }

            property.SetValue(target.Value, propertyValue);
        }
示例#3
0
        public void SetValue_InstanceIsNull_ThrowTargetException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty"));

            // Call
            TestDelegate call = () => propertySpec.SetValue(null, 2);

            // Assert
            Assert.Throws <TargetException>(call);
        }
示例#4
0
        public void SetValue_ProperInstanceTypeAndValueType_PropertyIsUpdated()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty"));

            // Call
            propertySpec.SetValue(target, 2);

            // Assert
            Assert.AreEqual(2, target.IntegerProperty);
        }
示例#5
0
        public void SetValue_SettingValueOfIncorrectType_ThrowArgumentException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty"));

            // Call
            TestDelegate call = () => propertySpec.SetValue(target, new object());

            // Assert
            var exception = Assert.Throws <ArgumentException>(call);

            Assert.IsNull(exception.InnerException);
        }
示例#6
0
        public void SetValue_PropertyWithoutPublicSet_ThrowInvalidOperationException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("DoublePropertyWithOnlyGetter"));

            // Call
            TestDelegate call = () => propertySpec.SetValue(target, 2);

            // Assert
            var exception = Assert.Throws <InvalidOperationException>(call);

            Assert.AreEqual("Property lacks public setter!", exception.Message);
        }
示例#7
0
        public void SetValue_SettingValueResultsInException_ThrowTargetInvocationException()
        {
            // Setup
            var target = new ClassWithProperties();

            var propertySpec = new PropertySpec(target.GetType().GetProperty("ThrowsException"));

            // Call
            TestDelegate call = () => propertySpec.SetValue(target, "");

            // Assert
            Exception innerException = Assert.Throws <TargetInvocationException>(call).InnerException;

            Assert.IsInstanceOf <ArgumentException>(innerException);
        }