public void Equals_SuppliedObjectIsOfOtherType_ResultIsFalse() { // Arrange var package1 = new PackageInfo { Id = "Package1 ", Version = "1.0.0" }; object package2 = new object(); // Act bool result = package1.Equals(package2); // Assert Assert.IsFalse(result); }
public void Equals_TwoIdenticalConfigurations_WithDifferentWhitespaces_ResultIsFalse() { // Arrange var package1 = new PackageInfo { Id = "Package1 ", Version = "1.0.0" }; var package2 = new PackageInfo { Id = " Package1", Version = "1.0.0" }; // Act bool result = package1.Equals(package2); // Assert Assert.IsFalse(result); }
public void Equals_SuppliedObjectIsNull_ResultIsFalse() { // Arrange var package1 = new PackageInfo { Id = "Package1 ", Version = "1.0.0" }; PackageInfo package2 = null; // Act bool result = package1.Equals(package2); // Assert Assert.IsFalse(result); }
public void CanBeDeserialized() { // Arrange var package = new PackageInfo { Id = "Package", Version = "1.0.0" }; // Act string json = JsonConvert.SerializeObject(package); var deserializedpackage = JsonConvert.DeserializeObject<PackageInfo>(json); // Assert Assert.AreEqual(package.Id, deserializedpackage.Id); Assert.AreEqual(package.Version, deserializedpackage.Version); }
public void AddOrUpdate_PackageInfoIsInvalid_ResultIsFalse() { // Arrange var packageInfo = new PackageInfo(); var applicationInformation = new ApplicationInformation { ConfigurationFileFolder = Environment.CurrentDirectory }; var packageInfoFilesystemPersistence = new Mock<IFilesystemPersistence<PackageInfo[]>>(); var packageConfigurationAccessor = new PackageConfigurationAccessor(applicationInformation, packageInfoFilesystemPersistence.Object); // Act var result = packageConfigurationAccessor.AddOrUpdate(packageInfo); // Assert Assert.AreEqual(result.Status, ServiceResultType.Failure); }
public void AddOrUpdate_ConfigFileContainsOneEntry_NewPackageHasVersionNumberThatIsWhitespace_ResultContainsOnlyTheOldEntry() { // Arrange string packageId = "Sample.Package"; var previousPackage = new PackageInfo { Id = packageId, Version = "1.0.0" }; this.CreateConfigFile(new[] { previousPackage }); string newVersionNumber = " "; var updatedPackage = new PackageInfo { Id = packageId, Version = newVersionNumber }; // Act this.packageConfigurationAccessor.AddOrUpdate(updatedPackage); // Assert var results = this.packageConfigurationAccessor.GetInstalledPackages(); Assert.AreEqual(1, results.Count()); Assert.AreEqual(previousPackage, results.First()); }
public void AddOrUpdate_PackageListIsEmpty_AddNewPackage_OnePackageIsSaved() { // Arrange var packageInfo = new PackageInfo { Id = "Package.A", Version = "1.0.0.0" }; var applicationInformation = new ApplicationInformation { ConfigurationFileFolder = Environment.CurrentDirectory }; var packageInfoFilesystemPersistence = new Mock<IFilesystemPersistence<PackageInfo[]>>(); var packages = new PackageInfo[] { }; packageInfoFilesystemPersistence.Setup(p => p.Load(It.IsAny<string>())).Returns(packages); var packageConfigurationAccessor = new PackageConfigurationAccessor(applicationInformation, packageInfoFilesystemPersistence.Object); // Act packageConfigurationAccessor.AddOrUpdate(packageInfo); // Assert packageInfoFilesystemPersistence.Verify( p => p.Save(It.Is<PackageInfo[]>(packageList => packageList.Count() == 1 && packageList.Contains(packageInfo)), It.IsAny<string>()), Times.Once()); }
public IServiceResult AddOrUpdate(PackageInfo packageInfo) { if (packageInfo == null) { throw new ArgumentNullException("packageInfo"); } if (!packageInfo.IsValid) { return new FailureResult(Resources.PackageConfigurationAccessor.AddOrUpdateInvalidPackageMessageTemplate, packageInfo); } var packages = this.GetExistingPackageConfigurationList().ToDictionary(p => p.Id, p => p); if (packages.Keys.Any(id => id.Equals(packageInfo.Id, StringComparison.OrdinalIgnoreCase))) { var existingEntry = packages[packageInfo.Id]; // update if (existingEntry.Equals(packageInfo) == false) { packages[packageInfo.Id] = packageInfo; } } else { // add packages.Add(packageInfo.Id, packageInfo); } // sort var packagesSorted = packages.Values.OrderBy(PackageSortKeySelector).ToArray(); // save if (!this.SaveNewPackageConfigurationList(packagesSorted)) { return new FailureResult(Resources.PackageConfigurationAccessor.AddOrUpdateSaveFailedMessageTemplate, packageInfo); } return new SuccessResult(Resources.PackageConfigurationAccessor.AddOrUpdateSucceededMessageTemplate, packageInfo); }
public void GetInstalledPackages_ConfigFileContainsThreeDistinctInvalidEntries_ResultContainsZeroEntries() { // Arrange var package1 = new PackageInfo { Id = "ID1", Version = null }; var package2 = new PackageInfo { Id = "ID2", Version = string.Empty }; var package3 = new PackageInfo { Id = string.Empty, Version = "1.0.0" }; var packages = new[] { package1, package2, package3 }; this.CreateConfigFile(packages); // Act var results = this.packageConfigurationAccessor.GetInstalledPackages(); // Assert Assert.AreEqual(0, results.Count()); }
public void GetInstalledPackages_ConfigFileContainsSusortedEntries_ResultSetIsSortedById() { // Arrange var packageC = new PackageInfo { Id = "C", Version = "1.0.0" }; var packageB = new PackageInfo { Id = "B", Version = "1.0.0" }; var packageA = new PackageInfo { Id = "A", Version = "1.0.0" }; var packageD = new PackageInfo { Id = "D", Version = "1.0.0" }; var packages = new[] { packageC, packageB, packageA, packageD }; this.CreateConfigFile(packages); // Act var results = this.packageConfigurationAccessor.GetInstalledPackages(); // Assert Assert.AreEqual(4, results.Count()); Assert.IsTrue(results.Skip(0).Take(1).First().Equals(packageA)); Assert.IsTrue(results.Skip(1).Take(1).First().Equals(packageB)); Assert.IsTrue(results.Skip(2).Take(1).First().Equals(packageC)); Assert.IsTrue(results.Skip(3).Take(1).First().Equals(packageD)); }
public void GetInstalledPackages_ConfigFileContainsThreeDistinctEntries_ResultContainsThreeEntries() { // Arrange var package1 = new PackageInfo { Id = "ID1", Version = "1.0.0" }; var package2 = new PackageInfo { Id = "ID2", Version = "1.0.0" }; var package3 = new PackageInfo { Id = "ID3", Version = "1.0.0" }; var packages = new[] { package1, package2, package3 }; this.CreateConfigFile(packages); // Act var results = this.packageConfigurationAccessor.GetInstalledPackages(); // Assert Assert.AreEqual(3, results.Count()); Assert.IsTrue(results.Contains(package1)); Assert.IsTrue(results.Contains(package2)); Assert.IsTrue(results.Contains(package3)); }
public void AddOrUpdate_ConfigFileContainsOneEntry_NewPackage_ResultContainsTwoEntries() { // Arrange var previousPackage = new PackageInfo { Id = "ID1", Version = "1.0.0" }; this.CreateConfigFile(new[] { previousPackage }); var newPackage = new PackageInfo { Id = "ID2", Version = "1.0.0" }; // Act this.packageConfigurationAccessor.AddOrUpdate(newPackage); // Assert var results = this.packageConfigurationAccessor.GetInstalledPackages(); Assert.AreEqual(2, results.Count()); Assert.AreEqual(previousPackage, results.Skip(0).Take(1).First()); Assert.AreEqual(newPackage, results.Skip(1).Take(1).First()); }
public void AddOrUpdate_ConfigFileIsEmpty_NewPackage_ResultContainsTheNewEntry() { // Arrange this.CreateConfigFile(new PackageInfo[] { }); var newPackage = new PackageInfo { Id = "ID1", Version = "1.0.0" }; // Act this.packageConfigurationAccessor.AddOrUpdate(newPackage); // Assert var results = this.packageConfigurationAccessor.GetInstalledPackages(); Assert.AreEqual(1, results.Count()); Assert.AreEqual(newPackage, results.First()); }
public void IsValid_IdAndVersionPropertyAreSet_ResultIsTrue(string id, string version) { // Arrange var packageInfo = new PackageInfo { Id = id, Version = version }; // Act bool result = packageInfo.IsValid; // Assert Assert.IsTrue(result); }
public void Equals_TwoIdenticalPackages_ResultIsTrue() { // Arrange var package1 = new PackageInfo { Id = "Package1", Version = "1.0.0" }; var package2 = new PackageInfo { Id = "Package1", Version = "1.0.0" }; // Act bool result = package1.Equals(package2); // Assert Assert.IsTrue(result); }
public void Remove_ConfigFileContainsThreeEntries_SecondEntryIsRemoved_ResultContainsOnlyTwoEntries() { // Arrange var package1 = new PackageInfo { Id = "ID1", Version = "1.0.0" }; var package2 = new PackageInfo { Id = "ID2", Version = "1.0.0" }; var package3 = new PackageInfo { Id = "ID3", Version = "1.0.0" }; var packages = new[] { package1, package2, package3 }; this.CreateConfigFile(packages); // Act this.packageConfigurationAccessor.Remove(package2.Id); // Assert var results = this.packageConfigurationAccessor.GetInstalledPackages(); Assert.AreEqual(2, results.Count()); Assert.AreEqual(package1, results.Skip(0).Take(1).First()); Assert.AreEqual(package3, results.Skip(1).Take(1).First()); }
public void GetHashCode_SameHashCodeIsReturnedEveryTimeTheMethodIsCalled_AsLongAsTheObjectDoesNotChange() { // Arrange var packageId = "Package"; var packageVersion = "1.0.0"; var package = new PackageInfo { Id = packageId, Version = packageVersion }; int expectedHashcode = package.GetHashCode(); for (var i = 0; i < 100; i++) { // Act package.Id = packageId; package.Version = packageVersion; int generatedHashCode = package.GetHashCode(); // Assert Assert.AreEqual(expectedHashcode, generatedHashCode); } }
public void ToString_IdIsSet_VersionIsNotSet_ResultContainsId() { // Arrange var packageInfo = new PackageInfo { Id = "Package" }; // Act string result = packageInfo.ToString(); // Assert Assert.IsTrue(result.Contains(packageInfo.Id)); }
public void ToString_PropertiesAreNotSet_ResultIsTypeName() { // Arrange var packageInfo = new PackageInfo(); // Act string result = packageInfo.ToString(); // Assert Assert.AreEqual(typeof(PackageInfo).Name, result); }
public void ToString_IdIsNotSet_VersionIsSet_ResultIsTypeName() { // Arrange var packageInfo = new PackageInfo { Version = "1.0.0" }; // Act string result = packageInfo.ToString(); // Assert Assert.AreEqual(typeof(PackageInfo).Name, result); }
public void ToString_ContainsVersion() { // Arrange var package = new PackageInfo { Id = "Package", Version = "1.0.0" }; // Act string result = package.ToString(); // Assert Assert.IsTrue(result.Contains(package.Version)); }
public void Serialization_IsValidPropertyIsNotSerialized() { // Arrange var package = new PackageInfo { Id = "Package", Version = "1.0.0" }; // Act string json = JsonConvert.SerializeObject(package); // Assert Assert.IsFalse(json.Contains("IsValid")); Assert.IsFalse(json.Contains(package.IsValid.ToString(CultureInfo.InvariantCulture))); }
public void GetInstalledPackages_ConfigFileContainsTwoIdenticalEntries_ResultContainsOnlyOneEntry() { // Arrange var package1Id = "ID1"; var package1Version = "1.0.0"; var package1 = new PackageInfo { Id = package1Id, Version = package1Version }; var packages = new[] { package1, package1 }; this.CreateConfigFile(packages); // Act var results = this.packageConfigurationAccessor.GetInstalledPackages(); // Assert Assert.IsNotNull(results); Assert.AreEqual(1, results.Count()); Assert.AreEqual(package1Id, results.First().Id); Assert.AreEqual(package1Version, results.First().Version); }
public void GetHashCode_TwoIdenticalObjects_BothUninitialized_HashCodesAreEqual() { // Arrange var package1 = new PackageInfo(); var package2 = new PackageInfo(); // Act int hashCodeObject1 = package1.GetHashCode(); int hashCodeObject2 = package2.GetHashCode(); // Assert Assert.AreEqual(hashCodeObject1, hashCodeObject2); }
public void Remove_ConfigFileContainsThreeEntries_AllThreeEntriesGetRemoved_ResultContainsZeroEntries() { // Arrange var package1 = new PackageInfo { Id = "ID1", Version = "1.0.0" }; var package2 = new PackageInfo { Id = "ID2", Version = "1.0.0" }; var package3 = new PackageInfo { Id = "ID3", Version = "1.0.0" }; var packages = new[] { package1, package2, package3 }; this.CreateConfigFile(packages); // Act this.packageConfigurationAccessor.Remove(package1.Id); this.packageConfigurationAccessor.Remove(package2.Id); this.packageConfigurationAccessor.Remove(package3.Id); // Assert var results = this.packageConfigurationAccessor.GetInstalledPackages(); Assert.AreEqual(0, results.Count()); }
public void IsSerializable() { // Arrange var package = new PackageInfo { Id = "Package", Version = "1.0.0" }; // Act string json = JsonConvert.SerializeObject(package); // Assert Assert.IsTrue(json.Contains(package.Id)); Assert.IsTrue(json.Contains(package.Version)); }
private void CreateConfigFile(PackageInfo[] packageInfos) { string json = JsonConvert.SerializeObject(packageInfos); File.WriteAllText(PackageConfigurationAccessor.PackageConfigurationFileName, json, Encoding.UTF8); }
public void GetHashCode_ForAllUniqueObject_AUniqueHashCodeIsReturned() { var hashCodes = new Dictionary<int, PackageInfo>(); for (var i = 0; i < 10000; i++) { // Act var packageInfo = new PackageInfo { Id = Guid.NewGuid().ToString(), Version = Guid.NewGuid().ToString() }; int generatedHashCode = packageInfo.GetHashCode(); // Assert Assert.IsFalse(hashCodes.ContainsKey(generatedHashCode)); hashCodes.Add(generatedHashCode, packageInfo); } }
public void AddOrUpdate_ConfigFileContainsOneEntry_NewPackageIsUpdatedVersion_ResultContainsOneEntryWithNewVersionNumber() { // Arrange string packageId = "Sample.Package"; var package1 = new PackageInfo { Id = packageId, Version = "1.0.0" }; this.CreateConfigFile(new[] { package1 }); string newVersionNumber = "1.0.1"; var updatedPackage = new PackageInfo { Id = packageId, Version = newVersionNumber }; // Act this.packageConfigurationAccessor.AddOrUpdate(updatedPackage); // Assert var results = this.packageConfigurationAccessor.GetInstalledPackages(); Assert.AreEqual(1, results.Count()); Assert.AreEqual(newVersionNumber, results.First().Version); }
public void GetHashCode_TwoIdenticalObjects_BothInitialized_HashCodesAreEqual() { // Arrange var package1 = new PackageInfo { Id = "Package1", Version = "1.0.0" }; var package2 = new PackageInfo { Id = "Package1", Version = "1.0.0" }; // Act int hashCodeObject1 = package1.GetHashCode(); int hashCodeObject2 = package2.GetHashCode(); // Assert Assert.AreEqual(hashCodeObject1, hashCodeObject2); }