using System; using System.Collections.Generic; using System.Linq; using Centron.BusinessLogic; using Centron.BusinessLogic.Sales.Support; using Centron.BusinessLogic.WebServices.Devices; using Centron.BusinessLogic.WebServices.Sales.Support; using Centron.Data.Entities.Sales.Support; using Centron.Host.RestRequests; using Centron.Interfaces.BL; using Centron.Tests.EndToEnd.Infrastructure; using CentronSoftware.Centron.WebServices.Entities.Devices; using Xunit.Abstractions; using Xunit; namespace Centron.Tests.EndToEnd.Tests.Devices; public class DeviceGeneralTests : EndToEndTest { public DeviceGeneralTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { Verifier.Settings.IgnoreProperties(p => p.CreatedDate, p => p.ChangedDate); } public override void Execute() { Verifier.VerifySql("DeviceGeneralTests.Test0CheckAccountDevicesEntries", "SELECT I3D, AccountI3D, DescriptionRTF, Description, DeviceId, CreatedByI3D, ChangedByI3D, SerialNumber, Model, Manufacturer, Location, IsDeleted, DeletedByI3D, OriginKind, BranchI3D FROM AccountDevices"); var newAccountDevice = Test1CreateNewAccountDevice(false); Test2UpdateAccountDevice(newAccountDevice); Test3GetAccountDeviceAndUpdate(); Test4TestGetAccountDevicesFilter(); Test5DeleteAccountDevice(newAccountDevice.I3D); var assignedDeviceI3D = Test6AssignAccountDeviceToTicket(ticketI3D: 1332, accountDeviceI3D: newAccountDevice.I3D); Test7GetAccountDeviceLogsTest(newAccountDevice.I3D); Verifier.VerifySql("DeviceGeneralTests.Test8CheckAccountDevicesEntries", "SELECT I3D, AccountI3D, DescriptionRTF, Description, DeviceId, CreatedByI3D, ChangedByI3D, SerialNumber, Model, Manufacturer, Location, IsDeleted, DeletedByI3D, OriginKind, BranchI3D FROM AccountDevices"); Verifier.VerifySql("DeviceGeneralTests.Test8CheckAccountDeviceUrisEntries", "SELECT * FROM AccountDeviceUris"); Verifier.VerifySql("DeviceGeneralTests.Test8CheckAccountDevicesToTicketsEntries", "SELECT * FROM AccountDevicesToTickets"); Verifier.VerifySql("DeviceGeneralTests.Test8CheckAccountDeviceLogsEntries", "SELECT I3D, AccountDeviceI3D, CreatedByI3D, Message FROM AccountDeviceLogs"); Test9GetTicketsByAccountDevice(assignedDeviceI3D); } private void Test9GetTicketsByAccountDevice(int accountDeviceI3D) { using var session = new BLSession(); var tickets = session.GetBL().GetHelpdesksThroughPaging(new HelpdeskFilter { AccountDeviceI3Ds = new List { accountDeviceI3D } }, HelpdeskSort.Number, false, 1, int.MaxValue, base.GetLoggedInUser()).ThrowIfError(); Verifier.Verify("DeviceGeneralTests.Test9GetTicketsByAccountDevice", tickets.Result.Select(s => s.Number).ToList()); } private int Test6AssignAccountDeviceToTicket(int ticketI3D, int accountDeviceI3D) { // Step 1: Assign account device to ticket using var session = new BLSession(); var assignRequest = new UpdateTicketAccountDevicesRequest { TicketI3D = ticketI3D, AccountDeviceI3Ds = new List { accountDeviceI3D }, IsAdditive = true }; var assignResult = session.GetBL() .UpdateTicketAccountDevices(assignRequest, GetLoggedInUser()); assignResult.ThrowIfError(); // Verify that the device is assigned to the ticket var checkAssigned = session.GetBL() .SearchAccountDevices(new GetAccountDeviceFilter { TicketI3D = ticketI3D, IncludeDeleted = true }) .ThrowIfError(); Verifier.Verify("DeviceGeneralTests.Test6AssignAccountDeviceToTicket_Step1_CheckAssigned", checkAssigned); // Step 2: Assign other devices to the same ticket var newAccountDevice = Test1CreateNewAccountDevice(true); var step2AccountDeviceI3D = newAccountDevice.I3D; assignRequest = new UpdateTicketAccountDevicesRequest { TicketI3D = ticketI3D, AccountDeviceI3Ds = new List { newAccountDevice.I3D }, IsAdditive = true }; session.GetBL().UpdateTicketAccountDevices(assignRequest, GetLoggedInUser()) .ThrowIfError(); // Verify that both devices are assigned to the ticket checkAssigned = session.GetBL() .SearchAccountDevices(new GetAccountDeviceFilter { TicketI3D = ticketI3D, IncludeDeleted = true }) .ThrowIfError(); // Step 3: Assign multiple devices to the ticket without additive newAccountDevice = Test1CreateNewAccountDevice(true); var step3AccountDeviceI3D = newAccountDevice.I3D; assignRequest = new UpdateTicketAccountDevicesRequest { TicketI3D = ticketI3D, AccountDeviceI3Ds = new List { step3AccountDeviceI3D, step2AccountDeviceI3D }, IsAdditive = false // Overwrites existing assignments }; session.GetBL().UpdateTicketAccountDevices(assignRequest, GetLoggedInUser()) .ThrowIfError(); // Verify that only the new devices are assigned to the ticket checkAssigned = session.GetBL() .SearchAccountDevices(new GetAccountDeviceFilter { TicketI3D = ticketI3D, IncludeDeleted = true }) .ThrowIfError(); Verifier.Verify("DeviceGeneralTests.Test6AssignAccountDeviceToTicket_Step3_CheckAssigned", checkAssigned); // Step 4: Get logs from the original account device var logs = session.GetBL().GetAccountDeviceLogs(accountDeviceI3D); Verifier.Verify("DeviceGeneralTests.Test6AssignAccountDeviceToTicket_Logs", logs); return step2AccountDeviceI3D; } private void Test5DeleteAccountDevice(int accountDeviceI3D) { using var session = new BLSession(); session.GetBL() .DeleteAccountDevices( new DeleteAccountDevicesRequest { AccountDeviceI3Ds = new List { accountDeviceI3D } }, GetLoggedInUser()).ThrowIfError(); var checkDeleted = session.GetBL() .GetAccountDevice(new List { accountDeviceI3D }, null).ThrowIfError(); Verifier.Verify("DeviceGeneralTests.Test5DeleteAccountDevice_CheckDeleted", checkDeleted); var logs = session.GetBL().GetAccountDeviceLogs(accountDeviceI3D); Verifier.Verify("DeviceGeneralTests.Test5DeleteAccountDevice_Logs", logs); } private void Test4TestGetAccountDevicesFilter() { using var session = new BLSession(); var filter = new GetAccountDeviceFilter() { IncludeDeleted = false }; var result = session.GetBL().SearchAccountDevices(filter); Assert.NotNull(result); Assert.DoesNotContain(result.Data, device => device.IsDeleted); filter = new GetAccountDeviceFilter() { IncludeDeleted = true }; result = session.GetBL().SearchAccountDevices(filter); Assert.NotNull(result); Assert.DoesNotContain(result.Data, device => device.IsDeleted); filter = new GetAccountDeviceFilter() { AccountI3Ds = new List { 1, 2, 3 } }; result = session.GetBL().SearchAccountDevices(filter); Assert.NotNull(result); Assert.All(result.Data, device => Assert.Contains(device.AccountI3D, filter.AccountI3Ds)); filter = new GetAccountDeviceFilter() { AccountDeviceI3Ds = new List { 1, 2, 3 } }; result = session.GetBL().SearchAccountDevices(filter); Assert.NotNull(result); Assert.All(result.Data, device => Assert.Contains(device.I3D, filter.AccountDeviceI3Ds)); filter = new GetAccountDeviceFilter() { ShortName = "ShortName" }; result = session.GetBL().SearchAccountDevices(filter); Assert.NotNull(result); Assert.All(result.Data, device => Assert.Equal(filter.ShortName, device.ShortName)); filter = new GetAccountDeviceFilter() { DeviceId = "DeviceId" }; result = session.GetBL().SearchAccountDevices(filter); Assert.NotNull(result); Assert.All(result.Data, device => Assert.Equal(filter.DeviceId, device.DeviceId)); filter = new GetAccountDeviceFilter() { SearchText = "" }; result = session.GetBL().SearchAccountDevices(filter); Assert.NotNull(result); Assert.All(result.Data, device => Assert.True(device.ShortName.Contains(filter.SearchText) || device.Description.Contains(filter.SearchText) || device.DeviceId.Contains(filter.SearchText))); } private void Test3GetAccountDeviceAndUpdate() { using var session = new BLSession(); // Step 1 : Search for account devices var searchResult = session.GetBL().SearchAccountDevices(new GetAccountDeviceFilter() { IncludeDeleted = false }).ThrowIfError(); Verifier.Verify("DeviceGeneralTests.Test3GetAccountDeviceAndUpdate_Step1_GetAccountDevices", searchResult); // Step 2 : Get the first account device var device = session.GetBL() .GetAccountDevice(new List { searchResult.First().I3D }, null).ThrowIfError(); Verifier.Verify("DeviceGeneralTests.Test3GetAccountDeviceAndUpdate_Step2_GetAccountDevice", device); var firstAccountDevice = device.FirstOrDefault(); // Step 3 : Update the account device if (firstAccountDevice != null) { firstAccountDevice.ShortName = "Updated name 2"; var updateResult = session.GetBL() .SaveAccountDevice(GetLoggedInUser(), firstAccountDevice).ThrowIfError(); Verifier.Verify("DeviceGeneralTests.Test3GetAccountDeviceAndUpdate_Step3_UpdateAccountDevice", updateResult); } } private AccountDeviceDTO Test1CreateNewAccountDevice(bool additionalCreates) { using var session = new BLSession(); var newAccountDevice = new AccountDeviceDTO { AccountI3D = 4, ShortName = "Test", Description = "Test Description", DescriptionRTF = "Test Description RTF", DeviceId = "Test Device Id", WarrantyExpiryDate = DateTime.Now, CreatedDate = DateTime.Now, CreatedByI3D = 1, ChangedDate = DateTime.Now, ChangedByI3D = 1, SerialNumber = "Test Serial Number", Model = "Test Model", Manufacturer = "Test Manufacturer", Location = "Test Location", IsDeleted = false, DeletedDate = null, DeletedByI3D = 0, OriginKind = AccountDeviceOriginKind.Other, BranchI3D = 0, Uris = new List() { new AccountDeviceUriDTO() { Kind = AccountDeviceUriKind.TeamViewer, Uri = "Test Uri TeamViewer" }, new AccountDeviceUriDTO() { Kind = AccountDeviceUriKind.RDP, Uri = "Test Uri RDP" } } }; var result = session.GetBL().SaveAccountDevice(GetLoggedInUser(), newAccountDevice) .ThrowIfError(); if (!additionalCreates) { Verifier.Verify("DeviceGeneralTests.Test1CreateNewAccountDevice", result); } // Test Logs //var logs = session.GetBL().GetAccountDeviceLogs(newAccountDevice.I3D); return result; } private void Test2UpdateAccountDevice(AccountDeviceDTO accountDevice) { using var session = new BLSession(); accountDevice.AccountI3D = 4; accountDevice.ShortName = "Updated name"; accountDevice.DescriptionRTF = "Updated Description RTF"; accountDevice.Description = "Updated Description"; accountDevice.DeviceId = "Updated Device Id"; accountDevice.WarrantyExpiryDate = DateTime.Now; accountDevice.SerialNumber = "Updated Serial Number"; accountDevice.Model = "Updated Model"; accountDevice.Manufacturer = "Updated Manufacturer"; accountDevice.Location = "Updated Location"; accountDevice.IsDeleted = false; accountDevice.OriginKind = AccountDeviceOriginKind.Other; accountDevice.BranchI3D = 1; accountDevice.Uris.First().Uri = "Updated Uri TeamViewer"; accountDevice.Uris.Remove(accountDevice.Uris.Last()); accountDevice.Uris.Add(new AccountDeviceUriDTO() { Kind = AccountDeviceUriKind.Supremo, Uri = "https://supremo.com/135412", AccountDeviceI3D = accountDevice.I3D }); var result = session.GetBL().SaveAccountDevice(GetLoggedInUser(), accountDevice) .ThrowIfError(); Verifier.Verify("DeviceGeneralTests.Test2UpdateAccountDevice", result); } private void Test7GetAccountDeviceLogsTest(int accountDeviceI3D) { using var session = new BLSession(); var logs = session.GetBL().GetAccountDeviceLogs(accountDeviceI3D); Verifier.Verify("DeviceGeneralTests.Test7GetAccountDeviceLogsTest", logs); } }