Exemplo n.º 1
0
        public void Diffing_EqualStrings()
        {
            // Testing data
            string   left  = Base64String.DecodeBase64String("AAAAAA==");
            string   right = Base64String.DecodeBase64String("AAAAAA==");
            DiffData diff  = new DiffData
            {
                Id        = 1,
                LeftData  = left,
                RightData = right
            };

            // Expected result
            DiffResponse expected = new DiffResponse
            {
                DiffResultType = "Equals",
                Diffs          = null
            };

            // Actual result
            DiffLogic    logic  = new DiffLogic();
            DiffResponse actual = logic.Diffing(diff);

            // Assert
            Assert.AreEqual(expected.DiffResultType, actual.DiffResultType);
            Assert.AreEqual(expected.Diffs, actual.Diffs);
        }
Exemplo n.º 2
0
 private static void OutputUsers(DiffResponse result)
 {
     foreach (var user in result.Users)
     {
         Console.WriteLine("UserObjectId: {0}  UPN: {1}  Name: {2}  E-Mail: {3}", user.Id, user.Upn,
                           user.DisplayName, user.OtherMails.FirstOrDefault());
     }
 }
Exemplo n.º 3
0
        // Function for diff-ing data
        public DiffResponse Diffing(DiffData diff)
        {
            DiffResponse response  = new DiffResponse();
            List <Diff>  diffs     = new List <Diff>();
            string       leftDiff  = diff.LeftData;
            string       rightDiff = diff.RightData;
            int          leftLen   = leftDiff.Length;
            int          rightLen  = rightDiff.Length;

            // If the strings aren't of the same length we aren't interested in differences
            // so we first compare their lengths
            if (leftLen != rightLen)
            {
                response.DiffResultType = "SizeDoNotMatch";
            }
            else
            {
                // If the strings are of the same length we compare them.
                // If they are not equal we locate and return differences.
                int prevDiff = -1; // Index of the last diff object in the list diffs
                int lastDiff = -2; // Index of the last string chars that were recognized as different

                for (int i = 0; i < leftLen; i++)
                {
                    if (leftDiff[i] != rightDiff[i])
                    {
                        // If previous chars of both strings were equal this diff represents a start of a new different substring
                        if (lastDiff != i - 1)
                        {
                            prevDiff += 1;
                            Diff d = new Diff
                            {
                                Offset = i,
                                Length = 1
                            };
                            diffs.Add(d);
                        }
                        // Otherwise we just increase a length of previous diff
                        else
                        {
                            diffs[prevDiff].Length += 1;
                        }
                        lastDiff = i;
                    }
                }
                // We check if we found any differences
                if (prevDiff == -1)
                {
                    response.DiffResultType = "Equals";
                }
                else
                {
                    response.DiffResultType = "ContentDoNotMatch";
                    response.Diffs          = diffs;
                }
            }
            return(response);
        }
Exemplo n.º 4
0
        public async Task ReturnsCorrectResult(int id, DiffResponse response, Type expectedType)
        {
            // arrange
            _diffServiceMock.Setup(x => x.GetDiffAsync(id)).Returns(Task.FromResult(response));

            // act
            var result = await _target.Get(id);

            // assert
            Assert.Equal(expectedType.FullName, result.GetType().FullName);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Get(int id)
        {
            DiffResponse diff = await _service.GetDiffAsync(id);

            if (diff == null)
            {
                return(NotFound());
            }

            return(Ok(diff));
        }
Exemplo n.º 6
0
        public void Diffing_DifferentStrings()
        {
            // Testing data
            string   left  = Base64String.DecodeBase64String("AAAAAA==");
            string   right = Base64String.DecodeBase64String("AQABAQ==");
            DiffData diff  = new DiffData
            {
                Id        = 1,
                LeftData  = left,
                RightData = right
            };

            // Expected result
            List <Diff> diffs = new List <Diff>
            {
                new Diff
                {
                    Offset = 0,
                    Length = 1
                },
                new Diff
                {
                    Offset = 2,
                    Length = 2
                }
            };

            DiffResponse expected = new DiffResponse
            {
                DiffResultType = "ContentDoNotMatch",
                Diffs          = diffs
            };

            // Actual result
            DiffLogic    logic  = new DiffLogic();
            DiffResponse actual = logic.Diffing(diff);

            // Assert
            Assert.AreEqual(expected.DiffResultType, actual.DiffResultType);
            Assert.AreEqual(expected.Diffs.Count, actual.Diffs.Count);

            int length = expected.Diffs.Count;

            for (int i = 0; i < length; i++)
            {
                Assert.AreEqual(expected.Diffs[i].Length, actual.Diffs[i].Length);
                Assert.AreEqual(expected.Diffs[i].Offset, actual.Diffs[i].Offset);
            }
        }
Exemplo n.º 7
0
        internal static string GetDeltaToken(DiffResponse data)
        {
            if (data.NextLink != null)
            {
                var delta = data.NextLink.ExtractNamedQueryParameter("deltaLink", false);
                return(delta);
            }

            if (data.DeltaLink != null)
            {
                var delta = data.DeltaLink.ExtractNamedQueryParameter("deltaLink", false);
                return(delta);
            }

            return(string.Empty);
        }
Exemplo n.º 8
0
        public async Task Get_Valid_Diff()
        {
            double difference = await _sensorService.GetDifferenceAsync("000D6F0003141E14");

            var expectedResult = new DiffResponse("Success", difference);

            var client = _webApplicationFactory.CreateClient();

            var response = await client.GetAsync(@"https://localhost:44338/api/diff/000D6F0003141E14");

            response.EnsureSuccessStatusCode();
            Expect.Equal("application/json; charset=utf-8", response.Content.Headers.ContentType.ToString());

            var responseString = await response.Content.ReadAsStringAsync();

            Expect.DeepEqualLowerCaseFields(expectedResult, responseString);
        }
Exemplo n.º 9
0
        public async Task <IActionResult> GetDifference(string id)
        {
            try{
                var difference = await _sensorService.GetDifferenceAsync(id);

                DiffResponse response = new DiffResponse("Success", difference);

                return(Ok(response));
            }
            catch (InvalidOperationException)
            {
                // TODO log exception
                return(NotFound(new DiffResponse(id)));
            }
            catch (Exception)
            {
                //TODO log exception
                return(StatusCode(500, new DiffResponse("Internal server error")));
            }
        }
Exemplo n.º 10
0
        public ActionResult Get(int id)
        {
            try
            {
                var diff = diffLogic.GetDiffDataById(id);

                if (diffLogic.IsDiffMissingData(diff))
                {
                    return(NotFound());
                }

                DiffResponse d    = diffLogic.Diffing(diff);
                string       json = JsonConvert.SerializeObject(d, Formatting.Indented, new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });

                return(Ok(json));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
        public async void GetDiff_OnSuccess_ShouldReturnResponseFromResponseOK(string correlationId, DiffResponse response,
                                                                               IActionResult actionResult)
        {
            _diffService.Get(correlationId).Returns(response);
            _responseCreator.ResponseOK(response).Returns(actionResult);

            var result = await _sut.GetDiff(correlationId);

            result.Should().Be(actionResult);
        }
Exemplo n.º 12
0
        public async void Get_WhenDiffIsFound_ShouldReturnDiffResponse(string correlationId, EqualDiff diff, DiffResponse diffResponse)
        {
            _cache.GetAsync <Diff>($"diff_{correlationId}").Returns(diff);

            _mapper.Map <DiffResponse>(diff).Returns(diffResponse);

            var result = await _sut.Get(correlationId);

            result.Should().Be(diffResponse);
        }
Exemplo n.º 13
0
 public static GraphResponse Create(DiffResponse data)
 {
     return(new GraphResponse {
         Data = data
     });
 }