public void ProcessRequest_FileAlreadyExistsButHashIsDifferent_ShouldNotifyClientToSendFile()
        {
            // Arrange
            var configuration = MockConfiguration();

            var(fileSystem, _, _) = MockFileSystem(OutputDirectory, new MemoryStream(_fileContent));
            var hashAlgorithm = MD5.Create();
            var dialogServer  = MockDialogServer(_incorrectHash);

            // Act
            var receiveStrategy = new ChangeReceiverStrategy(dialogServer.Object, configuration.Object, hashAlgorithm, fileSystem.Object);

            receiveStrategy.ProcessRequest();

            // Assert
            dialogServer.Verify(d => d.NotifyClient(true), Times.Once);
        }
        public void ProcessRequest_FileAlreadyExistsButHashIsDifferentAndRelativeDirectoryDoesnExist_ShouldCreateRelativeDirectory()
        {
            // Arrange
            var configuration = MockConfiguration();

            var(fileSystem, _, directory) = MockFileSystem(OutputDirectory, new MemoryStream(_fileContent));
            var hashAlgorithm = MD5.Create();
            var dialogServer  = MockDialogServer(_incorrectHash);

            // Act
            var receiveStrategy = new ChangeReceiverStrategy(dialogServer.Object, configuration.Object, hashAlgorithm, fileSystem.Object);

            receiveStrategy.ProcessRequest();

            // Assert
            directory.Verify(dir => dir.Exists("C:\\MyFolder\\sub"));
            directory.Verify(dir => dir.CreateDirectory("C:\\MyFolder\\sub"));
        }
        public void ProcessRequest_FileAlreadyExistsButHashIsDifferentAndOldTmpFileExists_ShouldRemoveOldTmpFile()
        {
            // Arrange
            var configuration = MockConfiguration();

            var(fileSystem, file, _) = MockFileSystem(OutputDirectory, new MemoryStream(_fileContent));
            file.Setup(f => f.Exists("C:\\MyFolder\\sub\\1.txt.tmp")).Returns(true);
            var hashAlgorithm = MD5.Create();
            var dialogServer  = MockDialogServer(_incorrectHash);

            // Act
            var receiveStrategy = new ChangeReceiverStrategy(dialogServer.Object, configuration.Object, hashAlgorithm, fileSystem.Object);

            receiveStrategy.ProcessRequest();

            // Assert
            file.Verify(f => f.Exists("C:\\MyFolder\\sub\\1.txt.tmp"));
            file.Verify(f => f.Delete("C:\\MyFolder\\sub\\1.txt.tmp"));
        }
        public void ProcessRequest_FileNotExistAndFirstSegmentNotExists_ShouldReadSegmentFromSource()
        {
            // Arrange
            var configuration = MockConfiguration();

            var(fileSystem, file, _) = MockFileSystem(OutputDirectory, new MemoryStream(_fileContent));
            var destinationBuffer = new byte[16];
            var segmentPath       = SetupSegmentPolicy(destinationBuffer, file, false);

            var hashAlgorithm = MD5.Create();
            var dialogServer  = MockDialogServer(_incorrectHash);

            SetupDialogSegmentPolicy(dialogServer);

            // Act
            var receiveStrategy = new ChangeReceiverStrategy(dialogServer.Object, configuration.Object, hashAlgorithm, fileSystem.Object);

            receiveStrategy.ProcessRequest();

            // Assert
            dialogServer.Verify(d => d.NotifyClient(true), Times.Exactly(2));
            file.Verify(f => f.WriteAllBytes(segmentPath, _incorrectHash));
            Assert.Equal(_incorrectHash, destinationBuffer);
        }
        public void ProcessRequest_FileNotExistButFirstSegmentExists_ShouldTakeSegmentFromCache()
        {
            // Arrange
            var configuration = MockConfiguration();

            var(fileSystem, file, _) = MockFileSystem(OutputDirectory, new MemoryStream(_fileContent));
            var destinationBuffer = new byte[16];

            SetupSegmentPolicy(destinationBuffer, file, true);

            var hashAlgorithm = MD5.Create();
            var dialogServer  = MockDialogServer(_incorrectHash);

            SetupDialogSegmentPolicy(dialogServer);

            // Act
            var receiveStrategy = new ChangeReceiverStrategy(dialogServer.Object, configuration.Object, hashAlgorithm, fileSystem.Object);

            receiveStrategy.ProcessRequest();

            // Assert
            dialogServer.Verify(d => d.NotifyClient(false), Times.Once);
            Assert.Equal(_fileContent, destinationBuffer);
        }