public void RequestFilesFromThePostsFolder()
        {
            String rootPath         = $"c:\\{string.Empty.GetRandom()}";
            var    connectionString = new ConnectionStringBuilder("this")
                                      .AddFilePath(rootPath)
                                      .Build();

            var postFiles = new MockMediaFileCollectionBuilder()
                            .AddRandomPosts(1)
                            .Build(rootPath);

            var directoryProvider = new MockDirectoryServiceBuilder()
                                    .AddPostFiles(postFiles.Select(f => f.GetFilename()))
                                    .Build(rootPath);

            var fileSystem = new MockFileServiceBuilder()
                             .AddPosts(postFiles)
                             .Build();

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileSystem.Object)
                         .AddDirectoryService(directoryProvider.Object)
                         .Build(connectionString);

            var posts = target.GetAllPosts();

            directoryProvider.VerifyAll();
        }
        public void IgnoreFilesWithoutJsonExtension()
        {
            String rootPath         = $"c:\\{string.Empty.GetRandom()}";
            var    connectionString = new ConnectionStringBuilder("this")
                                      .AddFilePath(rootPath)
                                      .Build();

            var postFiles = new MockMediaFileCollectionBuilder()
                            .AddRandomPosts(7)
                            .Build(rootPath)
                            .ToArray();

            // Setup some bad file extensions
            postFiles[0].Extension = string.Empty;
            postFiles[2].Extension = ".ppt";
            postFiles[4].Extension = ".txt";
            postFiles[5].Extension = ".com";

            var directoryProvider = new MockDirectoryServiceBuilder()
                                    .AddPostFiles(postFiles.Select(f => f.GetFilename()))
                                    .Build(rootPath);

            var fileSystem = new MockFileServiceBuilder()
                             .AddPosts(postFiles)
                             .Build();

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileSystem.Object)
                         .AddDirectoryService(directoryProvider.Object)
                         .Build(connectionString);

            var posts = target.GetAllPosts();

            Assert.Equal(3, posts.Count());
        }
        private static ContentItem ExecutePropertyTest(String postFileContent)
        {
            String rootPath         = $"c:\\{string.Empty.GetRandom()}";
            var    connectionString = new ConnectionStringBuilder("this")
                                      .AddFilePath(rootPath)
                                      .Build();

            var postFiles = new MockMediaFileCollectionBuilder()
                            .AddPost(postFileContent)
                            .Build(rootPath);

            var directoryProvider = new MockDirectoryServiceBuilder()
                                    .AddPostFiles(postFiles.Select(f => f.GetFilename()))
                                    .Build(rootPath);

            var fileSystem = new MockFileServiceBuilder()
                             .AddPosts(postFiles)
                             .Build();

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileSystem.Object)
                         .AddDirectoryService(directoryProvider.Object)
                         .Build(connectionString);

            var pages = target.GetAllPosts();

            return(pages.ToArray()[0]);
        }
        public void ReturnAllPostsIfAllAreValid()
        {
            Int32 postCount = 3;

            String rootPath         = $"c:\\{string.Empty.GetRandom()}";
            var    connectionString = new ConnectionStringBuilder("this")
                                      .AddFilePath(rootPath)
                                      .Build();

            var postFiles = new MockMediaFileCollectionBuilder()
                            .AddRandomPosts(postCount)
                            .Build(rootPath);

            var directoryProvider = new MockDirectoryServiceBuilder()
                                    .AddPostFiles(postFiles.Select(f => f.GetFilename()))
                                    .Build(rootPath);

            var fileSystem = new MockFileServiceBuilder()
                             .AddPosts(postFiles)
                             .Build();

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileSystem.Object)
                         .AddDirectoryService(directoryProvider.Object)
                         .Build(connectionString);

            var posts = target.GetAllPosts();

            Assert.Equal(postCount, posts.Count());
        }
        public void ReturnOneEntityForEachItemInTheFolder()
        {
            Int32  count        = 25.GetRandom(10);
            String rootPath     = $"c:\\{string.Empty.GetRandom()}";
            String relativePath = string.Empty.GetRandom();
            String folderPath   = Path.Combine(rootPath, relativePath);

            var connectionString = new ConnectionStringBuilder("this")
                                   .AddFilePath(rootPath)
                                   .Build();

            var files = new SourceFileCollectionBuilder()
                        .AddRandomFiles(count, relativePath)
                        .Build();

            var fileService = new MockFileServiceBuilder()
                              .AddSourceFiles(files)
                              .Build();

            var directoryService = new MockDirectoryServiceBuilder()
                                   .AddSourceFiles(files)
                                   .Build(rootPath);

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileService.Object)
                         .AddDirectoryService(directoryService.Object)
                         .Build(connectionString);

            var actual = target.GetFolderContents(relativePath);

            Assert.Equal(count, actual.Count());
        }
        public void ReturnTheCorrectContentsForEachItem()
        {
            Int32  count        = 25.GetRandom(10);
            String rootPath     = $"c:\\{string.Empty.GetRandom()}";
            String relativePath = string.Empty.GetRandom();
            String folderPath   = Path.Combine(rootPath, relativePath);

            var connectionString = new ConnectionStringBuilder("this")
                                   .AddFilePath(rootPath)
                                   .Build();

            var files = new SourceFileCollectionBuilder()
                        .AddRandomFiles(count, relativePath)
                        .Build();

            var fileService = new MockFileServiceBuilder()
                              .AddSourceFiles(files)
                              .Build();

            var directoryService = new MockDirectoryServiceBuilder()
                                   .AddSourceFiles(files)
                                   .Build(rootPath);

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileService.Object)
                         .AddDirectoryService(directoryService.Object)
                         .Build(connectionString);

            var actual = target.GetFolderContents(relativePath);

            foreach (var file in files)
            {
                Assert.Equal(file.Contents, actual.Single(a => a.FileName == file.FileName).Contents);
            }
        }
        public void RequestTheContentsOfTheCorrectFolderPath()
        {
            Int32  count        = 25.GetRandom(10);
            String rootPath     = $"c:\\{string.Empty.GetRandom()}";
            String relativePath = string.Empty.GetRandom();
            String folderPath   = Path.Combine(rootPath, relativePath);

            var connectionString = new ConnectionStringBuilder("this")
                                   .AddFilePath(rootPath)
                                   .Build();

            var files = new SourceFileCollectionBuilder()
                        .AddRandomFiles(count, relativePath)
                        .Build();

            var fileService = new MockFileServiceBuilder()
                              .AddSourceFiles(files)
                              .Build();

            var directoryService = new MockDirectoryServiceBuilder()
                                   .AddSourceFiles(files)
                                   .Build(rootPath);

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileService.Object)
                         .AddDirectoryService(directoryService.Object)
                         .Build(connectionString);

            var actual = target.GetFolderContents(relativePath);

            directoryService.Verify(fs => fs.EnumerateFiles(folderPath), Times.Once);
        }
        public void ReturnTheCorrectNumberOfTagsEvenIfAddedAtDifferentTimes()
        {
            var expected1 = 30.GetRandom(3);
            var expected2 = 30.GetRandom(3);
            var expected  = expected1 + expected2;

            var tags1 = new List <string>();

            for (Int32 i = 0; i < expected1; i++)
            {
                tags1.Add(string.Empty.GetRandom());
            }

            var tags2 = new List <string>();

            for (Int32 i = 0; i < expected2; i++)
            {
                tags2.Add(string.Empty.GetRandom());
            }

            String rootPath         = $"c:\\{string.Empty.GetRandom()}";
            var    connectionString = new ConnectionStringBuilder("this")
                                      .AddFilePath(rootPath)
                                      .Build();

            var postJson = new MediaPostBuilder()
                           .UseRandomFlickrPost()
                           .ClearTags()
                           .AddTags(tags1)
                           .AddTags(tags2)
                           .Build();

            var postFiles = new MockMediaFileCollectionBuilder()
                            .AddPost(postJson)
                            .Build(rootPath);

            var directoryProvider = new MockDirectoryServiceBuilder()
                                    .AddPostFiles(postFiles.Select(f => f.GetFilename()))
                                    .Build(rootPath);

            var fileSystem = new MockFileServiceBuilder()
                             .AddPosts(postFiles)
                             .Build();

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileSystem.Object)
                         .AddDirectoryService(directoryProvider.Object)
                         .Build(connectionString);

            var pages  = target.GetAllPosts();
            var actual = pages.Single().Tags.Count();

            Assert.Equal(expected, actual);
        }
        public void SkipItemsThatRequireAuthorizationToAccess()
        {
            Int32  count        = 35.GetRandom(20);
            String rootPath     = $"c:\\{string.Empty.GetRandom()}";
            String relativePath = string.Empty.GetRandom();
            String folderPath   = Path.Combine(rootPath, relativePath);

            String fakeFolder = String.Empty.GetRandom();
            String fakePath   = Path.Combine(rootPath, fakeFolder);

            var connectionString = new ConnectionStringBuilder("this")
                                   .AddFilePath(rootPath)
                                   .Build();

            var files = new SourceFileCollectionBuilder()
                        .AddRandomFiles(count, relativePath)
                        .Build();

            var directoryService = new MockDirectoryServiceBuilder()
                                   .AddSourceFiles(files)
                                   .Build(rootPath);

            var fileServiceBuilder = new MockFileServiceBuilder();

            Int32 expected = 0;

            foreach (var file in files)
            {
                bool secured = true.GetRandom();
                fileServiceBuilder.AddSecuredSourceFile(file, secured);
                if (!secured)
                {
                    expected++;
                }
            }

            var fileService = fileServiceBuilder.Build();

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileService.Object)
                         .AddDirectoryService(directoryService.Object)
                         .Build(connectionString);

            var actual = target.GetFolderContents(relativePath);

            Assert.Equal(expected, actual.Count());
        }
        public void ReturnTheCorrectTagsFromAMultiTagPost()
        {
            var expectedCount = 30.GetRandom(3);
            var tags          = new List <string>();

            for (Int32 i = 0; i < expectedCount; i++)
            {
                tags.Add(string.Empty.GetRandom());
            }
            String expected = tags.AsHash();

            String rootPath         = $"c:\\{string.Empty.GetRandom()}";
            var    connectionString = new ConnectionStringBuilder("this")
                                      .AddFilePath(rootPath)
                                      .Build();

            var postJson = new MediaPostBuilder()
                           .UseRandomFlickrPost()
                           .ClearTags()
                           .AddTags(tags)
                           .Build();

            var postFiles = new MockMediaFileCollectionBuilder()
                            .AddPost(postJson)
                            .Build(rootPath);

            var directoryProvider = new MockDirectoryServiceBuilder()
                                    .AddPostFiles(postFiles.Select(f => f.GetFilename()))
                                    .Build(rootPath);

            var fileSystem = new MockFileServiceBuilder()
                             .AddPosts(postFiles)
                             .Build();

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileSystem.Object)
                         .AddDirectoryService(directoryProvider.Object)
                         .Build(connectionString);

            var pages  = target.GetAllPosts();
            var actual = pages.Single().Tags.AsHash();

            Assert.Equal(expected, actual);
        }
        public void ReturnAnEmptyCollectionIfTheFolderDoesNotExist()
        {
            Int32  count        = 25.GetRandom(10);
            String rootPath     = $"c:\\{string.Empty.GetRandom()}";
            String relativePath = string.Empty.GetRandom();
            String folderPath   = Path.Combine(rootPath, relativePath);

            String fakeFolder = String.Empty.GetRandom();
            String fakePath   = Path.Combine(rootPath, fakeFolder);

            var connectionString = new ConnectionStringBuilder("this")
                                   .AddFilePath(rootPath)
                                   .Build();

            var files = new SourceFileCollectionBuilder()
                        .AddRandomFiles(count, relativePath)
                        .Build();

            var fileService = new MockFileServiceBuilder()
                              .AddSourceFiles(files)
                              .Build();

            var directoryService = new MockDirectoryServiceBuilder()
                                   .AddSourceFiles(files)
                                   .Build(rootPath);

            directoryService.Setup(fs => fs.Exists(fakePath)).Returns(false);
            directoryService.Setup(fs => fs.EnumerateFiles(fakeFolder))
            .Throws(new System.IO.DirectoryNotFoundException());

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileService.Object)
                         .AddDirectoryService(directoryService.Object)
                         .Build(connectionString);

            var actual = target.GetFolderContents(fakePath);

            Assert.Empty(actual);
        }
        public void ReturnTheTagFromASingleTagPost()
        {
            String expected = string.Empty.GetRandom();

            String rootPath         = $"c:\\{string.Empty.GetRandom()}";
            var    connectionString = new ConnectionStringBuilder("this")
                                      .AddFilePath(rootPath)
                                      .Build();

            var postJson = new MediaPostBuilder()
                           .UseRandomFlickrPost()
                           .ClearTags()
                           .AddTag(expected)
                           .Build();

            var postFiles = new MockMediaFileCollectionBuilder()
                            .AddPost(postJson)
                            .Build(rootPath);

            var directoryProvider = new MockDirectoryServiceBuilder()
                                    .AddPostFiles(postFiles.Select(f => f.GetFilename()))
                                    .Build(rootPath);

            var fileSystem = new MockFileServiceBuilder()
                             .AddPosts(postFiles)
                             .Build();

            var target = new ContentRepositoryBuilder()
                         .AddFileService(fileSystem.Object)
                         .AddDirectoryService(directoryProvider.Object)
                         .Build(connectionString);

            var pages  = target.GetAllPosts();
            var actual = pages.Single().Tags.Single();

            Assert.Equal(expected, actual);
        }