pszczyg

PowerShell cmdlet ready to be unit tested

May 6th, 2021 (edited)
724
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Management.Automation;
  3. using System.Collections.Generic;
  4. // ReSharper disable UnusedAutoPropertyAccessor.Global - for PowerShell purposes
  5. // ReSharper disable MemberCanBePrivate.Global - for PowerShell purposes
  6.  
  7. namespace CoreView.Management.PowerShell.Commands {
  8.     [Cmdlet(VerbsLifecycle.Invoke, "CVApi")]
  9.     public class InvokeCVApiCommand : PSCmdlet {
  10.         [Parameter(Mandatory = false, HelpMessage = "Output folder. If empty, cmdlet folder will be used")]
  11.         public string OutputFolder { get; set; }
  12.  
  13.         [Parameter(Mandatory = true, HelpMessage = "JSON file containing functions with parameters.", ParameterSetName = "FileToProcess")]
  14.         public string FunctionFilePath { get; set; }
  15.        
  16.         private readonly ISimpleLogger _logger;
  17.         private readonly IInvocationFileManager _fileManager;
  18.         private readonly ApiInvoker _apiInvoker;
  19.         private readonly IParameterParser<InvokeCvApiInputParameters, InvokeCvApiParsedParameters> _parameterParser;
  20.         private readonly IContextProvider _contextProvider;
  21.        
  22.         // ReSharper disable once UnusedMember.Global - used automatically
  23.         public InvokeCVApiCommand() {
  24.             _logger = new SimpleLogger(new CmdletLogger(this));
  25.             var fileInteractor = new FileInteractor();
  26.             _fileManager = new InvocationFileManager(_logger, fileInteractor);
  27.             _parameterParser = new InvokeCvApiParameterParser(fileInteractor, _fileManager, _logger);
  28.             _apiInvoker = new ApiInvoker(_logger);
  29.         }
  30.  
  31.         public InvokeCVApiCommand(IContextProvider contextProvider,
  32.             IInvocationFileManager fileManager,
  33.             ISimpleLogger logger,
  34.             IParameterParser<InvokeCvApiInputParameters, InvokeCvApiParsedParameters> parser) {
  35.             _fileManager = fileManager;
  36.             _logger = logger;
  37.             _parameterParser = parser;
  38.             _apiInvoker = new ApiInvoker(_logger);
  39.             _contextProvider = contextProvider;
  40.         }
  41.  
  42.         protected override void ProcessRecord() {
  43.             Environment.CurrentDirectory = _contextProvider.GetCurrentDirectory(this);
  44.             base.ProcessRecord();
  45.             var commandParams = new InvokeCvApiInputParameters(
  46.                 OutputFolder,
  47.                 FunctionFilePath,
  48.                 _contextProvider.GetCurrentDirectory(this));
  49.  
  50.             try {
  51.                 var parsedParameters = _parameterParser.Parse(commandParams);
  52.                 var invocationResults = InvokeAllApiMethods(parsedParameters);
  53.                 _fileManager.WriteLogToFile(_logger, parsedParameters.LogFilePath);
  54.                 WriteObject(invocationResults);
  55.             } catch (Exception ex) {
  56.                 _logger.Exception("Unexpected exception occurred. Program terminates.", ex);
  57.             }
  58.         }
  59.  
  60.         internal void ProcessInternal() {
  61.             BeginProcessing();
  62.             ProcessRecord();
  63.             EndProcessing();
  64.         }
  65.  
  66.  
  67.         private List<Tuple<FunctionInvocationData, FunctionInvocationResult>> InvokeAllApiMethods(InvokeCvApiParsedParameters parsedParameters) {
  68.             var invocationResults = new List<Tuple<FunctionInvocationData, FunctionInvocationResult>>();
  69.             foreach (var invocationData in parsedParameters.AllRequiredInvocations)
  70.             {
  71.                 var invocationResult = _apiInvoker.InvokeApiCommand(invocationData, parsedParameters,
  72.                     _contextProvider.GetLegacyContext());
  73.                 invocationResults.Add(new Tuple<FunctionInvocationData, FunctionInvocationResult>(invocationData, invocationResult));
  74.             }
  75.  
  76.             _logger.Info($"All {parsedParameters.AllRequiredInvocations.Count} invocations completed.");
  77.            
  78.             return invocationResults;
  79.         }
  80.     }
  81. }
  82.  
RAW Paste Data