Пример #1
0
        /// <summary>
        /// Return the comparison of the 2 Jsons based on the data received by parameter
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ResultDiff GetDiff(Data data)
        {
            // Decode the Jsons
            byte[] leftJsonByte  = Convert.FromBase64String(data.LeftSide);
            byte[] rightJsonByte = Convert.FromBase64String(data.RightSide);
            var    leftJson      = Encoding.UTF8.GetString(leftJsonByte);
            var    rightJson     = Encoding.UTF8.GetString(rightJsonByte);

            var result = new ResultDiff(); // Creates an object to manipulate the result of the comparison

            // Validates if the both Jsons are equal
            if (leftJson.SequenceEqual(rightJson))
            {
                result.AreEqual = true; // add the value into the object indicating that the Jsons are equal
                result.SameSize = true; // add the value into the object indicating that the Jsons have the same size
            }
            else
            {
                // Validate if the Jsons have the same size
                if (leftJsonByte.Length.Equals(rightJsonByte.Length))
                {
                    result.SameSize = true;                                         // add the value into the object indicating that the Jsons have the same size
                }
                var diffPatch = new JsonDiffPatch();                                // Creates an object of a nuget package extension responsible for compare the Jsons
                var diffObj   = JObject.Parse(diffPatch.Diff(leftJson, rightJson)); // Get the difference between the Jsons
                result.Diffs = diffObj;                                             // Add the differences into the object
            }
            // Add the Size of the Jsons into the object
            result.LetfSize  = leftJson.Length;
            result.RightSize = rightJson.Length;

            return(result); // Return the result of the comparison
        }
Пример #2
0
        public async Task ValidateDiffEndpointSameSizeDifferentData()
        {
            //Arrange
            var contentLeft  = new StringContent(item1, UnicodeEncoding.UTF8, "application/json");
            var contentRight = new StringContent(item2, UnicodeEncoding.UTF8, "application/json");
            HttpResponseMessage responseLeft = await client.PostAsync(
                string.Format("{0}/{1}/{2}", baseURL, id, left), contentLeft);

            responseLeft.EnsureSuccessStatusCode();
            HttpResponseMessage responseRight = await client.PostAsync(
                string.Format("{0}/{1}/{2}", baseURL, id, right), contentRight);

            responseRight.EnsureSuccessStatusCode();

            // Act
            ResultDiff          resultDiff = null;
            HttpResponseMessage response   = await client.GetAsync(string.Format("{0}/{1}", baseURL, id));

            if (response.IsSuccessStatusCode)
            {
                resultDiff = await response.Content.ReadAsAsync <ResultDiff>();
            }

            // Assert
            Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
            Assert.IsTrue(resultDiff.SameSize);
            Assert.IsFalse(resultDiff.AreEqual);
        }