317 lines
12 KiB
C#
317 lines
12 KiB
C#
using AyCode.Core.Enums;
|
||
using AyCode.Core.Loggers;
|
||
using AyCode.Utils.Extensions;
|
||
using FruitBank.Common;
|
||
using FruitBank.Common.Dtos;
|
||
using FruitBank.Common.Entities;
|
||
using FruitBank.Common.Interfaces;
|
||
using FruitBank.Common.Loggers;
|
||
using FruitBankHybrid.Shared.Services.SignalRs;
|
||
using System.Diagnostics.CodeAnalysis;
|
||
using FruitBank.Common.SignalRs;
|
||
using AyCode.Services.SignalRs;
|
||
|
||
namespace FruitBankHybrid.Shared.Tests;
|
||
|
||
/// <summary>
|
||
/// Teszt a TestSignalREndpoint-hoz.
|
||
/// FONTOS: A SANDBOX-ot manu<6E>lisan kell elind<6E>tani a tesztek futtat<61>sa el<65>tt!
|
||
/// Ind<6E>t<EFBFBD>s: dotnet run --project Mango.Sandbox.EndPoints --urls http://localhost:59579
|
||
/// </summary>
|
||
[TestClass]
|
||
public class SandboxEndpointSimpleTests
|
||
{
|
||
private static readonly string SandboxUrl = FruitBankConstClient.BaseUrl; //"http://localhost:59579";
|
||
private static readonly string HubUrl = $"{SandboxUrl}/fbHub";
|
||
|
||
// Teszt SignalR Tags (TestSignalRTags-b<>l)
|
||
private const int PingTag = SignalRTags.PingTag;
|
||
private const int EchoTag = SignalRTags.EchoTag;
|
||
private const int GetTestItemsTag = 9003;
|
||
|
||
private FruitBankSignalRClient _signalRClient = null!;
|
||
|
||
[TestInitialize]
|
||
public void TestInit()
|
||
{
|
||
if (!SandboxUrl.Contains("localhost:")) throw new Exception("NEM LOCALHOST-ON TESZTEL<45>NK!");
|
||
|
||
_signalRClient = TestSignalRClientFactory.Create(nameof(SandboxEndpointSimpleTests));
|
||
}
|
||
|
||
#region HTTP Endpoint Tests
|
||
|
||
[TestMethod]
|
||
public async Task HealthEndpoint_ReturnsSuccess()
|
||
{
|
||
using var httpClient = new HttpClient();
|
||
var response = await httpClient.GetAsync($"{SandboxUrl}/health");
|
||
Assert.IsTrue(response.IsSuccessStatusCode, $"Health endpoint returned {response.StatusCode}");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task RootEndpoint_ReturnsSandboxIsRunning()
|
||
{
|
||
using var httpClient = new HttpClient();
|
||
var response = await httpClient.GetStringAsync(SandboxUrl);
|
||
Assert.AreEqual("SANDBOX is running!", response);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region SignalR Connection Tests
|
||
|
||
[TestMethod]
|
||
public async Task SignalR_Negotiate_ReturnsSuccess()
|
||
{
|
||
using var httpClient = new HttpClient();
|
||
var response = await httpClient.PostAsync($"{HubUrl}/negotiate?negotiateVersion=1", null);
|
||
Assert.IsTrue(response.IsSuccessStatusCode, $"SignalR negotiate returned {response.StatusCode}");
|
||
}
|
||
|
||
[TestMethod]
|
||
public async Task SignalR_Connect_Succeeds()
|
||
{
|
||
var testItems = await _signalRClient.GetAllAsync<List<TestItem>>(GetTestItemsTag);
|
||
Assert.IsNotNull(testItems);
|
||
}
|
||
|
||
public class TestItem
|
||
{
|
||
public int Id { get; set; }
|
||
public string Name { get; set; } = string.Empty;
|
||
public decimal Value { get; set; }
|
||
}
|
||
|
||
//[TestMethod]
|
||
//public async Task SignalR_Connect_Succeeds()
|
||
//{
|
||
// var connection = new HubConnectionBuilder()
|
||
// .WithUrl(HubUrl)
|
||
// .Build();
|
||
|
||
// try
|
||
// {
|
||
// await connection.StartAsync();
|
||
// Assert.AreEqual(HubConnectionState.Connected, connection.State);
|
||
// }
|
||
// finally
|
||
// {
|
||
// await connection.StopAsync();
|
||
// }
|
||
//}
|
||
|
||
//#endregion
|
||
|
||
//#region TestSignalREndpoint Tests
|
||
|
||
//[TestMethod]
|
||
//public async Task SignalR_Ping_ReturnsResponse()
|
||
//{
|
||
// var testMessage = "Hello SignalR!";
|
||
// await TestSignalREndpoint(PingTag, testMessage, "Ping", response =>
|
||
// {
|
||
// Assert.IsNotNull(response, "Response should not be null");
|
||
|
||
// // Parse JSON response
|
||
// using var jsonDoc = JsonDocument.Parse(response);
|
||
// var root = jsonDoc.RootElement;
|
||
|
||
// // Ellen<65>rizz<7A>k, hogy van Message property
|
||
// Assert.IsTrue(root.TryGetProperty("Message", out var messageElement) ||
|
||
// root.TryGetProperty("message", out messageElement),
|
||
// "Response should contain 'Message' property");
|
||
|
||
// Console.WriteLine($"[Ping] Received message: {messageElement.GetString()}");
|
||
// });
|
||
//}
|
||
|
||
//[TestMethod]
|
||
//public async Task SignalR_Echo_ReturnsEchoedData()
|
||
//{
|
||
// var request = new { Id = 42, Name = "TestName" };
|
||
// await TestSignalREndpoint(EchoTag, request, "Echo", response =>
|
||
// {
|
||
// Assert.IsNotNull(response, "Response should not be null");
|
||
|
||
// using var jsonDoc = JsonDocument.Parse(response);
|
||
// var root = jsonDoc.RootElement;
|
||
|
||
// // Ellen<65>rizz<7A>k az Id-t
|
||
// Assert.IsTrue(root.TryGetProperty("Id", out var idElement) ||
|
||
// root.TryGetProperty("id", out idElement),
|
||
// "Response should contain 'Id' property");
|
||
// Assert.AreEqual(42, idElement.GetInt32(), "Id should be 42");
|
||
|
||
// // Ellen<65>rizz<7A>k a Name-et
|
||
// Assert.IsTrue(root.TryGetProperty("Name", out var nameElement) ||
|
||
// root.TryGetProperty("name", out nameElement),
|
||
// "Response should contain 'Name' property");
|
||
// Assert.AreEqual("TestName", nameElement.GetString(), "Name should be 'TestName'");
|
||
|
||
// Console.WriteLine($"[Echo] Received: Id={idElement.GetInt32()}, Name={nameElement.GetString()}");
|
||
// });
|
||
//}
|
||
|
||
//[TestMethod]
|
||
//public async Task SignalR_GetTestItems_ReturnsItemList()
|
||
//{
|
||
// await TestSignalREndpoint(GetTestItemsTag, null, "GetTestItems", response =>
|
||
// {
|
||
// Assert.IsNotNull(response, "Response should not be null");
|
||
|
||
// using var jsonDoc = JsonDocument.Parse(response);
|
||
// var root = jsonDoc.RootElement;
|
||
|
||
// // Ellen<65>rizz<7A>k, hogy t<>mb-e
|
||
// Assert.AreEqual(JsonValueKind.Array, root.ValueKind, "Response should be an array");
|
||
// Assert.IsTrue(root.GetArrayLength() > 0, "Array should have items");
|
||
|
||
// Console.WriteLine($"[GetTestItems] Received {root.GetArrayLength()} items");
|
||
|
||
// // Ellen<65>rizz<7A>k az els<6C> elemet
|
||
// var firstItem = root[0];
|
||
// Assert.IsTrue(firstItem.TryGetProperty("Id", out _) || firstItem.TryGetProperty("id", out _),
|
||
// "Item should have 'Id' property");
|
||
// Assert.IsTrue(firstItem.TryGetProperty("Name", out _) || firstItem.TryGetProperty("name", out _),
|
||
// "Item should have 'Name' property");
|
||
// });
|
||
//}
|
||
|
||
//#endregion
|
||
|
||
//#region EREDETI BUSINESS ENDPOINT TESZTEK - KIKOMMENTEZVE
|
||
|
||
//// ===========================================
|
||
//// === Az al<61>bbi tesztek az eredeti 3 endpoint-ot tesztelik ===
|
||
//// === Vissza<7A>ll<6C>t<EFBFBD>shoz: t<>r<EFBFBD>ld a kommenteket <20>s regisztr<74>ld az endpoint-okat a Program.cs-ben ===
|
||
//// ===========================================
|
||
|
||
//// [TestMethod]
|
||
//// public async Task SignalR_GetMeasuringUsers_ReturnsJson()
|
||
//// {
|
||
//// await TestSignalREndpoint(GetMeasuringUsersTag, null, "GetMeasuringUsers");
|
||
//// }
|
||
|
||
//// [TestMethod]
|
||
//// public async Task SignalR_GetStockQuantityHistoryDtos_ReturnsJson()
|
||
//// {
|
||
//// await TestSignalREndpoint(GetStockQuantityHistoryDtosTag, null, "GetStockQuantityHistoryDtos");
|
||
//// }
|
||
|
||
//// [TestMethod]
|
||
//// public async Task SignalR_GetStockQuantityHistoryDtosByProductId_ReturnsJson()
|
||
//// {
|
||
//// // ProductId = 10
|
||
//// await TestSignalREndpoint(GetStockQuantityHistoryDtosByProductIdTag, 10, "GetStockQuantityHistoryDtosByProductId");
|
||
//// }
|
||
|
||
//// [TestMethod]
|
||
//// public async Task SignalR_GetShippingDocumentsByShippingId_ReturnsJson()
|
||
//// {
|
||
//// // ShippingId = 5
|
||
//// await TestSignalREndpoint(GetShippingDocumentsByShippingIdTag, 5, "GetShippingDocumentsByShippingId");
|
||
//// }
|
||
|
||
//// [TestMethod]
|
||
//// public async Task SignalR_GetOrderDtoById_ReturnsJson()
|
||
//// {
|
||
//// // OrderId = 15
|
||
//// await TestSignalREndpoint(GetOrderDtoByIdTag, 15, "GetOrderDtoById");
|
||
//// }
|
||
|
||
//// [TestMethod]
|
||
//// public async Task SignalR_GetStockTakingItemsById_ReturnsJson()
|
||
//// {
|
||
//// // StockTakingItemId = 200
|
||
//// await TestSignalREndpoint(GetStockTakingItemsByIdTag, 200, "GetStockTakingItemsById");
|
||
//// }
|
||
|
||
//#endregion
|
||
|
||
//#region Helper Methods
|
||
|
||
//private async Task TestSignalREndpoint(int tag, object? parameter, string endpointName, Action<string?>? validateResponse = null)
|
||
//{
|
||
// var connection = new HubConnectionBuilder()
|
||
// .WithUrl(HubUrl)
|
||
// .Build();
|
||
|
||
// string? receivedJson = null;
|
||
// int receivedTag = -1;
|
||
// var responseReceived = new TaskCompletionSource<bool>();
|
||
|
||
// connection.On<int, byte[]>("ReceiveMessage", (responseTag, data) =>
|
||
// {
|
||
// receivedTag = responseTag;
|
||
// if (data != null && data.Length > 0)
|
||
// {
|
||
// receivedJson = Encoding.UTF8.GetString(data);
|
||
// }
|
||
// responseReceived.TrySetResult(true);
|
||
// });
|
||
|
||
// try
|
||
// {
|
||
// await connection.StartAsync();
|
||
// Assert.AreEqual(HubConnectionState.Connected, connection.State, $"Failed to connect to SignalR hub for {endpointName}");
|
||
|
||
// // K<>sz<73>ts<74>k el a request data-t
|
||
// // Ha nincs param<61>ter, null-t k<>ld<6C>nk (nem <20>res byte t<>mb<6D>t!)
|
||
// byte[]? requestData = parameter != null
|
||
// ? Encoding.UTF8.GetBytes(JsonSerializer.Serialize(parameter))
|
||
// : null;
|
||
|
||
// // A Hub met<65>dus neve: OnReceiveMessage (3 param<61>ter: messageTag, messageBytes, requestId)
|
||
// await connection.InvokeAsync("OnReceiveMessage", tag, requestData, (int?)null);
|
||
|
||
// var completed = await Task.WhenAny(responseReceived.Task, Task.Delay(15000));
|
||
|
||
// if (completed == responseReceived.Task)
|
||
// {
|
||
// Console.WriteLine($"[{endpointName}] Response tag: {receivedTag}");
|
||
// Console.WriteLine($"[{endpointName}] Response JSON: {receivedJson?.Substring(0, Math.Min(500, receivedJson?.Length ?? 0))}...");
|
||
|
||
// // Ellen<65>rizz<7A>k, hogy valid JSON-e (ha van adat)
|
||
// if (!string.IsNullOrEmpty(receivedJson))
|
||
// {
|
||
// try
|
||
// {
|
||
// using var jsonDoc = JsonDocument.Parse(receivedJson);
|
||
// Assert.IsTrue(
|
||
// jsonDoc.RootElement.ValueKind == JsonValueKind.Array ||
|
||
// jsonDoc.RootElement.ValueKind == JsonValueKind.Object ||
|
||
// jsonDoc.RootElement.ValueKind == JsonValueKind.Null,
|
||
// $"[{endpointName}] Response is not a valid JSON");
|
||
|
||
// // Custom validation
|
||
// validateResponse?.Invoke(receivedJson);
|
||
// }
|
||
// catch (JsonException ex)
|
||
// {
|
||
// Assert.Fail($"[{endpointName}] Invalid JSON response: {ex.Message}");
|
||
// }
|
||
// }
|
||
// }
|
||
// else
|
||
// {
|
||
// Assert.AreEqual(HubConnectionState.Connected, connection.State,
|
||
// $"[{endpointName}] Connection was closed - check SANDBOX logs for DI errors");
|
||
// }
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// Assert.Fail($"[{endpointName}] SignalR error: {ex.Message}. Check SANDBOX logs for missing DI registrations.");
|
||
// }
|
||
// finally
|
||
// {
|
||
// if (connection.State == HubConnectionState.Connected)
|
||
// {
|
||
// await connection.StopAsync();
|
||
// }
|
||
// }
|
||
//}
|
||
|
||
#endregion
|
||
}
|