Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Read/BackgroundDataEntityReadExtensions.cs =================================================================== diff -u -r2865b0fc522e96a2f12c47f29805b13259ecde4f -r68e06c08cb60c1c42e5c3b03fd3af070748a6bca --- Application/Ringtoets/src/Application.Ringtoets.Storage/Read/BackgroundDataEntityReadExtensions.cs (.../BackgroundDataEntityReadExtensions.cs) (revision 2865b0fc522e96a2f12c47f29805b13259ecde4f) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Read/BackgroundDataEntityReadExtensions.cs (.../BackgroundDataEntityReadExtensions.cs) (revision 68e06c08cb60c1c42e5c3b03fd3af070748a6bca) @@ -64,23 +64,11 @@ { KeyValuePair parameter = backgroundDataMetaEntity.Read(); - if (ValidKey(parameter.Key, backgroundData.BackgroundMapDataType)) - { - backgroundData.Parameters[parameter.Key] = parameter.Value; - } + backgroundData.Parameters[parameter.Key] = parameter.Value; } } return backgroundData; } - - private static bool ValidKey(string keyToValidate, BackgroundMapDataType backgroundMapDataType) - { - return backgroundMapDataType == BackgroundMapDataType.Wmts - ? keyToValidate == BackgroundDataIdentifiers.SourceCapabilitiesUrl - || keyToValidate == BackgroundDataIdentifiers.SelectedCapabilityIdentifier - || keyToValidate == BackgroundDataIdentifiers.PreferredFormat - : keyToValidate == BackgroundDataIdentifiers.WellKnownTileSource; - } } } \ No newline at end of file Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Read/BackgroundDataEntityReadExtensionsTest.cs =================================================================== diff -u -ra4da96890c5eb0f73300e05256e3e56755334378 -r68e06c08cb60c1c42e5c3b03fd3af070748a6bca --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Read/BackgroundDataEntityReadExtensionsTest.cs (.../BackgroundDataEntityReadExtensionsTest.cs) (revision a4da96890c5eb0f73300e05256e3e56755334378) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Read/BackgroundDataEntityReadExtensionsTest.cs (.../BackgroundDataEntityReadExtensionsTest.cs) (revision 68e06c08cb60c1c42e5c3b03fd3af070748a6bca) @@ -183,65 +183,5 @@ CollectionAssert.IsEmpty(backgroundData.Parameters); Assert.AreEqual(isConfigured, backgroundData.IsConfigured); } - - [Test] - [TestCase(BackgroundMapDataType.Wmts)] - [TestCase(BackgroundMapDataType.WellKnown)] - public void Read_BackgroundDataMetaEntityKeyNotValid_MetaDataNotAddedToBackgroundData(BackgroundMapDataType backgroundMapDataType) - { - // Setup - const string invalidKey = "some key"; - - var entity = new BackgroundDataEntity - { - BackgroundDataMetaEntities = new List - { - new BackgroundDataMetaEntity - { - Key = invalidKey, - Value = "//url" - }, - new BackgroundDataMetaEntity - { - Key = BackgroundDataIdentifiers.SelectedCapabilityIdentifier, - Value = "SelectedCapabilityIdentifier" - }, - new BackgroundDataMetaEntity - { - Key = BackgroundDataIdentifiers.SourceCapabilitiesUrl, - Value = "SourceCapabilitiesUrl" - }, - new BackgroundDataMetaEntity - { - Key = BackgroundDataIdentifiers.PreferredFormat, - Value = "PreferredFormat" - }, - new BackgroundDataMetaEntity - { - Key = BackgroundDataIdentifiers.WellKnownTileSource, - Value = "WellKnownTileSource" - } - }, - IsConfigured = Convert.ToByte(true), - BackgroundDataType = Convert.ToByte(backgroundMapDataType) - }; - - // Call - BackgroundData backgroundData = entity.Read(); - - // Assert - if (backgroundMapDataType == BackgroundMapDataType.Wmts) - { - Assert.AreEqual(3, backgroundData.Parameters.Count); - } - else if (backgroundMapDataType == BackgroundMapDataType.WellKnown) - { - Assert.AreEqual(1, backgroundData.Parameters.Count); - } - - var addedKeys = backgroundData.Parameters.Select(backgroundDataParameter => backgroundDataParameter.Key).ToList(); - CollectionAssert.AreEquivalent(addedKeys, backgroundData.Parameters.Keys); - CollectionAssert.DoesNotContain(addedKeys, invalidKey); - } } } \ No newline at end of file Index: Core/Common/src/Core.Common.Base/Core.Common.Base.csproj =================================================================== diff -u -r1b9445050ddc7786014349d7014c7c4d85242a5d -r68e06c08cb60c1c42e5c3b03fd3af070748a6bca --- Core/Common/src/Core.Common.Base/Core.Common.Base.csproj (.../Core.Common.Base.csproj) (revision 1b9445050ddc7786014349d7014c7c4d85242a5d) +++ Core/Common/src/Core.Common.Base/Core.Common.Base.csproj (.../Core.Common.Base.csproj) (revision 68e06c08cb60c1c42e5c3b03fd3af070748a6bca) @@ -93,6 +93,7 @@ + Index: Core/Common/src/Core.Common.Base/FilteredKeyDictionary.cs =================================================================== diff -u --- Core/Common/src/Core.Common.Base/FilteredKeyDictionary.cs (revision 0) +++ Core/Common/src/Core.Common.Base/FilteredKeyDictionary.cs (revision 68e06c08cb60c1c42e5c3b03fd3af070748a6bca) @@ -0,0 +1,197 @@ +// Copyright (C) Stichting Deltares 2016. 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 Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser 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; +using System.Collections.Generic; +using System.Linq; + +namespace Core.Common.Base +{ + /// + /// Dictionary that only allows items to be added for given keys. + /// + /// The type of the keys. + /// The type of the values. + public class FilteredKeyDictionary : IDictionary + { + private readonly TKey[] allowedKeys; + private readonly IDictionary dictionary = new Dictionary(); + + /// + /// Creates a new instance of . + /// + /// The key values that are allowed to add items with. + /// Thrown when is null. + public FilteredKeyDictionary(TKey[] allowedKeys) + { + if (allowedKeys == null) + { + throw new ArgumentNullException(nameof(allowedKeys)); + } + this.allowedKeys = allowedKeys; + } + + /// + /// Gets or sets the value for the given key. + /// + /// The key to get or set the value for. + /// The element with the specified key. + /// Thrown when is null. + /// Thrown when the property is retrieved and is not found. + /// Thrown when is not allowed + /// to be added to the . + public TValue this[TKey key] + { + get + { + return dictionary[key]; + } + set + { + ValidateKey(key); + + dictionary[key] = value; + } + } + + /// + /// Validates if the key is valid. + /// + /// The key to validate. + /// Thrown when is not allowed + /// to be added to the . + private void ValidateKey(TKey key) + { + if (!allowedKeys.Contains(key)) + { + throw new InvalidOperationException($"Key '{key}' is not allowed to add to the dictionary."); + } + } + + public int Count + { + get + { + return dictionary.Count; + } + } + + public bool IsReadOnly + { + get + { + return dictionary.IsReadOnly; + } + } + + public ICollection Keys + { + get + { + return dictionary.Keys; + } + } + + public ICollection Values + { + get + { + return dictionary.Values; + } + } + + public IEnumerator> GetEnumerator() + { + return dictionary.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// Adds an item to the . + /// + /// The object to add to the . + /// Thrown when an element with the same key already exists in + /// the . + /// Thrown when the key of is + /// not allowed to be added to the . + public void Add(KeyValuePair item) + { + Add(item.Key, item.Value); + } + + /// + /// Adds an element with the provided key and value to the . + /// + /// The object to use as the key of the element to add. + /// The object to use as the value of the element to add. + /// Thrown when is null. + /// Thrown when an element with the same key already exists in + /// the . + /// Thrown when is not allowed + /// to be added to the . + public void Add(TKey key, TValue value) + { + ValidateKey(key); + + dictionary.Add(key, value); + } + + public void Clear() + { + dictionary.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return dictionary.Contains(item); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + dictionary.CopyTo(array, arrayIndex); + } + + public bool Remove(KeyValuePair item) + { + return Remove(item.Key); + } + + public bool ContainsKey(TKey key) + { + return dictionary.ContainsKey(key); + } + + public bool Remove(TKey key) + { + return dictionary.Remove(key); + } + + public bool TryGetValue(TKey key, out TValue value) + { + return dictionary.TryGetValue(key, out value); + } + } +} \ No newline at end of file Index: Core/Common/test/Core.Common.Base.Test/Core.Common.Base.Test.csproj =================================================================== diff -u -r1b9445050ddc7786014349d7014c7c4d85242a5d -r68e06c08cb60c1c42e5c3b03fd3af070748a6bca --- Core/Common/test/Core.Common.Base.Test/Core.Common.Base.Test.csproj (.../Core.Common.Base.Test.csproj) (revision 1b9445050ddc7786014349d7014c7c4d85242a5d) +++ Core/Common/test/Core.Common.Base.Test/Core.Common.Base.Test.csproj (.../Core.Common.Base.Test.csproj) (revision 68e06c08cb60c1c42e5c3b03fd3af070748a6bca) @@ -87,6 +87,7 @@ + Index: Core/Common/test/Core.Common.Base.Test/FilteredKeyDictionaryTest.cs =================================================================== diff -u --- Core/Common/test/Core.Common.Base.Test/FilteredKeyDictionaryTest.cs (revision 0) +++ Core/Common/test/Core.Common.Base.Test/FilteredKeyDictionaryTest.cs (revision 68e06c08cb60c1c42e5c3b03fd3af070748a6bca) @@ -0,0 +1,553 @@ +// Copyright (C) Stichting Deltares 2016. 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 Lesser 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser 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 NUnit.Framework; + +namespace Core.Common.Base.Test +{ + [TestFixture] + public class FilteredKeyDictionaryTest + { + [Test] + public void Constructor_AllowedKeysNull_ThrowArgumentNullException() + { + // Call + TestDelegate test = () => new FilteredKeyDictionary(null); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("allowedKeys", exception.ParamName); + } + + [Test] + public void Constructor_ExpectedValues() + { + // Setup + var keys = new[] + { + "key 1", + "key 2" + }; + + // Call + var dictionary = new FilteredKeyDictionary(keys); + + // Assert + Assert.IsInstanceOf>(dictionary); + CollectionAssert.IsEmpty(dictionary); + } + + [Test] + public void Indexer_GetItemForNonExistingKey_ThrowKeyNotFoundException() + { + // Setup + var dictionary = new FilteredKeyDictionary(new string[0]); + + // Call + TestDelegate call = () => + { + object item = dictionary["some key"]; + }; + + // Assert + Assert.Throws(call); + } + + [Test] + public void Indexer_GetElementForExistingKey_ReturnExpectedItem() + { + // Setup + const string key = "some key"; + var value = new object(); + + FilteredKeyDictionary dictionary = GetDictionary(); + dictionary.Add(key, value); + + // Call + object retrievedItem = dictionary[key]; + + // Assert + Assert.AreSame(value, retrievedItem); + } + + [Test] + public void Indexer_AddElementForInvalidKey_ThrowInvalidOperationException() + { + // Setup + FilteredKeyDictionary dictionary = GetDictionary(); + var value = new object(); + + // Call + TestDelegate test = () => dictionary["invalid"] = value; + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("Key 'invalid' is not allowed to add to the dictionary.", exception.Message); + CollectionAssert.DoesNotContain(dictionary, new KeyValuePair("invalid", value)); + } + + [Test] + public void Indexer_AddElementForValidKey_KeyAdded() + { + // Setup + FilteredKeyDictionary dictionary = GetDictionary(); + var value = new object(); + var keyValuePair = new KeyValuePair("some key", value); + + // Precondition + CollectionAssert.DoesNotContain(dictionary, keyValuePair); + + // Call + dictionary["some key"] = value; + + // Assert + CollectionAssert.Contains(dictionary, keyValuePair); + } + + [Test] + public void Count_Always_ReturnExpectedNumberOfElements() + { + // Setup + const string key = "some key"; + + var dictionary = new FilteredKeyDictionary(new[] + { + key + }) + { + { + key, new object() + } + }; + + // Call + int nrOfElements = dictionary.Count; + + // Assert + Assert.AreEqual(1, nrOfElements); + } + + [Test] + public void IsReadOnly_Always_ReturnFalse() + { + // Setup + var dictionary = new FilteredKeyDictionary(new string[0]); + + // Call + bool isReadOnly = dictionary.IsReadOnly; + + // Assert + Assert.IsFalse(isReadOnly); + } + + [Test] + public void Keys_Always_ReturnAddedKeys() + { + // Setup + const string key1 = "first key"; + const string key2 = "some key"; + const string key3 = "other key"; + + var dictionary = new FilteredKeyDictionary(new[] + { + key1, + key2, + key3 + }) + { + { + key1, new object() + }, + { + key2, new object() + }, + { + key3, new object() + } + }; + + // Call + ICollection keys = dictionary.Keys; + + // Assert + CollectionAssert.AreEquivalent(new[] + { + key1, + key2, + key3 + }, keys); + } + + [Test] + public void Values_Always_ReturnAddedValues() + { + // Setup + const string key = "some key"; + + object value1 = "value 1"; + object value2 = new object(); + object value3 = "value 3"; + + FilteredKeyDictionary dictionary = GetDictionary(); + dictionary.Add(key, value2); + + // Call + ICollection values = dictionary.Values; + + // Assert + CollectionAssert.AreEquivalent(new[] + { + value1, + value2, + value3 + }, values); + } + + [Test] + public void Add_KeyValid_ItemAdded() + { + // Setup + const string key = "some key"; + var value = new object(); + + var dictionary = new FilteredKeyDictionary(new[] + { + key + }); + + // Call + dictionary.Add(key, value); + + // Assert + Assert.AreEqual(1, dictionary.Count); + KeyValuePair item = dictionary.First(); + Assert.AreEqual(key, item.Key); + Assert.AreSame(value, item.Value); + } + + [Test] + public void AddItem_KeyValid_ItemAdded() + { + // Setup + const string key = "some key"; + var value = new object(); + + var dictionary = new FilteredKeyDictionary(new[] + { + key + }); + + // Call + dictionary.Add(new KeyValuePair(key, value)); + + // Assert + Assert.AreEqual(1, dictionary.Count); + KeyValuePair item = dictionary.First(); + Assert.AreEqual(key, item.Key); + Assert.AreSame(value, item.Value); + } + + [Test] + public void Add_KeyInvalid_ThrowInvalidOperationException() + { + // Setup + const string key = "some key"; + + var dictionary = new FilteredKeyDictionary(new[] + { + key + }); + + // Call + TestDelegate test = () => dictionary.Add("invalid key", new object()); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("Key 'invalid key' is not allowed to add to the dictionary.", exception.Message); + CollectionAssert.IsEmpty(dictionary); + } + + [Test] + public void AddItem_KeyInvalid_ThrowInvalidOperationException() + { + // Setup + const string key = "some key"; + + var dictionary = new FilteredKeyDictionary(new[] + { + key + }); + + // Call + TestDelegate test = () => dictionary.Add(new KeyValuePair("invalid key", new object())); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("Key 'invalid key' is not allowed to add to the dictionary.", exception.Message); + CollectionAssert.IsEmpty(dictionary); + } + + [Test] + public void Clear_Always_ClearDictionary() + { + // Setup + const string key = "some key"; + + FilteredKeyDictionary dictionary = GetDictionary(); + dictionary.Add(key, new object()); + + // Precondition + Assert.AreEqual(3, dictionary.Count); + + // Call + dictionary.Clear(); + + // Assert + CollectionAssert.IsEmpty(dictionary); + } + + [Test] + public void Contains_ItemInDictionary_ReturnTrue() + { + // Setup + const string key = "some key"; + var value = new object(); + + FilteredKeyDictionary dictionary = GetDictionary(); + dictionary.Add(key, value); + + // Call + bool contains = dictionary.Contains(new KeyValuePair(key, value)); + + // Assert + Assert.IsTrue(contains); + } + + [Test] + public void Contains_ItemNotInDictionary_ReturnFalse() + { + // Setup + const string key = "some key"; + + var value = new object(); + + FilteredKeyDictionary dictionary = GetDictionary(); + + // Call + bool contains = dictionary.Contains(new KeyValuePair(key, value)); + + // Assert + Assert.IsFalse(contains); + } + + [Test] + public void RemoveItem_ItemNotInDictionary_ReturnFalse() + { + // Setup + const string key = "some key"; + var value = new object(); + + FilteredKeyDictionary dictionary = GetDictionary(); + + // Call + bool removed = dictionary.Remove(new KeyValuePair(key, value)); + + // Assert + Assert.IsFalse(removed); + } + + [Test] + public void RemoveItem_ItemInDictionary_ItemRemovedAndReturnTrue() + { + // Setup + const string key = "some key"; + var value = new object(); + + FilteredKeyDictionary dictionary = GetDictionary(); + var item = new KeyValuePair(key, value); + dictionary.Add(item); + + // Precondition + CollectionAssert.Contains(dictionary, item); + + // Call + bool removed = dictionary.Remove(item); + + // Assert + Assert.IsTrue(removed); + CollectionAssert.DoesNotContain(dictionary, item); + } + + [Test] + public void Remove_KeyNotInDictionary_ReturnFalse() + { + // Setup + const string key = "some key"; + + FilteredKeyDictionary dictionary = GetDictionary(); + + // Call + bool removed = dictionary.Remove(key); + + // Assert + Assert.IsFalse(removed); + } + + [Test] + public void Remove_KeyInDictionary_ItemRemovedAndReturnTrue() + { + // Setup + const string key = "some key"; + + var value = new object(); + + FilteredKeyDictionary dictionary = GetDictionary(); + var item = new KeyValuePair(key, value); + dictionary.Add(item); + + // Precondition + CollectionAssert.Contains(dictionary, item); + + // Call + bool removed = dictionary.Remove(key); + + // Assert + Assert.IsTrue(removed); + CollectionAssert.DoesNotContain(dictionary, item); + } + + [Test] + public void ContainsKey_KeyNotInDictionary_ReturnFalse() + { + // Setup + const string key = "some key"; + + FilteredKeyDictionary dictionary = GetDictionary(); + + // Call + bool contains = dictionary.ContainsKey(key); + + // Assert + Assert.IsFalse(contains); + } + + [Test] + public void ContainsKey_KeyInDictionary_ReturnTrue() + { + // Setup + const string key = "some key"; + var value = new object(); + + FilteredKeyDictionary dictionary = GetDictionary(); + var item = new KeyValuePair(key, value); + dictionary.Add(item); + + // Call + bool contains = dictionary.ContainsKey(key); + + // Assert + Assert.IsTrue(contains); + } + + [Test] + public void CopyTo_ValidData_CopiesItemToGivenArray() + { + // Setup + FilteredKeyDictionary dictionary = GetDictionary(); + + var array = new[] + { + new KeyValuePair("key", new object()), + new KeyValuePair("key2", new object()) + }; + + // Precondition + CollectionAssert.AreNotEqual(dictionary, array); + + // Call + dictionary.CopyTo(array, 0); + + // Assert + CollectionAssert.AreEqual(dictionary, array); + } + + [Test] + public void TryGetValue_KeyNotInDictionary_ReturnFalse() + { + // Setup + const string key = "some key"; + + FilteredKeyDictionary dictionary = GetDictionary(); + + object value; + + // Call + bool succeeded = dictionary.TryGetValue(key, out value); + + // Assert + Assert.IsFalse(succeeded); + } + + [Test] + public void TryGetValue_KeyInDictionary_ReturnTrue() + { + // Setup + const string key = "some key"; + var value = new object(); + + FilteredKeyDictionary dictionary = GetDictionary(); + var item = new KeyValuePair(key, value); + dictionary.Add(item); + + // Call + bool succeeded = dictionary.TryGetValue(key, out value); + + // Assert + Assert.IsTrue(succeeded); + } + + private static FilteredKeyDictionary GetDictionary() + { + const string key1 = "first key"; + const string key2 = "some key"; + const string key3 = "other key"; + + var dictionary = new FilteredKeyDictionary(new[] + { + key1, + key2, + key3 + }) + { + { + key1, "value 1" + }, + { + key3, "value 3" + } + }; + return dictionary; + } + } +} \ No newline at end of file Index: Ringtoets/Common/src/Ringtoets.Common.Data/AssessmentSection/BackgroundData.cs =================================================================== diff -u -rd2d96421974b56eb204dd8f756748aa582da0874 -r68e06c08cb60c1c42e5c3b03fd3af070748a6bca --- Ringtoets/Common/src/Ringtoets.Common.Data/AssessmentSection/BackgroundData.cs (.../BackgroundData.cs) (revision d2d96421974b56eb204dd8f756748aa582da0874) +++ Ringtoets/Common/src/Ringtoets.Common.Data/AssessmentSection/BackgroundData.cs (.../BackgroundData.cs) (revision 68e06c08cb60c1c42e5c3b03fd3af070748a6bca) @@ -20,7 +20,6 @@ // All rights reserved. using System; -using System.Collections.Generic; using Core.Common.Base; using Core.Common.Base.Data; using Ringtoets.Common.Data.Properties; @@ -46,7 +45,14 @@ { IsVisible = true; transparency = new RoundedDouble(transparencyNumberOfDecimals); - Parameters = new Dictionary(); + Parameters = new FilteredKeyDictionary( + new[] + { + BackgroundDataIdentifiers.SourceCapabilitiesUrl, + BackgroundDataIdentifiers.SelectedCapabilityIdentifier, + BackgroundDataIdentifiers.PreferredFormat, + BackgroundDataIdentifiers.WellKnownTileSource + }); } /// @@ -97,6 +103,6 @@ /// /// Gets the parameters that are configured for a background map data. /// - public IDictionary Parameters { get; } + public FilteredKeyDictionary Parameters { get; } } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Data.Test/AssessmentSection/BackgroundDataTest.cs =================================================================== diff -u -rc0387d79bcdc500558362264d5bc08834ebe6db5 -r68e06c08cb60c1c42e5c3b03fd3af070748a6bca --- Ringtoets/Common/test/Ringtoets.Common.Data.Test/AssessmentSection/BackgroundDataTest.cs (.../BackgroundDataTest.cs) (revision c0387d79bcdc500558362264d5bc08834ebe6db5) +++ Ringtoets/Common/test/Ringtoets.Common.Data.Test/AssessmentSection/BackgroundDataTest.cs (.../BackgroundDataTest.cs) (revision 68e06c08cb60c1c42e5c3b03fd3af070748a6bca) @@ -20,6 +20,8 @@ // All rights reserved. using System; +using System.Collections.Generic; +using System.Linq; using Core.Common.Base.Data; using Core.Common.TestUtil; using NUnit.Framework; @@ -84,5 +86,65 @@ string paramName = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(call, message).ParamName; Assert.AreEqual("value", paramName); } + + [Test] + [TestCase("SourceCapabilitiesUrl")] + [TestCase("SelectedCapabilityIdentifier")] + [TestCase("PreferredFormat")] + [TestCase("WellKnownTileSource")] + public void Parameters_AllowedKeys_ItemAddedToDictionary(string allowedKey) + { + // Setup + const string value = "some value"; + var backgroundData = new BackgroundData(); + + // Precondition + CollectionAssert.IsEmpty(backgroundData.Parameters); + + // Call + backgroundData.Parameters.Add(allowedKey, value); + + // Assert + Assert.AreEqual(1, backgroundData.Parameters.Count); + KeyValuePair item = backgroundData.Parameters.First(); + Assert.AreEqual(allowedKey, item.Key); + Assert.AreEqual(value, item.Value); + } + + [Test] + public void Parameters_AddOtherThanAllowed_ThrowInvalidOperationException() + { + // Setup + var backgroundData = new BackgroundData(); + + // Precondition + CollectionAssert.IsEmpty(backgroundData.Parameters); + + // Call + TestDelegate test = () => backgroundData.Parameters.Add("invalid key", "test"); + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("Key 'invalid key' is not allowed to add to the dictionary.", exception.Message); + CollectionAssert.IsEmpty(backgroundData.Parameters); + } + + [Test] + public void Parameters_AddOtherThanAllowed_ThrowInvalidOperationExceptions() + { + // Setup + var backgroundData = new BackgroundData(); + + // Precondition + CollectionAssert.IsEmpty(backgroundData.Parameters); + + // Call + TestDelegate test = () => backgroundData.Parameters["invalid key"] = "test"; + + // Assert + var exception = Assert.Throws(test); + Assert.AreEqual("Key 'invalid key' is not allowed to add to the dictionary.", exception.Message); + CollectionAssert.IsEmpty(backgroundData.Parameters); + } } } \ No newline at end of file