From e36f41b0c458d5db0eafd02e1b8c894fe8f22f92 Mon Sep 17 00:00:00 2001 From: Ilya Naydanov Date: Sat, 22 Jul 2023 01:16:44 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B7=D0=BE=D0=BB=D0=B2=D0=B0=20=D0=B8=D0=BC=D0=B5=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=B7=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=81=D0=B8=D0=BC=D0=BE=D1=81=D1=82=D0=B5=D0=B9=20=D1=87=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B7=20ServiceProvider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DependencyInjection/UnityTest.cs | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Tests/NewPlatform.Flexberry.ORM.ODataService.Tests/DependencyInjection/UnityTest.cs diff --git a/Tests/NewPlatform.Flexberry.ORM.ODataService.Tests/DependencyInjection/UnityTest.cs b/Tests/NewPlatform.Flexberry.ORM.ODataService.Tests/DependencyInjection/UnityTest.cs new file mode 100644 index 00000000..67a57f32 --- /dev/null +++ b/Tests/NewPlatform.Flexberry.ORM.ODataService.Tests/DependencyInjection/UnityTest.cs @@ -0,0 +1,127 @@ +namespace NewPlatform.Flexberry.ORM.ODataService.Tests.DependencyInjection +{ + using ICSSoft.Services; + using ICSSoft.STORMNET.Security; + + using Microsoft.Extensions.DependencyInjection; + + using Unity; + + using Xunit; + + /// + /// Unity dependecy injection tests. + /// + public class UnityTest : BaseODataServiceIntegratedTest + { +#if NETCOREAPP + /// + /// Default constructor. + /// + /// Custom web application factory for tests. + /// Debug output. + public UnityTest(CustomWebApplicationFactory factory, Xunit.Abstractions.ITestOutputHelper output) + : base(factory, output) + { + } +#endif +#if NETCOREAPP + /// + /// Resolving a named dependency through extension method of IServiceProvider from ICSSoft.Services. + /// + [Fact] + public void NamedDependencyResolveTest() + { + // Arrange + var serviceProvider = _factory.Services; + var unityContainer = serviceProvider.GetService(); + var securityManager = new EmptySecurityManager(); + + // Act + unityContainer.RegisterInstance("named dependency", securityManager); + + // Assert + var service = serviceProvider.GetService("named dependency"); + Assert.Equal(securityManager, service); + } + + /// + /// Resolving non registered named dependency through extension method of IServiceProvider from ICSSoft.Services. + /// + [Fact] + public void NamedDependencyNotRegisteredResolveTest() + { + // Arrange + var serviceProvider = _factory.Services; + var unityContainer = serviceProvider.GetService(); + + // Act + // - none - + + // Assert + Assert.Throws(() => serviceProvider.GetService("non-existing")); + } + + /// + /// Resolving regular (typed) dependency. + /// We check that our extension method does not break regular resolving. + /// + [Fact] + public void TypeDependencyResolveTest() + { + // Arrange + var serviceProvider = _factory.Services; + var unityContainer = serviceProvider.GetService(); + var securityManager = new EmptySecurityManager(); + + // Act + unityContainer.RegisterInstance(securityManager); + + // Assert + var service = serviceProvider.GetService(); + Assert.Equal(securityManager, service); + } + + /// + /// Registered dependencies are successfully injected into the constructor of a resolved class. + /// We check that our extension method does not break regular resolving. + /// + [Fact] + public void TypeDependencyConstructorInjectionTest() + { + // Arrange + var serviceProvider = _factory.Services; + var unityContainer = serviceProvider.GetService(); + var securityManager = new EmptySecurityManager(); + + // Act + unityContainer.RegisterInstance(securityManager); + unityContainer.RegisterSingleton(); + + // Assert + var service = serviceProvider.GetService(); + Assert.Equal(securityManager, service.SecurityManager); + } +#endif + + /// + /// For testing that registered dependencies are succesfully injected into a constructor of a resolved class. + /// + internal class ConstructorInjectionTestObject + { + /// + /// Injected dependency. + /// + internal ISecurityManager SecurityManager { get; set; } + + /// + /// A constructor with injected dependency. + /// + /// Injected dependency. + internal ConstructorInjectionTestObject(ISecurityManager securityManager) + { + this.SecurityManager = securityManager; + } + } + } +}