コード例 #1
0
        public void ReadFromStreamAsync_Deserializes_Properties_Correctly()
        {
            // Arrange
            Car car = new Car();
            string inputString =
                "{\"class\":[],\"properties\":{\"colour\":\"Black\",\"numberOfWheels\":5},\"entities\":[]}";

            // Act
            using (MemoryStream stream = new MemoryStream(System.Text.Encoding.Default.GetBytes(inputString)))
            {
                var content = new StreamContent(stream);

                var task = formatter.ReadFromStreamAsync(typeof(Car), stream, content, null);
                car = task.Result as Car;
            }

            // Assert
            Assert.AreEqual("Black", car.Colour);
            Assert.AreEqual(5, car.NumberOfWheels);
        }
コード例 #2
0
        public void WriteToStreamAsync_Serializes_Class_Correctly()
        {
            // Arrange
            Car myCar = new Car();
            myCar.Class.Add("Car Class");

            // Act
            IEntity entityAfter = GetIEntityResultsFromWriteToStreamAsync(myCar);

            // Assert - Have properties
            Assert.IsTrue(entityAfter.Class.Contains("Car Class"));
        }
コード例 #3
0
        public void WriteToStreamAsync_Serializes_SubEntities_Of_SubEntitiess_Correctly()
        {
            // Arrange
            Car myCar = new Car();
            Wheel myWheel = new Wheel();

            WheelNut myNut = new WheelNut();
            myNut.IsLockNut = true;
            myNut.AddClass("WheelNut");
            myNut.AddRel("/rels/wheelnut");

            // Add the subentity to the subentity
            myWheel.AddSubEntity(myNut);

            myWheel.id = 1;
            myWheel.Size = "124x55x18";

            myWheel.Title = "My Car Wheel";
            myWheel.AddRel("/rels/car/wheel");

            myWheel.AddClass("Wheel")
                .AddAction(new WebApiContrib.MediaType.Hypermedia.Action("Inflate", "Inflate the wheel", HTTP_Method.PUT, new Uri("https://api.test.com/wheel/inflate")))
                .AddLink(new SelfLink(new Uri("https://api.test.com/wheel/1")));

            myCar.Entities.Add(myWheel);

            // Act
            IEntity entityAfter = GetIEntityResultsFromWriteToStreamAsync(myCar);

            // Assert - Have properties
            string entityString = entityAfter.Entities[0].ToString();

            string expectedString = @"{
              ""class"": [
            ""Wheel""
              ],
              ""title"": ""My Car Wheel"",
              ""rel"": [
            ""/rels/car/wheel""
              ],
              ""properties"": {
            ""id"": 1,
            ""size"": ""124x55x18""
              },
              ""entities"": [
            {
              ""class"": [
            ""WheelNut""
              ],
              ""rel"": [
            ""/rels/wheelnut""
              ],
              ""properties"": {
            ""id"": 0,
            ""isLockNut"": true
              },
              ""entities"": []
            }
              ],
              ""actions"": [
            {
              ""name"": ""Inflate"",
              ""class"": [
            ""Inflate""
              ],
              ""method"": ""PUT"",
              ""href"": ""https://api.test.com/wheel/inflate"",
              ""title"": ""Inflate the wheel"",
              ""type"": ""application/json"",
              ""fields"": []
            }
              ],
              ""links"": [
            {
              ""rel"": [
            ""self""
              ],
              ""href"": ""https://api.test.com/wheel/1"",
              ""title"": null,
              ""type"": null
            }
              ]
            }";
            // Assert - Match
            Assert.AreEqual(expectedString, entityString);
        }
コード例 #4
0
        public void WriteToStreamAsync_Serializes_Actions_Correctly()
        {
            // Arrange
            Car myCar = new Car();
            Uri actionUri = new Uri("https://api.test.com/car/start");
            myCar.Actions.Add(new WebApiContrib.MediaType.Hypermedia.Action("Start", "Start the car", HTTP_Method.POST, actionUri));

            // Act
            string entityAfter = GetStringResultsFromWriteToStreamAsync(myCar);

            // Assert - Have properties
            string expectedEntity = @"{""class"":[],""properties"":{""numberOfWheels"":0},""entities"":[],""actions"":[{""name"":""Start"",""class"":[""Start""],""method"":""POST"",""href"":""https://api.test.com/car/start"",""title"":""Start the car"",""type"":""application/json"",""fields"":[]}]}";

            // Assert - Match
            Assert.AreEqual(expectedEntity, entityAfter);
        }