public void CanSetValueBasedOnKey() { MockPropertyBag defaultPropertyBag = new MockPropertyBag(); var target = new HierarchicalConfig(defaultPropertyBag, new MockConfigSettingSerializer()); target.SetInPropertyBag("Key", 3, defaultPropertyBag); }
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 GetSettingFrom_GetASetting_NoKeyReturnsNull() { //Arrange var hierarchy = new TestablePropertyBagHierarchy(); BIPropertyBag bag = GetPropertyBag(ConfigLevel.CurrentSPWeb); hierarchy.AddPropertyBag(bag); string key = TestsConstants.TestGuidName; var config = new HierarchicalConfig(hierarchy); ConfigurationException configEx = null; //Act try { string target = config.GetByKey <string>(key, ConfigLevel.CurrentSPWeb); } catch (ConfigurationException ex) { configEx = ex; } //Assert Assert.IsNotNull(configEx); Assert.IsTrue(configEx.Message.Contains(key)); }
public void GetSettingFrom_GetASettingWithHierarchyDoesntFindKey_FromLevelAboveKey() { //Arrange var hierarchy = new TestablePropertyBagHierarchy(); string key = TestsConstants.TestGuidName; string namespacedKey = ConfigManager.PnPKeyNamespace + "." + key; string value = "{80C23A3E-566B-4B11-A881-5868F2BCB198}"; BIPropertyBag bag = GetPropertyBag(ConfigLevel.CurrentSPWeb); bag.Values[namespacedKey] = value; hierarchy.AddPropertyBag(bag); hierarchy.AddPropertyBag(GetPropertyBag(ConfigLevel.CurrentSPSite)); hierarchy.AddPropertyBag(GetPropertyBag(ConfigLevel.CurrentSPWebApplication)); var target = new HierarchicalConfig(hierarchy); ConfigurationException configEx = null; //Act try { string result = target.GetByKey <string>(key, ConfigLevel.CurrentSPSite); } catch (ConfigurationException ex) { configEx = ex; } //Assert Assert.IsNotNull(configEx); Assert.IsTrue(configEx.Message.Contains(key)); }
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 Cleanup() { SPSite spSite = new SPSite("http://localhost:9001/sites/pssportal"); SPContext spContext = SPContext.GetContext(spSite.RootWeb); Isolate.WhenCalled(() => SPContext.Current.Site).WillReturn(spSite); Isolate.WhenCalled(() => SPContext.Current).WillReturn(spContext); List <string> keys = new List <string>() { "IntegrationTest-FarmLevelKey", "IntegrationTest-WebAppLevelKey", "IntegrationTest-SiteLevelKey", "IntegrationTest-WebLevelKey" }; HierarchicalConfig hierarchicalConfig = new HierarchicalConfig(); foreach (string key in keys) { hierarchicalConfig.RemoveKeyFromPropertyBag(key, SPContext.Current.Web); hierarchicalConfig.RemoveKeyFromPropertyBag(key, SPContext.Current.Site); hierarchicalConfig.RemoveKeyFromPropertyBag(key, SPContext.Current.Site.WebApplication); hierarchicalConfig.RemoveKeyFromPropertyBag(key, SPFarm.Local); } Isolate.CleanUp(); }
public void CanSetAndGetValues() { SPSite spSite = new SPSite("http://localhost:9001/sites/pssportal"); SPContext spContext = SPContext.GetContext(spSite.RootWeb); Isolate.WhenCalled(() => SPContext.Current.Site).WillReturn(spSite); Isolate.WhenCalled(() => SPContext.Current).WillReturn(spContext); //Set Values at levels of hierarchy HierarchicalConfig target1 = new HierarchicalConfig(); target1.SetInPropertyBag("IntegrationTest-FarmLevelKey", "FarmLevelValue", new SPFarmPropertyBag()); target1.SetInPropertyBag("IntegrationTest-WebAppLevelKey", "WebAppLevelValue", new SPWebAppPropertyBag()); target1.SetInPropertyBag("IntegrationTest-SiteLevelKey", "SiteLevelValue", new SPSitePropertyBag()); target1.SetInPropertyBag("IntegrationTest-WebLevelKey", "WebLevelValue", new SPWebPropertyBag()); HierarchicalConfig target2 = new HierarchicalConfig(); Assert.AreEqual("FarmLevelValue", target2.GetByKey <string>("IntegrationTest-FarmLevelKey")); Assert.AreEqual("WebAppLevelValue", target2.GetByKey <string>("IntegrationTest-WebAppLevelKey")); Assert.AreEqual("SiteLevelValue", target2.GetByKey <string>("IntegrationTest-SiteLevelKey")); Assert.AreEqual("WebLevelValue", target2.GetByKey <string>("IntegrationTest-WebLevelKey")); Assert.AreEqual("FarmLevelValue", SPFarm.Local.Properties["IntegrationTest-FarmLevelKey"]); Assert.AreEqual("WebAppLevelValue", SPContext.Current.Site.WebApplication.Properties["IntegrationTest-WebAppLevelKey"]); Assert.AreEqual("SiteLevelValue", SPContext.Current.Site.RootWeb.AllProperties["IntegrationTest-SiteLevelKey"]); Assert.AreEqual("WebLevelValue", SPContext.Current.Web.AllProperties["IntegrationTest-WebLevelKey"]); }
public void CanSetValueAndOverrideAtLowerLevel() { SPSite spSite = new SPSite("http://localhost:9001/sites/pssportal"); SPContext spContext = SPContext.GetContext(spSite.RootWeb); Isolate.WhenCalled(() => SPContext.Current.Site).WillReturn(spSite); Isolate.WhenCalled(() => SPContext.Current).WillReturn(spContext); //Set Values at levels of hierarchy HierarchicalConfig target = new HierarchicalConfig(); target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "FarmLevelValue", new SPFarmPropertyBag()); Assert.AreEqual("FarmLevelValue", target.GetByKey <string>("IntegrationTest-FarmLevelKey")); target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "Over-riddenValue1", new SPWebAppPropertyBag()); Assert.AreEqual("Over-riddenValue1", target.GetByKey <string>("IntegrationTest-FarmLevelKey")); target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "Over-riddenValue2", new SPWebAppPropertyBag()); Assert.AreEqual("Over-riddenValue2", target.GetByKey <string>("IntegrationTest-FarmLevelKey")); target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "Over-riddenValue3", new SPSitePropertyBag()); Assert.AreEqual("Over-riddenValue3", target.GetByKey <string>("IntegrationTest-FarmLevelKey")); target.SetInPropertyBag("IntegrationTest-FarmLevelKey", "Over-riddenValue4", new SPWebPropertyBag()); Assert.AreEqual("Over-riddenValue4", target.GetByKey <string>("IntegrationTest-FarmLevelKey")); }
public void CanGetValueBasedOnKey() { MockPropertyBag defaultPropertyBag = new MockPropertyBag(); defaultPropertyBag["key"] = "3"; var target = new HierarchicalConfig(defaultPropertyBag, new MockConfigSettingSerializer()); int configValue = target.GetByKey <int>("key"); Assert.AreEqual(3, configValue); }
public void SerializeErrorGivesClearErrroMessage() { MockPropertyBag defaultPropertyBag = new MockPropertyBag(); var target = new HierarchicalConfig(defaultPropertyBag, new MockConfigSettingSerializer() { ThrowError = true }); target.SetInPropertyBag("key", "MyValue", defaultPropertyBag); }
public void GetValueWillUseHierarchy() { MockPropertyBag parent = BuildPropertyBagHierarcy(); parent["value"] = "value"; var target = new HierarchicalConfig(parent.Children.First()); Assert.AreEqual("value", target.GetByKey <string>("value")); }
public void SetConfigValueLogs() { SPFarm fakeFarm = Isolate.Fake.Instance <SPFarm>(); Isolate.WhenCalled(() => fakeFarm.Properties).WillReturn(new Hashtable()); var target = new HierarchicalConfig(new MockPropertyBag()); target.SetInPropertyBag("myKey", "myValue", fakeFarm); Assert.AreEqual("Set value in hierarchical config.\n\tKey: 'myKey'\n\tLevel: 'CurrentSPFarm'\n\tValue: 'myValue'", logger.TraceMessage); }
public void KeyExistsWillUseHierarchy() { MockPropertyBag parent = BuildPropertyBagHierarcy(); var target = new HierarchicalConfig(parent.Children.First()); Assert.IsFalse(target.ContainsKey("value")); parent["value"] = "value"; Assert.IsTrue(target.ContainsKey("value")); }
public void DeSerializeErrorGivesClearErrroMessage() { MockPropertyBag defaultPropertyBag = new MockPropertyBag(); defaultPropertyBag.values.Add("key", "abc"); var target = new HierarchicalConfig(defaultPropertyBag, new MockConfigSettingSerializer() { ThrowError = true }); target.GetByKey <DateTime>("key"); }
public void ContainsFrom_ContainsASettingWithHierarchy_NoKeyReturnsFalse() { //Arrange var hierarchy = new TestablePropertyBagHierarchy(); string key = TestsConstants.TestGuidName; bool expected = false; hierarchy.AddPropertyBag(GetPropertyBag(ConfigLevel.CurrentSPWeb)); hierarchy.AddPropertyBag(GetPropertyBag(ConfigLevel.CurrentSPSite)); var config = new HierarchicalConfig(hierarchy); //Act bool target = config.ContainsKey(key, ConfigLevel.CurrentSPWeb); //Assert Assert.AreEqual(expected, target); }
public void GetByKey_GetValueWithNullKey_ThrowsArgumentNullException() { var target = new HierarchicalConfig(); bool expectedExceptionThrown = false; //Act try { int configValue = target.GetByKey <int>(null); } catch (ArgumentNullException) { expectedExceptionThrown = true; } //Assert Assert.IsTrue(expectedExceptionThrown); }
public void ContainsFrom_ContainsASetting_WithNullKeyThrowsArgumentNullException() { //Arrange bool expectedExceptionThrown = false; var hierarchy = new TestablePropertyBagHierarchy(); var config = new HierarchicalConfig(hierarchy); //Act try { config.ContainsKey(null, ConfigLevel.CurrentSPSite); } catch (ArgumentNullException) { expectedExceptionThrown = true; } Assert.IsTrue(expectedExceptionThrown); }
public void ContainsKey_IfContextIsNull_ThrowsNoSharePointException() { //Arrange var target = new HierarchicalConfig(); bool expectedExceptionThrown = false; // Act try { target.GetByKey <DateTime>("key"); } catch (NoSharePointContextException) { expectedExceptionThrown = true; } // Assert Assert.IsTrue(expectedExceptionThrown); }
public void GetSettingFrom_GetASetting_WithNullKey() { //Arrange var hierarchy = new TestablePropertyBagHierarchy(); bool expectedExceptionThrown = false; var target = new HierarchicalConfig(hierarchy); //Act try { target.GetByKey <string>(null, ConfigLevel.CurrentSPSite); } catch (ArgumentNullException) { expectedExceptionThrown = true; } Assert.IsTrue(expectedExceptionThrown); }
public void KeyNotFoundThrows() { var target = new HierarchicalConfig(new MockPropertyBag()); try { target.GetByKey <string>("key"); Assert.Fail(); } catch (ConfigurationException configurationException) { Assert.AreEqual("There was no value configured for key 'key' in a propertyBag with level 'CurrentSPFarm' or above.", configurationException.Message); } catch (Exception) { Assert.Fail(); } }
public void GetSettingFrom_GetASetting() { //Arrange var hierarchy = new TestablePropertyBagHierarchy(); BIPropertyBag bag = GetPropertyBag(ConfigLevel.CurrentSPSite); hierarchy.AddPropertyBag(bag); string key = TestsConstants.TestGuidName; string namespacedKey = ConfigManager.PnPKeyNamespace + "." + key; string expected = "{80C23A3E-566B-4B11-A881-5868F2BCB198}"; bag.Values[namespacedKey] = expected; bag.Level = ConfigLevel.CurrentSPWeb; var config = new HierarchicalConfig(hierarchy); //Act string target = config.GetByKey <string>(key, ConfigLevel.CurrentSPWeb); //Assert Assert.AreEqual(expected, target); }
public void ContainsFrom_ContainsASetting() { //Arrange var hierarchy = new TestablePropertyBagHierarchy(); bool expected = true; BIPropertyBag bag = GetPropertyBag(ConfigLevel.CurrentSPWeb); hierarchy.AddPropertyBag(bag); string key = TestsConstants.TestGuidName; string namespacedKey = ConfigManager.PnPKeyNamespace + "." + key; string value = "{80C23A3E-566B-4B11-A881-5868F2BCB198}"; bag.Values[namespacedKey] = value; var config = new HierarchicalConfig(hierarchy); //Act bool target = config.ContainsKey(key, ConfigLevel.CurrentSPWeb); //Assert Assert.AreEqual(expected, target); }
public void GetByKey_GetByValueWithLevelWithNullKey_ThrowsArgumentNullException() { //Arrange var locator = SharePointServiceLocator.GetCurrent() as ActivatingServiceLocator; var context = new MSPContext(); MSPContext.CurrentGet = () => context; var target = new HierarchicalConfig(); bool expectedExceptionThrown = false; //Act try { string expectedContains = target.GetByKey <string>(null, ConfigLevel.CurrentSPFarm); } catch (ArgumentNullException) { expectedExceptionThrown = true; } //Assert Assert.IsTrue(expectedExceptionThrown); }
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 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); }
public void UsingSiteAsPrefixFails() { var target = new HierarchicalConfig(new MockPropertyBag()); target.SetInPropertyBag("Site_BadValue", "value", new MockPropertyBag()); }
public void IfContextIsNullDefaultPropertyBagThrows() { var target = new HierarchicalConfig(); target.GetByKey <DateTime>("key"); }