public void GetByKey_DeSerializeWrongType_ThrowsConfigurationException() { //Arrange var propertyBag = new BIPropertyBag(); propertyBag.Values.Count = 0; string key = "key"; string nameSpacedKey = ConfigManager.PnPKeyNamespace + "." + key; string expectedError = "SerializationException"; propertyBag.Values[nameSpacedKey] = "abc"; var stack = new BIConfigStack(); stack.Bags.Add(propertyBag); string errString = null; //Act var target = new HierarchicalConfig(stack, new MockConfigSettingSerializer() { ThrowError = true }); try { target.GetByKey <DateTime>("key"); } catch (ConfigurationException ex) { errString = ex.ToString(); } // Assert Assert.IsNotNull(errString); Assert.IsTrue(errString.Contains(expectedError)); }
public void SerializationFailureThrowsConfigurationException() { //Arrange var propertyBag = new BIPropertyBag(); propertyBag.Values.Count = 0; string errString = null; string expectedError = "SerializationException"; var hierarchy = new BIConfigStack(); hierarchy.Bags.Add(propertyBag); // Act var target = new ConfigManager(hierarchy, new MockConfigSettingSerializer() { ThrowError = true }); try { target.SetInPropertyBag("key", "MyValue", propertyBag); } catch (ConfigurationException ex) { errString = ex.Message; } // Assert Assert.IsNotNull(errString); Assert.IsTrue(errString.Contains(expectedError)); }
public void GetByKey_KeyNotFound_ThrowsConfigurationException() { // Arrange var defaultPropertyBag = new BIPropertyBag(); defaultPropertyBag.Values.Count = 0; var stack = new BIConfigStack(); stack.Bags.Add(defaultPropertyBag); bool expectedExceptionThrown = false; var target = new HierarchicalConfig(stack); //Act try { target.GetByKey <string>("key"); } catch (ConfigurationException) { expectedExceptionThrown = true; } // Assert Assert.IsTrue(expectedExceptionThrown, "Failed to throw exception on key not found"); }
public void SaveValueExceedsRetryFails() { //Arrange string key = "{6313EE1A-5A12-46A3-A537-4905678FBD9E}"; var propBag = new BIPropertyBag(); int retry = 0; bool expectedExceptionThrown = false; propBag.ItemSetStringString = (k, v) => { if (retry < 3) { retry++; var ex = new MSPUpdatedConcurrencyException(); throw ex.Instance; } else { propBag.Values[k] = v; } }; propBag.Values.Count = 0; var hierarchy = new BIConfigStack(); hierarchy.Bags.Add(propBag); var target = new ConfigManager(hierarchy, new MockConfigSettingSerializer()); //Act try { target.SetInPropertyBag(key, 3, propBag); } catch (ConfigurationException) { expectedExceptionThrown = true; } //Assert Assert.IsTrue(expectedExceptionThrown); }
public void RemoveKeyFromPropertyBagSucceeds() { //Arrange BIPropertyBag bag = new BIPropertyBag(); string namespacedKey = ConfigManager.PnPKeyNamespace + "." + TestsConstants.TestGuidName; bag.Values.SetOne(namespacedKey, "{821FC688-2C8C-4F8B-A700-EDB81400B63B}"); var hierarchy = new BIConfigStack(); hierarchy.Bags.Add(new BIPropertyBag()); var target = new ConfigManager(hierarchy); //Act Assert.IsTrue(bag.Values.ContainsKey(namespacedKey)); target.RemoveKeyFromPropertyBag(TestsConstants.TestGuidName, bag); //Assert Assert.IsFalse(bag.Values.ContainsKey(namespacedKey)); }
public void SetValueBasedOnKeySucceeds() { //Arrange string key = "{6313EE1A-5A12-46A3-A537-4905678FBD9E}"; var propBag = new BIPropertyBag(); propBag.Values.Count = 0; var hierarchy = new BIConfigStack(); hierarchy.Bags.Add(propBag); var target = new ConfigManager(hierarchy, new MockConfigSettingSerializer()); //Act target.SetInPropertyBag(key, 3, propBag); //Assert Assert.IsTrue(target.ContainsKeyInPropertyBag(key, propBag)); int value = target.GetFromPropertyBag <int>(key, propBag); Assert.IsTrue(value == 3); }
public void SetConfigValueLogs() { //Arrange var propertyBag = new BIPropertyBag(); propertyBag.Values.Count = 0; var farm = new BSPFarm(); string key = "myKey"; string value = "myValue"; var hierarchy = new BIConfigStack(); hierarchy.Bags.Add(propertyBag); //Act var target = new ConfigManager(hierarchy); target.SetInPropertyBag(key, value, target.GetPropertyBag(ConfigLevel.CurrentSPFarm)); //Assert StringAssert.Contains(logger.TraceMessage, key); StringAssert.Contains(logger.TraceMessage, value); }
public void ContainsKey_ContainsWithHierarchy() { //Arrange var parent = new BIPropertyBag(); var child = new BIPropertyBag(); string key = "value"; string namespaceKey = ConfigManager.PnPKeyNamespace + "." + key; parent.Values.Count = 0; child.Values.Count = 0; var stack = new BIConfigStack(); stack.Bags.Add(child); stack.Bags.Add(parent); //Act, Assert var target = new HierarchicalConfig(stack); IPropertyBag parentBag = parent; Assert.IsFalse(target.ContainsKey("value")); parentBag[namespaceKey] = null; Assert.IsTrue(target.ContainsKey(key)); }
public void UsingSiteAsSuffixThrowsException() { //Arrange string key = "{821FC688-2C8C-4F8B-A700-EDB81400B63B}" + SPSitePropertyBag.KeySuffix; var hierarchy = new BIConfigStack(); hierarchy.Bags.Add(new BIPropertyBag()); var target = new ConfigManager(hierarchy); bool expectedExceptionThrown = false; //Act try { target.SetInPropertyBag(key, "value", new BIPropertyBag()); } catch (ConfigurationException) { expectedExceptionThrown = true; } //Assert Assert.IsTrue(expectedExceptionThrown); }
public void SaveValueWithRetrySucceeds() { //Arrange string key = "{6313EE1A-5A12-46A3-A537-4905678FBD9E}"; var propBag = new BIPropertyBag(); int retry = 0; propBag.ItemSetStringString = (k, v) => { if (retry < 2) { retry++; var ex = new MSPUpdatedConcurrencyException(); throw ex.Instance; } else { propBag.Values[k] = v; } }; propBag.Values.Count = 0; var hierarchy = new BIConfigStack(); hierarchy.Bags.Add(propBag); var target = new ConfigManager(hierarchy, new MockConfigSettingSerializer()); //Act target.SetInPropertyBag(key, 3, propBag); //Assert Assert.IsTrue(target.ContainsKeyInPropertyBag(key, propBag)); int value = target.GetFromPropertyBag <int>(key, propBag); Assert.IsTrue(value == 3); }
public void GetByKey_GetValue_WillUseHierarchy() { //Arrange string key = "{6313EE1A-5A12-46A3-A537-4905678FBD9E}"; string namespacedKey = ConfigManager.PnPKeyNamespace + "." + key; string value = "{821FC688-2C8C-4F8B-A700-EDB81400B63B}"; var parent = new BIPropertyBag(); var child = new BIPropertyBag(); parent.Values.SetOne(namespacedKey, value); child.Values.Count = 0; var stack = new BIConfigStack(); stack.Bags.Add(child); stack.Bags.Add(parent); //Act var target = new HierarchicalConfig(stack); string actual = target.GetByKey <string>(key); // Assert Assert.AreEqual(value, actual); }
public void GetByKey_GetValueByKey() { //Arrange // This is an example of a behaved type // (a stub that is extended to implement general behavior // and can be re-used) var defaultPropertyBag = new BIPropertyBag(); string key = "key"; int expected = 3; string namespaceKey = ConfigManager.PnPKeyNamespace + "." + key; defaultPropertyBag.Values.SetOne(namespaceKey, expected.ToString()); var stack = new BIConfigStack(); stack.Bags.Add(defaultPropertyBag); var serializer = new SIConfigSettingSerializer() { DeserializeTypeString = (type, data) => { object ret = null; if (type == typeof(int)) { ret = int.Parse(data); } return(ret); }, }; //Act var target = new HierarchicalConfig(stack, serializer); int configValue = target.GetByKey <int>(key); //Assert Assert.AreEqual(expected, configValue); }