diff --git a/AsbCloudWebApi/Controllers/MockController.cs b/AsbCloudWebApi/Controllers/MockController.cs index 0c95725e..4cec7ce2 100644 --- a/AsbCloudWebApi/Controllers/MockController.cs +++ b/AsbCloudWebApi/Controllers/MockController.cs @@ -1,72 +1,119 @@ -using Microsoft.AspNetCore.Mvc; +using AsbCloudApp.Exceptions; +using AsbCloudWebApi.SignalR; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; +using System.Threading; +using System.Threading.Tasks; -namespace AsbCloudWebApi.Controllers +namespace AsbCloudWebApi.Controllers; + +/// +/// Имитирует разные типы ответа сервера +/// +[Route("api/[controller]")] +[ApiController] +public class MockController : ControllerBase { - /// - /// Имитирует разные типы ответа сервера - /// - [Route("api/[controller]")] - [ApiController] - public class MockController : ControllerBase + private readonly IServiceProvider provider; + + public MockController(IServiceProvider provider) { - /// - /// имитирует http-400 - /// - [HttpGet("400")] - [ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)] - public IActionResult Get400([FromQuery, Required]IDictionary args) + this.provider = provider; + } + + /// + /// имитирует http-400 + /// + [HttpGet("400")] + [ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)] + public IActionResult Get400([FromQuery, Required]IDictionary args) + { + var errors = new Dictionary(); + + foreach (var arg in args) { - var errors = new Dictionary(); - - foreach (var arg in args) - { - var countOfErrors = ((arg.Key + arg.Value).Length % 3) + 1; - var errorsText = Enumerable.Range(0, countOfErrors) - .Select(i => $"{arg.Value} не соответствует критериям проверки № {i}"); + var countOfErrors = ((arg.Key + arg.Value).Length % 3) + 1; + var errorsText = Enumerable.Range(0, countOfErrors) + .Select(i => $"{arg.Value} не соответствует критериям проверки № {i}"); - errors.Add(arg.Key, errorsText.ToArray()); - } - - if (errors.Any()) - { - var problem = new ValidationProblemDetails(errors); - return BadRequest(problem); - } - else - { - var problem = new ValidationProblemDetails { Detail = "at least one argument must be provided" }; - return BadRequest(problem); - } + errors.Add(arg.Key, errorsText.ToArray()); } - /// - /// имитирует http-403 - /// - [HttpGet("403")] - public IActionResult Get403() + if (errors.Any()) { - return Forbid(); + var problem = new ValidationProblemDetails(errors); + return BadRequest(problem); } - - /// - /// имитирует http-401 - /// - [HttpGet("401")] - public IActionResult Get401() + else { - return Unauthorized(); - } - - /// - /// имитирует http-500 - /// - [HttpGet("500")] - public IActionResult Get500() - { - throw new System.Exception("Это тестовое исключение"); + var problem = new ValidationProblemDetails { Detail = "at least one argument must be provided" }; + return BadRequest(problem); } } + + /// + /// имитирует http-403 + /// + [HttpGet("403")] + public IActionResult Get403() + { + return Forbid(); + } + + /// + /// имитирует http-401 + /// + [HttpGet("401")] + public IActionResult Get401() + { + return Unauthorized(); + } + + /// + /// имитирует http-500 + /// + [HttpGet("500")] + public IActionResult Get500() + { + throw new System.Exception("Это тестовое исключение"); + } + + /// + /// имитация отправки SignalR данных + /// + /// + /// + /// + /// Поддерживаемые hubЫ: wellInfo, notifications, telemetry, reports + /// + /// Название вызываемого на клиенте метода. Прим.:"ReceiveDataSaub". Список методов см. в swagger definition signalr + /// Группа пользователей. Прим.: "well_1". Если не задана - все пользователи. Шаблон формирования групп см. описание методов в swagger definition signalr + /// передаваемая нагрузка. (json) + /// + /// + [HttpPost("signalr/hubs/{hubName}/{methodName}/{groupName}")] + [Authorize] + public async Task PostAsync(string hubName, string methodName, string? groupName, object body, CancellationToken token) + { + IHubClients clients = hubName.ToLower() switch { + "wellinfo" => provider.GetRequiredService>().Clients, + "notifications" => provider.GetRequiredService>().Clients, + "telemetry" => provider.GetRequiredService>().Clients, + "reports" => provider.GetRequiredService>().Clients, + _ => throw new ArgumentInvalidException(nameof(hubName), "hubName does not listed"), + }; + + IClientProxy selectedClients = string.IsNullOrEmpty(groupName) + ? clients.All + : clients.Group(groupName); + + await selectedClients.SendAsync(methodName, body, token); + return Ok(); + } }