public void When_SettingRightWithInvalidBase64Characters_Should_ReturnBadRequest()
        {
            var dataComparisonService = new DataComparisonService(new InMemoryDataComparisonRepository());
            var controller = new DiffController(dataComparisonService);

            var result = controller.SetRight(new SetDataModel { Data = "WRONG BASE64 DATA" });
            Assert.IsType<BadRequestErrorMessageResult>(result);
        }
        public void When_SettingLeft_And_ComparisonItemIsNotFound_Should_ThrowInvalidOpException()
        {
            var repositoryMock = new Mock<IDataComparisonRepository>(MockBehavior.Strict);
            repositoryMock.Setup(x => x.Get()).Returns<DataComparisonItem>(null);
            var dataComparisonService = new DataComparisonService(repositoryMock.Object);

            Assert.Throws<InvalidOperationException>(() => dataComparisonService.SetLeft(GetData("test data")));
        }
        public void When_SettingLeft_Should_NotChangeRightData()
        {
            var comparisonItem = new DataComparisonItem(GetData("left"), GetData("right"));
            var repositoryMock = new Mock<IDataComparisonRepository>(MockBehavior.Strict);
            repositoryMock.Setup(x => x.Get()).Returns(comparisonItem).Verifiable();
            repositoryMock.Setup(x => x.Save(It.Is<DataComparisonItem>(item => GetData("right").SequenceEqual(item.Right)))).Verifiable();
            var dataComparisonService = new DataComparisonService(repositoryMock.Object);

            dataComparisonService.SetLeft(GetData("test data"));

            repositoryMock.VerifyAll();
        }
        public void When_Comparing_And_RightEqualsLeft_Should_ReturnEqualState()
        {
            var dataComparisonService = new DataComparisonService(new InMemoryDataComparisonRepository());
            var controller = new DiffController(dataComparisonService);

            var result = controller.SetLeft(new SetDataModel { Data = CreateDataFromUnicodeString("test data") });
            Assert.IsType<OkResult>(result);

            result = controller.SetRight(new SetDataModel { Data = CreateDataFromUnicodeString("test data") });
            Assert.IsType<OkResult>(result);

            var compResult = controller.GetDiff();
            Assert.Equal(ComparisonState.Equal, compResult.ComparisonState);
        }
        public void When_SettingLeft_And_RightWithDifferentSizeData_Should_ReturnNoDifferences()
        {
            var dataComparisonService = new DataComparisonService(new InMemoryDataComparisonRepository());
            var controller = new DiffController(dataComparisonService);

            var result = controller.SetLeft(new SetDataModel { Data = CreateDataFromUnicodeString("333") });
            Assert.IsType<OkResult>(result);

            result = controller.SetRight(new SetDataModel { Data = CreateDataFromUnicodeString("4444") });
            Assert.IsType<OkResult>(result);

            var compResult = controller.GetDiff();
            Assert.Null(compResult.Differences);
        }
        public void When_Comparing_Should_ReturnGetDataComparisonItem()
        {
            var expectedResult = new ComparisonResult(ComparisonState.Equal);
            var comparisonItemMock = new Mock<DataComparisonItem>(MockBehavior.Strict);
            comparisonItemMock.Setup(x => x.Compare()).Returns(expectedResult).Verifiable();

            var repositoryMock = new Mock<IDataComparisonRepository>(MockBehavior.Strict);
            repositoryMock.Setup(x => x.Get()).Returns(comparisonItemMock.Object).Verifiable();
            var dataComparisonService = new DataComparisonService(repositoryMock.Object);

            var result = dataComparisonService.Compare();

            Assert.Equal(expectedResult, result);
            comparisonItemMock.Verify();
            repositoryMock.Verify();
        }
        public void When_GettingDifferences_Should_ReturnDifferences()
        {
            var dataComparisonService = new DataComparisonService(new InMemoryDataComparisonRepository());
            var controller = new DiffController(dataComparisonService);

            var left = CreateDataFromBytes(new byte[] { 1, 1, 0, 0, 1, 0, 1, 1, 1, 1 });
            var result = controller.SetLeft(new SetDataModel { Data = left });
            Assert.IsType<OkResult>(result);

            var right = CreateDataFromBytes(new byte[] { 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 });
            result = controller.SetRight(new SetDataModel { Data = right });
            Assert.IsType<OkResult>(result);

            var compResult = controller.GetDiff();

            Assert.Equal(ComparisonState.DifferentContent, compResult.ComparisonState);
            Assert.Equal(2, compResult.Differences.Count);

            var diff1 = compResult.Differences.ElementAt(0);
            Assert.Equal(3, diff1.Offset);
            Assert.Equal(4, diff1.Length);

            var diff2 = compResult.Differences.ElementAt(1);
            Assert.Equal(8, diff2.Offset);
            Assert.Equal(1, diff2.Length);
        }