public void OnceUponALovelyDayWhenAttemptingToInvokeTryGetConnectionStringIWasExpectingTheKeyToNotBeFound() { IConnectionStringProvider provider = new JsonPathConnectionStringProvider(CONFIGURATION_FILE, "$.ConnectionStrings.MongoConnection" + "Thou shall not find the key!"); bool result = provider.TryGetConnectionString(out string outConnection); Assert.False(result, "The connection string should not be found."); Assert.Null(outConnection); }
public void TryGetConnectionString_FileNotFound() { IConnectionStringProvider provider = new JsonPathConnectionStringProvider(CONFIGURATION_FILE + "Thou shall not find the file!", "$.ConnectionStrings.MongoConnection"); bool result = provider.TryGetConnectionString(out string outConnection); Assert.False(result, "The file should not be found."); Assert.Null(outConnection); }
public void TryGetConnectionString() { IConnectionStringProvider provider = new JsonPathConnectionStringProvider(CONFIGURATION_FILE, "$.ConnectionStrings.MongoConnection"); bool result = provider.TryGetConnectionString(out string outConnection); Assert.True(result, "The connection string was not found!"); Assert.Equal("mongodb://some-mongo:27017/some-db", outConnection); }
private static IConnectionStringProvider ParseConnectionStringProvider(JsonNode node, IFilePathMapper pathMapper) { if (node.AsObject == null) { return(new StaticConnectionStringProvider(node.Value)); } JsonClass objClass = node.AsObject; SupportedConnectionStringLocators locator; if (!Enum.TryParse(node["locator"]?.Value, out locator)) { throw new NAMEException($"The locator {node["locator"]?.Value} is not supported."); } IConnectionStringProvider provider = null; string key; switch (locator) { #if NET45 case SupportedConnectionStringLocators.ConnectionStrings: key = node["key"]?.Value; provider = new ConnectionStringsConnectionStringProvider(key); break; case SupportedConnectionStringLocators.AppSettings: key = node["key"]?.Value; provider = new AppSettingsConnectionStringProvider(key); break; case SupportedConnectionStringLocators.VSSettingsFile: key = node["key"]?.Value; string section = node["section"]?.Value; if (string.IsNullOrEmpty(section)) { throw new ArgumentNullException("section", "The section must be specified."); } provider = new VisualStudioSetingsFileConnectionStringProvider(section, key); break; #endif case SupportedConnectionStringLocators.JSONPath: { key = node["expression"]?.Value; string file = node["file"]?.Value; if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException("file", "The file must be specified."); } provider = new JsonPathConnectionStringProvider(pathMapper.MapPath(file), key); } break; case SupportedConnectionStringLocators.XPath: { key = node["expression"]?.Value; string file = node["file"]?.Value; if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException("file", "The file must be specified."); } provider = new XpathConnectionStringProvider(pathMapper.MapPath(file), key); } break; default: throw new NAMEException($"The locator {locator.ToString()} is not supported.."); } if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException($"The connection string key/expression must be specified."); } return(provider); }