public void HttpParameterDescription_Default_Ctor()
 {
     HttpParameterDescription hpd = new HttpParameterDescription();
     Assert.IsNull(hpd.ParameterType, "ProcessorType should have been null");
     Assert.IsNull(hpd.Name, "Name should have been null");
     Assert.IsNull(hpd.Namespace, "Namespace should have been null");
     Assert.AreEqual(0, hpd.Index, "Index should have been zero");
     Assert.IsNull(hpd.MessagePartDescription, "Internal messagePartDescription should be null");
 }
        public void HttpParameterDescription_Construct_From_MessagePartDescription()
        {
            OperationDescription od = GetOperationDescription(typeof(MockService3), "SampleMethod");
            MessagePartDescription mpd = od.Messages[1].Body.ReturnValue;
            HttpParameterDescription hpd = new HttpParameterDescription(mpd);

            Assert.AreEqual("SampleMethodResult", hpd.Name, "Name was not set correctly");
            Assert.AreEqual(typeof(string), hpd.ParameterType, "ProcessorType was not set correctly");
            Assert.AreEqual(0, hpd.Index, "Index was not set correctly");
            Assert.AreSame(mpd, hpd.MessagePartDescription, "Internal messagePartDescription should be what we passed to ctor");
        }
 public JsonValueProcessor(HttpParameterDescription parameter,MediaTypeProcessorMode mode )
     : base(parameter, mode)
 {
     if (parameter != null)
     {
         parameterType = parameter.ParameterType;
         isJsonValueParameter = typeof (JsonValue).IsAssignableFrom(parameterType);
         if (!isJsonValueParameter)
             serializer = new DataContractJsonSerializer(parameterType);
     }
 }
        public void HttpParameterDescription_Construct_From_Simple_Properties()
        {
            HttpParameterDescription hpd = new HttpParameterDescription()
            {
                Name = "Sample",
                Namespace = "SampleNS",
                ParameterType = typeof(string),
                Index = 1
            };

            Assert.AreEqual("Sample", hpd.Name, "Name was not set correctly");
            Assert.AreEqual("SampleNS", hpd.Namespace, "Namespace was not set correctly");
            Assert.AreEqual(typeof(string), hpd.ParameterType, "ProcessorType was not set correctly");
            Assert.AreEqual(1, hpd.Index, "Index was not set correctly");
        }
        public void HttpParameterDescriptionCollection_Unsynchronized_Remove()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };
            HttpParameterDescription hpd3 = new HttpParameterDescription()
            {
                Name = "Third",
                Namespace = "ThirdNS",
                Index = 2,
                ParameterType = typeof(double)
            };

            coll.Add(hpd1);
            coll.Add(hpd2);

            // Remove
            coll.Remove(hpd3);
            Assert.AreEqual(2, coll.Count, "Remove failed");
            Assert.IsFalse(coll.Contains(hpd3), "Remove still shows contains");

            // Remove negative
            Assert.IsFalse(coll.Remove(hpd3), "Redundant remove should have returned false");

            ExceptionAssert.ThrowsArgumentNull(
                "Remove throws argument null for null item",
                "item",
                () => coll.Remove(null)
                );
        }
        public void HttpParameterDescriptionCollection_Unsynchronized_Insert()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };
            HttpParameterDescription hpd3 = new HttpParameterDescription()
            {
                Name = "Third",
                Namespace = "ThirdNS",
                Index = 2,
                ParameterType = typeof(double)
            };

            // Insert semamtics allow index==Count.  Verify.
            coll.Insert(0, hpd1);
            coll.Insert(1, hpd2);

            // Now really insert between
            coll.Insert(1, hpd3);

            Assert.AreEqual(3, coll.Count, "Insert failed");
            Assert.AreSame(hpd3, coll[1], "Insert went to wrong spot");
            Assert.AreSame(hpd2, coll[2], "Insert did not move items");

            // Insert negative
            ExceptionAssert.ThrowsArgumentNull(
                "Insert throws argument null for null item",
                "item",
                () => coll.Insert(0, null)
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "Insert should throw for negative index",
                () => coll.Insert(-1, hpd3),
                "index"
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "Insert should throw for too large index",
                () => coll.Insert(4, hpd3),
                "index"
                );
        }
        public void HttpParameterDescriptionCollection_Unsynchronized_IndexOf()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };
            HttpParameterDescription hpd3 = new HttpParameterDescription()
            {
                Name = "Third",
                Namespace = "ThirdNS",
                Index = 2,
                ParameterType = typeof(double)
            };

            coll.Add(hpd1);
            coll.Add(hpd2);

            // IndexOf
            Assert.AreEqual(0, coll.IndexOf(hpd1), "IndexOf[0] incorrect");
            Assert.AreEqual(1, coll.IndexOf(hpd2), "IndexOf[1] incorrect");
            Assert.AreEqual(-1, coll.IndexOf(hpd3), "IndexOf[none] incorrect");

            // IndexOf negative
            ExceptionAssert.ThrowsArgumentNull(
                "IndexOf with null should throw",
                "item",
                () => coll.IndexOf(null));
        }
        public void HttpParameterDescriptionCollection_Unsynchronized_Indexer()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };
            HttpParameterDescription hpd3 = new HttpParameterDescription()
            {
                Name = "Third",
                Namespace = "ThirdNS",
                Index = 2,
                ParameterType = typeof(double)
            };
            HttpParameterDescription hpdTemp = null;

            coll.Add(hpd1);
            coll.Add(hpd2);

            // Indexer get
            Assert.AreEqual(2, coll.Count, "Count incorrect");
            Assert.AreSame(hpd1, coll[0], "Indexer[0] incorrect");
            Assert.AreSame(hpd2, coll[1], "Indexer[1] incorrect");

            // Indexer get negative
            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "Indexer should throw for too large index",
                () => hpdTemp = coll[2],
                "index"
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "Indexer should throw for negative index",
                () => hpdTemp = coll[-1],
                "index"
                );

            // Indexer set
            coll[1] = hpd3;
            Assert.AreSame(hpd3, coll[1], "Indexer set failed");

            // Indexer set null item
            ExceptionAssert.ThrowsArgumentNull(
                "Indexer set with null should throw",
                "value",
                () => coll[0] = null);

            // Indexer set negative
            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "Indexer should throw for too large index",
                () => coll[5] = hpd2,
                "index"
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "Indexer should throw for negative index",
                () => coll[-1] = hpd2,
                "index"
                );
        }
        public void HttpParameterDescriptionCollection_Unsynchronized_Implements_Clear()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };

            coll.Add(hpd1);
            coll.Add(hpd2);

            // Clear
            coll.Clear();
            Assert.AreEqual(0, coll.Count, "Clear failed");
        }
 public void HttpParameterDescription_Type_Property_Updates_MessagePartDescription()
 {
     OperationDescription od = GetOperationDescription(typeof(MockService3), "SampleInOutMethod");
     MessagePartDescription mpd = od.Messages[0].Body.Parts[0];
     HttpParameterDescription hpd = new HttpParameterDescription(mpd);
     hpd.ParameterType = typeof(float);
     Assert.AreEqual(typeof(float), mpd.Type, "Setting type on http parameter description should update messagePartDescription");
     Assert.AreEqual(typeof(float), hpd.ParameterType, "Setting type did not retain value");
 }
        public void HttpParameterDescriptionCollection_Synchronized_Incomplete_CopyTo()
        {
            OperationDescription od = GetOperationDescription(typeof(MockService3), "SampleInOutMethod");
            HttpOperationDescription hod = od.ToHttpOperationDescription();
            HttpParameterDescriptionCollection hpdColl = new HttpParameterDescriptionCollection(od, isOutputCollection: false);
            HttpParameterDescription hpd = hod.InputParameters[0];

            Assert.IsTrue(hpdColl.Contains(hpd), "Prove Contains works prior to clearing");

            // Zap both inputs and outputs
            MessageDescription mdInput = od.Messages[0];
            MessageDescription mdOutput = od.Messages[1];
            od.Messages.Clear();

            HttpParameterDescription[] arr = new HttpParameterDescription[2];

            // CopyTo should throw ArgumentOutOfRange for any copy request
            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "Expected ArgumentOutOfRangeException on empty Messages",
                () => hpdColl.CopyTo(arr, 0),
                "arrayIndex"
            );

            od.Messages.Add(mdInput);

            hpdColl.CopyTo(arr, 0);
            Assert.AreEqual(hpd.MessagePartDescription, arr[0].MessagePartDescription, "Copy did not yield expected instance");
        }
        public void HttpParameterDescriptionCollection_Unsynchronized_CopyTo()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };
            HttpParameterDescription hpd3 = new HttpParameterDescription()
            {
                Name = "Third",
                Namespace = "ThirdNS",
                Index = 2,
                ParameterType = typeof(double)
            };

            coll.Add(hpd1);
            coll.Add(hpd2);

            // CopyTo
            HttpParameterDescription[] arr = new HttpParameterDescription[2];
            coll.CopyTo(arr, 0);
            Assert.AreSame(hpd1, arr[0], "CopyTo[0] failed");
            Assert.AreSame(hpd2, arr[1], "CopyTo[1] failed");

            // CopyTo negative tests
            ExceptionAssert.ThrowsArgumentNull(
                "CopyTo throws argument null for null array",
                "array",
                () => coll.CopyTo(null, 0)
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "CopyTo should throw for negative index",
                () => coll.CopyTo(arr, -1),
                "arrayIndex"
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "CopyTo should throw for too large an index",
                () => coll.CopyTo(arr, 2),
                "arrayIndex"
                );
        }
        public void HttpParameterDescriptionCollection_Unsynchronized_Contains()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };
            HttpParameterDescription hpd3 = new HttpParameterDescription()
            {
                Name = "Third",
                Namespace = "ThirdNS",
                Index = 2,
                ParameterType = typeof(double)
            };

            coll.Add(hpd1);
            coll.Add(hpd2);

            // Contains
            Assert.IsTrue(coll.Contains(hpd1), "Contains[0] incorrect");
            Assert.IsTrue(coll.Contains(hpd2), "Contains[1] incorrect");
            Assert.IsFalse(coll.Contains(hpd3), "Contains[none] incorrect");

            // Contains negative
            ExceptionAssert.ThrowsArgumentNull(
                "IndexOf with null should throw",
                "item",
                () => coll.Contains(null));
        }
 public void HttpParameterDescription_Index_Property_Updated_From_MessagePartDescription()
 {
     OperationDescription od = GetOperationDescription(typeof(MockService3), "SampleInOutMethod");
     MessagePartDescription mpd = od.Messages[0].Body.Parts[0];
     HttpParameterDescription hpd = new HttpParameterDescription(mpd);
     mpd.Index = 5;
     Assert.AreEqual(5, hpd.Index, "Setting index on messagePartDescription should update http parameter description");
 }
        public void HttpParameterDescriptionCollection_Unsynchronized_RemoveAt()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };
            HttpParameterDescription hpd3 = new HttpParameterDescription()
            {
                Name = "Third",
                Namespace = "ThirdNS",
                Index = 2,
                ParameterType = typeof(double)
            };

            coll.Add(hpd1);
            coll.Add(hpd2);

            // RemoveAt
            coll.Add(hpd3);
            Assert.AreEqual(3, coll.Count, "Add failed");
            Assert.IsTrue(coll.Contains(hpd3), "Contains after add failed");
            coll.RemoveAt(2);
            Assert.AreEqual(2, coll.Count, "RemoveAt count failed");
            Assert.IsFalse(coll.Contains(hpd3), "RemoveAt+Contains failed");

            // RemoveAt negative
            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "RemoveAt should throw for negative index",
                () => coll.RemoveAt(-1),
                "index"
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "RemoveAt should throw for too large index",
                () => coll.RemoveAt(3),
                "index"
                );
        }
        public void HttpParameterDescriptionCollection_Synchronized_CopyTo()
        {
            OperationDescription od1 = GetOperationDescription(typeof(MockService3), "SampleInOutMethod");
            OperationDescription od2 = GetOperationDescription(typeof(MockService3), "SampleMethod");
            MessagePartDescriptionCollection mpdColl = od1.Messages[0].Body.Parts;
            Assert.AreEqual(2, mpdColl.Count, "MessagePartDescriptionCollection should show 2 existing input parameters");

            MessagePartDescriptionCollection mpdColl2 = od2.Messages[0].Body.Parts;
            Assert.AreEqual(1, mpdColl2.Count, "MessagePartDescriptionCollection 2 should show 1 existing input parameters");

            // Pull out individual parts to test synching at item level
            MessagePartDescription mpd1 = mpdColl[0];
            MessagePartDescription mpd2 = mpdColl[1];

            // Use a MPD from a 2nd collection so we can add and remove it
            MessagePartDescription mpd3 = mpdColl2[0];

            // This ctor creates the synchronized form of the collection.   It should immediately reflect
            // the state of the MPD collection
            HttpParameterDescriptionCollection hpdColl = new HttpParameterDescriptionCollection(od1, isOutputCollection: false);
            Assert.IsNotNull(hpdColl, "Failed to create HttpParameterDescriptionCollection");
            Assert.AreEqual(2, hpdColl.Count, "HttpParameterDescriptionCollection should show 2 existing input parameters");

            // Extension method creates synched version of HPD from MPD's
            HttpParameterDescription hpd1 = mpd1.ToHttpParameterDescription();
            HttpParameterDescription hpd2 = mpd2.ToHttpParameterDescription();

            // Ensure the extension method created HPD's that point to the idential MPD
            Assert.AreEqual(mpd1, hpd1.MessagePartDescription, "HttParameterDescription 1 linked to wrong MessagePartDescription");
            Assert.AreEqual(mpd2, hpd2.MessagePartDescription, "HttParameterDescription 2 linked to wrong MessagePartDescription");

            // Keep one from 2nd collection
            HttpParameterDescription hpd3 = mpd3.ToHttpParameterDescription();

            // CopyTo
            HttpParameterDescription[] arr = new HttpParameterDescription[2];
            hpdColl.CopyTo(arr, 0);
            Assert.AreSame(hpd1.MessagePartDescription, arr[0].MessagePartDescription, "CopyTo[0] failed");
            Assert.AreSame(hpd2.MessagePartDescription, arr[1].MessagePartDescription, "CopyTo[1] failed");

            // CopyTo negative tests
            ExceptionAssert.ThrowsArgumentNull(
                "CopyTo throws argument null for null array",
                "array",
                () => hpdColl.CopyTo(null, 0)
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "CopyTo should throw for negative index",
                () => hpdColl.CopyTo(arr, -1),
                "arrayIndex"
                );

            ExceptionAssert.Throws(
                typeof(ArgumentOutOfRangeException),
                "CopyTo should throw for too large an index",
                () => hpdColl.CopyTo(arr, 2),
                "arrayIndex"
                );
        }
        public void HttpParameterDescriptionCollection_Unsynchronized_GetEnumerator()
        {
            HttpParameterDescriptionCollection coll = new HttpParameterDescriptionCollection();
            HttpParameterDescription hpd1 = new HttpParameterDescription()
            {
                Name = "First",
                Namespace = "FirstNS",
                Index = 0,
                ParameterType = typeof(string)
            };
            HttpParameterDescription hpd2 = new HttpParameterDescription()
            {
                Name = "Second",
                Namespace = "SecondNS",
                Index = 1,
                ParameterType = typeof(int)
            };
            HttpParameterDescription hpd3 = new HttpParameterDescription()
            {
                Name = "Third",
                Namespace = "ThirdNS",
                Index = 2,
                ParameterType = typeof(double)
            };

            coll.Add(hpd1);
            coll.Add(hpd2);

            // GetEnumerator
            IEnumerator<HttpParameterDescription> ie = coll.GetEnumerator();
            Assert.IsNotNull(ie, "GetEnumerator failed");
            object[] items = EnumeratorToArray(ie);
            AssertSame(coll, items, "Generic enumerator");

            // Non-generic GetEnumerator
            IEnumerator iec = ((IEnumerable)coll).GetEnumerator();
            Assert.IsNotNull(iec, "GetEnumerator failed");
            items = EnumeratorToArray(iec);
            AssertSame(coll, items, "Non-generic enumerator");
        }
        public void HttpParameterDescriptionCollection_Synchronized_Collection_Throws_New_Mock_HttpParameterDescriptions()
        {
            OperationDescription od = GetOperationDescription(typeof(MockService3), "SampleInOutMethod");
            MessagePartDescriptionCollection mpdColl = od.Messages[0].Body.Parts;
            HttpParameterDescriptionCollection hpdColl = new HttpParameterDescriptionCollection(od, isOutputCollection: false);
            Assert.IsFalse(hpdColl.IsReadOnly, "Collection should not be readonly");

            // Create a new HPD from simple types
            HttpParameterDescription hpd = new HttpParameterDescription()
            {
                Name = "MockHpd",
                Namespace = "MockHpdNS",
                Index = 2,
                ParameterType = this.GetType()
            };

            ExceptionAssert.ThrowsInvalidOperation(
                "Should throw if attempt to add unsynchronized item to synchronized collection",
                () => hpdColl.Add(hpd));

            ExceptionAssert.ThrowsInvalidOperation(
                "Should throw if attempt to insert unsynchronized item to synchronized collection",
                () => hpdColl.Insert(0, hpd));

            ExceptionAssert.ThrowsInvalidOperation(
                "Should throw if attempt to test contains of unsynchronized item to synchronized collection",
                () => hpdColl.Contains(hpd));

            ExceptionAssert.ThrowsInvalidOperation(
                "Should throw if attempt to test contains of unsynchronized item to synchronized collection",
                () => hpdColl.IndexOf(hpd));
        }
 public void HttpParameterDescription_Name_Immutable_From_MessagePartDescription()
 {
     OperationDescription od = GetOperationDescription(typeof(MockService3), "SampleInOutMethod");
     HttpParameterDescription hpd = new HttpParameterDescription(od.Messages[0].Body.Parts[0]);
     ExceptionAssert.Throws(
         typeof(NotSupportedException),
         "Setting name property should throw",
         () => { hpd.Name = "newName"; }
         );
 }