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; + } + } + } +}