public async Task ScanLocationAsync_ReportsProgressCorrectly()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();
            Action <int> addFileDelegate = (int id) =>
            {
                string path    = string.Format(@"C:\foobar\file{0}.txt", id);
                string content = string.Format("This is a test file{0}.", id);
                dictionaryMockFileData.Add(path, new MockFileData(content));
            };

            for (int i = 0; i < 100; i++)
            {
                addFileDelegate(i);
            }
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            // ACT
            await scannedFileStore.ScanLocationsAsync(locations, progress);

            // ASSERT
            Assert.AreEqual(progress.ReportedValues.Count, 100);
            Assert.AreEqual(progress.ReportedValues[49], 50);
            Assert.AreEqual(progress.ReportedValues[99], 100);
        }
        public async Task ScanLocationAsync_StoresScannedLocation()
        {
            // ARRANGE
            List <ScannedLocation>            scannedLocations       = new List <ScannedLocation>();
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foobar\file.txt", new MockFileData("This is a test file{0}."));
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            service.Setup(t => t.InsertScannedLocationAsync(It.IsAny <ScannedLocation>()))
            .Returns(Task.CompletedTask)
            .Callback((ScannedLocation s) => { scannedLocations.Add(s); });

            // ACT
            await scannedFileStore.ScanLocationsAsync(locations, progress);

            // ASSERT
            Assert.AreEqual(locations[0], scannedLocations[0].Path);
        }
        public async Task RescanLocationAsync_RemovesScannedFilesNotFoundInDiretory()
        {
            // ARRANGE
            MockFileSystem fileSystem = new MockFileSystem();

            fileSystem.AddDirectory(@"C:\foobar");
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            service.Setup(t => t.ListScannedFilePathsAsync(It.IsAny <List <string> >())).ReturnsAsync((List <string> locs) =>
            {
                return(new List <string>()
                {
                    @"C:\foobar\foo.bar"
                });
            });

            // ACT
            await scannedFileStore.RescanLocationsAsync(locations, progress);

            // ASSERT
            service.Verify(t => t.RemoveScannedFilesByFilePathAsync(It.Is <string>(s => s.Equals(@"C:\foobar\foo.bar"))));
        }
        public async Task RescanLocationAsync_StoresAddedScannedFilesFromDiretory()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foobar\file1.txt", new MockFileData("This is a test file."));
            dictionaryMockFileData.Add(@"C:\foobar\file2.txt", new MockFileData("This is another test file."));
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            service.Setup(t => t.ListScannedFilePathsAsync(It.IsAny <List <string> >())).ReturnsAsync((List <string> locs) =>
            {
                return(new List <string>()
                {
                    @"C:\foobar\foo.bar"
                });
            });

            // ACT
            await scannedFileStore.RescanLocationsAsync(locations, progress);

            // ASSERT
            service.Verify(t => t.InsertScannedFileAsync(It.IsAny <ScannedFile>()), Times.Exactly(2), "The correct number of files were not inserted.");
        }
        public async Task RescanLocationAsync_ReportsProgressCorrectly()
        {
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <ScannedFile>           scannedFiles     = new List <ScannedFile>();
            Action <int> addFileDelegate = (int id) =>
            {
                string path    = string.Format(@"C:\foobar\file{0}.txt", id);
                string content = string.Format("This is a test file{0}.", id);
                fileSystem.AddFile(path, new MockFileData(content));
            };
            Action <int> addFileRemovalDelegate = (int id) =>
            {
                string path = string.Format(@"C:\foobar\oldfile{0}.txt", id);
                string name = string.Format("oldfile{0}.txt", id);
                byte[] hash = new byte[32];
                hash[0] = System.Convert.ToByte(id);
                scannedFiles.Add(new ScannedFile()
                {
                    Path = path,
                    Name = name,
                    Hash = hash
                });
            };

            for (int i = 0; i < 50; i++)
            {
                addFileDelegate(i);
                addFileRemovalDelegate(i);
            }
            service.Setup(t => t.ListScannedFilePathsAsync(It.IsAny <List <string> >()))
            .ReturnsAsync((List <string> locs) =>
            {
                return(scannedFiles.Select(t => t.Path).ToList());
            });
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            // ACT
            await scannedFileStore.RescanLocationsAsync(locations, progress);

            // ASSERT
            Assert.AreEqual(progress.ReportedValues.Count, 100);
            Assert.AreEqual(progress.ReportedValues[49], 50);
            Assert.AreEqual(progress.ReportedValues[99], 100);
        }
        public async Task RemoveFile_UpdatesProgress()
        {
            // ARRANGE
            MockFileSystem               fileSystem       = new MockFileSystem();
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();

            // ACT
            await scannedFileStore.RemoveFile(@"C:\foo\bar.txt", progress, 1, 1);

            // ASSERT
            Assert.AreEqual(100, progress.ReportedValues[0]);
        }
        public async Task ScanLocationAsync_ExistingScanedLocationsPurged()
        {
            // ARRANGE
            Dictionary <string, MockFileData> dictionaryMockFileData = new Dictionary <string, MockFileData>();

            dictionaryMockFileData.Add(@"C:\foobar\file.txt", new MockFileData("This is a test file{0}."));
            MockFileSystem               fileSystem       = new MockFileSystem(dictionaryMockFileData);
            Mock <IFileHashService>      service          = new Mock <IFileHashService>();
            ScannedFileStore             scannedFileStore = new ScannedFileStore(fileSystem, service.Object);
            MockScannedFileStoreProgress progress         = new MockScannedFileStoreProgress();
            List <string> locations = new List <string>()
            {
                @"C:\foobar"
            };

            // ACT
            await scannedFileStore.ScanLocationsAsync(locations, progress);

            // ASSERT
            service.Verify(t => t.PurgeScannedLocationsAsync(It.IsAny <List <string> >()));
        }