Last active
August 29, 2015 14:17
-
-
Save deejaygraham/7787aa578a33816c24e8 to your computer and use it in GitHub Desktop.
Integration testing of Azure targetted code without compute emulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Deployed implementation | |
/// </summary> | |
public class AzureRoleSettings : IRoleSettings | |
{ | |
public string ReadValue(string key) | |
{ | |
return RoleEnvironment.GetConfigurationSettingValue(key); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IRoleSettings | |
{ | |
string ReadValue(string key); | |
} | |
/// <summary> | |
/// Deployed implementation | |
/// </summary> | |
public class AzureRoleSettings : IRoleSettings | |
{ | |
public string ReadValue(string key) | |
{ | |
return RoleEnvironment.GetConfigurationSettingValue(key); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Integration test version, reads from local cscfg | |
/// </summary> | |
public class LocalCloudConfigRoleSettings : IRoleSettings | |
{ | |
private Dictionary<string, string> nameValuePairs = new Dictionary<string, string>(); | |
public static LocalCloudConfigRoleSettings FromCloudConfig(string path, string roleName) | |
{ | |
LocalCloudConfigRoleSettings settings = new LocalCloudConfigRoleSettings(); | |
XElement doc = XElement.Load(path); | |
const string RoleElement = "Role"; | |
const string NameAttribute = "name"; | |
// namespace for cscfgs | |
XNamespace scns = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"; | |
// find the correct role | |
XElement requiredRole = (from role in doc.Descendants(scns + RoleElement) | |
where (string)role.Attribute(NameAttribute) == roleName | |
select role).FirstOrDefault(); | |
if (requiredRole != null) | |
{ | |
const string SettingElement = "Setting"; | |
const string ValueAttribute = "value"; | |
// import each name value pair | |
foreach (XElement setting in requiredRole.Descendants(scns + SettingElement)) | |
{ | |
XAttribute name = setting.Attributes(NameAttribute).FirstOrDefault(); | |
XAttribute value = setting.Attributes(ValueAttribute).FirstOrDefault(); | |
if (name != null && value != null) | |
{ | |
settings.nameValuePairs.Add(name.Value, value.Value); | |
} | |
} | |
} | |
return settings; | |
} | |
public string ReadValue(string key) | |
{ | |
return this.nameValuePairs[key]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Unit test version (or mock IRoleSettings) | |
/// </summary> | |
public class StubRoleSettings : IRoleSettings | |
{ | |
public Dictionary<string, string> nameValuePairs = new Dictionary<string, string>(); | |
public string ReadValue(string key) | |
{ | |
return this.nameValuePairs[key]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment