public void Constructor_IPackageRepositoryFactory_CreateRepository_GetsCalled_ForeachEntryInRepositoryConfigurations()
        {
            // Arrange
            bool config1GotCalled = false;
            bool config2GotCalled = false;

            var sourceRepositoryConfiguration1 = new SourceRepositoryConfiguration { Name = "Test Repo 1", Url = new Uri("https://example1.org/api/v2/") };
            var sourceRepositoryConfiguration2 = new SourceRepositoryConfiguration { Name = "Test Repo 2", Url = new Uri("https://example2.org/api/v2/") };

            var sourceRepositoryProviderMock = new Mock<ISourceRepositoryProvider>();
            sourceRepositoryProviderMock.Setup(r => r.GetRepositoryConfigurations()).Returns(new[] { sourceRepositoryConfiguration1, sourceRepositoryConfiguration2 });

            var packageRepositoryFactoryMock = new Mock<IPackageRepositoryFactory>();
            packageRepositoryFactoryMock.Setup(f => f.CreateRepository(sourceRepositoryConfiguration1.Url.ToString())).Returns(
                () =>
                    {
                        config1GotCalled = true;
                        return new Mock<IPackageRepository>().Object;
                    });

            packageRepositoryFactoryMock.Setup(f => f.CreateRepository(sourceRepositoryConfiguration2.Url.ToString())).Returns(
                () =>
                {
                    config2GotCalled = true;
                    return new Mock<IPackageRepository>().Object;
                });

            // Act
            new PackageRepositoryBrowser(sourceRepositoryProviderMock.Object, packageRepositoryFactoryMock.Object);

            // Assert
            Assert.IsTrue(config1GotCalled);
            Assert.IsTrue(config2GotCalled);
        }
        public void Equals_SuppliedObjectIsOfOtherType_ResultIsFalse()
        {
            // Arrange
            var repositoryConfiguration1 = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };
            object repositoryConfiguration2 = new object();

            // Act
            bool result = repositoryConfiguration1.Equals(repositoryConfiguration2);

            // Assert
            Assert.IsFalse(result);
        }
        public void Equals_TwoIdenticalConfigurations_ResultIsTrue()
        {
            // Arrange
            var repositoryConfiguration1 = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };
            var repositoryConfiguration2 = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            bool result = repositoryConfiguration1.Equals(repositoryConfiguration2);

            // Assert
            Assert.IsTrue(result);
        }
        public void CanBeDeserialized()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            string json = JsonConvert.SerializeObject(repositoryConfiguration);
            var deserializedRepositoryConfiguration = JsonConvert.DeserializeObject<SourceRepositoryConfiguration>(json);

            // Assert
            Assert.AreEqual(repositoryConfiguration.Name, deserializedRepositoryConfiguration.Name);
            Assert.AreEqual(repositoryConfiguration.Url, deserializedRepositoryConfiguration.Url);
        }
        public void ToString_UrlIsNull_ResultContainsName()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = "Some Repository", Url = null };

            // Act
            string result = repositoryConfiguration.ToString();

            // Assert
            Assert.IsTrue(result.Contains(repositoryConfiguration.Name));
        }
        public void ToString_PropertiesAreNotSet_ResultIsTypeName()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration();

            // Act
            string result = repositoryConfiguration.ToString();

            // Assert
            Assert.AreEqual(typeof(SourceRepositoryConfiguration).Name, result);
        }
        public void ToString_ContainsRepositoryUrl()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            string result = repositoryConfiguration.ToString();

            // Assert
            Assert.IsTrue(result.Contains(repositoryConfiguration.Url.ToString()));
        }
        public void Serialization_IsValidPropertyIsNotSerialized()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            string json = JsonConvert.SerializeObject(repositoryConfiguration);

            // Assert
            Assert.IsFalse(json.Contains("IsValid"));
            Assert.IsFalse(json.Contains(repositoryConfiguration.IsValid.ToString(CultureInfo.InvariantCulture)));
        }
        public void GetHashCode_ForAllUniqueObject_AUniqueHashCodeIsReturned()
        {
            var hashCodes = new Dictionary<int, SourceRepositoryConfiguration>();

            for (var i = 0; i < 10000; i++)
            {
                // Act
                var repositoryConfiguration = new SourceRepositoryConfiguration
                    { Name = Guid.NewGuid().ToString(), Url = new Uri("http://" + Guid.NewGuid().ToString()) };

                int generatedHashCode = repositoryConfiguration.GetHashCode();

                // Assert
                Assert.IsFalse(hashCodes.ContainsKey(generatedHashCode));
                hashCodes.Add(generatedHashCode, repositoryConfiguration);
            }
        }
        public void IsValid_NameIsWhitespace_ResultIsFalse()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = " ", Url = new Uri("http://example.com") };

            // Act
            bool result = repositoryConfiguration.IsValid;

            // Assert
            Assert.IsFalse(result);
        }
        public void IsValid_NameIsSet_UrlIsSet_ResultIsTrue()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            bool result = repositoryConfiguration.IsValid;

            // Assert
            Assert.IsTrue(result);
        }
        public void IsSerializable()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            string json = JsonConvert.SerializeObject(repositoryConfiguration);

            // Assert
            Assert.IsTrue(json.Contains(repositoryConfiguration.Name));
            Assert.IsTrue(json.Contains(repositoryConfiguration.Url.ToString()));
        }
        public void GetHashCode_TwoIdenticalObjects_BothUninitialized_HashCodesAreEqual()
        {
            // Arrange
            var repositoryConfiguration1 = new SourceRepositoryConfiguration();
            var repositoryConfiguration2 = new SourceRepositoryConfiguration();

            // Act
            int hashCodeObject1 = repositoryConfiguration1.GetHashCode();
            int hashCodeObject2 = repositoryConfiguration2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
        public void GetHashCode_TwoIdenticalObjects_BothInitialized_HashCodesAreEqual()
        {
            // Arrange
            var repositoryConfiguration1 = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };
            var repositoryConfiguration2 = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            int hashCodeObject1 = repositoryConfiguration1.GetHashCode();
            int hashCodeObject2 = repositoryConfiguration2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
        public void GetHashCode_SameHashCodeIsReturnedEveryTimeTheMethodIsCalled_AsLongAsTheObjectDoesNotChange()
        {
            // Arrange
            string repositoryName = "Some Repository";
            string repositoryUrl = "http://example.com";
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = repositoryName, Url = new Uri(repositoryUrl) };

            int expectedHashcode = repositoryConfiguration.GetHashCode();

            for (var i = 0; i < 100; i++)
            {
                // Act
                repositoryConfiguration.Name = repositoryName;
                repositoryConfiguration.Url = new Uri(repositoryUrl);
                int generatedHashCode = repositoryConfiguration.GetHashCode();

                // Assert
                Assert.AreEqual(expectedHashcode, generatedHashCode);
            }
        }
        public void IsValid_PropertyIsNotSerialed()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            string json = JsonConvert.SerializeObject(repositoryConfiguration);

            // Assert
            string isValidPropertyName =
                typeof(SourceRepositoryConfiguration).GetProperties().First(p => p.Name.Equals("isvalid", StringComparison.OrdinalIgnoreCase)).Name;

            Assert.IsFalse(json.ToLower().Contains(isValidPropertyName.ToLower()));
        }
        public void RepositoryConfigurations_ReturnsAllConfigurationsProvidedByTheSourceRepositoryProvider()
        {
            // Arrange
            var sourceRepositoryConfiguration1 = new SourceRepositoryConfiguration { Name = "Test Repo 1", Url = new Uri("https://example1.org/api/v2/") };
            var sourceRepositoryConfiguration2 = new SourceRepositoryConfiguration { Name = "Test Repo 2", Url = new Uri("https://example2.org/api/v2/") };

            var repositoryConfigurations = new List<SourceRepositoryConfiguration> { sourceRepositoryConfiguration1, sourceRepositoryConfiguration2 };

            var sourceRepositoryProviderMock = new Mock<ISourceRepositoryProvider>();
            sourceRepositoryProviderMock.Setup(r => r.GetRepositoryConfigurations()).Returns(repositoryConfigurations);

            var packageRepositoryFactoryMock = new Mock<IPackageRepositoryFactory>();
            packageRepositoryFactoryMock.Setup(f => f.CreateRepository(It.IsAny<string>())).Returns(new Mock<IPackageRepository>().Object);

            // Act
            var packageRepositoryBrowser = new PackageRepositoryBrowser(sourceRepositoryProviderMock.Object, packageRepositoryFactoryMock.Object);

            // Assert
            Assert.AreEqual(repositoryConfigurations, packageRepositoryBrowser.RepositoryConfigurations.ToList());
        }
        public void IsValid_UrlIsNull_ResultIsFalse()
        {
            // Arrange
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = "Some Repository", Url = null };

            // Act
            bool result = repositoryConfiguration.IsValid;

            // Assert
            Assert.IsFalse(result);
        }
 private bool SaveNewSourceRepositoryConfigurationList(SourceRepositoryConfiguration[] repositoriesConfiguration)
 {
     return this.filesystemPersistence.Save(repositoriesConfiguration, this.sourceConfigurationFilePath);
 }
        public void ResetRepositoryConfiguration()
        {
            // Arrange
            var config = new SourceRepositoryConfiguration { Name = "Test Repo j32kl4jkl12j4kl32j4klj32", Url = new Uri("C:\\local-test-repo") };
            this.sourceRepositoryProvider.SaveRepositoryConfiguration(config.Name, config.Url.ToString());
            string fileContentBefore = File.ReadAllText(ConfigFileSourceRepositoryProvider.SourceRepositoryConfigurationFileName);

            // Act
            this.sourceRepositoryProvider.ResetRepositoryConfiguration();

            // Assert
            string fileContentAfter = File.ReadAllText(ConfigFileSourceRepositoryProvider.SourceRepositoryConfigurationFileName);
            Assert.AreNotEqual(fileContentBefore, fileContentAfter);
            Assert.IsFalse(this.sourceRepositoryProvider.GetRepositoryConfigurations().Any(c => c.Name.Equals(config.Name)));
        }