Ejemplo n.º 1
0
        public void Convert_DictionaryWithDifferentObjectTypes_ReturnsCorrectValue()
        {
            var converter = new XmlRpcValueToDataConverter();

            var xmlRpcValue = new StructValue(new Dictionary <string, XmlRpcValue>
            {
                { "TestProp1", new StringValue("Hallo") },
                { "TestProp2", new StringValue("abcd") },
                { "TestProp3", new IntegerValue(5678) },
                { "TestProp4", new BooleanValue(true) }
            });

            var value = converter.Convert(xmlRpcValue, typeof(Dictionary <string, object>)) as IDictionary <string, object>;

            Assert.NotNull(value);

            Assert.Equal(4, value.Keys.Count);

            var firstStringValue  = value["TestProp1"];
            var secondStringValue = value["TestProp2"];
            var integerValue      = value["TestProp3"];
            var boolValue         = value["TestProp4"];

            Assert.Equal("Hallo", firstStringValue);
            Assert.Equal("abcd", secondStringValue);
            Assert.Equal(5678, integerValue);
            Assert.Equal(true, boolValue);
        }
Ejemplo n.º 2
0
        public void Convert_DictionaryWithXmlRpcValue_ReturnsCorrectValue()
        {
            var converter = new XmlRpcValueToDataConverter();

            var xmlRpcValue = new StructValue(new Dictionary <string, XmlRpcValue>
            {
                { "TestProp1", new StringValue("1234") },
                { "TestProp2", new StringValue("abcd") }
            });

            var value = converter.Convert(xmlRpcValue, typeof(Dictionary <string, XmlRpcValue>)) as IDictionary <string, XmlRpcValue>;

            Assert.NotNull(value);

            Assert.Equal(2, value.Keys.Count);

            var firstValue  = value["TestProp1"] as StringValue;
            var secondValue = value["TestProp2"] as StringValue;

            Assert.NotNull(firstValue);
            Assert.NotNull(secondValue);

            Assert.Equal("1234", firstValue.Value);
            Assert.Equal("abcd", secondValue.Value);
        }
        public void Convert_XmlRpcValueToObjectAndBack_OutputIsEqualToInput()
        {
            var xmlRpcValue = new StructValue();

            xmlRpcValue.Value["IntTest"]    = new IntegerValue(987);
            xmlRpcValue.Value["StringTest"] = new StringValue("1234Test");
            xmlRpcValue.Value["SubData"]    = new StructValue(new Dictionary <string, XmlRpcValue>
            {
                { "Id", new IntegerValue(4567) }
            });
            xmlRpcValue.Value["SubItems"] = new ArrayValue(new []
            {
                new StructValue(new Dictionary <string, XmlRpcValue>
                {
                    { "Id", new IntegerValue(13579) },
                    { "Name", new StringValue("SubItem0") }
                }),
                new StructValue(new Dictionary <string, XmlRpcValue>
                {
                    { "Id", new IntegerValue(24680) },
                    { "Name", new StringValue("SubItem1") }
                })
            });

            var objectToXmlRpcConverter = new DataToXmlRpcValueConverter();

            var xmlRpcToObjectConverter = new XmlRpcValueToDataConverter();

            var obj = xmlRpcToObjectConverter.Convert <StructTestDataWithArray>(xmlRpcValue);

            var convertedXmlRpcValue = objectToXmlRpcConverter.Convert(obj);

            Assert.NotNull(convertedXmlRpcValue);
        }
Ejemplo n.º 4
0
        public void Convert_SimpleDataType_ReturnsCorrectValue()
        {
            var converter = new XmlRpcValueToDataConverter();

            var xmlRpcValue = new StringValue("qwertz");

            var value = converter.Convert(xmlRpcValue, typeof(string)) as string;

            Assert.NotNull(value);
            Assert.Equal("qwertz", value);
        }
        // ReSharper disable once MemberCanBeMadeStatic.Local
        private T GetItem <T>(XmlRpcValue xmlRpcValue)
        {
            var xmlRpcValueToDataConverter = new XmlRpcValueToDataConverter();

            if (typeof(T) == typeof(XmlRpcValue))
            {
                return((T)(object)xmlRpcValue);
            }

            return(xmlRpcValueToDataConverter.Convert <T>(xmlRpcValue));
        }
        // ReSharper disable once MemberCanBeMadeStatic.Local
        private async Task <T> GetObjectValueInternalAsync <T>(RequestData requestData)
        {
            var xmlRpcResponse = await requestData.Client.InvokeExAsync(requestData.MethodName, requestData.Arguments).ConfigureAwait(false);

            var xmlRpcValue = xmlRpcResponse.Results.First().Values.First();

            var xmlRpcValueToDataConverter = new XmlRpcValueToDataConverter();

            var value = xmlRpcValueToDataConverter.Convert <T>(xmlRpcValue);

            return(value);
        }
Ejemplo n.º 7
0
        public void Convert_ArrayDataTypeAsArray_ReturnsCorrectValue()
        {
            var converter = new XmlRpcValueToDataConverter();

            var xmlRpcValue = new ArrayValue(new[] { new StringValue("1234"), new StringValue("abcd") });

            var value = converter.Convert(xmlRpcValue, typeof(string[])) as string[];

            Assert.NotNull(value);
            Assert.Equal(2, value.Length);
            Assert.Equal("1234", value[0]);
            Assert.Equal("abcd", value[1]);
        }
Ejemplo n.º 8
0
        public void Convert_ArrayDataTypeAsEnumerable_ReturnsCorrectValue()
        {
            var converter = new XmlRpcValueToDataConverter();

            var xmlRpcValue = new ArrayValue(new [] { new StringValue("1234"), new StringValue("abcd") });

            var value = converter.Convert(xmlRpcValue, typeof(IEnumerable <string>)) as IEnumerable <string>;

            Assert.NotNull(value);
            Assert.Equal(2, value.Count());
            Assert.Equal("1234", value.First());
            Assert.Equal("abcd", value.Last());
        }
Ejemplo n.º 9
0
        public void Convert_StringToObject_ObjectIsString()
        {
            var converter = new XmlRpcValueToDataConverter();

            var xmlRpcValue = new StructValue(new Dictionary <string, XmlRpcValue>
            {
                { "Value", new StringValue("Test") }
            });

            var dataObject =
                converter.Convert(xmlRpcValue, typeof(StructTestDataWithObjectValue)) as StructTestDataWithObjectValue;

            Assert.Equal("Test", dataObject?.Value);
        }
        // ReSharper disable once MemberCanBeMadeStatic.Local
        private async Task <IEnumerable <T> > GetEnumerableInternalAsync <T>(RequestData requestData)
        {
            var xmlRpcResponse = await requestData.Client.InvokeExAsync(requestData.MethodName, requestData.Arguments).ConfigureAwait(false);

            if (xmlRpcResponse.Results.First().Values.First() is not ArrayValue xmlRpcValue)
            {
                throw new InvalidOperationException("Xml rpc result must be an array");
            }

            var xmlRpcValueToDataConverter = new XmlRpcValueToDataConverter();

            var value = xmlRpcValue.Value.Select(x => xmlRpcValueToDataConverter.Convert <T>(x));

            return(value);
        }
        public void Convert_TestDataWithEnumAndBack_IsMappedCorrect(TestEnum testEnum)
        {
            var testData = new StructTestDataWithConverter
            {
                TestValue = testEnum
            };

            var objectToXmlRpcConverter = new DataToXmlRpcValueConverter();

            var xmlRpcValueToObjectConverter = new XmlRpcValueToDataConverter();

            var obj = objectToXmlRpcConverter.Convert(testData);

            var convertedValue = xmlRpcValueToObjectConverter.Convert <StructTestDataWithConverter>(obj);

            Assert.Equal(testEnum, convertedValue.TestValue);
        }
Ejemplo n.º 12
0
        private static object[] GetParameters(IReadOnlyList <ParameterInfo> parameterInfos, IReadOnlyCollection <XmlRpcValue> parameters)
        {
            if (!parameterInfos.Any())
            {
                return(Array.Empty <object>());
            }

            if (parameters.Count != parameterInfos.Count)
            {
                throw new TargetParameterCountException(
                          $"Xml Rpc method parameter count mismatch. Method parameter count ({parameterInfos.Count}) != calling parameter count ({parameters.Count})");
            }

            var converter = new XmlRpcValueToDataConverter();

            return(parameters
                   .Select((x, index) => converter.Convert(x, parameterInfos[index].ParameterType))
                   .ToArray());
        }
        public void Convert_TestDataToXmlRpcAndBack_IsMappedCorrectToObject()
        {
            const int    intData    = 1234;
            const string stringData = "TestData";

            var testData = new StructTestDataWithArray
            {
                IntValue    = intData,
                StringValue = stringData,
                SubItems    = new[]
                {
                    new SubTestData
                    {
                        Id   = 2345,
                        Name = "Item1"
                    },
                    new SubTestData
                    {
                        Id   = 3456,
                        Name = "Item2"
                    }
                }
            };

            var objectToXmlRpcConverter = new DataToXmlRpcValueConverter();

            var xmlRpcToObjectConverter = new XmlRpcValueToDataConverter();

            var xmlRpcValue = objectToXmlRpcConverter.Convert(testData);

            var convertedTestData = xmlRpcToObjectConverter.Convert <StructTestDataWithArray>(xmlRpcValue);

            Assert.Equal(testData.IntValue, convertedTestData.IntValue);
            Assert.Equal(testData.StringValue, convertedTestData.StringValue);
            Assert.Null(convertedTestData.SubData);
            Assert.Equal(testData.SubItems.Length, convertedTestData.SubItems.Length);
            for (var i = 0; i < testData.SubItems.Length; i++)
            {
                Assert.Equal(testData.SubItems[i].Id, convertedTestData.SubItems[i].Id);
                Assert.Equal(testData.SubItems[i].Name, convertedTestData.SubItems[i].Name);
            }
        }
Ejemplo n.º 14
0
        public void Convert_ArrayDataTypeAsArrayXmlRpcValue_ReturnsCorrectValue()
        {
            var converter = new XmlRpcValueToDataConverter();

            var xmlRpcValue = new ArrayValue(new[] { new StringValue("1234"), new StringValue("abcd") });

            var value = converter.Convert(xmlRpcValue, typeof(XmlRpcValue[])) as XmlRpcValue[];

            Assert.NotNull(value);
            Assert.Equal(2, value.Length);

            var firstValue  = value[0] as StringValue;
            var secondValue = value[1] as StringValue;

            Assert.NotNull(firstValue);
            Assert.NotNull(secondValue);

            Assert.Equal("1234", firstValue.Value);
            Assert.Equal("abcd", secondValue.Value);
        }