1 |
fad0afca
|
Dominik Chlouba
|
using ExtCore.Data.Abstractions;
|
2 |
|
|
using ExtCore.Data.EntityFramework;
|
3 |
|
|
using ExtCore.Infrastructure;
|
4 |
|
|
using FluentValidation;
|
5 |
|
|
using Leuze.Core.Application.Behaviors;
|
6 |
|
|
using Leuze.Core.Application.Identity;
|
7 |
|
|
using Leuze.Core.Infrastructure.Persistence.Interceptors;
|
8 |
|
|
using MediatR;
|
9 |
|
|
using Microsoft.AspNetCore.Http;
|
10 |
|
|
using Microsoft.AspNetCore.Identity;
|
11 |
|
|
using Microsoft.EntityFrameworkCore;
|
12 |
|
|
using Microsoft.Extensions.DependencyInjection;
|
13 |
|
|
using Microsoft.Extensions.Logging;
|
14 |
|
|
using Microsoft.Extensions.Options;
|
15 |
|
|
using Serilog;
|
16 |
|
|
using System;
|
17 |
|
|
using System.Linq;
|
18 |
|
|
|
19 |
|
|
namespace Leuze.Tests.Configuration
|
20 |
|
|
{
|
21 |
|
|
/// <summary>
|
22 |
|
|
///
|
23 |
|
|
/// </summary>
|
24 |
|
|
public class DatabaseInMemoryFixture : IDisposable
|
25 |
|
|
{
|
26 |
|
|
/// <summary>
|
27 |
|
|
/// Provider for registered services for fixture
|
28 |
|
|
/// </summary>
|
29 |
|
|
public IServiceProvider Services { get; private set; }
|
30 |
|
|
|
31 |
|
|
/// <summary>
|
32 |
|
|
/// Instance of database context
|
33 |
|
|
/// </summary>
|
34 |
|
|
public TestingStorageContext Storage { get; private set; }
|
35 |
|
|
|
36 |
|
|
/// <summary>
|
37 |
|
|
/// Instance of user manager
|
38 |
|
|
/// </summary>
|
39 |
|
|
public UserManager<ApplicationUser> UserManager { get; private set; }
|
40 |
|
|
|
41 |
|
|
/// <summary>
|
42 |
|
|
/// Instance of role manager
|
43 |
|
|
/// </summary>
|
44 |
|
|
public RoleManager<ApplicationRole> RoleManager { get; private set; }
|
45 |
|
|
|
46 |
578c842e
|
Tomáš Orlovský
|
/// <summary>
|
47 |
|
|
/// Instance of role manager
|
48 |
|
|
/// </summary>
|
49 |
|
|
public SignInManager<ApplicationUser> SignInManager { get; private set; }
|
50 |
|
|
|
51 |
fad0afca
|
Dominik Chlouba
|
/// <summary>
|
52 |
|
|
///
|
53 |
|
|
/// </summary>
|
54 |
|
|
/// <param name="services"></param>
|
55 |
|
|
public DatabaseInMemoryFixture(ServiceCollection services)
|
56 |
|
|
{
|
57 |
|
|
Log.Logger = new LoggerConfiguration()
|
58 |
|
|
.WriteTo.Console()
|
59 |
|
|
.CreateLogger();
|
60 |
|
|
|
61 |
|
|
Utilities.LoadExtensions();
|
62 |
|
|
ConfigureServices(services);
|
63 |
|
|
Services = services.BuildServiceProvider();
|
64 |
|
|
|
65 |
|
|
// Assign shortcuts accessors to registered components
|
66 |
|
|
UserManager = Services.GetRequiredService<UserManager<ApplicationUser>>();
|
67 |
|
|
RoleManager = Services.GetRequiredService<RoleManager<ApplicationRole>>();
|
68 |
578c842e
|
Tomáš Orlovský
|
SignInManager = Services.GetRequiredService<SignInManager<ApplicationUser>>();
|
69 |
fad0afca
|
Dominik Chlouba
|
|
70 |
|
|
// Storage: create using registered db context / storage context
|
71 |
|
|
Storage = (TestingStorageContext)Services.GetRequiredService<IStorageContext>();
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
/// <summary>
|
75 |
|
|
///
|
76 |
|
|
/// </summary>
|
77 |
|
|
public void Dispose() { }
|
78 |
|
|
|
79 |
|
|
private void ConfigureServices(IServiceCollection services)
|
80 |
|
|
{
|
81 |
|
|
//Interceptors
|
82 |
|
|
services.AddScoped<DomainEventsInterceptor>();
|
83 |
|
|
|
84 |
|
|
// DbContext/IStorageContext
|
85 |
|
|
services.AddDbContext<TestingStorageContext>((serviceProvider, options) =>
|
86 |
|
|
{
|
87 |
|
|
options.UseInMemoryDatabase("TestingInMemoryDatabase");
|
88 |
|
|
var domainEventsInterceptor = serviceProvider.GetService<DomainEventsInterceptor>();
|
89 |
587b80b5
|
Dominik Chlouba
|
options.AddInterceptors(domainEventsInterceptor);
|
90 |
fad0afca
|
Dominik Chlouba
|
});
|
91 |
|
|
|
92 |
|
|
// Register UserManager & RoleManager
|
93 |
|
|
services.AddIdentity<ApplicationUser, ApplicationRole>()
|
94 |
|
|
.AddEntityFrameworkStores<TestingStorageContext>()
|
95 |
|
|
.AddDefaultTokenProviders();
|
96 |
|
|
|
97 |
|
|
// UserManager & RoleManager require logging and HttpContext dependencies
|
98 |
|
|
services.AddLogging();
|
99 |
|
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
100 |
|
|
services.AddAutoMapper(ExtensionManager.Assemblies);
|
101 |
|
|
services.AddValidatorsFromAssemblies(ExtensionManager.Assemblies);
|
102 |
|
|
services.AddMediatR(ExtensionManager.Assemblies.ToArray());
|
103 |
|
|
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
|
104 |
|
|
|
105 |
|
|
// Register database-specific storage context implementation.
|
106 |
|
|
services.AddScoped<IStorageContext, TestingStorageContext>();
|
107 |
|
|
|
108 |
578c842e
|
Tomáš Orlovský
|
|
109 |
|
|
|
110 |
fad0afca
|
Dominik Chlouba
|
}
|
111 |
|
|
|
112 |
|
|
/// <summary>
|
113 |
|
|
/// For use by DbContextFactory.
|
114 |
|
|
/// </summary>
|
115 |
|
|
/// <returns></returns>
|
116 |
|
|
public static DbContextOptionsBuilder<TestingStorageContext> GetDbContextOptionsBuilder()
|
117 |
|
|
{
|
118 |
|
|
var optionsBuilder = new DbContextOptionsBuilder<TestingStorageContext>();
|
119 |
|
|
optionsBuilder.UseInMemoryDatabase("TestingInMemoryDatabase");
|
120 |
|
|
return optionsBuilder;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
/// <summary>
|
124 |
|
|
/// Utility class to use the IOptions pattern as required by IStorage implementations constructors.
|
125 |
|
|
/// </summary>
|
126 |
|
|
private class TestStorageContextOptions : IOptions<StorageContextOptions>
|
127 |
|
|
{
|
128 |
|
|
public TestStorageContextOptions(StorageContextOptions value_)
|
129 |
|
|
{
|
130 |
|
|
Value = value_;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
public StorageContextOptions Value { get; }
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
}
|