using System; using System.Collections.Generic; using Centron.BusinessLogic; using Centron.BusinessLogic.Telemetry; using Centron.Data.Entities.Telemetry; using Centron.Tests.EndToEnd.Infrastructure; using Xunit; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.Tests.Telemetry { public class TelemetryUpsertApiCall_DistinguishesByUserKindAndLicenseGuid : EndToEndTest { public TelemetryUpsertApiCall_DistinguishesByUserKindAndLicenseGuid(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public override void Execute() { int methodI3D = 0; this.WithSession(session => { var resolve = session.GetBL().ResolveApiMethodNameI3Ds(new[] { "M" }); Assert.True(resolve.IsSuccess); methodI3D = resolve.Data["M"]; }); var bucket = new DateTime(2026, 5, 2, 12, 15, 0, DateTimeKind.Utc); // 4 distinct keys: User+Centron, WebAccount+Centron, User+ServiceBoard, AccessToken+NULL. // The NULL-LicenseKind row exercises the ISNULL(-1) MERGE matcher in TelemetryBL. // Note: increments are pre-aggregated by the runtime aggregator (one row per key); we // mirror that contract here — MERGE source must not contain duplicate keys. var increments = new List { new() { UserID = 11, UserKind = TelemetryUserKind.User, LicenseKind = TelemetryLicenseKind.Centron, MethodNameI3D = methodI3D, BucketStartUtc = bucket, Increment = 1 }, new() { UserID = 11, UserKind = TelemetryUserKind.WebAccount, LicenseKind = TelemetryLicenseKind.Centron, MethodNameI3D = methodI3D, BucketStartUtc = bucket, Increment = 1 }, new() { UserID = 11, UserKind = TelemetryUserKind.User, LicenseKind = TelemetryLicenseKind.ServiceBoard, MethodNameI3D = methodI3D, BucketStartUtc = bucket, Increment = 1 }, new() { UserID = 11, UserKind = TelemetryUserKind.AccessToken, LicenseKind = null, MethodNameI3D = methodI3D, BucketStartUtc = bucket, Increment = 2 } }; this.WithSession(session => { var result = session.GetBL().UpsertApiCallBatch(increments); Assert.True(result.IsSuccess); }); Assert.Equal(4, this.Sql($"SELECT COUNT(*) FROM dbo.ApiCallTelemetry WHERE UserID = 11 AND MethodNameI3D = {methodI3D} AND BucketStartUtc = '2026-05-02T12:15:00'")); // The NULL-LicenseKind row matched via ISNULL sentinel and stored Count=2. Assert.Equal(2, this.Sql($"SELECT [Count] FROM dbo.ApiCallTelemetry WHERE UserID = 11 AND UserKind = 2 AND LicenseKind IS NULL AND MethodNameI3D = {methodI3D} AND BucketStartUtc = '2026-05-02T12:15:00'")); } } }