public NpmRepositoryTest()
        {
            RepoUrl = Environment.GetEnvironmentVariable(EnvVar_Url);
            ApiKey  = Environment.GetEnvironmentVariable(EnvVar_ApiKey);
            Assert.True(!string.IsNullOrEmpty(RepoUrl), $"No environment variable found for {EnvVar_Url}");
            Assert.True(!string.IsNullOrEmpty(ApiKey), $"No environment variable found for {EnvVar_ApiKey}");

            ServiceProvider = new ServiceCollection()
                              .AddLogging(loggingBuilder => { loggingBuilder.AddDebug(); })
                              .BuildServiceProvider();

            LoggerFactory = ServiceProvider.GetRequiredService <ILoggerFactory>();

            var sourceRepoFeedConfig = new Settings.Feed
            {
                Name = "packagerepo.npm.test-source",
            };

            sourceRepoFeedConfig.Settings.Add("Uri", (RepoUrl.EndsWith("/") ? RepoUrl.Substring(0, RepoUrl.Length - 1) : RepoUrl) + "-source/");
            sourceRepoFeedConfig.Settings.Add("ApiKey", ApiKey);

            var targetRepoFeedConfig = new Settings.Feed
            {
                Name = "packagerepo.npm.test",
            };

            targetRepoFeedConfig.Settings.Add("Uri", RepoUrl);
            targetRepoFeedConfig.Settings.Add("ApiKey", ApiKey);

            SourceRepo = new NpmRepository(sourceRepoFeedConfig, LoggerFactory);
            TargetRepo = new NpmRepository(targetRepoFeedConfig, LoggerFactory);
        }
        public void Test_DirectoryRepositoryFactory()
        {
            var appSettings = new Settings.ApplicationSettings();
            var factory     = new DirectoryRepositoryFactory(appSettings, ServiceProvider, LoggerFactory);
            var feed        = new Settings.Feed {
                Name = "", Type = "Directory"
            };

            var sut = factory.Create(feed);

            Assert.Equal("Directory", sut.RepositoryType);
        }
        public async Task Test_NpmRepository_Feed_Access_Forbidden()
        {
            var repoFeedConfig = new Settings.Feed
            {
                Name = "npm.test",
            };

            repoFeedConfig.Settings.Add("Uri", RepoUrl);
            var sourceRepo = new NpmRepository(repoFeedConfig, LoggerFactory);

            await Assert.ThrowsAsync <WebException>(async() => await sourceRepo.ListAsync(null, false, false).ToListAsync());
        }
        public async Task Test_NpmRepository_Uri_Doesnt_Exist()
        {
            var repoFeedConfig = new Settings.Feed
            {
                Name = "npm.notfound"
            };

            repoFeedConfig.Settings.Add("Uri", "http://www.somedummywebsiteurl.com/");

            var sourceRepo = new NpmRepository(repoFeedConfig, LoggerFactory);

            await Assert.ThrowsAsync <WebException>(async() => await sourceRepo.ListAsync(null, false, false).ToListAsync());
        }
        public async Task Test_ProgetRepository_Uri_Doesnt_Exist()
        {
            var repoFeedConfig = new Settings.Feed
            {
                Name = "choco.notfound",
            };

            repoFeedConfig.Settings.Add("Uri", RepoUrl.TrimEnd('/') + "-not-found");

            var sourceRepo = new ProgetRepository(repoFeedConfig, LoggerFactory);

            await Assert.ThrowsAsync <WebException>(async() => await sourceRepo.ListAsync(NotepadPlusPlusPackageId, false, true).ToListAsync());
        }
        public async Task Test_DirectoryRepository_Fetch_Package_Not_Found()
        {
            var localRepoFeedConfig = new Settings.Feed
            {
                Name = "local.choco",
            };

            localRepoFeedConfig.Settings.Add("Uri", LocalRepoFolder);

            var sourceRepo = new DirectoryRepository(localRepoFeedConfig, LoggerFactory);

            Assert.Null(await sourceRepo.FetchAsync("PackageJunkName", "1.0.0"));
        }
        public async Task Test_NpmRepository_Feed_Doesnt_Exist()
        {
            var repoFeedConfig = new Settings.Feed
            {
                Name = "npm.notfound"
            };

            repoFeedConfig.Settings.Add("Uri", RepoUrl + "npm.notfound");
            repoFeedConfig.Settings.Add("ApiKey", ApiKey);

            var sourceRepo = new NpmRepository(repoFeedConfig, LoggerFactory);

            await Assert.ThrowsAsync <WebException>(async() => await sourceRepo.ListAsync(null, false, false).ToListAsync());
        }
        public async Task Test_DirectoryRepository_Uri_Doesnt_Exist()
        {
            var localRepoFeedConfig = new Settings.Feed
            {
                Name = "local.choco.notfound",
            };

            localRepoFeedConfig.Settings.Add("Uri", LocalRepoFolder + "-not-found");

            var sourceRepo = new DirectoryRepository(localRepoFeedConfig, LoggerFactory);

            var packages = await sourceRepo.ListAsync(NotepadPlusPlusPackageId, false, true).ToListAsync();

            Assert.Empty(packages);
        }
        public async Task Test_ProgetRepository_Copy_And_Delete_Packages()
        {
            var localRepoFeedConfig = new Settings.Feed
            {
                Name = "local.choco",
            };

            localRepoFeedConfig.Settings.Add("Uri", LocalRepoFolder);

            var targetRepoFeedConfig = new Settings.Feed
            {
                Name = "nuget.test",
            };

            targetRepoFeedConfig.Settings.Add("Uri", RepoUrl);
            targetRepoFeedConfig.Settings.Add("ApiKey", ApiKey);

            var sourceRepo = new DirectoryRepository(localRepoFeedConfig, LoggerFactory);
            var targetRepo = new ProgetRepository(targetRepoFeedConfig, LoggerFactory);

            var sourcePackages = await sourceRepo.ListAsync(NotepadPlusPlusPackageId, false, true).ToListAsync();

            foreach (var package in sourcePackages)
            {
                var p = await sourceRepo.FetchAsync(package.Id, package.Version);

                Assert.NotNull(p.Content);
                await targetRepo.AddAsync(p);
            }

            var targetPackages = await targetRepo.ListAsync(NotepadPlusPlusPackageId, false, true).ToListAsync();

            Assert.Equal(NotepadPlusPlusPackageCount, sourcePackages.Count);
            Assert.Equal(NotepadPlusPlusPackageCount, targetPackages.Count);

            foreach (var targetPackage in targetPackages)
            {
                var targetPackageWithContent = await targetRepo.FetchAsync(targetPackage.Id, targetPackage.Version);

                Assert.NotNull(targetPackageWithContent.Content);
                await targetRepo.DeleteAsync(targetPackage.Id, targetPackage.Version);
            }

            targetPackages = await targetRepo.ListAsync(NotepadPlusPlusPackageId, false, true).ToListAsync();

            Assert.Empty(targetPackages);
        }
        public async Task Test_DirectoryRepository_List()
        {
            var localRepoFeedConfig = new Settings.Feed
            {
                Name = "local.choco",
            };

            localRepoFeedConfig.Settings.Add("Uri", LocalRepoFolder);

            var sourceRepo = new DirectoryRepository(localRepoFeedConfig, LoggerFactory);

            var packages = await sourceRepo.ListAsync(NotepadPlusPlusPackageId, false, true).ToListAsync();

            var packagesPrerelease          = (await sourceRepo.ListAsync(NotepadPlusPlusPackageId, true, true).ToListAsync()).Where(p => p.IsPrerelease).ToList();
            var packagesIncludingPrerelease = await sourceRepo.ListAsync(NotepadPlusPlusPackageId, true, true).ToListAsync();

            Assert.Equal(NotepadPlusPlusPackageCount, packages.Count);
            Assert.Single(packagesPrerelease);
            Assert.Equal(NotepadPlusPlusPackageCount + NotepadPlusPlusPrereleasePackageCount, packagesIncludingPrerelease.Count);
        }
        public async Task Test_ProgetRepository_Fetch_Package_Not_Found()
        {
            var sourceRepoFeedConfig = new Settings.Feed
            {
                Name = "nuget.test",
            };

            sourceRepoFeedConfig.Settings.Add("Uri", RepoUrl);
            sourceRepoFeedConfig.Settings.Add("ApiKey", ApiKey);

            var sourceRepo = new ProgetRepository(sourceRepoFeedConfig, LoggerFactory);

            try
            {
                Assert.Null(await sourceRepo.FetchAsync("PackageJunkName", "1.0.0"));
            }
            catch (Exception)
            {
                // Expected
            }
        }