public void Make_sure_duplicate_file_names_are_extended_with_suffix()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();
            // Pick a directory and add files with same name
            var dir = package.Folders.FirstOrDefault(f => f.Id == "Dir00000032");

            dir.Files.AddRange(new[]
            {
                new RepositoryFile {
                    Id = "T1", LogicalName = "p1.pdf", PhysicalName = "p1.pdf"
                },
                new RepositoryFile {
                    Id = "T2", LogicalName = "p1.pdf", PhysicalName = "p1.pdf"
                },
                new RepositoryFile {
                    Id = "T3", LogicalName = "p1.pdf", PhysicalName = "p1.pdf"
                }
            });

            // Act
            validator.EnsureValidPhysicalFileAndFolderNames(package, @"C:\Users\jlang\Downloads\Repository");

            // Assert
            var flatList = validator.ConvertToRepositoryObject(package);

            flatList.First(f => f.RepositoryId == "T1").Name.Should().Be("p1.pdf");
            flatList.First(f => f.RepositoryId == "T2").Name.Should().Be("p1_1.pdf");
            flatList.First(f => f.RepositoryId == "T3").Name.Should().Be("p1_2.pdf");
        }
        public void Make_sure_very_long_duplicate_file_names_on_deep_nested_folders_are_extended_with_suffix()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();

            // Get to the lowest level folder
            var dir = package.Folders.First();

            while (dir.Folders.Any())
            {
                dir = dir.Folders.First();
            }

            // Add duplicate files too long names
            dir.Files.AddRange(new[]
            {
                new RepositoryFile
                {
                    Id          = "T1",
                    LogicalName =
                        "this is a very long name for the pdf file that might lead to problems when saving the content to disk and might be problematic when creating the package.pdf",
                    PhysicalName =
                        "this is a very long name for the pdf file that might lead to problems when saving the content to disk and might be problematic when creating the package.pdf"
                },
                new RepositoryFile
                {
                    Id          = "T2",
                    LogicalName =
                        "this is a very long name for the pdf file that might lead to problems when saving the content to disk and might be problematic when creating the package.pdf",
                    PhysicalName =
                        "this is a very long name for the pdf file that might lead to problems when saving the content to disk and might be problematic when creating the package.pdf"
                },
                new RepositoryFile
                {
                    Id          = "T3",
                    LogicalName =
                        "this is a very long name for the pdf file that might lead to problems when saving the content to disk and might be problematic when creating the package.pdf",
                    PhysicalName =
                        "this is a very long name for the pdf file that might lead to problems when saving the content to disk and might be problematic when creating the package.pdf"
                }
            });

            // Act
            validator.EnsureValidPhysicalFileAndFolderNames(package, @"C:\Users\jlang\Downloads\Repository");

            // Assert
            var flatList = validator.ConvertToRepositoryObject(package);

            flatList.First(f => f.RepositoryId == "T1").Name.Should().Be("this is a very long name for.pdf");
            flatList.First(f => f.RepositoryId == "T2").Name.Should().Be("this is a very long name for_1.pdf");
            flatList.First(f => f.RepositoryId == "T3").Name.Should().Be("this is a very long name for_2.pdf");
        }
        public void Make_sure_a_normal_root_path_shortens_max_path_length_to_exactly_200_chars()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();

            // Act
            validator.EnsureValidPhysicalFileAndFolderNames(package, @"C:\Users\jlang\Downloads\Repository\");
            var list = validator.ConvertToRepositoryObject(package);

            // Assert
            validator.MaxPathLength.Should().Be(200);
        }
        public void Make_sure_too_long_path_names_are_shortened()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();

            // Act
            validator.EnsureValidPhysicalFileAndFolderNames(package, @"C:\Users\jlang\Downloads\Repository");
            var list = validator.ConvertToRepositoryObject(package);

            // Assert
            (list.Max(l => l.FullName.Length) > validator.MaxPathLength).Should().BeFalse();
        }
        public void Make_sure_a_long_root_path_shortens_max_path_length_below_200_chars()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();

            // Act
            validator.EnsureValidPhysicalFileAndFolderNames(package,
                                                            @"C:\Users\jlang\Downloads\Repository\with a very long path name\that exceeds 60 chars\just for testing");
            var list = validator.ConvertToRepositoryObject(package);

            // Assert
            validator.MaxPathLength.Should().BeLessThan(200);
        }
        public void Make_sure_we_have_too_long_path_names_correct()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();

            validator.CreateValidNames(package);

            // Act
            var list = validator.ConvertToRepositoryObject(package);

            // Assert
            (list.Max(l => l.FullName.Length) > validator.MaxPathLength).Should().BeTrue();
        }
        public void Update_package_file_with_new_name()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();

            // Act
            validator.UpdatePackage(package, new KeyValuePair <string, string>("F00000119", "Happy"));


            // Assert
            var flatList = validator.ConvertToRepositoryObject(package);

            flatList.First(f => f.RepositoryId == "F00000119").Name.Should().Be("Happy");
        }
        public void Make_sure_trainling_dots_at_the_end_of_names_are_removed()
        {
            // Arrange
            var validator = new PackageValidator();
            // file 119 and Dir 5 have illegal endings in test file
            var package = CreateTestData();

            // Act
            validator.EnsureValidPhysicalFileAndFolderNames(package, @"C:\Users\jlang\Downloads\Repository");

            // Assert
            var flatList = validator.ConvertToRepositoryObject(package);

            flatList.First(f => f.RepositoryId == "F00000119").Name.Should().Be("TABLE BROWN.pdf");
            flatList.First(f => f.RepositoryId == "Dir00000005").Name.Should().Be("Zjing Oil");
        }
        public void Make_sure_duplicate_file_names_are_extended_with_suffix2()
        {
            // Arrange
            var validator = new PackageValidator();

            // Sample Package has one duplicate file "\PLUS_3\PLUS3_Bern_V1\P0.pdf"
            var packageTestData = File.ReadAllText(Path.Combine(TestContext.CurrentContext.TestDirectory, "packageSampleData.json"), Encoding.UTF8);
            var package         = JsonConvert.DeserializeObject <RepositoryPackage>(packageTestData);

            // Act
            validator.EnsureValidPhysicalFileAndFolderNames(package, @"C:\Users\jlang\Downloads\Repository");

            // Assert
            var flatList = validator.ConvertToRepositoryObject(package);

            flatList.First(f => f.RepositoryId == "sdb:digitalFile|685fbf49-d67c-4f60-9842-96c35d009210").Name.Should().Be("P0_1.pdf");
        }
        public void Make_sure_object_count_of_temp_object_is_correct()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();

            validator.CreateValidNames(package);

            // Act
            var list = validator.ConvertToRepositoryObject(package);

            // Assert
            var numFolders = 2 + 2 * 2 + 4 * 2 + 8 * 2 + 16 * 2;
            var numFiles   = 2 + 2 * 2 + 4 * 2 + 8 * 2 + 16 * 2 + 32 * 2;

            list.Count.Should().Be(numFolders + numFiles);
            list.Count(f => f.Type == TempValidationObjectType.File).Should().Be(numFiles);
            list.Count(f => f.Type == TempValidationObjectType.Folder).Should().Be(numFolders);
            list.Max(l => l.HierachyLevel).Should().Be(MaxLevel + 1); // 5 Folder levels + 1 File Level
        }
        public void Make_sure_duplicate_file_names_on_deep_nested_folders_with_illegal_chars_are_extended_with_suffix()
        {
            // Arrange
            var validator = new PackageValidator();
            var package   = CreateTestData();

            // Get to the lowest level folder
            var dir = package.Folders.First();

            while (dir.Folders.Any())
            {
                dir = dir.Folders.First();
            }

            // Add duplicate files with illegal chars
            dir.Files.AddRange(new[]
            {
                new RepositoryFile {
                    Id = "T1", LogicalName = "p??1.pdf", PhysicalName = "p??1.pdf"
                },
                new RepositoryFile {
                    Id = "T2", LogicalName = "p??1.pdf", PhysicalName = "p??1.pdf"
                },
                new RepositoryFile {
                    Id = "T3", LogicalName = "p??1.pdf", PhysicalName = "p??1.pdf"
                }
            });

            // Act
            validator.EnsureValidPhysicalFileAndFolderNames(package, @"C:\Users\jlang\Downloads\Repository");

            // Assert
            var flatList = validator.ConvertToRepositoryObject(package);

            flatList.First(f => f.RepositoryId == "T1").Name.Should().Be("p__1.pdf");
            flatList.First(f => f.RepositoryId == "T2").Name.Should().Be("p__1_1.pdf");
            flatList.First(f => f.RepositoryId == "T3").Name.Should().Be("p__1_2.pdf");
        }