diff --git a/Connectors.sln b/Connectors.sln index 57513c8..5647be1 100644 --- a/Connectors.sln +++ b/Connectors.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.27130.2010 +VisualStudioVersion = 15.0.27130.2027 MinimumVisualStudioVersion = 15.0.26124.0 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9BB0395E-B595-4B95-89ED-F55AF12B4EE7}" EndProject @@ -52,6 +52,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.CloudFoundry.Conne EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.CloudFoundry.ConnectorBase.Test", "test\Steeltoe.CloudFoundry.ConnectorBase.Test\Steeltoe.CloudFoundry.ConnectorBase.Test.csproj", "{B40DE919-FBC0-4CC6-B36F-98364FCA4203}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.CloudFoundry.ConnectorAutofac", "src\Steeltoe.CloudFoundry.ConnectorAutofac\Steeltoe.CloudFoundry.ConnectorAutofac.csproj", "{73EEC113-97AC-4F74-8099-2C0EDEAA9596}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.CloudFoundry.ConnectorAutofac.Test", "test\Steeltoe.CloudFoundry.ConnectorAutofac.Test\Steeltoe.CloudFoundry.ConnectorAutofac.Test.csproj", "{59F8BEF1-D25D-4B18-A060-97E4C5B37600}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -158,6 +162,30 @@ Global {B40DE919-FBC0-4CC6-B36F-98364FCA4203}.Release|x64.Build.0 = Release|Any CPU {B40DE919-FBC0-4CC6-B36F-98364FCA4203}.Release|x86.ActiveCfg = Release|Any CPU {B40DE919-FBC0-4CC6-B36F-98364FCA4203}.Release|x86.Build.0 = Release|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Debug|x64.ActiveCfg = Debug|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Debug|x64.Build.0 = Debug|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Debug|x86.ActiveCfg = Debug|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Debug|x86.Build.0 = Debug|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Release|Any CPU.Build.0 = Release|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Release|x64.ActiveCfg = Release|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Release|x64.Build.0 = Release|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Release|x86.ActiveCfg = Release|Any CPU + {73EEC113-97AC-4F74-8099-2C0EDEAA9596}.Release|x86.Build.0 = Release|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Debug|Any CPU.Build.0 = Debug|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Debug|x64.ActiveCfg = Debug|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Debug|x64.Build.0 = Debug|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Debug|x86.ActiveCfg = Debug|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Debug|x86.Build.0 = Debug|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|Any CPU.ActiveCfg = Release|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|Any CPU.Build.0 = Release|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|x64.ActiveCfg = Release|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|x64.Build.0 = Release|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|x86.ActiveCfg = Release|Any CPU + {59F8BEF1-D25D-4B18-A060-97E4C5B37600}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -171,6 +199,8 @@ Global {D7296042-50AA-4570-BD5F-15DEAD416EDF} = {1CEE3AF7-DC06-4BBE-B28D-62FB1BD4A7A5} {F9A2807C-292F-49EB-B23A-FBB04527BA5C} = {9BB0395E-B595-4B95-89ED-F55AF12B4EE7} {B40DE919-FBC0-4CC6-B36F-98364FCA4203} = {1CEE3AF7-DC06-4BBE-B28D-62FB1BD4A7A5} + {73EEC113-97AC-4F74-8099-2C0EDEAA9596} = {9BB0395E-B595-4B95-89ED-F55AF12B4EE7} + {59F8BEF1-D25D-4B18-A060-97E4C5B37600} = {1CEE3AF7-DC06-4BBE-B28D-62FB1BD4A7A5} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {112EBBFA-82B9-483E-B258-8611B70B3A3A} diff --git a/config/versions-dev.props b/config/versions-dev.props index 2b03b02..8798541 100644 --- a/config/versions-dev.props +++ b/config/versions-dev.props @@ -6,6 +6,7 @@ 2.0.0 2.0.0 2.0.0 + 4.6.1 4.4.0 1.0.2 6.1.3 @@ -18,6 +19,7 @@ 2.0.0 5.0.1 6.9.10 + 4.4.2 10.0.3 15.5.0 2.3.1 diff --git a/config/versions-master.props b/config/versions-master.props index fb26a9d..509702b 100644 --- a/config/versions-master.props +++ b/config/versions-master.props @@ -6,6 +6,7 @@ 2.0.0 2.0.0 2.0.0 + 4.6.1 4.4.0 1.0.2 6.1.3 @@ -18,6 +19,7 @@ 2.0.0 5.0.1 6.9.10 + 4.4.2 10.0.3 15.5.0 2.3.1 diff --git a/config/versions-update.props b/config/versions-update.props index 989eddb..7967ba5 100644 --- a/config/versions-update.props +++ b/config/versions-update.props @@ -16,6 +16,7 @@ 1.1.1 4.1.3 6.9.9 + 4.4.2 9.0.1 15.5.0 2.3.1 diff --git a/config/versions.props b/config/versions.props index 0315cf0..2dc63ce 100644 --- a/config/versions.props +++ b/config/versions.props @@ -6,6 +6,7 @@ 2.0.0 2.0.0 2.0.0 + 4.6.1 4.4.0 1.0.2 6.1.3 @@ -18,6 +19,7 @@ 3.2.5 5.0.1 6.9.10 + 4.4.2 10.0.3 15.5.0 2.3.1 diff --git a/src/Steeltoe.CloudFoundry.ConnectorAutofac/HystrixContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorAutofac/HystrixContainerBuilderExtensions.cs new file mode 100644 index 0000000..ee43c9f --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorAutofac/HystrixContainerBuilderExtensions.cs @@ -0,0 +1,65 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Autofac.Builder; +using Microsoft.Extensions.Configuration; +using Steeltoe.CloudFoundry.Connector; +using Steeltoe.CloudFoundry.Connector.Hystrix; +using Steeltoe.CloudFoundry.Connector.Services; +using System; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac +{ + public static class HystrixContainerBuilderExtensions + { + private static string[] rabbitAssemblies = new string[] { "RabbitMQ.Client" }; + private static string[] rabbitTypeNames = new string[] { "RabbitMQ.Client.ConnectionFactory" }; + + public static IRegistrationBuilder RegisterHystrixConnection(this ContainerBuilder container, IConfiguration config, string serviceName = null) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (config == null) + { + throw new ArgumentNullException(nameof(config)); + } + + Type rabbitFactory = ConnectorHelpers.FindType(rabbitAssemblies, rabbitTypeNames); + if (rabbitFactory == null) + { + throw new ConnectorException("Unable to find RabbitMQ ConnectionFactory type. RabbitMQ.Client assembly may be missing"); + } + + HystrixRabbitMQServiceInfo info; + + if (serviceName == null) + { + info = config.GetSingletonServiceInfo(); + } + else + { + info = config.GetRequiredServiceInfo(serviceName); + } + + HystrixProviderConnectorOptions hystrixConfig = new HystrixProviderConnectorOptions(config); + HystrixProviderConnectorFactory factory = new HystrixProviderConnectorFactory(info, hystrixConfig, rabbitFactory); + + return container.Register(c => factory.Create(null)).As(); + } + } +} diff --git a/src/Steeltoe.CloudFoundry.ConnectorAutofac/MySqlContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorAutofac/MySqlContainerBuilderExtensions.cs new file mode 100644 index 0000000..a7e7c08 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorAutofac/MySqlContainerBuilderExtensions.cs @@ -0,0 +1,55 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Autofac.Builder; +using Microsoft.Extensions.Configuration; +using Steeltoe.CloudFoundry.Connector; +using Steeltoe.CloudFoundry.Connector.MySql; +using Steeltoe.CloudFoundry.Connector.Services; +using System; +using System.Data; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac +{ + public static class MySqlContainerBuilderExtensions + { + private static string[] mySqlAssemblies = new string[] { "MySql.Data", "MySqlConnector" }; + private static string[] mySqlTypeNames = new string[] { "MySql.Data.MySqlClient.MySqlConnection" }; + + public static IRegistrationBuilder RegisterMySqlConnection(this ContainerBuilder container, IConfiguration config) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (config == null) + { + throw new ArgumentNullException(nameof(config)); + } + + Type mySqlConnection = ConnectorHelpers.FindType(mySqlAssemblies, mySqlTypeNames); + if (mySqlConnection == null) + { + throw new ConnectorException("Unable to find MySqlConnection, are you missing a MySql reference?"); + } + + MySqlProviderConnectorOptions mySqlConfig = new MySqlProviderConnectorOptions(config); + MySqlServiceInfo info = config.GetSingletonServiceInfo(); + MySqlProviderConnectorFactory factory = new MySqlProviderConnectorFactory(info, mySqlConfig, mySqlConnection); + return container.Register(c => factory.Create(null)).As(); + } + } +} diff --git a/src/Steeltoe.CloudFoundry.ConnectorAutofac/OAuthContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorAutofac/OAuthContainerBuilderExtensions.cs new file mode 100644 index 0000000..28ef66c --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorAutofac/OAuthContainerBuilderExtensions.cs @@ -0,0 +1,56 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Autofac.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Options; +using Steeltoe.CloudFoundry.Connector; +using Steeltoe.CloudFoundry.Connector.OAuth; +using Steeltoe.CloudFoundry.Connector.Services; +using System; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac +{ + public static class OAuthContainerBuilderExtensions + { + public static IRegistrationBuilder RegisterOAuthServiceOptions(this ContainerBuilder container, IConfiguration config, string serviceName = null) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (config == null) + { + throw new ArgumentNullException(nameof(config)); + } + + OAuthConnectorOptions oauthConfig = new OAuthConnectorOptions(config); + + SsoServiceInfo info; + if (serviceName == null) + { + info = config.GetSingletonServiceInfo(); + } + else + { + info = config.GetRequiredServiceInfo(serviceName); + } + + OAuthConnectorFactory factory = new OAuthConnectorFactory(info, oauthConfig); + return container.Register(c => factory.Create(null)).As>(); + } + } +} diff --git a/src/Steeltoe.CloudFoundry.ConnectorAutofac/PostgreSqlContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorAutofac/PostgreSqlContainerBuilderExtensions.cs new file mode 100644 index 0000000..f1b0ad1 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorAutofac/PostgreSqlContainerBuilderExtensions.cs @@ -0,0 +1,55 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Autofac.Builder; +using Microsoft.Extensions.Configuration; +using Steeltoe.CloudFoundry.Connector; +using Steeltoe.CloudFoundry.Connector.PostgreSql; +using Steeltoe.CloudFoundry.Connector.Services; +using System; +using System.Data; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac +{ + public static class PostgreSqlContainerBuilderExtensions + { + private static string[] postgreSqlAssemblies = new string[] { "Npgsql" }; + private static string[] postgreSqlTypeNames = new string[] { "Npgsql.NpgsqlConnection" }; + + public static IRegistrationBuilder RegisterPostgreSqlConnection(this ContainerBuilder container, IConfiguration config) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (config == null) + { + throw new ArgumentNullException(nameof(config)); + } + + Type postgreSqlConnection = ConnectorHelpers.FindType(postgreSqlAssemblies, postgreSqlTypeNames); + if (postgreSqlConnection == null) + { + throw new ConnectorException("Unable to find NpgsqlConnection, are you missing a reference to Npgsql?"); + } + + var postgreSqlConfig = new PostgresProviderConnectorOptions(config); + PostgresServiceInfo info = config.GetSingletonServiceInfo(); + PostgresProviderConnectorFactory factory = new PostgresProviderConnectorFactory(info, postgreSqlConfig, postgreSqlConnection); + return container.Register(c => factory.Create(null)).As(); + } + } +} diff --git a/src/Steeltoe.CloudFoundry.ConnectorAutofac/RabbitMQContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorAutofac/RabbitMQContainerBuilderExtensions.cs new file mode 100644 index 0000000..81c90d1 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorAutofac/RabbitMQContainerBuilderExtensions.cs @@ -0,0 +1,66 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Autofac.Builder; +using Microsoft.Extensions.Configuration; +using Steeltoe.CloudFoundry.Connector; +using Steeltoe.CloudFoundry.Connector.RabbitMQ; +using Steeltoe.CloudFoundry.Connector.Services; +using System; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac +{ + public static class RabbitMQContainerBuilderExtensions + { + private static string[] rabbitMQAssemblies = new string[] { "RabbitMQ.Client" }; + private static string[] rabbitMQInterfaceTypeNames = new string[] { "RabbitMQ.Client.IConnectionFactory" }; + private static string[] rabbitMQImplementationTypeNames = new string[] { "RabbitMQ.Client.ConnectionFactory" }; + + public static IRegistrationBuilder RegisterRabbitMQConnection(this ContainerBuilder container, IConfiguration config, string serviceName = null) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (config == null) + { + throw new ArgumentNullException(nameof(config)); + } + + Type rabbitMQInterfaceType = ConnectorHelpers.FindType(rabbitMQAssemblies, rabbitMQInterfaceTypeNames); + Type rabbitMQImplementationType = ConnectorHelpers.FindType(rabbitMQAssemblies, rabbitMQImplementationTypeNames); + if (rabbitMQInterfaceType == null || rabbitMQImplementationType == null) + { + throw new ConnectorException("Unable to find ConnectionFactory, are you missing RabbitMQ assembly"); + } + + RabbitMQServiceInfo info; + if (serviceName == null) + { + info = config.GetSingletonServiceInfo(); + } + else + { + info = config.GetRequiredServiceInfo(serviceName); + } + + RabbitMQProviderConnectorOptions rabbitMQConfig = new RabbitMQProviderConnectorOptions(config); + RabbitMQProviderConnectorFactory factory = new RabbitMQProviderConnectorFactory(info, rabbitMQConfig, rabbitMQImplementationType); + + return container.Register(c => factory.Create(null)).As(rabbitMQInterfaceType, rabbitMQImplementationType); + } + } +} diff --git a/src/Steeltoe.CloudFoundry.ConnectorAutofac/RedisContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorAutofac/RedisContainerBuilderExtensions.cs new file mode 100644 index 0000000..dca76d9 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorAutofac/RedisContainerBuilderExtensions.cs @@ -0,0 +1,107 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Autofac.Builder; +using Microsoft.Extensions.Configuration; +using Steeltoe.CloudFoundry.Connector; +using Steeltoe.CloudFoundry.Connector.Redis; +using Steeltoe.CloudFoundry.Connector.Services; +using Steeltoe.CloudFoundry.ConnectorBase.Cache; +using System; +using System.Reflection; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac +{ + public static class RedisContainerBuilderExtensions + { + public static IRegistrationBuilder RegisterDistributedRedisCache( + this ContainerBuilder container, + IConfiguration config, + string serviceName = null) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (config == null) + { + throw new ArgumentNullException(nameof(config)); + } + + Type redisInterface = RedisTypeLocator.MicrosoftRedisInterface; + Type redisImplementation = RedisTypeLocator.MicrosoftRedisImplementation; + Type redisOptions = RedisTypeLocator.MicrosoftRedisOptions; + if (redisInterface == null || redisImplementation == null || redisOptions == null) + { + throw new ConnectorException("Unable to find required Redis types, are you missing the Microsoft.Extensions.Caching.Redis Nuget package?"); + } + + RedisServiceInfo info; + if (serviceName != null) + { + info = config.GetRequiredServiceInfo(serviceName); + } + else + { + info = config.GetSingletonServiceInfo(); + } + + RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config); + RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, null); + return container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation); + } + + public static IRegistrationBuilder RegisterRedisConnectionMultiplexer( + this ContainerBuilder container, + IConfiguration config, + string serviceName = null) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (config == null) + { + throw new ArgumentNullException(nameof(config)); + } + + Type redisInterface = RedisTypeLocator.StackExchangeRedisInterface; + Type redisImplementation = RedisTypeLocator.StackExchangeRedisImplementation; + Type redisOptions = RedisTypeLocator.StackExchangeRedisOptions; + MethodInfo initializer = RedisTypeLocator.StackExchangeInitializer; + + if (redisInterface == null || redisImplementation == null || redisOptions == null || initializer == null) + { + throw new ConnectorException("Unable to find required Redis types, are you missing a StackExchange.Redis Nuget Package?"); + } + + RedisServiceInfo info; + if (serviceName != null) + { + info = config.GetRequiredServiceInfo(serviceName); + } + else + { + info = config.GetSingletonServiceInfo(); + } + + RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config); + RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig, redisImplementation, redisOptions, initializer ?? null); + return container.Register(c => factory.Create(null)).As(redisInterface, redisImplementation); + } + } +} diff --git a/src/Steeltoe.CloudFoundry.ConnectorAutofac/SqlServerContainerBuilderExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorAutofac/SqlServerContainerBuilderExtensions.cs new file mode 100644 index 0000000..0b51bf8 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorAutofac/SqlServerContainerBuilderExtensions.cs @@ -0,0 +1,55 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Autofac.Builder; +using Microsoft.Extensions.Configuration; +using Steeltoe.CloudFoundry.Connector; +using Steeltoe.CloudFoundry.Connector.Services; +using Steeltoe.CloudFoundry.Connector.SqlServer; +using System; +using System.Data; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac +{ + public static class SqlServerContainerBuilderExtensions + { + private static string[] sqlServerAssemblies = new string[] { "System.Data.SqlClient" }; + private static string[] sqlServerTypeNames = new string[] { "System.Data.SqlClient.SqlConnection" }; + + public static IRegistrationBuilder RegisterSqlServerConnection(this ContainerBuilder container, IConfiguration config) + { + if (container == null) + { + throw new ArgumentNullException(nameof(container)); + } + + if (config == null) + { + throw new ArgumentNullException(nameof(config)); + } + + Type sqlServerConnection = ConnectorHelpers.FindType(sqlServerAssemblies, sqlServerTypeNames); + if (sqlServerConnection == null) + { + throw new ConnectorException("Unable to find System.Data.SqlClient.SqlConnection, are you missing a System.Data.SqlClient reference?"); + } + + var sqlServerConfig = new SqlServerProviderConnectorOptions(config); + var info = config.GetSingletonServiceInfo(); + var factory = new SqlServerProviderConnectorFactory(info, sqlServerConfig, sqlServerConnection); + return container.Register(c => factory.Create(null)).As(); + } + } +} diff --git a/src/Steeltoe.CloudFoundry.ConnectorAutofac/Steeltoe.CloudFoundry.ConnectorAutofac.csproj b/src/Steeltoe.CloudFoundry.ConnectorAutofac/Steeltoe.CloudFoundry.ConnectorAutofac.csproj new file mode 100644 index 0000000..f3fb325 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorAutofac/Steeltoe.CloudFoundry.ConnectorAutofac.csproj @@ -0,0 +1,40 @@ + + + + CloudFoundry Connector Extensions for Autofac + $(SteeltoeVersion) + $(VersionSuffix) + Pivotal;dtillman + netstandard2.0 + Steeltoe.CloudFoundry.ConnectorAutofac + Steeltoe.CloudFoundry.ConnectorAutofac + CloudFoundry;ASPNET;Autofac + https://steeltoe.io/images/transparent.png + https://steeltoe.io + https://www.apache.org/licenses/LICENSE-2.0 + + + bin\$(Configuration)\$(TargetFramework)\Steeltoe.CloudFoundry.ConnectorAutofac.xml + + + + + + + + All + + + + + + + SA1101;SA1124;SA1201;SA1309;SA1310;SA1401;SA1600;SA1652;1591 + + + + stylecop.json + Always + + + diff --git a/src/Steeltoe.CloudFoundry.ConnectorBase/Cache/RedisTypeLocator.cs b/src/Steeltoe.CloudFoundry.ConnectorBase/Cache/RedisTypeLocator.cs new file mode 100644 index 0000000..384d7e3 --- /dev/null +++ b/src/Steeltoe.CloudFoundry.ConnectorBase/Cache/RedisTypeLocator.cs @@ -0,0 +1,50 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Steeltoe.CloudFoundry.Connector; +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Text; + +namespace Steeltoe.CloudFoundry.ConnectorBase.Cache +{ + public static class RedisTypeLocator + { + private static string[] msftRedisAssemblies = new string[] { "Microsoft.Extensions.Caching.Abstractions", "Microsoft.Extensions.Caching.Redis" }; + private static string[] msftRedisInterfaceTypeNames = new string[] { "Microsoft.Extensions.Caching.Distributed.IDistributedCache" }; + private static string[] msftRedisImplementationTypeNames = new string[] { "Microsoft.Extensions.Caching.Redis.RedisCache" }; + private static string[] msftRedisOptionNames = new string[] { "Microsoft.Extensions.Caching.Redis.RedisCacheOptions" }; + private static string[] stackExchangeRedisAssemblies = new string[] { "StackExchange.Redis", "StackExchange.Redis.StrongName" }; + private static string[] stackExchangeRedisInterfaceTypeNames = new string[] { "StackExchange.Redis.IConnectionMultiplexer" }; + private static string[] stackExchangeRedisImplementationTypeNames = new string[] { "StackExchange.Redis.ConnectionMultiplexer" }; + private static string[] stackExchangeRedisOptionNames = new string[] { "StackExchange.Redis.ConfigurationOptions" }; + + public static Type MicrosoftRedisInterface => ConnectorHelpers.FindType(msftRedisAssemblies, msftRedisInterfaceTypeNames); + + public static Type MicrosoftRedisImplementation => ConnectorHelpers.FindType(msftRedisAssemblies, msftRedisImplementationTypeNames); + + public static Type MicrosoftRedisOptions => ConnectorHelpers.FindType(msftRedisAssemblies, msftRedisOptionNames); + + public static Type StackExchangeRedisInterface => ConnectorHelpers.FindType(stackExchangeRedisAssemblies, stackExchangeRedisInterfaceTypeNames); + + public static Type StackExchangeRedisImplementation => ConnectorHelpers.FindType(stackExchangeRedisAssemblies, stackExchangeRedisImplementationTypeNames); + + public static Type StackExchangeRedisOptions => ConnectorHelpers.FindType(stackExchangeRedisAssemblies, stackExchangeRedisOptionNames); + + public static MethodInfo StackExchangeInitializer => + ConnectorHelpers.FindMethod(StackExchangeRedisImplementation, "Connect", new Type[] { StackExchangeRedisOptions, typeof(TextWriter) }); + } +} diff --git a/src/Steeltoe.CloudFoundry.ConnectorBase/CircuitBreaker/HystrixConnectionFactory.cs b/src/Steeltoe.CloudFoundry.ConnectorBase/CircuitBreaker/HystrixConnectionFactory.cs index e70bb0a..31036a6 100644 --- a/src/Steeltoe.CloudFoundry.ConnectorBase/CircuitBreaker/HystrixConnectionFactory.cs +++ b/src/Steeltoe.CloudFoundry.ConnectorBase/CircuitBreaker/HystrixConnectionFactory.cs @@ -20,12 +20,7 @@ namespace Steeltoe.CloudFoundry.Connector.Hystrix { public HystrixConnectionFactory(object realFactory) { - if (realFactory == null) - { - throw new ArgumentNullException(nameof(realFactory)); - } - - ConnectionFactory = realFactory; + ConnectionFactory = realFactory ?? throw new ArgumentNullException(nameof(realFactory)); } public object ConnectionFactory { get; private set; } diff --git a/src/Steeltoe.CloudFoundry.ConnectorBase/CircuitBreaker/HystrixProviderConnectorFactory.cs b/src/Steeltoe.CloudFoundry.ConnectorBase/CircuitBreaker/HystrixProviderConnectorFactory.cs index baef15b..2973279 100644 --- a/src/Steeltoe.CloudFoundry.ConnectorBase/CircuitBreaker/HystrixProviderConnectorFactory.cs +++ b/src/Steeltoe.CloudFoundry.ConnectorBase/CircuitBreaker/HystrixProviderConnectorFactory.cs @@ -29,19 +29,9 @@ namespace Steeltoe.CloudFoundry.Connector.Hystrix public HystrixProviderConnectorFactory(HystrixRabbitMQServiceInfo sinfo, HystrixProviderConnectorOptions config, Type connectFactory) { - if (config == null) - { - throw new ArgumentNullException(nameof(config)); - } - - if (connectFactory == null) - { - throw new ArgumentNullException(nameof(connectFactory)); - } - _info = sinfo; - _config = config; - _type = connectFactory; + _config = config ?? throw new ArgumentNullException(nameof(config)); + _type = connectFactory ?? throw new ArgumentNullException(nameof(connectFactory)); _setUri = FindSetUriMethod(_type); if (_setUri == null) { diff --git a/src/Steeltoe.CloudFoundry.ConnectorCore/RedisCacheServiceCollectionExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorCore/RedisCacheServiceCollectionExtensions.cs index 5c504d9..d1a0359 100644 --- a/src/Steeltoe.CloudFoundry.ConnectorCore/RedisCacheServiceCollectionExtensions.cs +++ b/src/Steeltoe.CloudFoundry.ConnectorCore/RedisCacheServiceCollectionExtensions.cs @@ -16,6 +16,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Steeltoe.CloudFoundry.Connector.Services; +using Steeltoe.CloudFoundry.ConnectorBase.Cache; using System; using System.IO; using System.Reflection; @@ -195,37 +196,26 @@ namespace Steeltoe.CloudFoundry.Connector.Redis private static void DoAddIDistributedCache(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime) { - string[] redisAssemblies = new string[] { "Microsoft.Extensions.Caching.Abstractions", "Microsoft.Extensions.Caching.Redis" }; - string[] redisInterfaceTypeNames = new string[] { "Microsoft.Extensions.Caching.Distributed.IDistributedCache" }; - string[] redisImplementationTypeNames = new string[] { "Microsoft.Extensions.Caching.Redis.RedisCache" }; - string[] redisOptionNames = new string[] { "Microsoft.Extensions.Caching.Redis.RedisCacheOptions" }; - - Type redisInterface = ConnectorHelpers.FindType(redisAssemblies, redisInterfaceTypeNames); - Type redisConnection = ConnectorHelpers.FindType(redisAssemblies, redisImplementationTypeNames); - Type redisOptions = ConnectorHelpers.FindType(redisAssemblies, redisOptionNames); - - if (redisInterface == null || redisConnection == null || redisOptions == null) + Type interfaceType = RedisTypeLocator.MicrosoftRedisInterface; + Type connectionType = RedisTypeLocator.MicrosoftRedisImplementation; + Type optionsType = RedisTypeLocator.MicrosoftRedisOptions; + if (interfaceType == null || connectionType == null || optionsType == null) { throw new ConnectorException("Unable to find required Redis types, are you missing the Microsoft.Extensions.Caching.Redis Nuget package?"); } RedisCacheConnectorOptions redisConfig = new RedisCacheConnectorOptions(config); - RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig, redisConnection, redisOptions, null); - services.Add(new ServiceDescriptor(redisInterface, factory.Create, contextLifetime)); - services.Add(new ServiceDescriptor(redisConnection, factory.Create, contextLifetime)); + RedisServiceConnectorFactory factory = new RedisServiceConnectorFactory(info, redisConfig, connectionType, optionsType, null); + services.Add(new ServiceDescriptor(interfaceType, factory.Create, contextLifetime)); + services.Add(new ServiceDescriptor(connectionType, factory.Create, contextLifetime)); } private static void DoAddConnectionMultiplexer(IServiceCollection services, RedisServiceInfo info, IConfiguration config, ServiceLifetime contextLifetime) { - string[] redisAssemblies = new string[] { "StackExchange.Redis", "StackExchange.Redis.StrongName" }; - string[] redisInterfaceTypeNames = new string[] { "StackExchange.Redis.IConnectionMultiplexer" }; - string[] redisImplementationTypeNames = new string[] { "StackExchange.Redis.ConnectionMultiplexer" }; - string[] redisOptionNames = new string[] { "StackExchange.Redis.ConfigurationOptions" }; - - Type redisInterface = ConnectorHelpers.FindType(redisAssemblies, redisInterfaceTypeNames); - Type redisImplementation = ConnectorHelpers.FindType(redisAssemblies, redisImplementationTypeNames); - Type redisOptions = ConnectorHelpers.FindType(redisAssemblies, redisOptionNames); - MethodInfo initializer = ConnectorHelpers.FindMethod(redisImplementation, "Connect", new Type[] { redisOptions, typeof(TextWriter) }); + Type redisInterface = RedisTypeLocator.StackExchangeRedisInterface; + Type redisImplementation = RedisTypeLocator.StackExchangeRedisImplementation; + Type redisOptions = RedisTypeLocator.StackExchangeRedisOptions; + MethodInfo initializer = RedisTypeLocator.StackExchangeInitializer; if (redisInterface == null || redisImplementation == null || redisOptions == null || initializer == null) { diff --git a/src/Steeltoe.CloudFoundry.ConnectorCore/SqlServerProviderServiceCollectionExtensions.cs b/src/Steeltoe.CloudFoundry.ConnectorCore/SqlServerProviderServiceCollectionExtensions.cs index e5a60af..5aeb08d 100644 --- a/src/Steeltoe.CloudFoundry.ConnectorCore/SqlServerProviderServiceCollectionExtensions.cs +++ b/src/Steeltoe.CloudFoundry.ConnectorCore/SqlServerProviderServiceCollectionExtensions.cs @@ -24,7 +24,6 @@ namespace Steeltoe.CloudFoundry.Connector.SqlServer public static class SqlServerProviderServiceCollectionExtensions { private static string[] sqlServerAssemblies = new string[] { "System.Data.SqlClient" }; - private static string[] sqlServerTypeNames = new string[] { "System.Data.SqlClient.SqlConnection" }; /// @@ -90,7 +89,7 @@ namespace Steeltoe.CloudFoundry.Connector.SqlServer Type sqlServerConnection = ConnectorHelpers.FindType(sqlServerAssemblies, sqlServerTypeNames); if (sqlServerConnection == null) { - throw new ConnectorException("Unable to find SqlServerConnection, are you missing SqlServer ADO.NET assembly"); + throw new ConnectorException("Unable to find System.Data.SqlClient.SqlConnection, are you missing a reference to System.Data.SqlClient?"); } SqlServerProviderConnectorOptions sqlServerConfig = new SqlServerProviderConnectorOptions(config); diff --git a/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/HystrixContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/HystrixContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..50513f6 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/HystrixContainerBuilderExtensionsTest.cs @@ -0,0 +1,62 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Microsoft.Extensions.Configuration; +using Steeltoe.CloudFoundry.Connector.Hystrix; +using System; +using Xunit; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac.Test +{ + public class HystrixContainerBuilderExtensionsTest + { + [Fact] + public void RegisterHystrixConnection_Requires_Builder() + { + // arrange + IConfiguration config = new ConfigurationBuilder().Build(); + + // act & assert + Assert.Throws(() => HystrixContainerBuilderExtensions.RegisterHystrixConnection(null, config)); + } + + [Fact] + public void RegisterHystrixConnection_Requires_Config() + { + // arrange + ContainerBuilder cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => HystrixContainerBuilderExtensions.RegisterHystrixConnection(cb, null)); + } + + [Fact] + public void RegisterHystrixConnection_AddsToContainer() + { + // arrange + ContainerBuilder container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = HystrixContainerBuilderExtensions.RegisterHystrixConnection(container, config); + var services = container.Build(); + var hystrixFactory = services.Resolve(); + + // assert + Assert.NotNull(hystrixFactory); + Assert.IsType(hystrixFactory); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/MySqlContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/MySqlContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..3da0878 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/MySqlContainerBuilderExtensionsTest.cs @@ -0,0 +1,63 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Microsoft.Extensions.Configuration; +using MySql.Data.MySqlClient; +using System; +using System.Data; +using Xunit; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac.Test +{ + public class MySqlContainerBuilderExtensionsTest + { + [Fact] + public void RegisterMySqlConnection_Requires_Builder() + { + // arrange + IConfiguration config = new ConfigurationBuilder().Build(); + + // act & assert + Assert.Throws(() => MySqlContainerBuilderExtensions.RegisterMySqlConnection(null, config)); + } + + [Fact] + public void RegisterMySqlConnection_Requires_Config() + { + // arrange + ContainerBuilder cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => MySqlContainerBuilderExtensions.RegisterMySqlConnection(cb, null)); + } + + [Fact] + public void RegisterMySqlConnection_AddsToContainer() + { + // arrange + ContainerBuilder container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = MySqlContainerBuilderExtensions.RegisterMySqlConnection(container, config); + var services = container.Build(); + var dbConn = services.Resolve(); + + // assert + Assert.NotNull(dbConn); + Assert.IsType(dbConn); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/OAuthContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/OAuthContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..6995efb --- /dev/null +++ b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/OAuthContainerBuilderExtensionsTest.cs @@ -0,0 +1,64 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Options; +using Steeltoe.CloudFoundry.Connector; +using Steeltoe.CloudFoundry.Connector.OAuth; +using System; +using Xunit; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac.Test +{ + public class OAuthContainerBuilderExtensionsTest + { + [Fact] + public void RegisterOAuthServiceOptions_Requires_Builder() + { + // arrange + IConfiguration config = new ConfigurationBuilder().Build(); + + // act & assert + Assert.Throws(() => OAuthContainerBuilderExtensions.RegisterOAuthServiceOptions(null, config)); + } + + [Fact] + public void RegisterOAuthServiceOptions_Requires_Config() + { + // arrange + ContainerBuilder cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => OAuthContainerBuilderExtensions.RegisterOAuthServiceOptions(cb, null)); + } + + [Fact] + public void RegisterOAuthServiceOptions_AddsToContainer() + { + // arrange + ContainerBuilder container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = OAuthContainerBuilderExtensions.RegisterOAuthServiceOptions(container, config); + var services = container.Build(); + var options = services.Resolve>(); + + // assert + Assert.NotNull(options); + Assert.IsType>(options); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/PostgreSqlContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/PostgreSqlContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..91d55bd --- /dev/null +++ b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/PostgreSqlContainerBuilderExtensionsTest.cs @@ -0,0 +1,67 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Microsoft.Extensions.Configuration; +using Npgsql; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac.Test +{ + public class PostgreSqlContainerBuilderExtensionsTest + { + [Fact] + public void RegisterPostgreSqlConnection_Requires_Builder() + { + // arrange + IConfiguration config = new ConfigurationBuilder().Build(); + + // act & assert + Assert.Throws(() => PostgreSqlContainerBuilderExtensions.RegisterPostgreSqlConnection(null, config)); + } + + [Fact] + public void RegisterPostgreSqlConnection_Requires_Config() + { + // arrange + ContainerBuilder cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => PostgreSqlContainerBuilderExtensions.RegisterPostgreSqlConnection(cb, null)); + } + + [Fact] + public void RegisterPostgreSqlConnection_AddsToContainer() + { + // arrange + ContainerBuilder container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = PostgreSqlContainerBuilderExtensions.RegisterPostgreSqlConnection(container, config); + var services = container.Build(); + var dbConn = services.Resolve(); + + // assert + Assert.NotNull(dbConn); + Assert.IsType(dbConn); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/RabbitMQContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/RabbitMQContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..7d88bd3 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/RabbitMQContainerBuilderExtensionsTest.cs @@ -0,0 +1,65 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Microsoft.Extensions.Configuration; +using RabbitMQ.Client; +using System; +using Xunit; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac.Test +{ + public class RabbitMQContainerBuilderExtensionsTest + { + [Fact] + public void RegisterRabbitMQConnection_Requires_Builder() + { + // arrange + IConfiguration config = new ConfigurationBuilder().Build(); + + // act & assert + Assert.Throws(() => RabbitMQContainerBuilderExtensions.RegisterRabbitMQConnection(null, config)); + } + + [Fact] + public void RegisterRabbitMQConnection_Requires_Config() + { + // arrange + ContainerBuilder cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => RabbitMQContainerBuilderExtensions.RegisterRabbitMQConnection(cb, null)); + } + + [Fact] + public void RegisterRabbitMQConnection_AddsToContainer() + { + // arrange + ContainerBuilder container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = RabbitMQContainerBuilderExtensions.RegisterRabbitMQConnection(container, config); + var services = container.Build(); + var rabbitMQIFactory = services.Resolve(); + var rabbitMQFactory = services.Resolve(); + + // assert + Assert.NotNull(rabbitMQIFactory); + Assert.NotNull(rabbitMQFactory); + Assert.IsType(rabbitMQIFactory); + Assert.IsType(rabbitMQFactory); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/RedisContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/RedisContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..2a4cec6 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/RedisContainerBuilderExtensionsTest.cs @@ -0,0 +1,116 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Redis; +using Microsoft.Extensions.Configuration; +using StackExchange.Redis; +using System; +using System.Collections.Generic; +using Xunit; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac.Test +{ + public class RedisContainerBuilderExtensionsTest + { + [Fact] + public void RegisterRedisCacheConnection_Requires_Builder() + { + // arrange + IConfiguration config = new ConfigurationBuilder().Build(); + + // act & assert + Assert.Throws(() => RedisContainerBuilderExtensions.RegisterDistributedRedisCache(null, config)); + } + + [Fact] + public void RegisterRedisCacheConnection_Requires_Config() + { + // arrange + ContainerBuilder cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => RedisContainerBuilderExtensions.RegisterDistributedRedisCache(cb, null)); + } + + [Fact] + public void RegisterRedisCacheConnection_AddsToContainer() + { + // arrange + ContainerBuilder container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = RedisContainerBuilderExtensions.RegisterDistributedRedisCache(container, config); + var services = container.Build(); + var redisCache = services.Resolve(); + var redisCacheFromI = services.Resolve(); + + // assert + Assert.NotNull(redisCache); + Assert.IsType(redisCache); + Assert.NotNull(redisCacheFromI); + Assert.IsType(redisCacheFromI); + } + + [Fact] + public void RegisterRedisConnectionMultiplexerConnection_Requires_Builder() + { + // arrange + IConfiguration config = new ConfigurationBuilder().Build(); + + // act & assert + Assert.Throws(() => RedisContainerBuilderExtensions.RegisterRedisConnectionMultiplexer(null, config)); + } + + [Fact] + public void RegisterRedisConnectionMultiplexerConnection_Requires_Config() + { + // arrange + ContainerBuilder cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => RedisContainerBuilderExtensions.RegisterRedisConnectionMultiplexer(cb, null)); + } + + [Fact] + public void RegisterRedisConnectionMultiplexerConnection_AddsToContainer() + { + // arrange + ContainerBuilder container = new ContainerBuilder(); + var appsettings = new Dictionary() + { + ["redis:client:abortOnConnectFail"] = "false", + ["redis:client:connectTimeout"] = "1" + }; + + ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddInMemoryCollection(appsettings); + var config = configurationBuilder.Build(); + + // act + var regBuilder = RedisContainerBuilderExtensions.RegisterRedisConnectionMultiplexer(container, config); + var services = container.Build(); + var redisConnectionMultiplexer = services.Resolve(); + var redisIConnectionMultiplexer = services.Resolve(); + + // assert + Assert.NotNull(redisConnectionMultiplexer); + Assert.IsType(redisConnectionMultiplexer); + Assert.NotNull(redisIConnectionMultiplexer); + Assert.IsType(redisIConnectionMultiplexer); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/SqlServerContainerBuilderExtensionsTest.cs b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/SqlServerContainerBuilderExtensionsTest.cs new file mode 100644 index 0000000..6586d1c --- /dev/null +++ b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/SqlServerContainerBuilderExtensionsTest.cs @@ -0,0 +1,63 @@ +// Copyright 2017 the original author or authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using Autofac; +using Microsoft.Extensions.Configuration; +using System; +using System.Data; +using System.Data.SqlClient; +using Xunit; + +namespace Steeltoe.CloudFoundry.ConnectorAutofac.Test +{ + public class SqlServerContainerBuilderExtensionsTest + { + [Fact] + public void RegisterSqlServerConnection_Requires_Builder() + { + // arrange + IConfiguration config = new ConfigurationBuilder().Build(); + + // act & assert + Assert.Throws(() => SqlServerContainerBuilderExtensions.RegisterSqlServerConnection(null, config)); + } + + [Fact] + public void RegisterSqlServerConnection_Requires_Config() + { + // arrange + ContainerBuilder cb = new ContainerBuilder(); + + // act & assert + Assert.Throws(() => SqlServerContainerBuilderExtensions.RegisterSqlServerConnection(cb, null)); + } + + [Fact] + public void RegisterSqlServerConnection_AddsToContainer() + { + // arrange + ContainerBuilder container = new ContainerBuilder(); + IConfiguration config = new ConfigurationBuilder().Build(); + + // act + var regBuilder = SqlServerContainerBuilderExtensions.RegisterSqlServerConnection(container, config); + var services = container.Build(); + var dbConn = services.Resolve(); + + // assert + Assert.NotNull(dbConn); + Assert.IsType(dbConn); + } + } +} diff --git a/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/Steeltoe.CloudFoundry.ConnectorAutofac.Test.csproj b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/Steeltoe.CloudFoundry.ConnectorAutofac.Test.csproj new file mode 100644 index 0000000..42003b4 --- /dev/null +++ b/test/Steeltoe.CloudFoundry.ConnectorAutofac.Test/Steeltoe.CloudFoundry.ConnectorAutofac.Test.csproj @@ -0,0 +1,44 @@ + + + + + net461 + + + + + + + + + + + + + + + + All + + + + + SA1101;SA1124;SA1201;SA1309;SA1310;SA1401;SA1600;SA1652;1591 + + + + stylecop.json + Always + + + + + + + + + + PreserveNewest + + + \ No newline at end of file diff --git a/test/Steeltoe.CloudFoundry.ConnectorBase.Test/Cache/RedisServiceConnectorFactoryTest.cs b/test/Steeltoe.CloudFoundry.ConnectorBase.Test/Cache/RedisServiceConnectorFactoryTest.cs index 3162f56..2701720 100644 --- a/test/Steeltoe.CloudFoundry.ConnectorBase.Test/Cache/RedisServiceConnectorFactoryTest.cs +++ b/test/Steeltoe.CloudFoundry.ConnectorBase.Test/Cache/RedisServiceConnectorFactoryTest.cs @@ -63,7 +63,8 @@ namespace Steeltoe.CloudFoundry.Connector.Redis.Test Port = 1234, Password = "password", InstanceName = "instanceId", - AbortOnConnectFail = false + AbortOnConnectFail = false, + ConnectTimeout = 1 }; RedisServiceInfo si = new RedisServiceInfo("myId", "127.0.0.1", 4321, "sipassword") { diff --git a/test/Steeltoe.CloudFoundry.ConnectorBase.Test/Steeltoe.CloudFoundry.ConnectorBase.Test.csproj b/test/Steeltoe.CloudFoundry.ConnectorBase.Test/Steeltoe.CloudFoundry.ConnectorBase.Test.csproj index 2920970..ab45d1d 100644 --- a/test/Steeltoe.CloudFoundry.ConnectorBase.Test/Steeltoe.CloudFoundry.ConnectorBase.Test.csproj +++ b/test/Steeltoe.CloudFoundry.ConnectorBase.Test/Steeltoe.CloudFoundry.ConnectorBase.Test.csproj @@ -22,7 +22,7 @@ - + diff --git a/test/Steeltoe.CloudFoundry.ConnectorCore.Test/RedisCacheServiceCollectionExtensionsTest.cs b/test/Steeltoe.CloudFoundry.ConnectorCore.Test/RedisCacheServiceCollectionExtensionsTest.cs index c32a2dd..b4b6711 100644 --- a/test/Steeltoe.CloudFoundry.ConnectorCore.Test/RedisCacheServiceCollectionExtensionsTest.cs +++ b/test/Steeltoe.CloudFoundry.ConnectorCore.Test/RedisCacheServiceCollectionExtensionsTest.cs @@ -194,7 +194,8 @@ namespace Steeltoe.CloudFoundry.Connector.Redis.Test ["redis:client:host"] = "127.0.0.1", ["redis:client:port"] = "1234", ["redis:client:password"] = "password", - ["redis:client:abortOnConnectFail"] = "false" + ["redis:client:abortOnConnectFail"] = "false", + ["redis:client:connectTimeout"] = "1" }; ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); diff --git a/test/Steeltoe.CloudFoundry.ConnectorCore.Test/Steeltoe.CloudFoundry.ConnectorCore.Test.csproj b/test/Steeltoe.CloudFoundry.ConnectorCore.Test/Steeltoe.CloudFoundry.ConnectorCore.Test.csproj index b911d01..dd6dc3a 100644 --- a/test/Steeltoe.CloudFoundry.ConnectorCore.Test/Steeltoe.CloudFoundry.ConnectorCore.Test.csproj +++ b/test/Steeltoe.CloudFoundry.ConnectorCore.Test/Steeltoe.CloudFoundry.ConnectorCore.Test.csproj @@ -24,14 +24,13 @@ - - - + +