using System; using System.Collections.Generic; using System.Data; using System.Linq; using Centron.BusinessLogic; using Centron.BusinessLogic.Administration.Logins; using Centron.BusinessLogic.EmployeeArea; using Centron.BusinessLogic.Mail.Factory; using Centron.DAO; using Centron.Data.Entities.Administration.Logins; using Centron.Tests.EndToEnd.Infrastructure.Fixtures; using Xunit; using Xunit.Abstractions; using Xunit.Extensions.AssemblyFixture; using Centron.Core; using Centron.Interfaces.Administration.Logins; using Centron.Interfaces.BL; namespace Centron.Tests.EndToEnd.Infrastructure { public abstract class EndToEndTest : CentronTest, IAssemblyFixture, IAssemblyFixture, IAssemblyFixture { [Fact] public abstract void Execute(); protected LoggedInUser GetLoggedInUser(int userI3D = 11) { using var session = new BLSession(); return new LoggedInUser(session.GetBL().GetAppUser(f => f.I3D == userI3D)); } private Dictionary _ticketId = new(); protected LoggedInUser GetLoggedInUserWebAccount(int webAccountI3D = 6) { using var session = new BLSession(); var webAccountAppUser = session.GetBL().GetAppUserForWebaccounts().ThrowIfError(); var emp = webAccountAppUser.Employee; //Make sure the employee is loaded too var appUser = emp.AppUser; //Make sure this is loaded as well var webAccount = session.GetBL().GetWebAccountByI3D(webAccountI3D); var rights = webAccount.WebRights.ToList(); //Make sure the rights are loaded too var existingTicketId = this._ticketId.TryGetValue(webAccountI3D, out var i) ? i : null; var ticket = existingTicketId switch { not null => session.GetBL().GetTicket(existingTicketId).ThrowIfError(), null => session.GetBL().CreateNewTicket( ApplicationKind.Developer, LicenseGuids.Developer, "Test-Device", webAccountAppUser, webAccount).ThrowIfError() }; // Update web-account, to make sure we get current version of the web-account, with the current rights ticket.WebAccount = webAccount; this._ticketId[webAccountI3D] = ticket.TicketId; return new LoggedInUser(webAccountAppUser, ticket); } public EndToEndTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { Guard.NotNull(testOutputHelper, nameof(testOutputHelper)); Database.RestoreFromSnapshot(); } public void Sql(string statement, Action addParametersAction = null) { using (var session = new DAOSession()) { session.Advanced.RawSqlAccess.ExecuteNonQueryTransactionSave(statement, addParametersAction).ThrowIfError(); } } public T Sql(string statement, Action addParametersAction = null) { using (var session = new DAOSession()) { return session.Advanced.RawSqlAccess.ExecuteScalarTransactionSave(statement, addParametersAction).ThrowIfError(); } } public void ExecuteMatrix(T1[] input1, Action execute) { this.ExecuteMatrix( input1, new object[]{null}, (inputItem1, _) => execute(inputItem1)); } public void ExecuteMatrix(T1[] input1, T2[] input2, Action execute) { this.ExecuteMatrix( input1, input2, new object[] {null}, (inputItem1, inputItem2, _) => execute(inputItem1, inputItem2)); } public void ExecuteMatrix(T1[] input1, T2[] input2, T3[] input3, Action execute) { foreach (var inputItem1 in input1) { foreach (var inputItem2 in input2) { foreach (var inputItem3 in input3) { execute(inputItem1, inputItem2, inputItem3); } } } } public void WithSession(Action action) { using (var session = new BLSession()) { action(session); } } public T WithSession(Func getter) { using (var session = new BLSession()) { return getter(session); } } } }