/// <summary> /// Provides the beginning /// </summary> ///<value>A list of business rule processors to execute</value> public virtual ServiceResult Execute(bool abortIfFailure) { AbortOnFailure = abortIfFailure; ServiceResult result = new ServiceResult(); for (int i = 0; i < ProcessorList.Count; i++) { currentProcessorIndex = i; if (ProcessorList[i] == null) { throw new ArgumentNullException(string.Format("The process located at index {0} of the ProcessorList is null.", i)); } //Fire Process Starting Event OnProcessStarting((ProcessorBase)ProcessorList[i]); ProcessorList[i].ChangeProcessorState(ProcessorState.Running); IProcessorResult processResult = null; try { //Begin Process processResult = ProcessorList[i].Process(); } catch (Exception exc) { Logger.Write(exc); result.ExceptionList.Add((ProcessorBase)ProcessorList[i], exc); } //if the processor didn't do cleanup, cleanup by default. if (ProcessorList[i].CurrentProcessorState == ProcessorState.Running) { ProcessorList[i].ChangeProcessorState(processResult.Result ? ProcessorState.Completed : ProcessorState.Stopped); } if (processResult != null) { //Add to Processor Result List result.ProcessorResultList.Add(processResult); //Add To Aggregated Broken Rules List foreach (BrokenRulesList list in processResult.BrokenRulesLists.Values) { result.ProcessBrokenRuleLists.Add((ProcessorBase)ProcessorList[i], list); } } //Fire Process Ending Event OnProcessEnded(ProcessorList[i] as ProcessorBase); if ((processResult == null || !processResult.Result) && AbortOnFailure) { return(result); } } return(result); }
// TODO: [TestCase()] public void ValidJsonObjectIsProcessed(string definitionPath, string inputJsonFilePath) { var inputJson = File.ReadAllText(inputJsonFilePath); var definition = JsonConvert.DeserializeObject <ScriptProcessorDefinition>(File.ReadAllText(definitionPath)); var sut = new ScriptProcessor(definition); IProcessorResult processorResult = null; Assert.That(async() => processorResult = await sut.Process(DataModificationType.Created, nameof(UnitTestInputObject), "FakeID", inputJson), Throws.Nothing); Assert.That(processorResult, Is.Not.Null.And.TypeOf <SuccessProcessorResult>()); var outputObjects = ((SuccessProcessorResult)processorResult).Objects; Assert.That(outputObjects, Is.Not.Null); Assert.That(outputObjects.Count, Is.EqualTo(1)); var outputObject = outputObjects.Single(); Assert.That(outputObject.DataType, Is.EqualTo(definition.OutputType)); Assert.That(outputObject.Json, Is.Not.Empty); Console.WriteLine(outputObject.Json); }
/// <summary> /// Executes the processors in the processor list /// <remarks> /// If abortIfFailure is set to true then the execution will halt on the first failure ///</remarks> /// </summary> public virtual ServiceResult Execute(bool abortIfFailure) { AbortOnFailure = abortIfFailure; ServiceResult result = ServiceProcessResult; for (int i = 0; i < ProcessorList.Count; i++) { currentProcessorIndex = i; if (ProcessorList[i] == null) { throw new ArgumentNullException(string.Format("The process located at index {0} of the ProcessorList is null.", i)); } OnProcessStarting((ProcessorBase)ProcessorList[i]); //Fire Process Starting Event ProcessorList[i].ChangeProcessorState(ProcessorState.Running); IProcessorResult processResult = null; try { processResult = ProcessorList[i].Process(); //Begin Process } catch (Exception exc) { Logger.Write(exc); result.ExceptionList.Add((ProcessorBase)ProcessorList[i], exc); ProcessorList[i].ChangeProcessorState(ProcessorState.Stopped); } //if the processor didn't do cleanup, cleanup by default. if (ProcessorList[i].CurrentProcessorState == ProcessorState.Running) { ProcessorList[i].ChangeProcessorState(processResult.Result ? ProcessorState.Completed : ProcessorState.Stopped); } if (processResult != null) { //Add to Processor Result List // NOTE: I have a feeling this could throw an exception as well. result.ProcessorResultList.Add(processResult); //Add To Aggregated Broken Rules List foreach (BrokenRulesList items in processResult.BrokenRulesLists.Values) { // I really don't like how everything is casted to a ProcessorBase but yet the list just uses the interface... // This is really really bad :\ and will be changed in 3.0. // NOTE: If you are creating your own processors, you MUST inherit from ProcessorBase. ProcessorBase processorBase = (ProcessorBase)ProcessorList[i]; if (!result.ProcessBrokenRuleLists.ContainsKey(processorBase)) { result.ProcessBrokenRuleLists.Add(processorBase, items); } else { foreach (BrokenRule item in items) { if (!result.ProcessBrokenRuleLists[processorBase].Contains(item)) { result.ProcessBrokenRuleLists[processorBase].Add(item); } } } } } OnProcessEnded(ProcessorList[i] as ProcessorBase); //Fire Process Ending Event if ((processResult == null || !processResult.Result) && AbortOnFailure) { return(result); } } return(result); }