// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Linq; using Core.Common.Base; using NUnit.Framework; using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.TestUtil; using Ringtoets.Common.Data.UpdateDataStrategies; namespace Ringtoets.Common.Data.Test.UpdateDataStrategies { [TestFixture] public class UpdateDataStrategyBaseTest { private const string sourceFilePath = "path"; [Test] public void DefaultConstructor_FailureMechanismNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => new ConcreteUpdateDataStrategy(null); // Assert string paramName = Assert.Throws(call).ParamName; Assert.AreEqual("failureMechanism", paramName); } [Test] public void DefaultConstructor_EqualityComparerNull_ThrowsArgumentNullException() { // Call TestDelegate call = () => new ConcreteUpdateDataStrategy(new TestFailureMechanism(), null); // Assert string paramName = Assert.Throws(call).ParamName; Assert.AreEqual("equalityComparer", paramName); } [Test] public void DefaultConstructor_FailureMechanismNotNull_DoesNotThrowException() { // Call TestDelegate call = () => new ConcreteUpdateDataStrategy(new TestFailureMechanism()); // Assert Assert.DoesNotThrow(call); } [Test] public void UpdateTargetCollectionData_TargetCollectionNull_ThrowsArgumentNullException() { // Setup var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); // Call TestDelegate call = () => strategy.ConcreteUpdateData(null, Enumerable.Empty(), string.Empty); // Assert string paramName = Assert.Throws(call).ParamName; Assert.AreEqual("targetDataCollection", paramName); } [Test] public void UpdateTargetCollectionData_ImportedDataCollectionNull_ThrowsArgumentNullException() { // Setup var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); var collection = new TestUniqueItemCollection(); // Call TestDelegate call = () => strategy.ConcreteUpdateData(collection, null, string.Empty); // Assert string paramName = Assert.Throws(call).ParamName; Assert.AreEqual("importedDataCollection", paramName); } [Test] public void UpdateTargetCollectionData_SourceFilePathNull_ThrowsArgumentNullException() { // Setup var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); var collection = new TestUniqueItemCollection(); // Call TestDelegate call = () => strategy.ConcreteUpdateData(collection, Enumerable.Empty(), null); // Assert string paramName = Assert.Throws(call).ParamName; Assert.AreEqual("sourceFilePath", paramName); } [Test] public void UpdateTargetCollectionData_WithEmptyCollectionAndImportedData_DoesNothing() { // Setup var collection = new TestUniqueItemCollection(); const string filePath = "path"; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); // Call IEnumerable affectedObjects = strategy.ConcreteUpdateData(collection, Enumerable.Empty(), filePath); // Assert Assert.IsFalse(strategy.IsUpdateDataCalled); Assert.IsFalse(strategy.IsRemoveObjectAndDependentDataCalled); CollectionAssert.IsEmpty(affectedObjects); CollectionAssert.IsEmpty(collection); Assert.AreEqual(filePath, collection.SourcePath); } [Test] public void UpdateTargetCollectionData_WithNonEmptyCollectionAndImportedDataEmpty_ClearsTargetCollection() { // Setup var collection = new TestUniqueItemCollection(); const string filePath = "path"; var itemsRemoved = new[] { new TestItem("Name A"), new TestItem("Name B") }; collection.AddRange(itemsRemoved, filePath); var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); strategy.ItemsToRemove = itemsRemoved; // Call IEnumerable affectedObjects = strategy.ConcreteUpdateData(collection, Enumerable.Empty(), filePath); // Assert Assert.IsFalse(strategy.IsUpdateDataCalled); Assert.IsTrue(strategy.IsRemoveObjectAndDependentDataCalled); CollectionAssert.IsEmpty(strategy.UpdateDataCallArguments); int nrOfExpectedRemovedDataCalls = itemsRemoved.Length; List> removeDataCallArguments = strategy.RemoveDataCallArguments; Assert.AreEqual(nrOfExpectedRemovedDataCalls, removeDataCallArguments.Count); for (var i = 0; i < nrOfExpectedRemovedDataCalls; i++) { Assert.AreSame(itemsRemoved[i], removeDataCallArguments[i].Item1); } IEnumerable expectedAffectedObjects = itemsRemoved.Concat(new IObservable[] { collection }); CollectionAssert.AreEquivalent(expectedAffectedObjects, affectedObjects); CollectionAssert.IsEmpty(collection); Assert.AreEqual(filePath, collection.SourcePath); } [Test] public void UpdateTargetCollectionData_WithEmptyCollectionAndImportedDataCollectionNotEmpty_AddsNewItems() { // Setup var collection = new TestUniqueItemCollection(); var importedItems = new[] { new TestItem("Name A"), new TestItem("Name B") }; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); // Call IEnumerable affectedObjects = strategy.ConcreteUpdateData(collection, importedItems, sourceFilePath); // Assert Assert.IsFalse(strategy.IsUpdateDataCalled); Assert.IsFalse(strategy.IsRemoveObjectAndDependentDataCalled); CollectionAssert.IsEmpty(strategy.UpdateDataCallArguments); CollectionAssert.IsEmpty(strategy.RemoveDataCallArguments); CollectionAssert.AreEqual(importedItems, collection); CollectionAssert.AreEquivalent(new[] { collection }, affectedObjects); } [Test] public void UpdateTargetCollectionData_ImportedDataContainsDuplicateData_ThrowsUpdateDataException() { // Setup var collection = new TestUniqueItemCollection(); var testItem = new TestItem("I am an expected item"); collection.AddRange(new[] { testItem }, sourceFilePath); const string duplicateName = "Duplicate Name"; var importedCollection = new[] { new TestItem(duplicateName), new TestItem(duplicateName) }; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); // Call TestDelegate call = () => strategy.ConcreteUpdateData(collection, importedCollection, sourceFilePath); // Assert var exception = Assert.Throws(call); const string message = "Geïmporteerde data moet unieke elementen bevatten."; Assert.AreEqual(message, exception.Message); CollectionAssert.AreEqual(new[] { testItem }, collection); } [Test] public void UpdateTargetCollectionData_CollectionNotEmptyAndImportedDataFullyOverlaps_UpdatesCollection() { // Setup const string itemOneName = "Item one"; const string itemTwoName = "Item Two"; var currentCollection = new[] { new TestItem(itemOneName), new TestItem(itemTwoName) }; var collection = new TestUniqueItemCollection(); collection.AddRange(currentCollection, sourceFilePath); var importedItems = new[] { currentCollection[0].DeepClone(), currentCollection[1].DeepClone() }; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); strategy.ItemsToUpdate = currentCollection; // Call IEnumerable affectedObjects = strategy.ConcreteUpdateData(collection, importedItems, sourceFilePath); // Assert Assert.IsTrue(strategy.IsUpdateDataCalled); Assert.IsFalse(strategy.IsRemoveObjectAndDependentDataCalled); int expectedNrOfUpdateCalls = currentCollection.Length; List> updateArgumentCalls = strategy.UpdateDataCallArguments; Assert.AreEqual(currentCollection.Length, updateArgumentCalls.Count); for (var i = 0; i < expectedNrOfUpdateCalls; i++) { Assert.AreSame(currentCollection[i], updateArgumentCalls[i].Item1); Assert.AreSame(importedItems[i], updateArgumentCalls[i].Item2); } CollectionAssert.IsEmpty(strategy.RemoveDataCallArguments); CollectionAssert.AreEqual(currentCollection, collection); CollectionAssert.AreEqual(new IObservable[] { collection, currentCollection[0], currentCollection[1] }, affectedObjects); } [Test] public void UpdateTargetCollectionData_CollectionNotEmptyAndNoPathAndImportedDataFullyOverlaps_UpdatesCollectionAndFilePath() { // Setup const string itemOneName = "Item one"; const string itemTwoName = "Item Two"; var currentCollection = new[] { new TestItem(itemOneName), new TestItem(itemTwoName) }; var collection = new TestUniqueItemCollection(); collection.AddRange(currentCollection, "Onbekend"); var importedItems = new[] { currentCollection[0].DeepClone(), currentCollection[1].DeepClone() }; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); strategy.ItemsToUpdate = currentCollection; const string newSourceFilePath = "Something/Different/From/Onbekend"; // Call IEnumerable affectedObjects = strategy.ConcreteUpdateData(collection, importedItems, newSourceFilePath); // Assert Assert.IsTrue(strategy.IsUpdateDataCalled); Assert.IsFalse(strategy.IsRemoveObjectAndDependentDataCalled); Assert.AreEqual(newSourceFilePath, collection.SourcePath); CollectionAssert.AreEqual(currentCollection, collection); CollectionAssert.AreEqual(new IObservable[] { collection, currentCollection[0], currentCollection[1] }, affectedObjects); } [Test] public void UpdateTargetCollectionData_CollectionNotEmptyAndImportedDataPartiallyOverlaps_UpdatesCollection() { // Setup var itemToUpdate = new TestItem("Item one"); var itemToRemove = new TestItem("Item Two"); var collection = new TestUniqueItemCollection(); collection.AddRange(new[] { itemToUpdate, itemToRemove }, sourceFilePath); var itemToAdd = new TestItem("Item Four"); var importedItems = new[] { itemToUpdate.DeepClone(), itemToAdd }; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); strategy.ItemsToUpdate = new[] { itemToUpdate }; strategy.ItemsToRemove = new[] { itemToRemove }; // Call IEnumerable affectedObjects = strategy.ConcreteUpdateData(collection, importedItems, sourceFilePath); // Assert Assert.IsTrue(strategy.IsUpdateDataCalled); Assert.IsTrue(strategy.IsRemoveObjectAndDependentDataCalled); const int expectedNrOfUpdateCalls = 1; List> updateDataCallArguments = strategy.UpdateDataCallArguments; Assert.AreEqual(expectedNrOfUpdateCalls, updateDataCallArguments.Count); Assert.AreSame(itemToUpdate, updateDataCallArguments[0].Item1); Assert.AreSame(importedItems[0], updateDataCallArguments[0].Item2); List> removeDataCallArguments = strategy.RemoveDataCallArguments; Assert.AreEqual(1, removeDataCallArguments.Count); Assert.AreSame(itemToRemove, removeDataCallArguments[0].Item1); var expectedCollection = new[] { itemToUpdate, itemToAdd }; CollectionAssert.AreEqual(expectedCollection, collection); CollectionAssert.AreEquivalent(new IObservable[] { itemToUpdate, itemToRemove, collection }, affectedObjects); } [Test] public void UpdateTargetCollectionData_CollectionNotEmptyAndImportedDataDoesNotOverlap_UpdatesCollection() { // Setup var currentCollection = new[] { new TestItem("Item one"), new TestItem("Item two") }; var collection = new TestUniqueItemCollection(); collection.AddRange(currentCollection, sourceFilePath); var importedItems = new[] { new TestItem("Item three"), new TestItem("Item four") }; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); strategy.ItemsToRemove = currentCollection; // Call IEnumerable affectedObjects = strategy.ConcreteUpdateData(collection, importedItems, sourceFilePath); // Assert Assert.IsFalse(strategy.IsUpdateDataCalled); Assert.IsTrue(strategy.IsRemoveObjectAndDependentDataCalled); CollectionAssert.AreEqual(importedItems, collection); IEnumerable expectedAffectedObjects = currentCollection.Concat(new IObservable[] { collection }); CollectionAssert.AreEquivalent(expectedAffectedObjects, affectedObjects); CollectionAssert.IsEmpty(strategy.UpdateDataCallArguments); int nrOfExpectedRemoveCalls = currentCollection.Length; List> removeDataCallArguments = strategy.RemoveDataCallArguments; Assert.AreEqual(nrOfExpectedRemoveCalls, removeDataCallArguments.Count); for (var i = 0; i < nrOfExpectedRemoveCalls; i++) { Assert.AreSame(currentCollection[i], removeDataCallArguments[i].Item1); } } [Test] public void UpdateTargetCollectionData_CollectionNotEmptyAndImportedDataHasDuplicateDefinitions_ThrowsUpdateDataException() { // Setup const string name = "Double Defined Name"; var currentCollection = new[] { new TestItem(name) }; var collection = new TestUniqueItemCollection(); collection.AddRange(currentCollection, sourceFilePath); var importedItems = new[] { new TestItem(name), new TestItem(name) }; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()); // Call TestDelegate call = () => strategy.ConcreteUpdateData(collection, importedItems, sourceFilePath); // Assert var exception = Assert.Throws(call); Assert.AreEqual("Geïmporteerde data moet unieke elementen bevatten.", exception.Message); } [Test] public void UpdateTargetCollectionData_CalledWithSameObjectReferences_ReturnsOnlyDistinctObjects() { // Setup var itemOne = new TestItem("Item one"); var itemTwo = new TestItem("Item two"); var currentCollection = new[] { itemOne, itemTwo }; var collection = new TestUniqueItemCollection(); collection.AddRange(currentCollection, "path"); var importedItems = new[] { itemOne, itemTwo }; var strategy = new ConcreteUpdateDataStrategy(new TestFailureMechanism()) { ItemsToUpdate = currentCollection, ItemsToUpdateFrom = importedItems }; // Call IEnumerable affectedObjects = strategy.ConcreteUpdateData(collection, importedItems, "path"); IEnumerable expectedAffectedObjects = new IObservable[] { collection, itemOne, itemTwo }; CollectionAssert.AreEqual(expectedAffectedObjects, affectedObjects); } private class ConcreteUpdateDataStrategy : UpdateDataStrategyBase { public ConcreteUpdateDataStrategy(TestFailureMechanism failureMechanism, IEqualityComparer comparer) : base(failureMechanism, comparer) {} public ConcreteUpdateDataStrategy(TestFailureMechanism failureMechanism) : base(failureMechanism, new NameComparer()) {} public bool IsUpdateDataCalled { get; private set; } public bool IsRemoveObjectAndDependentDataCalled { get; private set; } public IEnumerable ItemsToUpdate { private get; set; } = Enumerable.Empty(); public IEnumerable ItemsToUpdateFrom { private get; set; } = Enumerable.Empty(); public IEnumerable ItemsToRemove { private get; set; } = Enumerable.Empty(); /// /// Keeps track of which arguments were used when the is called. /// public List> UpdateDataCallArguments { get; } = new List>(); /// /// Keeps track of which argument parameters were used when the is called. /// public List> RemoveDataCallArguments { get; } = new List>(); public IEnumerable ConcreteUpdateData(ObservableUniqueItemCollectionWithSourcePath targetCollection, IEnumerable importedDataCollection, string sourceFilePath) { return UpdateTargetCollectionData(targetCollection, importedDataCollection, sourceFilePath); } protected override IEnumerable UpdateObjectAndDependentData(TestItem objectToUpdate, TestItem objectToUpdateFrom) { IsUpdateDataCalled = true; UpdateDataCallArguments.Add(new Tuple(objectToUpdate, objectToUpdateFrom)); return ItemsToUpdate.Concat(ItemsToUpdateFrom); } protected override IEnumerable RemoveObjectAndDependentData(TestItem removedObject) { IsRemoveObjectAndDependentDataCalled = true; RemoveDataCallArguments.Add(new Tuple(removedObject)); return ItemsToRemove; } private class NameComparer : IEqualityComparer { public bool Equals(TestItem x, TestItem y) { return x.Name == y.Name; } public int GetHashCode(TestItem obj) { return obj.Name.GetHashCode(); } } } private class TestUniqueItemCollection : ObservableUniqueItemCollectionWithSourcePath { public TestUniqueItemCollection() : base(item => item.Name, "TestItem", "naam") {} } private class TestItem : Observable { public TestItem(string name) { Name = name; } public string Name { get; } public TestItem DeepClone() { return new TestItem(Name); } public override string ToString() { return Name; } } } }