From 6593acbc200cedf9e93c0b617a25d7c28d034f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D1=8F=20=D0=91=D0=B8=D0=B7=D1=8E=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0?= Date: Thu, 26 Dec 2024 13:42:14 +0500 Subject: [PATCH 1/7] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D0=B0=D1=8F=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B1=D1=80=D0=B8=D0=BA=D0=B0=20=D1=80=D0=B5=D1=84=D0=B8?= =?UTF-8?q?=D1=82-=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=BE=D0=B2=20(Refi?= =?UTF-8?q?tClientFactory).=20=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BF=D0=BE=D0=BA=D0=B0=20=D1=82=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=BA=D0=BE=20=D0=B4=D0=BB=D1=8F=20ChangeLog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.API/DependencyInjection.cs | 6 --- DD.Persistence.API/Startup.cs | 4 +- DD.Persistence.App/appsettings.Tests.json | 2 +- DD.Persistence.App/appsettings.json | 5 +- .../Clients/ChangeLogClient.cs | 9 ++-- .../Interfaces/Refit/IRefitChangeLogClient.cs | 2 +- .../Clients/Interfaces/Refit/IRefitClient.cs | 10 ++++ DD.Persistence.Client/DependencyInjection.cs | 28 +++++++++++ DD.Persistence.Client/IRefitClientFactory.cs | 21 +++++++++ .../PersistenceClientFactory.cs | 14 +++--- DD.Persistence.Client/RefitClientFactory.cs | 46 +++++++++++++++++++ .../Controllers/ChangeLogControllerTest.cs | 13 ++++-- .../WebAppFactoryFixture.cs | 4 ++ 13 files changed, 138 insertions(+), 26 deletions(-) create mode 100644 DD.Persistence.Client/Clients/Interfaces/Refit/IRefitClient.cs create mode 100644 DD.Persistence.Client/DependencyInjection.cs create mode 100644 DD.Persistence.Client/IRefitClientFactory.cs create mode 100644 DD.Persistence.Client/RefitClientFactory.cs diff --git a/DD.Persistence.API/DependencyInjection.cs b/DD.Persistence.API/DependencyInjection.cs index b0a71d5..6b0754c 100644 --- a/DD.Persistence.API/DependencyInjection.cs +++ b/DD.Persistence.API/DependencyInjection.cs @@ -16,12 +16,6 @@ namespace DD.Persistence.API; public static class DependencyInjection { - //public static void MapsterSetup() - //{ - // TypeAdapterConfig.GlobalSettings.Default.Config - // .ForType() - // .Ignore(dest => dest.System, dest => dest.SystemId); - //} public static void AddSwagger(this IServiceCollection services, IConfiguration configuration) { services.AddSwaggerGen(c => diff --git a/DD.Persistence.API/Startup.cs b/DD.Persistence.API/Startup.cs index c6c44a4..8f07c31 100644 --- a/DD.Persistence.API/Startup.cs +++ b/DD.Persistence.API/Startup.cs @@ -1,6 +1,7 @@ using DD.Persistence.Database.Model; using DD.Persistence.Database.Postgres; using DD.Persistence.Repository; +using DD.Persistence.Client; namespace DD.Persistence.API; @@ -26,8 +27,7 @@ public class Startup services.AddJWTAuthentication(Configuration); services.AddMemoryCache(); services.AddServices(); - - //DependencyInjection.MapsterSetup(); + services.AddPersistenceClients(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/DD.Persistence.App/appsettings.Tests.json b/DD.Persistence.App/appsettings.Tests.json index 72c43d3..9934757 100644 --- a/DD.Persistence.App/appsettings.Tests.json +++ b/DD.Persistence.App/appsettings.Tests.json @@ -1,6 +1,6 @@ { "DbConnection": { - "Host": "postgres", + "Host": "localhost", "Port": 5432, "Database": "persistence", "Username": "postgres", diff --git a/DD.Persistence.App/appsettings.json b/DD.Persistence.App/appsettings.json index 8002fc7..7ad8c67 100644 --- a/DD.Persistence.App/appsettings.json +++ b/DD.Persistence.App/appsettings.json @@ -19,6 +19,7 @@ "password": 12345, "clientId": "webapi", "grantType": "password", - "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier": "7d9f3574-6574-4ca3-845a-0276eb4aa8f6" - } + "http://schemas.xmlsoap.org/ws/2005/05/identity /claims/nameidentifier": "7d9f3574-6574-4ca3-845a-0276eb4aa8f6" + }, + "ClientUrl": "http://localhost:5000/" } diff --git a/DD.Persistence.Client/Clients/ChangeLogClient.cs b/DD.Persistence.Client/Clients/ChangeLogClient.cs index e10008e..38c2ec4 100644 --- a/DD.Persistence.Client/Clients/ChangeLogClient.cs +++ b/DD.Persistence.Client/Clients/ChangeLogClient.cs @@ -3,16 +3,17 @@ using DD.Persistence.Client.Clients.Base; using DD.Persistence.Client.Clients.Interfaces; using DD.Persistence.Models; using DD.Persistence.Models.Requests; +using DD.Persistence.Client.Clients.Interfaces.Refit; namespace DD.Persistence.Client.Clients; public class ChangeLogClient : BaseClient, IChangeLogClient { - private readonly Interfaces.Refit.IRefitChangeLogClient refitChangeLogClient; + private readonly IRefitChangeLogClient refitChangeLogClient; - public ChangeLogClient(Interfaces.Refit.IRefitChangeLogClient refitChangeLogClient, ILogger logger) : base(logger) + public ChangeLogClient(IRefitClientFactory refitClientFactory, ILogger logger) : base(logger) { - this.refitChangeLogClient = refitChangeLogClient; - } + this.refitChangeLogClient = refitClientFactory.Create(); + } public async Task ClearAndAddRange(Guid idDiscriminator, IEnumerable dtos, CancellationToken token) { diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitChangeLogClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitChangeLogClient.cs index 958e24e..83c240f 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitChangeLogClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitChangeLogClient.cs @@ -4,7 +4,7 @@ using Refit; namespace DD.Persistence.Client.Clients.Interfaces.Refit; -public interface IRefitChangeLogClient : IDisposable +public interface IRefitChangeLogClient : IRefitClient, IDisposable { private const string BaseRoute = "/api/ChangeLog"; diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitClient.cs new file mode 100644 index 0000000..9568a51 --- /dev/null +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitClient.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DD.Persistence.Client.Clients.Interfaces.Refit; +public interface IRefitClient +{ +} diff --git a/DD.Persistence.Client/DependencyInjection.cs b/DD.Persistence.Client/DependencyInjection.cs new file mode 100644 index 0000000..ab4d80f --- /dev/null +++ b/DD.Persistence.Client/DependencyInjection.cs @@ -0,0 +1,28 @@ +using DD.Persistence.Client.Clients; +using DD.Persistence.Client.Clients.Interfaces; +using Microsoft.Extensions.DependencyInjection; + +namespace DD.Persistence.Client; + +/// +/// +/// +public static class DependencyInjection +{ + /// + /// + /// + /// + /// + public static IServiceCollection AddPersistenceClients(this IServiceCollection services) + { + services.AddSingleton(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>)); + services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + //services.AddTransient(); + return services; + } +} diff --git a/DD.Persistence.Client/IRefitClientFactory.cs b/DD.Persistence.Client/IRefitClientFactory.cs new file mode 100644 index 0000000..0b41a9a --- /dev/null +++ b/DD.Persistence.Client/IRefitClientFactory.cs @@ -0,0 +1,21 @@ +using DD.Persistence.Client.Clients.Interfaces.Refit; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DD.Persistence.Client; + +/// +/// Интерфейс для фабрики, которая создает refit-клиентов +/// +/// +public interface IRefitClientFactory where T : IRefitClient +{ + /// + /// Создание refit-клиента + /// + /// + public T Create(); +} diff --git a/DD.Persistence.Client/PersistenceClientFactory.cs b/DD.Persistence.Client/PersistenceClientFactory.cs index ad7c817..1ebfbd5 100644 --- a/DD.Persistence.Client/PersistenceClientFactory.cs +++ b/DD.Persistence.Client/PersistenceClientFactory.cs @@ -105,15 +105,15 @@ namespace DD.Persistence.Client /// Получить клиент для работы с записями ChangeLog /// /// - public IChangeLogClient GetChangeLogClient() - { - var logger = provider.GetRequiredService>(); + //public IChangeLogClient GetChangeLogClient() + //{ + // var logger = provider.GetRequiredService>(); - var restClient = RestService.For(httpClient, RefitSettings); - var client = new ChangeLogClient(restClient, logger); + // var restClient = RestService.For(httpClient, RefitSettings); + // var client = new ChangeLogClient(restClient, logger); - return client; - } + // return client; + //} /// /// Получить клиент для работы c параметрами Wits diff --git a/DD.Persistence.Client/RefitClientFactory.cs b/DD.Persistence.Client/RefitClientFactory.cs new file mode 100644 index 0000000..a83aae5 --- /dev/null +++ b/DD.Persistence.Client/RefitClientFactory.cs @@ -0,0 +1,46 @@ +using DD.Persistence.Client.Clients.Interfaces.Refit; +using DD.Persistence.Client.Helpers; +using Microsoft.Extensions.Configuration; +using Refit; +using System.Text.Json; + +namespace DD.Persistence.Client; + +/// +/// Фабрика, которая создает refit-клиентов +/// +/// +public class RefitClientFactory : IRefitClientFactory where T : IRefitClient +{ + private HttpClient client; + private RefitSettings refitSettings; + + /// + /// + /// + public RefitClientFactory(IConfiguration configuration) + { + this.client = new HttpClient(); + + var baseUrl = configuration.GetSection("ClientUrl").Get()!; + client.BaseAddress = new Uri(baseUrl); + client.Authorize(configuration); + + JsonSerializerOptions JsonSerializerOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + PropertyNameCaseInsensitive = true + }; + refitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); + } + /// + /// создание клиента + /// + /// + public T Create() + { + + + return RestService.For(client, refitSettings); + } +} diff --git a/DD.Persistence.IntegrationTests/Controllers/ChangeLogControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/ChangeLogControllerTest.cs index 3f1bb6c..64419e7 100644 --- a/DD.Persistence.IntegrationTests/Controllers/ChangeLogControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/ChangeLogControllerTest.cs @@ -7,6 +7,11 @@ using DD.Persistence.Models.Requests; using Xunit; using DD.Persistence.Client.Clients.Interfaces; using DD.Persistence.Client; +using DD.Persistence.Client.Clients.Interfaces.Refit; +using DD.Persistence.Client.Clients; +using Microsoft.Extensions.Logging; +using Refit; +using System.Net.Http; namespace DD.Persistence.IntegrationTests.Controllers; public class ChangeLogControllerTest : BaseIntegrationTest @@ -16,10 +21,12 @@ public class ChangeLogControllerTest : BaseIntegrationTest public ChangeLogControllerTest(WebAppFactoryFixture factory) : base(factory) { - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); + var refitClientFactory = scope.ServiceProvider + .GetRequiredService>(); + var logger = scope.ServiceProvider.GetRequiredService>(); - client = persistenceClientFactory.GetChangeLogClient(); + client = scope.ServiceProvider + .GetRequiredService(); } [Fact] diff --git a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs index 76cf71b..d953f50 100644 --- a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs +++ b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs @@ -46,6 +46,7 @@ public class WebAppFactoryFixture : WebApplicationFactory }); services.AddSingleton(); + services.AddPersistenceClients(); var serviceProvider = services.BuildServiceProvider(); @@ -56,6 +57,9 @@ public class WebAppFactoryFixture : WebApplicationFactory dbContext.Database.EnsureCreatedAndMigrated(); dbContext.SaveChanges(); }); + + builder.UseUrls("http://localhost:5000"); + } public override async ValueTask DisposeAsync() From d0cd647849e267f5c010aeb85e2281059f9099c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D1=8F=20=D0=91=D0=B8=D0=B7=D1=8E=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0?= Date: Thu, 26 Dec 2024 15:47:24 +0500 Subject: [PATCH 2/7] =?UTF-8?q?=D0=92=D1=8B=D0=B1=D1=80=D0=BE=D1=81=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD=D0=B8=D1=8F,?= =?UTF-8?q?=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=B2=20appsettings.json=20?= =?UTF-8?q?=D0=B2=20=D1=81=D0=B5=D0=BA=D1=86=D0=B8=D0=B8=20ClientUrl=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D1=83=D0=BA=D0=B0=D0=B7=D0=B0=D0=BD=20=D0=BF?= =?UTF-8?q?=D1=83=D1=82=D1=8C=20=D0=B4=D0=BE=20Persistence-=D1=81=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.Client/DD.Persistence.Client.csproj | 1 + DD.Persistence.Client/RefitClientFactory.cs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/DD.Persistence.Client/DD.Persistence.Client.csproj b/DD.Persistence.Client/DD.Persistence.Client.csproj index d1ae186..8acc827 100644 --- a/DD.Persistence.Client/DD.Persistence.Client.csproj +++ b/DD.Persistence.Client/DD.Persistence.Client.csproj @@ -53,6 +53,7 @@ + diff --git a/DD.Persistence.Client/RefitClientFactory.cs b/DD.Persistence.Client/RefitClientFactory.cs index a83aae5..2b010e2 100644 --- a/DD.Persistence.Client/RefitClientFactory.cs +++ b/DD.Persistence.Client/RefitClientFactory.cs @@ -1,7 +1,9 @@ using DD.Persistence.Client.Clients.Interfaces.Refit; using DD.Persistence.Client.Helpers; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; using Refit; +using System.Configuration; using System.Text.Json; namespace DD.Persistence.Client; @@ -18,11 +20,19 @@ public class RefitClientFactory : IRefitClientFactory where T : IRefitClie /// /// /// - public RefitClientFactory(IConfiguration configuration) + public RefitClientFactory(IConfiguration configuration, ILogger> logger) { this.client = new HttpClient(); - var baseUrl = configuration.GetSection("ClientUrl").Get()!; + var baseUrl = configuration.GetSection("ClientUrl").Get(); + if (String.IsNullOrEmpty(baseUrl)) + { + var exception = new SettingsPropertyNotFoundException("В настройках конфигурации не указан адрес Persistence сервиса."); + + logger.LogError(exception.Message); + + throw exception; + } client.BaseAddress = new Uri(baseUrl); client.Authorize(configuration); From 4ec4ec17ebff6304e5fb03b54a5189d8c50e8bbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D1=8F=20=D0=91=D0=B8=D0=B7=D1=8E=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0?= Date: Fri, 27 Dec 2024 00:37:52 +0500 Subject: [PATCH 3/7] =?UTF-8?q?=D0=9F=D1=83=D1=88=20=D0=B8=D0=B7=D0=BC?= =?UTF-8?q?=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B2=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=D0=B5=20=D1=81=20=D0=B8=D0=BD=D1=82?= =?UTF-8?q?=D0=B5=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=BE=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=BC=D0=B8=20=D1=82=D0=B5=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.API/DD.Persistence.API.csproj | 1 + .../Clients/DataSourceSystemClient.cs | 4 +-- .../Refit/IRefitDataSourceSystemClient.cs | 2 +- .../Interfaces/Refit/IRefitSetpointClient.cs | 2 +- .../Interfaces/Refit/IRefitWitsDataClient.cs | 2 +- .../Clients/SetpointClient.cs | 4 +-- DD.Persistence.Client/DependencyInjection.cs | 11 ++++---- .../PersistenceClientFactory.cs | 28 +++++++++---------- DD.Persistence.Client/RefitClientFactory.cs | 7 ++--- .../DataSourceSystemControllerTest.cs | 11 +++++--- .../Controllers/SetpointControllerTest.cs | 12 +++++--- .../Controllers/WitsDataControllerTest.cs | 12 +++++--- .../WebAppFactoryFixture.cs | 6 ++-- 13 files changed, 56 insertions(+), 46 deletions(-) diff --git a/DD.Persistence.API/DD.Persistence.API.csproj b/DD.Persistence.API/DD.Persistence.API.csproj index 5ed1097..e7d2e57 100644 --- a/DD.Persistence.API/DD.Persistence.API.csproj +++ b/DD.Persistence.API/DD.Persistence.API.csproj @@ -23,6 +23,7 @@ + diff --git a/DD.Persistence.Client/Clients/DataSourceSystemClient.cs b/DD.Persistence.Client/Clients/DataSourceSystemClient.cs index 45e2d29..7e70255 100644 --- a/DD.Persistence.Client/Clients/DataSourceSystemClient.cs +++ b/DD.Persistence.Client/Clients/DataSourceSystemClient.cs @@ -9,9 +9,9 @@ public class DataSourceSystemClient : BaseClient, IDataSourceSystemClient { private readonly IRefitDataSourceSystemClient dataSourceSystemClient; - public DataSourceSystemClient(IRefitDataSourceSystemClient dataSourceSystemClient, ILogger logger) : base(logger) + public DataSourceSystemClient(IRefitClientFactory dataSourceSystemClientFactory, ILogger logger) : base(logger) { - this.dataSourceSystemClient = dataSourceSystemClient; + this.dataSourceSystemClient = dataSourceSystemClientFactory.Create(); } public async Task Add(DataSourceSystemDto dataSourceSystemDto, CancellationToken token) diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitDataSourceSystemClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitDataSourceSystemClient.cs index a4b47eb..313d8cc 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitDataSourceSystemClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitDataSourceSystemClient.cs @@ -2,7 +2,7 @@ using Refit; namespace DD.Persistence.Client.Clients.Interfaces.Refit; -public interface IRefitDataSourceSystemClient : IDisposable +public interface IRefitDataSourceSystemClient : IRefitClient, IDisposable { private const string BaseRoute = "/api/dataSourceSystem"; diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitSetpointClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitSetpointClient.cs index b6e021a..1acb398 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitSetpointClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitSetpointClient.cs @@ -3,7 +3,7 @@ using Refit; namespace DD.Persistence.Client.Clients.Interfaces.Refit; -public interface IRefitSetpointClient : IDisposable +public interface IRefitSetpointClient : IRefitClient, IDisposable { private const string BaseRoute = "/api/setpoint"; diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitWitsDataClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitWitsDataClient.cs index 57bff6f..e7e6d9a 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitWitsDataClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitWitsDataClient.cs @@ -3,7 +3,7 @@ using DD.Persistence.Models; using Refit; namespace DD.Persistence.Client.Clients.Interfaces.Refit; -public interface IRefitWitsDataClient : IDisposable +public interface IRefitWitsDataClient : IRefitClient, IDisposable { private const string BaseRoute = "/api/witsData"; diff --git a/DD.Persistence.Client/Clients/SetpointClient.cs b/DD.Persistence.Client/Clients/SetpointClient.cs index 4a990c5..808f0b9 100644 --- a/DD.Persistence.Client/Clients/SetpointClient.cs +++ b/DD.Persistence.Client/Clients/SetpointClient.cs @@ -10,9 +10,9 @@ public class SetpointClient : BaseClient, ISetpointClient { private readonly IRefitSetpointClient refitSetpointClient; - public SetpointClient(IRefitSetpointClient refitSetpointClient, ILogger logger) : base(logger) + public SetpointClient(IRefitClientFactory refitSetpointClientFactory, ILogger logger) : base(logger) { - this.refitSetpointClient = refitSetpointClient; + this.refitSetpointClient = refitSetpointClientFactory.Create(); } public async Task> GetCurrent(IEnumerable setpointKeys, CancellationToken token) diff --git a/DD.Persistence.Client/DependencyInjection.cs b/DD.Persistence.Client/DependencyInjection.cs index ab4d80f..53791bb 100644 --- a/DD.Persistence.Client/DependencyInjection.cs +++ b/DD.Persistence.Client/DependencyInjection.cs @@ -18,11 +18,12 @@ public static class DependencyInjection { services.AddSingleton(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>)); services.AddTransient(); - //services.AddTransient(); - //services.AddTransient(); - //services.AddTransient(); - //services.AddTransient(); - //services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + //services.AddTransient(); + //services.AddTransient, TimeSeriesClient<>>(); + //services.AddTransient(); + //services.AddTransient(); return services; } } diff --git a/DD.Persistence.Client/PersistenceClientFactory.cs b/DD.Persistence.Client/PersistenceClientFactory.cs index 1ebfbd5..c48424c 100644 --- a/DD.Persistence.Client/PersistenceClientFactory.cs +++ b/DD.Persistence.Client/PersistenceClientFactory.cs @@ -47,15 +47,15 @@ namespace DD.Persistence.Client /// Получить клиент для работы с уставками /// /// - public ISetpointClient GetSetpointClient() - { - var logger = provider.GetRequiredService>(); + //public ISetpointClient GetSetpointClient() + //{ + // var logger = provider.GetRequiredService>(); - var restClient = RestService.For(httpClient, RefitSettings); - var client = new SetpointClient(restClient, logger); + // var restClient = RestService.For(httpClient, RefitSettings); + // var client = new SetpointClient(restClient, logger); - return client; - } + // return client; + //} /// /// Получить клиент для работы с технологическими сообщениями @@ -133,14 +133,14 @@ namespace DD.Persistence.Client /// Получить клиент для работы c системами /// /// - public IDataSourceSystemClient GetDataSourceSystemClient() - { - var logger = provider.GetRequiredService>(); + //public IDataSourceSystemClient GetDataSourceSystemClient() + //{ + // var logger = provider.GetRequiredService>(); - var restClient = RestService.For(httpClient, RefitSettings); - var client = new DataSourceSystemClient(restClient, logger); + // var restClient = RestService.For(httpClient, RefitSettings); + // var client = new DataSourceSystemClient(restClient, logger); - return client; - } + // return client; + //} } } diff --git a/DD.Persistence.Client/RefitClientFactory.cs b/DD.Persistence.Client/RefitClientFactory.cs index 2b010e2..758f79d 100644 --- a/DD.Persistence.Client/RefitClientFactory.cs +++ b/DD.Persistence.Client/RefitClientFactory.cs @@ -20,9 +20,10 @@ public class RefitClientFactory : IRefitClientFactory where T : IRefitClie /// /// /// - public RefitClientFactory(IConfiguration configuration, ILogger> logger) + public RefitClientFactory(IConfiguration configuration, ILogger> logger, HttpClient client) { - this.client = new HttpClient(); + //this.client = factory.CreateClient(); + this.client = client; var baseUrl = configuration.GetSection("ClientUrl").Get(); if (String.IsNullOrEmpty(baseUrl)) @@ -49,8 +50,6 @@ public class RefitClientFactory : IRefitClientFactory where T : IRefitClie /// public T Create() { - - return RestService.For(client, refitSettings); } } diff --git a/DD.Persistence.IntegrationTests/Controllers/DataSourceSystemControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/DataSourceSystemControllerTest.cs index 03d5b8a..853a549 100644 --- a/DD.Persistence.IntegrationTests/Controllers/DataSourceSystemControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/DataSourceSystemControllerTest.cs @@ -6,6 +6,8 @@ using DD.Persistence.Client.Clients.Interfaces; using DD.Persistence.Database.Entity; using DD.Persistence.Models; using Xunit; +using DD.Persistence.Client.Clients.Interfaces.Refit; +using Microsoft.Extensions.Logging; namespace DD.Persistence.IntegrationTests.Controllers { @@ -16,11 +18,12 @@ namespace DD.Persistence.IntegrationTests.Controllers private readonly IMemoryCache memoryCache; public DataSourceSystemControllerTest(WebAppFactoryFixture factory) : base(factory) { - var scope = factory.Services.CreateScope(); - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); + var refitClientFactory = scope.ServiceProvider + .GetRequiredService>(); + var logger = scope.ServiceProvider.GetRequiredService>(); - dataSourceSystemClient = persistenceClientFactory.GetDataSourceSystemClient(); + dataSourceSystemClient = scope.ServiceProvider + .GetRequiredService(); memoryCache = scope.ServiceProvider.GetRequiredService(); } diff --git a/DD.Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs index d79b6c0..27432f3 100644 --- a/DD.Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/SetpointControllerTest.cs @@ -4,6 +4,9 @@ using DD.Persistence.Client.Clients.Interfaces; using DD.Persistence.Database.Model; using System.Net; using Xunit; +using DD.Persistence.Client.Clients.Interfaces.Refit; +using DD.Persistence.Client.Clients; +using Microsoft.Extensions.Logging; namespace DD.Persistence.IntegrationTests.Controllers { @@ -17,11 +20,12 @@ namespace DD.Persistence.IntegrationTests.Controllers } public SetpointControllerTest(WebAppFactoryFixture factory) : base(factory) { - var scope = factory.Services.CreateScope(); - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); + var refitClientFactory = scope.ServiceProvider + .GetRequiredService>(); + var logger = scope.ServiceProvider.GetRequiredService>(); - setpointClient = persistenceClientFactory.GetSetpointClient(); + setpointClient = scope.ServiceProvider + .GetRequiredService(); } [Fact] diff --git a/DD.Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs index 6db5853..6b33804 100644 --- a/DD.Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs @@ -5,6 +5,9 @@ using System.Net; using Xunit; using DD.Persistence.Client.Clients.Interfaces; using DD.Persistence.Client; +using DD.Persistence.Client.Clients.Interfaces.Refit; +using DD.Persistence.Client.Clients; +using Microsoft.Extensions.Logging; namespace DD.Persistence.IntegrationTests.Controllers; public class WitsDataControllerTest : BaseIntegrationTest @@ -13,11 +16,12 @@ public class WitsDataControllerTest : BaseIntegrationTest public WitsDataControllerTest(WebAppFactoryFixture factory) : base(factory) { - var scope = factory.Services.CreateScope(); - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); + var refitClientFactory = scope.ServiceProvider + .GetRequiredService>(); + var logger = scope.ServiceProvider.GetRequiredService>(); - witsDataClient = persistenceClientFactory.GetWitsDataClient(); + witsDataClient = scope.ServiceProvider + .GetRequiredService(); } [Fact] diff --git a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs index d953f50..df6deb1 100644 --- a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs +++ b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs @@ -45,7 +45,8 @@ public class WebAppFactoryFixture : WebApplicationFactory return new TestHttpClientFactory(this); }); - services.AddSingleton(); + services.AddHttpClient(); + //services.AddSingleton(); services.AddPersistenceClients(); var serviceProvider = services.BuildServiceProvider(); @@ -57,9 +58,6 @@ public class WebAppFactoryFixture : WebApplicationFactory dbContext.Database.EnsureCreatedAndMigrated(); dbContext.SaveChanges(); }); - - builder.UseUrls("http://localhost:5000"); - } public override async ValueTask DisposeAsync() From 6873a32667e60fd0d723c8599b612b0e53bbc907 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 27 Dec 2024 13:35:18 +0500 Subject: [PATCH 4/7] =?UTF-8?q?=D0=98=D0=BD=D0=B6=D0=B5=D0=BA=D1=82=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=BE=D0=B9=20=D1=84=D0=B0=D0=B1=D1=80=D0=B8?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20PersistenceClientFact?= =?UTF-8?q?ory=20=D0=B2=20=D0=B8=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=BE=D0=BD=D0=BD=D1=8B=D1=85=20=D1=82=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Clients/Interfaces/ITimeSeriesClient.cs | 2 +- .../Refit/IRefitTechMessagesClient.cs | 2 +- .../Refit/IRefitTimeSeriesClient.cs | 4 +- .../Refit/IRefitTimestampedSetClient.cs | 2 +- .../Clients/TechMessagesClient.cs | 4 +- .../Clients/TimeSeriesClient.cs | 6 +- .../Clients/TimestampedSetClient.cs | 4 +- .../Clients/WitsDataClient.cs | 4 +- DD.Persistence.Client/DependencyInjection.cs | 11 +- .../PersistenceClientFactory.cs | 238 ++++----- .../Controllers/TechMessagesControllerTest.cs | 461 +++++++++--------- .../TimeSeriesBaseControllerTest.cs | 22 +- .../TimestampedSetControllerTest.cs | 11 +- .../Controllers/WitsDataControllerTest.cs | 15 +- 14 files changed, 399 insertions(+), 387 deletions(-) diff --git a/DD.Persistence.Client/Clients/Interfaces/ITimeSeriesClient.cs b/DD.Persistence.Client/Clients/Interfaces/ITimeSeriesClient.cs index 26bbfa6..db619e5 100644 --- a/DD.Persistence.Client/Clients/Interfaces/ITimeSeriesClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/ITimeSeriesClient.cs @@ -6,7 +6,7 @@ namespace DD.Persistence.Client.Clients.Interfaces; /// Клиент для работы с временными данными /// /// -public interface ITimeSeriesClient : IDisposable where TDto : class, new() +public interface ITimeSeriesClient : IDisposable where TDto : class, ITimeSeriesAbstractDto { /// /// Добавление записей diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs index 2638600..1fe63c1 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTechMessagesClient.cs @@ -5,7 +5,7 @@ using Refit; namespace DD.Persistence.Client.Clients.Interfaces.Refit { - public interface IRefitTechMessagesClient : IDisposable + public interface IRefitTechMessagesClient : IRefitClient, IDisposable { private const string BaseRoute = "/api/techMessages"; diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimeSeriesClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimeSeriesClient.cs index 2edc8fe..832cfde 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimeSeriesClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimeSeriesClient.cs @@ -2,8 +2,8 @@ using DD.Persistence.Models; using Refit; namespace DD.Persistence.Client.Clients.Interfaces.Refit; -public interface IRefitTimeSeriesClient : IDisposable - where TDto : class, new() +public interface IRefitTimeSeriesClient : IRefitClient, IDisposable + where TDto : class, ITimeSeriesAbstractDto { private const string BaseRoute = "/api/dataSaub"; diff --git a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimestampedSetClient.cs b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimestampedSetClient.cs index 14db284..6211d2d 100644 --- a/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimestampedSetClient.cs +++ b/DD.Persistence.Client/Clients/Interfaces/Refit/IRefitTimestampedSetClient.cs @@ -3,7 +3,7 @@ using Refit; namespace DD.Persistence.Client.Clients.Interfaces.Refit; -public interface IRefitTimestampedSetClient : IDisposable +public interface IRefitTimestampedSetClient : IRefitClient, IDisposable { private const string baseUrl = "/api/TimestampedSet/{idDiscriminator}"; diff --git a/DD.Persistence.Client/Clients/TechMessagesClient.cs b/DD.Persistence.Client/Clients/TechMessagesClient.cs index c981569..e23c961 100644 --- a/DD.Persistence.Client/Clients/TechMessagesClient.cs +++ b/DD.Persistence.Client/Clients/TechMessagesClient.cs @@ -11,9 +11,9 @@ public class TechMessagesClient : BaseClient, ITechMessagesClient { private readonly IRefitTechMessagesClient refitTechMessagesClient; - public TechMessagesClient(IRefitTechMessagesClient refitTechMessagesClient, ILogger logger) : base(logger) + public TechMessagesClient(IRefitClientFactory refitTechMessagesClientFactory, ILogger logger) : base(logger) { - this.refitTechMessagesClient = refitTechMessagesClient; + this.refitTechMessagesClient = refitTechMessagesClientFactory.Create(); } public async Task> GetPage(PaginationRequest request, CancellationToken token) diff --git a/DD.Persistence.Client/Clients/TimeSeriesClient.cs b/DD.Persistence.Client/Clients/TimeSeriesClient.cs index 2c15938..c75ec81 100644 --- a/DD.Persistence.Client/Clients/TimeSeriesClient.cs +++ b/DD.Persistence.Client/Clients/TimeSeriesClient.cs @@ -5,13 +5,13 @@ using DD.Persistence.Client.Clients.Interfaces.Refit; using DD.Persistence.Models; namespace DD.Persistence.Client.Clients; -public class TimeSeriesClient : BaseClient, ITimeSeriesClient where TDto : class, new() +public class TimeSeriesClient : BaseClient, ITimeSeriesClient where TDto : class, ITimeSeriesAbstractDto { private readonly IRefitTimeSeriesClient timeSeriesClient; - public TimeSeriesClient(IRefitTimeSeriesClient refitTechMessagesClient, ILogger> logger) : base(logger) + public TimeSeriesClient(IRefitClientFactory> refitTechMessagesClientFactory, ILogger> logger) : base(logger) { - this.timeSeriesClient = refitTechMessagesClient; + this.timeSeriesClient = refitTechMessagesClientFactory.Create(); } public async Task AddRange(IEnumerable dtos, CancellationToken token) diff --git a/DD.Persistence.Client/Clients/TimestampedSetClient.cs b/DD.Persistence.Client/Clients/TimestampedSetClient.cs index 38828b6..7d1ba56 100644 --- a/DD.Persistence.Client/Clients/TimestampedSetClient.cs +++ b/DD.Persistence.Client/Clients/TimestampedSetClient.cs @@ -9,9 +9,9 @@ public class TimestampedSetClient : BaseClient, ITimestampedSetClient { private readonly IRefitTimestampedSetClient refitTimestampedSetClient; - public TimestampedSetClient(IRefitTimestampedSetClient refitTimestampedSetClient, ILogger logger) : base(logger) + public TimestampedSetClient(IRefitClientFactory refitTimestampedSetClientFactory, ILogger logger) : base(logger) { - this.refitTimestampedSetClient = refitTimestampedSetClient; + this.refitTimestampedSetClient = refitTimestampedSetClientFactory.Create(); } public async Task AddRange(Guid idDiscriminator, IEnumerable sets, CancellationToken token) diff --git a/DD.Persistence.Client/Clients/WitsDataClient.cs b/DD.Persistence.Client/Clients/WitsDataClient.cs index 3251194..e2703dc 100644 --- a/DD.Persistence.Client/Clients/WitsDataClient.cs +++ b/DD.Persistence.Client/Clients/WitsDataClient.cs @@ -9,9 +9,9 @@ public class WitsDataClient : BaseClient, IWitsDataClient { private readonly IRefitWitsDataClient refitWitsDataClient; - public WitsDataClient(IRefitWitsDataClient refitWitsDataClient, ILogger logger) : base(logger) + public WitsDataClient(IRefitClientFactory refitWitsDataClientFactory, ILogger logger) : base(logger) { - this.refitWitsDataClient = refitWitsDataClient; + this.refitWitsDataClient = refitWitsDataClientFactory.Create(); } public async Task AddRange(IEnumerable dtos, CancellationToken token) diff --git a/DD.Persistence.Client/DependencyInjection.cs b/DD.Persistence.Client/DependencyInjection.cs index 53791bb..eced892 100644 --- a/DD.Persistence.Client/DependencyInjection.cs +++ b/DD.Persistence.Client/DependencyInjection.cs @@ -1,5 +1,6 @@ using DD.Persistence.Client.Clients; using DD.Persistence.Client.Clients.Interfaces; +using DD.Persistence.Models; using Microsoft.Extensions.DependencyInjection; namespace DD.Persistence.Client; @@ -16,14 +17,14 @@ public static class DependencyInjection /// public static IServiceCollection AddPersistenceClients(this IServiceCollection services) { - services.AddSingleton(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>)); + services.AddTransient(typeof(IRefitClientFactory<>), typeof(RefitClientFactory<>)); services.AddTransient(); services.AddTransient(); services.AddTransient(); - //services.AddTransient(); - //services.AddTransient, TimeSeriesClient<>>(); - //services.AddTransient(); - //services.AddTransient(); + services.AddTransient(); + services.AddTransient, TimeSeriesClient>(); + services.AddTransient(); + services.AddTransient(); return services; } } diff --git a/DD.Persistence.Client/PersistenceClientFactory.cs b/DD.Persistence.Client/PersistenceClientFactory.cs index c48424c..99660e9 100644 --- a/DD.Persistence.Client/PersistenceClientFactory.cs +++ b/DD.Persistence.Client/PersistenceClientFactory.cs @@ -1,146 +1,146 @@ -using Microsoft.Extensions.Configuration; -using DD.Persistence.Client.Clients.Interfaces; -using DD.Persistence.Client.Clients; -using DD.Persistence.Client.Helpers; -using Refit; -using DD.Persistence.Factories; -using DD.Persistence.Client.Clients.Interfaces.Refit; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.DependencyInjection; -using System.Text.Json; +//using Microsoft.Extensions.Configuration; +//using DD.Persistence.Client.Clients.Interfaces; +//using DD.Persistence.Client.Clients; +//using DD.Persistence.Client.Helpers; +//using Refit; +//using DD.Persistence.Factories; +//using DD.Persistence.Client.Clients.Interfaces.Refit; +//using Microsoft.Extensions.Logging; +//using Microsoft.Extensions.DependencyInjection; +//using System.Text.Json; -namespace DD.Persistence.Client -{ - /// - /// Фабрика клиентов для доступа к Persistence - сервису - /// - public class PersistenceClientFactory - { - private static readonly JsonSerializerOptions JsonSerializerOptions = new() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - PropertyNameCaseInsensitive = true - }; - private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); - private readonly IServiceProvider provider; - private HttpClient httpClient; - public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IServiceProvider provider, IConfiguration configuration) - { - this.provider = provider; +//namespace DD.Persistence.Client +//{ +// /// +// /// Фабрика клиентов для доступа к Persistence - сервису +// /// +// public class PersistenceClientFactory +// { +// private static readonly JsonSerializerOptions JsonSerializerOptions = new() +// { +// PropertyNamingPolicy = JsonNamingPolicy.CamelCase, +// PropertyNameCaseInsensitive = true +// }; +// private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); +// private readonly IServiceProvider provider; +// private HttpClient httpClient; +// public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IServiceProvider provider, IConfiguration configuration) +// { +// this.provider = provider; - httpClient = httpClientFactory.CreateClient(); +// httpClient = httpClientFactory.CreateClient(); - httpClient.Authorize(configuration); - } +// httpClient.Authorize(configuration); +// } - public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IAuthTokenFactory authTokenFactory, IServiceProvider provider, IConfiguration configuration) - { - this.provider = provider; +// public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IAuthTokenFactory authTokenFactory, IServiceProvider provider, IConfiguration configuration) +// { +// this.provider = provider; - httpClient = httpClientFactory.CreateClient(); +// httpClient = httpClientFactory.CreateClient(); - var token = authTokenFactory.GetToken(); - httpClient.Authorize(token); - } +// var token = authTokenFactory.GetToken(); +// httpClient.Authorize(token); +// } - /// - /// Получить клиент для работы с уставками - /// - /// - //public ISetpointClient GetSetpointClient() - //{ - // var logger = provider.GetRequiredService>(); +// /// +// /// Получить клиент для работы с уставками +// /// +// /// +// //public ISetpointClient GetSetpointClient() +// //{ +// // var logger = provider.GetRequiredService>(); - // var restClient = RestService.For(httpClient, RefitSettings); - // var client = new SetpointClient(restClient, logger); +// // var restClient = RestService.For(httpClient, RefitSettings); +// // var client = new SetpointClient(restClient, logger); - // return client; - //} +// // return client; +// //} - /// - /// Получить клиент для работы с технологическими сообщениями - /// - /// - public ITechMessagesClient GetTechMessagesClient() - { - var logger = provider.GetRequiredService>(); +// /// +// /// Получить клиент для работы с технологическими сообщениями +// /// +// /// +// //public ITechMessagesClient GetTechMessagesClient() +// //{ +// // var logger = provider.GetRequiredService>(); - var restClient = RestService.For(httpClient, RefitSettings); - var client = new TechMessagesClient(restClient, logger); +// // var restClient = RestService.For(httpClient, RefitSettings); +// // var client = new TechMessagesClient(restClient, logger); - return client; - } +// // return client; +// //} - /// - /// Получить клиент для работы с временными данными - /// - /// - /// - public ITimeSeriesClient GetTimeSeriesClient() - where TDto : class, new() - { - var logger = provider.GetRequiredService>>(); +// /// +// /// Получить клиент для работы с временными данными +// /// +// /// +// /// +// //public ITimeSeriesClient GetTimeSeriesClient() +// // where TDto : class, new() +// //{ +// // var logger = provider.GetRequiredService>>(); - var restClient = RestService.For>(httpClient, RefitSettings); - var client = new TimeSeriesClient(restClient, logger); +// // var restClient = RestService.For>(httpClient, RefitSettings); +// // var client = new TimeSeriesClient(restClient, logger); - return client; - } +// // return client; +// //} - /// - /// Получить клиент для работы с данными с отметкой времени - /// - /// - public ITimestampedSetClient GetTimestampedSetClient() - { - var logger = provider.GetRequiredService>(); +// /// +// /// Получить клиент для работы с данными с отметкой времени +// /// +// /// +// //public ITimestampedSetClient GetTimestampedSetClient() +// //{ +// // var logger = provider.GetRequiredService>(); - var restClient = RestService.For(httpClient, RefitSettings); - var client = new TimestampedSetClient(restClient, logger); +// // var restClient = RestService.For(httpClient, RefitSettings); +// // var client = new TimestampedSetClient(restClient, logger); - return client; - } +// // return client; +// //} - /// - /// Получить клиент для работы с записями ChangeLog - /// - /// - //public IChangeLogClient GetChangeLogClient() - //{ - // var logger = provider.GetRequiredService>(); +// /// +// /// Получить клиент для работы с записями ChangeLog +// /// +// /// +// //public IChangeLogClient GetChangeLogClient() +// //{ +// // var logger = provider.GetRequiredService>(); - // var restClient = RestService.For(httpClient, RefitSettings); - // var client = new ChangeLogClient(restClient, logger); +// // var restClient = RestService.For(httpClient, RefitSettings); +// // var client = new ChangeLogClient(restClient, logger); - // return client; - //} +// // return client; +// //} - /// - /// Получить клиент для работы c параметрами Wits - /// - /// - public IWitsDataClient GetWitsDataClient() - { - var logger = provider.GetRequiredService>(); +// /// +// /// Получить клиент для работы c параметрами Wits +// /// +// /// +// //public IWitsDataClient GetWitsDataClient() +// //{ +// // var logger = provider.GetRequiredService>(); - var restClient = RestService.For(httpClient, RefitSettings); - var client = new WitsDataClient(restClient, logger); +// // var restClient = RestService.For(httpClient, RefitSettings); +// // var client = new WitsDataClient(restClient, logger); - return client; - } +// // return client; +// //} - /// - /// Получить клиент для работы c системами - /// - /// - //public IDataSourceSystemClient GetDataSourceSystemClient() - //{ - // var logger = provider.GetRequiredService>(); +// /// +// /// Получить клиент для работы c системами +// /// +// /// +// //public IDataSourceSystemClient GetDataSourceSystemClient() +// //{ +// // var logger = provider.GetRequiredService>(); - // var restClient = RestService.For(httpClient, RefitSettings); - // var client = new DataSourceSystemClient(restClient, logger); +// // var restClient = RestService.For(httpClient, RefitSettings); +// // var client = new DataSourceSystemClient(restClient, logger); - // return client; - //} - } -} +// // return client; +// //} +// } +//} diff --git a/DD.Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs index bdcddb9..51afc02 100644 --- a/DD.Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/TechMessagesControllerTest.cs @@ -1,38 +1,41 @@ -using Microsoft.Extensions.Caching.Memory; -using Microsoft.Extensions.DependencyInjection; -using DD.Persistence.Client; +using DD.Persistence.Client; +using DD.Persistence.Client.Clients; using DD.Persistence.Client.Clients.Interfaces; +using DD.Persistence.Client.Clients.Interfaces.Refit; using DD.Persistence.Database.Entity; using DD.Persistence.Models; using DD.Persistence.Models.Enumerations; using DD.Persistence.Models.Requests; -using System.Net; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Xunit; namespace DD.Persistence.IntegrationTests.Controllers { - public class TechMessagesControllerTest : BaseIntegrationTest - { - private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey"; - private readonly ITechMessagesClient techMessagesClient; - private readonly IMemoryCache memoryCache; - public TechMessagesControllerTest(WebAppFactoryFixture factory) : base(factory) - { - var scope = factory.Services.CreateScope(); - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); + public class TechMessagesControllerTest : BaseIntegrationTest + { + private static readonly string SystemCacheKey = $"{typeof(Database.Entity.DataSourceSystem).FullName}CacheKey"; + private readonly ITechMessagesClient techMessagesClient; + private readonly IMemoryCache memoryCache; + public TechMessagesControllerTest(WebAppFactoryFixture factory) : base(factory) + { + var refitClientFactory = scope.ServiceProvider + .GetRequiredService>(); + var logger = scope.ServiceProvider.GetRequiredService>(); - techMessagesClient = persistenceClientFactory.GetTechMessagesClient(); - memoryCache = scope.ServiceProvider.GetRequiredService(); - } + techMessagesClient = scope.ServiceProvider + .GetRequiredService(); + memoryCache = scope.ServiceProvider.GetRequiredService(); + } - [Fact] - public async Task GetPage_returns_success() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); + [Fact] + public async Task GetPage_returns_success() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); var requestDto = new PaginationRequest() { @@ -44,235 +47,235 @@ namespace DD.Persistence.IntegrationTests.Controllers //act var response = await techMessagesClient.GetPage(requestDto, CancellationToken.None); - //assert - Assert.NotNull(response); - Assert.Empty(response.Items); - Assert.Equal(requestDto.Skip, response.Skip); - Assert.Equal(requestDto.Take, response.Take); - } + //assert + Assert.NotNull(response); + Assert.Empty(response.Items); + Assert.Equal(requestDto.Skip, response.Skip); + Assert.Equal(requestDto.Take, response.Take); + } - [Fact] - public async Task GetPage_AfterSave_returns_success() - { - //arrange - var dtos = await InsertRange(Guid.NewGuid()); - var dtosCount = dtos.Count(); - var requestDto = new PaginationRequest() - { - Skip = 0, - Take = 2, - SortSettings = nameof(TechMessage.CategoryId) - }; + [Fact] + public async Task GetPage_AfterSave_returns_success() + { + //arrange + var dtos = await InsertRange(Guid.NewGuid()); + var dtosCount = dtos.Count(); + var requestDto = new PaginationRequest() + { + Skip = 0, + Take = 2, + SortSettings = nameof(TechMessage.CategoryId) + }; //act var response = await techMessagesClient.GetPage(requestDto, CancellationToken.None); - //assert - Assert.NotNull(response); - Assert.Equal(dtosCount, response.Count); - } - - [Fact] - public async Task InsertRange_returns_success() - { - await InsertRange(Guid.NewGuid()); - } - - [Fact] - public async Task InsertRange_returns_BadRequest() - { - //arrange - const string exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса"; - var systemId = Guid.NewGuid(); - var dtos = new List() - { - new TechMessageDto() - { - EventId = Guid.NewGuid(), - CategoryId = -1, // < 0 - Timestamp = DateTimeOffset.UtcNow, - Text = string.Empty, // length < 0 - EventState = EventState.Triggered - } - }; - - try - { - //act - var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); - } - catch (Exception ex) - { - //assert - Assert.Equal(exceptionMessage, ex.Message); - } - } - - [Fact] - public async Task GetSystems_returns_success() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); - - //act - var response = await techMessagesClient.GetSystems(CancellationToken.None); - - //assert - Assert.NotNull(response); - Assert.Empty(response); - } - - [Fact] - public async Task GetSystems_AfterSave_returns_success() - { - //arrange - await InsertRange(Guid.NewGuid()); - - //act - var response = await techMessagesClient.GetSystems(CancellationToken.None); - - //assert - Assert.NotNull(response); - var expectedSystemCount = 1; - Assert.Equal(expectedSystemCount, response!.Count()); - } - - [Fact] - public async Task GetStatistics_returns_success() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); - - var categoryIds = new [] { 1, 2 }; - var systemIds = new [] { Guid.NewGuid() }; - - //act - var response = await techMessagesClient.GetStatistics(systemIds, categoryIds, CancellationToken.None); - - //assert - Assert.NotNull(response); - Assert.Empty(response); - } - - [Fact] - public async Task GetStatistics_AfterSave_returns_success() - { - //arrange - var categoryIds = new[] { 1 }; - var systemId = Guid.NewGuid(); - var dtos = await InsertRange(systemId); - var filteredDtos = dtos.Where(e => categoryIds.Contains(e.CategoryId)); - - //act - var response = await techMessagesClient.GetStatistics([systemId], categoryIds, CancellationToken.None); - - //assert - Assert.NotNull(response); - Assert.NotEmpty(response); - var categories = response - .FirstOrDefault()!.Categories - .Count(); - Assert.Equal(filteredDtos.Count(), categories); - } - - [Fact] - public async Task GetDatesRange_returns_NoContent() - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); - - //act - var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); - //assert - Assert.Null(response); - } + Assert.NotNull(response); + Assert.Equal(dtosCount, response.Count); + } - [Fact] - public async Task GetDatesRange_AfterSave_returns_success() - { - //arrange - await InsertRange(Guid.NewGuid()); + [Fact] + public async Task InsertRange_returns_success() + { + await InsertRange(Guid.NewGuid()); + } + + [Fact] + public async Task InsertRange_returns_BadRequest() + { + //arrange + const string exceptionMessage = "Ошибка валидации, формата или маршрутизации запроса"; + var systemId = Guid.NewGuid(); + var dtos = new List() + { + new TechMessageDto() + { + EventId = Guid.NewGuid(), + CategoryId = -1, // < 0 + Timestamp = DateTimeOffset.UtcNow, + Text = string.Empty, // length < 0 + EventState = EventState.Triggered + } + }; + + try + { + //act + var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); + } + catch (Exception ex) + { + //assert + Assert.Equal(exceptionMessage, ex.Message); + } + } + + [Fact] + public async Task GetSystems_returns_success() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + + //act + var response = await techMessagesClient.GetSystems(CancellationToken.None); + + //assert + Assert.NotNull(response); + Assert.Empty(response); + } + + [Fact] + public async Task GetSystems_AfterSave_returns_success() + { + //arrange + await InsertRange(Guid.NewGuid()); + + //act + var response = await techMessagesClient.GetSystems(CancellationToken.None); + + //assert + Assert.NotNull(response); + var expectedSystemCount = 1; + Assert.Equal(expectedSystemCount, response!.Count()); + } + + [Fact] + public async Task GetStatistics_returns_success() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + + var categoryIds = new[] { 1, 2 }; + var systemIds = new[] { Guid.NewGuid() }; + + //act + var response = await techMessagesClient.GetStatistics(systemIds, categoryIds, CancellationToken.None); + + //assert + Assert.NotNull(response); + Assert.Empty(response); + } + + [Fact] + public async Task GetStatistics_AfterSave_returns_success() + { + //arrange + var categoryIds = new[] { 1 }; + var systemId = Guid.NewGuid(); + var dtos = await InsertRange(systemId); + var filteredDtos = dtos.Where(e => categoryIds.Contains(e.CategoryId)); + + //act + var response = await techMessagesClient.GetStatistics([systemId], categoryIds, CancellationToken.None); + + //assert + Assert.NotNull(response); + Assert.NotEmpty(response); + var categories = response + .FirstOrDefault()!.Categories + .Count(); + Assert.Equal(filteredDtos.Count(), categories); + } + + [Fact] + public async Task GetDatesRange_returns_NoContent() + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); //act var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); - //assert - Assert.NotNull(response); - Assert.NotNull(response?.From); - Assert.NotNull(response?.To); - } + //assert + Assert.Null(response); + } - // [Fact] - // public async Task GetPart_returns_success() - // { - // //arrange - // var dateBegin = DateTimeOffset.UtcNow; - // var take = 2; + [Fact] + public async Task GetDatesRange_AfterSave_returns_success() + { + //arrange + await InsertRange(Guid.NewGuid()); - // //act - // var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None); + //act + var response = await techMessagesClient.GetDatesRangeAsync(CancellationToken.None); - // //assert - // Assert.NotNull(response); - // Assert.Empty(response); - //} + //assert + Assert.NotNull(response); + Assert.NotNull(response?.From); + Assert.NotNull(response?.To); + } - [Fact] - public async Task GetPart_AfterSave_returns_success() - { - //arrange - var dateBegin = DateTimeOffset.UtcNow; - var take = 1; - await InsertRange(Guid.NewGuid()); + // [Fact] + // public async Task GetPart_returns_success() + // { + // //arrange + // var dateBegin = DateTimeOffset.UtcNow; + // var take = 2; + + // //act + // var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None); + + // //assert + // Assert.NotNull(response); + // Assert.Empty(response); + //} + + [Fact] + public async Task GetPart_AfterSave_returns_success() + { + //arrange + var dateBegin = DateTimeOffset.UtcNow; + var take = 1; + await InsertRange(Guid.NewGuid()); //act var response = await techMessagesClient.GetPart(dateBegin, take, CancellationToken.None); - //assert - Assert.NotNull(response); - Assert.NotEmpty(response); - } + //assert + Assert.NotNull(response); + Assert.NotEmpty(response); + } - private async Task> InsertRange(Guid systemId) - { - //arrange - memoryCache.Remove(SystemCacheKey); - dbContext.CleanupDbSet(); - dbContext.CleanupDbSet(); + private async Task> InsertRange(Guid systemId) + { + //arrange + memoryCache.Remove(SystemCacheKey); + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); - var dtos = new List() - { - new TechMessageDto() - { - EventId = Guid.NewGuid(), - CategoryId = 1, - Timestamp = DateTimeOffset.UtcNow, - Text = nameof(TechMessageDto.Text), - EventState = Models.Enumerations.EventState.Triggered - }, - new TechMessageDto() - { - EventId = Guid.NewGuid(), - CategoryId = 2, - Timestamp = DateTimeOffset.UtcNow, - Text = nameof(TechMessageDto.Text), - EventState = Models.Enumerations.EventState.Triggered - } - }; + var dtos = new List() + { + new TechMessageDto() + { + EventId = Guid.NewGuid(), + CategoryId = 1, + Timestamp = DateTimeOffset.UtcNow, + Text = nameof(TechMessageDto.Text), + EventState = Models.Enumerations.EventState.Triggered + }, + new TechMessageDto() + { + EventId = Guid.NewGuid(), + CategoryId = 2, + Timestamp = DateTimeOffset.UtcNow, + Text = nameof(TechMessageDto.Text), + EventState = Models.Enumerations.EventState.Triggered + } + }; - //act - var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); + //act + var response = await techMessagesClient.AddRange(systemId, dtos, CancellationToken.None); - //assert - Assert.Equal(dtos.Count, response); + //assert + Assert.Equal(dtos.Count, response); return dtos; } diff --git a/DD.Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs index 7d75875..e8f05ce 100644 --- a/DD.Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/TimeSeriesBaseControllerTest.cs @@ -1,16 +1,19 @@ +using DD.Persistence.Client; +using DD.Persistence.Client.Clients; +using DD.Persistence.Client.Clients.Interfaces; +using DD.Persistence.Client.Clients.Interfaces.Refit; +using DD.Persistence.Database.Model; +using DD.Persistence.Models; using Mapster; using Microsoft.Extensions.DependencyInjection; -using DD.Persistence.Client; -using DD.Persistence.Client.Clients.Interfaces; -using DD.Persistence.Database.Model; -using System.Net; +using Microsoft.Extensions.Logging; using Xunit; namespace DD.Persistence.IntegrationTests.Controllers; public abstract class TimeSeriesBaseControllerTest : BaseIntegrationTest where TEntity : class, ITimestampedData, new() - where TDto : class, new() + where TDto : class, ITimeSeriesAbstractDto, new() { private readonly ITimeSeriesClient timeSeriesClient; @@ -18,11 +21,12 @@ public abstract class TimeSeriesBaseControllerTest : BaseIntegrat { dbContext.CleanupDbSet(); - var scope = factory.Services.CreateScope(); - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); + var refitClientFactory = scope.ServiceProvider + .GetRequiredService>>(); + var logger = scope.ServiceProvider.GetRequiredService>>(); - timeSeriesClient = persistenceClientFactory.GetTimeSeriesClient(); + timeSeriesClient = scope.ServiceProvider + .GetRequiredService>(); } public async Task InsertRangeSuccess(TDto dto) diff --git a/DD.Persistence.IntegrationTests/Controllers/TimestampedSetControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/TimestampedSetControllerTest.cs index 567b8c8..1e84e9d 100644 --- a/DD.Persistence.IntegrationTests/Controllers/TimestampedSetControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/TimestampedSetControllerTest.cs @@ -3,6 +3,9 @@ using DD.Persistence.Client; using DD.Persistence.Client.Clients.Interfaces; using DD.Persistence.Models; using Xunit; +using DD.Persistence.Client.Clients.Interfaces.Refit; +using DD.Persistence.Client.Clients; +using Microsoft.Extensions.Logging; namespace DD.Persistence.IntegrationTests.Controllers; public class TimestampedSetControllerTest : BaseIntegrationTest @@ -11,10 +14,12 @@ public class TimestampedSetControllerTest : BaseIntegrationTest public TimestampedSetControllerTest(WebAppFactoryFixture factory) : base(factory) { - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); + var refitClientFactory = scope.ServiceProvider + .GetRequiredService>(); + var logger = scope.ServiceProvider.GetRequiredService>(); - client = persistenceClientFactory.GetTimestampedSetClient(); + client = scope.ServiceProvider + .GetRequiredService(); } [Fact] diff --git a/DD.Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs b/DD.Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs index 6b33804..13e1116 100644 --- a/DD.Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs +++ b/DD.Persistence.IntegrationTests/Controllers/WitsDataControllerTest.cs @@ -1,13 +1,12 @@ -using Microsoft.Extensions.DependencyInjection; +using DD.Persistence.Client; +using DD.Persistence.Client.Clients; +using DD.Persistence.Client.Clients.Interfaces; +using DD.Persistence.Client.Clients.Interfaces.Refit; using DD.Persistence.Database.Entity; using DD.Persistence.Models; -using System.Net; -using Xunit; -using DD.Persistence.Client.Clients.Interfaces; -using DD.Persistence.Client; -using DD.Persistence.Client.Clients.Interfaces.Refit; -using DD.Persistence.Client.Clients; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Xunit; namespace DD.Persistence.IntegrationTests.Controllers; public class WitsDataControllerTest : BaseIntegrationTest @@ -17,7 +16,7 @@ public class WitsDataControllerTest : BaseIntegrationTest public WitsDataControllerTest(WebAppFactoryFixture factory) : base(factory) { var refitClientFactory = scope.ServiceProvider - .GetRequiredService>(); + .GetRequiredService>(); var logger = scope.ServiceProvider.GetRequiredService>(); witsDataClient = scope.ServiceProvider From 4cae12ddac4306dde20f83fe6bb82c89c2d37015 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 27 Dec 2024 13:37:38 +0500 Subject: [PATCH 5/7] =?UTF-8?q?=D0=9A=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9=20=D1=84=D0=B0=D0=B9=D0=BB=20appsettings.Tes?= =?UTF-8?q?t.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.App/appsettings.Tests.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DD.Persistence.App/appsettings.Tests.json b/DD.Persistence.App/appsettings.Tests.json index 9934757..72c43d3 100644 --- a/DD.Persistence.App/appsettings.Tests.json +++ b/DD.Persistence.App/appsettings.Tests.json @@ -1,6 +1,6 @@ { "DbConnection": { - "Host": "localhost", + "Host": "postgres", "Port": 5432, "Database": "persistence", "Username": "postgres", From 649c51a8ab8114c8fa6b5cb512aa27dbc34f9769 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Sat, 28 Dec 2024 15:30:31 +0500 Subject: [PATCH 6/7] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E:=20=D0=B0=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20=D0=BD?= =?UTF-8?q?=D0=B5=20=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B8=20RefitClientFact?= =?UTF-8?q?ory,=20=D0=B0=20=D1=81=D0=BD=D0=B0=D1=80=D1=83=D0=B6=D0=B8,=20?= =?UTF-8?q?=D0=BD=D0=B0=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80,=20=D0=B2=20In?= =?UTF-8?q?tegrationTests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.Client/IRefitClientFactory.cs | 5 - .../PersistenceClientFactory.cs | 146 ------------------ DD.Persistence.Client/RefitClientFactory.cs | 5 +- .../TestHttpClientFactory.cs | 14 +- .../WebAppFactoryFixture.cs | 13 +- 5 files changed, 19 insertions(+), 164 deletions(-) delete mode 100644 DD.Persistence.Client/PersistenceClientFactory.cs diff --git a/DD.Persistence.Client/IRefitClientFactory.cs b/DD.Persistence.Client/IRefitClientFactory.cs index 0b41a9a..0015a95 100644 --- a/DD.Persistence.Client/IRefitClientFactory.cs +++ b/DD.Persistence.Client/IRefitClientFactory.cs @@ -1,9 +1,4 @@ using DD.Persistence.Client.Clients.Interfaces.Refit; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace DD.Persistence.Client; diff --git a/DD.Persistence.Client/PersistenceClientFactory.cs b/DD.Persistence.Client/PersistenceClientFactory.cs deleted file mode 100644 index 99660e9..0000000 --- a/DD.Persistence.Client/PersistenceClientFactory.cs +++ /dev/null @@ -1,146 +0,0 @@ -//using Microsoft.Extensions.Configuration; -//using DD.Persistence.Client.Clients.Interfaces; -//using DD.Persistence.Client.Clients; -//using DD.Persistence.Client.Helpers; -//using Refit; -//using DD.Persistence.Factories; -//using DD.Persistence.Client.Clients.Interfaces.Refit; -//using Microsoft.Extensions.Logging; -//using Microsoft.Extensions.DependencyInjection; -//using System.Text.Json; - -//namespace DD.Persistence.Client -//{ -// /// -// /// Фабрика клиентов для доступа к Persistence - сервису -// /// -// public class PersistenceClientFactory -// { -// private static readonly JsonSerializerOptions JsonSerializerOptions = new() -// { -// PropertyNamingPolicy = JsonNamingPolicy.CamelCase, -// PropertyNameCaseInsensitive = true -// }; -// private static readonly RefitSettings RefitSettings = new(new SystemTextJsonContentSerializer(JsonSerializerOptions)); -// private readonly IServiceProvider provider; -// private HttpClient httpClient; -// public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IServiceProvider provider, IConfiguration configuration) -// { -// this.provider = provider; - -// httpClient = httpClientFactory.CreateClient(); - -// httpClient.Authorize(configuration); -// } - -// public PersistenceClientFactory(IHttpClientFactory httpClientFactory, IAuthTokenFactory authTokenFactory, IServiceProvider provider, IConfiguration configuration) -// { -// this.provider = provider; - -// httpClient = httpClientFactory.CreateClient(); - -// var token = authTokenFactory.GetToken(); -// httpClient.Authorize(token); -// } - -// /// -// /// Получить клиент для работы с уставками -// /// -// /// -// //public ISetpointClient GetSetpointClient() -// //{ -// // var logger = provider.GetRequiredService>(); - -// // var restClient = RestService.For(httpClient, RefitSettings); -// // var client = new SetpointClient(restClient, logger); - -// // return client; -// //} - -// /// -// /// Получить клиент для работы с технологическими сообщениями -// /// -// /// -// //public ITechMessagesClient GetTechMessagesClient() -// //{ -// // var logger = provider.GetRequiredService>(); - -// // var restClient = RestService.For(httpClient, RefitSettings); -// // var client = new TechMessagesClient(restClient, logger); - -// // return client; -// //} - -// /// -// /// Получить клиент для работы с временными данными -// /// -// /// -// /// -// //public ITimeSeriesClient GetTimeSeriesClient() -// // where TDto : class, new() -// //{ -// // var logger = provider.GetRequiredService>>(); - -// // var restClient = RestService.For>(httpClient, RefitSettings); -// // var client = new TimeSeriesClient(restClient, logger); - -// // return client; -// //} - -// /// -// /// Получить клиент для работы с данными с отметкой времени -// /// -// /// -// //public ITimestampedSetClient GetTimestampedSetClient() -// //{ -// // var logger = provider.GetRequiredService>(); - -// // var restClient = RestService.For(httpClient, RefitSettings); -// // var client = new TimestampedSetClient(restClient, logger); - -// // return client; -// //} - -// /// -// /// Получить клиент для работы с записями ChangeLog -// /// -// /// -// //public IChangeLogClient GetChangeLogClient() -// //{ -// // var logger = provider.GetRequiredService>(); - -// // var restClient = RestService.For(httpClient, RefitSettings); -// // var client = new ChangeLogClient(restClient, logger); - -// // return client; -// //} - -// /// -// /// Получить клиент для работы c параметрами Wits -// /// -// /// -// //public IWitsDataClient GetWitsDataClient() -// //{ -// // var logger = provider.GetRequiredService>(); - -// // var restClient = RestService.For(httpClient, RefitSettings); -// // var client = new WitsDataClient(restClient, logger); - -// // return client; -// //} - -// /// -// /// Получить клиент для работы c системами -// /// -// /// -// //public IDataSourceSystemClient GetDataSourceSystemClient() -// //{ -// // var logger = provider.GetRequiredService>(); - -// // var restClient = RestService.For(httpClient, RefitSettings); -// // var client = new DataSourceSystemClient(restClient, logger); - -// // return client; -// //} -// } -//} diff --git a/DD.Persistence.Client/RefitClientFactory.cs b/DD.Persistence.Client/RefitClientFactory.cs index 758f79d..9cd0bef 100644 --- a/DD.Persistence.Client/RefitClientFactory.cs +++ b/DD.Persistence.Client/RefitClientFactory.cs @@ -17,9 +17,7 @@ public class RefitClientFactory : IRefitClientFactory where T : IRefitClie private HttpClient client; private RefitSettings refitSettings; - /// - /// - /// + /// public RefitClientFactory(IConfiguration configuration, ILogger> logger, HttpClient client) { //this.client = factory.CreateClient(); @@ -35,7 +33,6 @@ public class RefitClientFactory : IRefitClientFactory where T : IRefitClie throw exception; } client.BaseAddress = new Uri(baseUrl); - client.Authorize(configuration); JsonSerializerOptions JsonSerializerOptions = new() { diff --git a/DD.Persistence.IntegrationTests/TestHttpClientFactory.cs b/DD.Persistence.IntegrationTests/TestHttpClientFactory.cs index b0f86c8..51b8783 100644 --- a/DD.Persistence.IntegrationTests/TestHttpClientFactory.cs +++ b/DD.Persistence.IntegrationTests/TestHttpClientFactory.cs @@ -1,4 +1,7 @@ -namespace DD.Persistence.IntegrationTests +using DD.Persistence.Client.Helpers; +using Microsoft.Extensions.Configuration; + +namespace DD.Persistence.IntegrationTests { /// /// Фабрика HTTP клиентов для интеграционных тестов @@ -6,14 +9,19 @@ public class TestHttpClientFactory : IHttpClientFactory { private readonly WebAppFactoryFixture factory; + private readonly IConfiguration configuration; - public TestHttpClientFactory(WebAppFactoryFixture factory) + public TestHttpClientFactory(WebAppFactoryFixture factory, IConfiguration configuration) { this.factory = factory; + this.configuration = configuration; } public HttpClient CreateClient(string name) { - return factory.CreateClient(); + var client = factory.CreateClient(); + client.Authorize(configuration); + + return client; } } } diff --git a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs index df6deb1..0d680b3 100644 --- a/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs +++ b/DD.Persistence.IntegrationTests/WebAppFactoryFixture.cs @@ -11,6 +11,9 @@ using DD.Persistence.Database.Model; using DD.Persistence.Database.Postgres; using RestSharp; using DD.Persistence.App; +using DD.Persistence.Client.Helpers; +using DD.Persistence.Factories; +using System.Net; namespace DD.Persistence.IntegrationTests; public class WebAppFactoryFixture : WebApplicationFactory @@ -40,13 +43,11 @@ public class WebAppFactoryFixture : WebApplicationFactory services.AddLogging(builder => builder.AddConsole()); services.RemoveAll(); - services.AddSingleton(provider => - { - return new TestHttpClientFactory(this); - }); - + services.AddSingleton((provider) => + { + return new TestHttpClientFactory(this, provider.GetRequiredService()); + }); services.AddHttpClient(); - //services.AddSingleton(); services.AddPersistenceClients(); var serviceProvider = services.BuildServiceProvider(); From 4c2b91cd2d40fe3855f2e8083b46cbbcce839d79 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 10 Jan 2025 13:44:33 +0500 Subject: [PATCH 7/7] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=81=D1=81=D1=8B=D0=BB=D0=BA=D0=B8=20=D0=BD=D0=B0=20?= =?UTF-8?q?Clients=20=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B8=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B5=D0=BA=D1=82=D0=B0=20=D1=81=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.API/DD.Persistence.API.csproj | 1 - DD.Persistence.API/Startup.cs | 2 -- 2 files changed, 3 deletions(-) diff --git a/DD.Persistence.API/DD.Persistence.API.csproj b/DD.Persistence.API/DD.Persistence.API.csproj index e7d2e57..5ed1097 100644 --- a/DD.Persistence.API/DD.Persistence.API.csproj +++ b/DD.Persistence.API/DD.Persistence.API.csproj @@ -23,7 +23,6 @@ - diff --git a/DD.Persistence.API/Startup.cs b/DD.Persistence.API/Startup.cs index 8f07c31..c5e8c5a 100644 --- a/DD.Persistence.API/Startup.cs +++ b/DD.Persistence.API/Startup.cs @@ -1,7 +1,6 @@ using DD.Persistence.Database.Model; using DD.Persistence.Database.Postgres; using DD.Persistence.Repository; -using DD.Persistence.Client; namespace DD.Persistence.API; @@ -27,7 +26,6 @@ public class Startup services.AddJWTAuthentication(Configuration); services.AddMemoryCache(); services.AddServices(); - services.AddPersistenceClients(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env)