using System; using Fab2ApprovalSystem.DMO; using Fab2ApprovalSystem.Models; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Fab2ApprovalTests.General; [TestClass] public class CorrectiveActionTests { #pragma warning disable CS8618 private static ILogger? _Logger; private static TestContext _TestContext; private static WebApplicationFactory _WebApplicationFactory; #pragma warning restore private static DateTime? SetD3DueDate(int cANo) => throw new NotImplementedException(); private static DateTime? SetD5D7DueDate(int cANo) => throw new NotImplementedException(); private static void NotifyApprovers(int cANo, int v) => throw new NotImplementedException(); private static void NotifySectionApprover(int cANo, int qAID, string sectionApproval) => throw new NotImplementedException(); private static void NotifyAssignee(int cANo, int d1AssigneeID, string v, DateTime? d3DueDate, DateTime? d5D7DueDate) => throw new NotImplementedException(); public static void SetGlobalVars(ILogger? logger, AppSettings appSettings) { logger?.LogDebug("Starting to set Fab2ApprovalSystem.Misc.GlobalVars"); Fab2ApprovalSystem.Misc.GlobalVars.AppSettings = appSettings; Fab2ApprovalSystem.Misc.GlobalVars.AttachmentUrl = appSettings.AttachmentUrl is null ? string.Empty : appSettings.AttachmentUrl; Fab2ApprovalSystem.Misc.GlobalVars.CA_BlankFormsLocation = appSettings.CABlankFormsLocation; Fab2ApprovalSystem.Misc.GlobalVars.DBConnection = appSettings.DBConnection; Fab2ApprovalSystem.Misc.GlobalVars.DB_CONNECTION_STRING = appSettings.DBConnectionString; Fab2ApprovalSystem.Misc.GlobalVars.hostURL = appSettings.HostURL; Fab2ApprovalSystem.Misc.GlobalVars.IS_INFINEON_DOMAIN = appSettings.IsInfineonDomain; Fab2ApprovalSystem.Misc.GlobalVars.MesaTemplateFiles = appSettings.MesaTemplateFiles; Fab2ApprovalSystem.Misc.GlobalVars.NDriveURL = appSettings.NDriveURL; Fab2ApprovalSystem.Misc.GlobalVars.SENDER_EMAIL = appSettings.SenderEmail; Fab2ApprovalSystem.Misc.GlobalVars.USER_ID = appSettings.UserId; Fab2ApprovalSystem.Misc.GlobalVars.USER_ISADMIN = appSettings.UserIsAdmin; Fab2ApprovalSystem.Misc.GlobalVars.WSR_URL = appSettings.WSR_URL; logger?.LogDebug("Finished setting Fab2ApprovalSystem.Misc.GlobalVars"); } [ClassInitialize] public static void ClassInitAsync(TestContext testContext) { _TestContext = testContext; _WebApplicationFactory = new WebApplicationFactory(); IServiceProvider serviceProvider = _WebApplicationFactory.Services.CreateScope().ServiceProvider; _Logger = serviceProvider.GetRequiredService>(); } private static void NonThrowTryCatch() { try { throw new Exception(); } catch (Exception) { } } private static void Edit(int currentUserId, CorrectiveActionDMO caDMO, CorrectiveAction model) { CorrectiveAction caPrevious = caDMO.GetCAItemReadOnly(model.CANo, currentUserId); if (currentUserId != caPrevious.D1AssigneeID && currentUserId != caPrevious.QAID && currentUserId != caPrevious.RequestorID) throw new Exception("User is not authorized to save the CA."); try { if (model.TriggerApproval) { //model.FollowUpDate = DateTime.Now.AddMonths(6); } caDMO.UpdateCorrectiveAction(model); if ((model.D1AssigneeID != model.CurrentD1AssigneeID && model.CASubmitted) || (model.CASubmitted && !caPrevious.CASubmitted)) { //Set Due Dates here: DateTime? D3DueDate = null; DateTime? D5D7DueDate = null; if (model.CAType != "D0") { D3DueDate = SetD3DueDate(model.CANo); if (model.CAType != "D3") { D5D7DueDate = SetD5D7DueDate(model.CANo); } } NotifyAssignee(model.CANo, model.D1AssigneeID, "CorrectiveActionAssignee.txt", D3DueDate, D5D7DueDate); } if (model.TriggerSectionApproval) { if (model.SectionApproval == "D5D6D7Validation") { caDMO.StartSectionApproval(model.CANo, model.RequestorID, model.SectionApproval); throw new Exception("Successfully Saved...Validation initiated!"); } caDMO.StartSectionApproval(model.CANo, model.QAID, model.SectionApproval); caDMO.StartSectionApproval(model.CANo, model.RequestorID, model.SectionApproval); NotifySectionApprover(model.CANo, model.QAID, model.SectionApproval); NotifySectionApprover(model.CANo, model.RequestorID, model.SectionApproval); //NotifyApprovers(model.CANo, 1); throw new Exception("Successfully Saved...Approval initiated!"); } if (model.TriggerApproval) { _ = caDMO.StartApproval(model.CANo, currentUserId, model.WorkFlowNumber); NotifyApprovers(model.CANo, 1); throw new Exception("Successfully Saved...Approval initiated!"); } } catch (Exception ex) { throw new Exception(ex.Message); } } internal static void TestCorrectiveAction(ILogger? logger, AppSettings appSettings, int caNo) { SetGlobalVars(logger, appSettings); CorrectiveAction ca; CorrectiveActionDMO caDMO = new(); ca = caDMO.GetCAItemReadOnly(caNo, appSettings.UserId); if (ca is null) throw new Exception($"{nameof(ca)}"); ca = caDMO.GetCAItem(caNo, appSettings.UserId); if (ca is null) throw new Exception($"{nameof(ca)}"); Edit(appSettings.UserId, caDMO, ca); } #if Release [Ignore] #endif [DataTestMethod] [DataRow(295)] public void TestCorrectiveActionIsAttachedOnly(int caNo) { _Logger?.LogInformation("Starting Web Application"); IServiceProvider? serviceProvider = _WebApplicationFactory?.Services.CreateScope().ServiceProvider; AppSettings? appSettings = serviceProvider?.GetRequiredService(); Assert.IsTrue(appSettings is not null); if (System.Diagnostics.Debugger.IsAttached) TestCorrectiveAction(_Logger, appSettings, caNo); _Logger?.LogInformation("{TestName} completed", _TestContext?.TestName); NonThrowTryCatch(); } }