void RegularMethodExecution(Isolated <PluginRuntimeHandler> appDomain, PluginExecutionDto pluginExecutionDto, IDev2MethodInfo dev2MethodInfo, int i, int update, IDSFDataObject dataObject) { var start = DateTime.Now; pluginExecutionDto.ObjectString = ObjectResult; var result = PluginServiceExecutionFactory.InvokePlugin(appDomain, pluginExecutionDto, dev2MethodInfo, out string objString); pluginExecutionDto.ObjectString = objString; ObjectResult = objString; var pluginAction = MethodsToRun[i]; pluginAction.MethodResult = result.MethodResult; pluginAction.HasError = result.HasError; pluginAction.ErrorMessage = result.ErrorMessage; dev2MethodInfo.HasError = result.HasError; dev2MethodInfo.ErrorMessage = result.ErrorMessage; if (result.HasError) { DispatchDebugStateForMethod(pluginAction, dataObject, update, false, start); return; } AssignMethodResult(pluginAction, update, dataObject, start); if (!string.IsNullOrEmpty(ObjectName) && !pluginExecutionDto.IsStatic) { var jToken = JToken.Parse(ObjectResult) as JContainer ?? ObjectResult.DeserializeToObject(); dataObject.Environment.AddToJsonObjects(ObjectName, jToken); } }
public void PluginRuntimeHandler_Run_WhenHasInnerError_ExpectInerErrors() { //------------Setup for test-------------------------- var type = typeof(Human); var svc = CreatePluginService(new List <IDev2MethodInfo> { new Dev2MethodInfo { Method = "set_Name", Parameters = new List <IMethodParameter>() { new ConstructorParameter() { Name = "value", Value = "Micky", TypeName = typeof(string).FullName, IsRequired = true } } } }, type, new ServiceConstructor()); //------------Execute Test--------------------------- var mock = new Mock <IAssemblyLoader>(); Assembly loadedAssembly; var handler = new PluginRuntimeHandler(mock.Object); var pluginInvokeArgs = new PluginInvokeArgs { MethodsToRun = svc.MethodsToRun, PluginConstructor = new PluginConstructor { ConstructorName = svc.Constructor.Name, Inputs = new List <IConstructorParameter>(), }, AssemblyLocation = type.Assembly.Location, AssemblyName = type.Assembly.FullName, Fullname = type.FullName, }; var pluginExecutionDto = new PluginExecutionDto(String.Empty) { Args = pluginInvokeArgs }; var exception = new Exception("err", new Exception()); mock.Setup(loader => loader.TryLoadAssembly(It.IsAny <string>(), It.IsAny <string>(), out loadedAssembly)) .Throws(exception); var dev2MethodInfo = pluginInvokeArgs.MethodsToRun.First(); string stringOBj; var run = handler.Run(dev2MethodInfo, pluginExecutionDto, out stringOBj); Assert.IsNotNull(run); Assert.IsTrue(run.HasError); Assert.IsFalse(string.IsNullOrEmpty(run.ErrorMessage)); }
#pragma warning disable S1541 // Methods and properties should not be too complex void TryExecuteService(int update, IDSFDataObject dataObject, PluginExecutionDto pluginExecutionDto, PluginInvokeArgs args) #pragma warning restore S1541 // Methods and properties should not be too complex { using (var appDomain = PluginServiceExecutionFactory.CreateAppDomain()) { if (dataObject.IsServiceTestExecution) { var serviceTestStep = dataObject.ServiceTest?.TestSteps?.Flatten(step => step.Children)?.FirstOrDefault(step => step.ActivityID == pluginExecutionDto.Args.PluginConstructor.ID); if (serviceTestStep != null && serviceTestStep.Type == StepType.Mock) { var start = DateTime.Now; MockConstructorExecution(dataObject, serviceTestStep, ref pluginExecutionDto); DebugStateForConstructorInputsOutputs(dataObject, update, true, start); } else { var start = DateTime.Now; RegularConstructorExecution(dataObject, appDomain, ref pluginExecutionDto); DebugStateForConstructorInputsOutputs(dataObject, update, false, start); } } else { var start = DateTime.Now; RegularConstructorExecution(dataObject, appDomain, ref pluginExecutionDto); DebugStateForConstructorInputsOutputs(dataObject, update, false, start); } var index = 0; foreach (var dev2MethodInfo in args.MethodsToRun) { if (dataObject.IsServiceTestExecution) { var serviceTestStep = dataObject.ServiceTest?.TestSteps?.Flatten(step => step.Children)?.FirstOrDefault(step => step.ActivityID == dev2MethodInfo.ID); MethodExecution(update, dataObject, pluginExecutionDto, appDomain, index, dev2MethodInfo, serviceTestStep); } else { RegularMethodExecution(appDomain, pluginExecutionDto, dev2MethodInfo, index, update, dataObject); } if (dev2MethodInfo.HasError) { break; } index++; } } }
private void MethodExecution(int update, IDSFDataObject dataObject, PluginExecutionDto pluginExecutionDto, Isolated <PluginRuntimeHandler> appDomain, int index, IDev2MethodInfo dev2MethodInfo, IServiceTestStep serviceTestStep) { if (serviceTestStep != null) { if (serviceTestStep.Type == StepType.Mock) { MockMethodExecution(dataObject, serviceTestStep, dev2MethodInfo, index); } else { RegularMethodExecution(appDomain, pluginExecutionDto, dev2MethodInfo, index, update, dataObject); } } else { RegularMethodExecution(appDomain, pluginExecutionDto, dev2MethodInfo, index, update, dataObject); } }
void ExecuteService(int update, out ErrorResultTO errors, IPluginConstructor constructor, INamespaceItem namespaceItem, IDSFDataObject dataObject) { _childStatesToDispatch = new List <IDebugState>(); errors = new ErrorResultTO(); PluginExecutionDto pluginExecutionDto; if (Constructor.IsExistingObject) { var existingObj = DataListUtil.AddBracketsToValueIfNotExist(Constructor.ConstructorName); var warewolfEvalResult = dataObject.Environment.EvalForJson(existingObj); var existingObject = ExecutionEnvironment.WarewolfEvalResultToString(warewolfEvalResult); pluginExecutionDto = new PluginExecutionDto(existingObject); ObjectResult = pluginExecutionDto.ObjectString; } else { pluginExecutionDto = new PluginExecutionDto(string.Empty); } constructor.Inputs = new List <IConstructorParameter>(); foreach (var parameter in ConstructorInputs) { var resultToString = GetEvaluatedResult(dataObject, parameter.Value, parameter.EmptyIsNull, update); constructor.Inputs.Add(new ConstructorParameter() { TypeName = parameter.TypeName, Name = parameter.Name, Value = resultToString, EmptyToNull = parameter.EmptyIsNull, IsRequired = parameter.RequiredField }); } var args = BuidlPluginInvokeArgs(update, constructor, namespaceItem, dataObject); pluginExecutionDto.Args = args; try { TryExecuteService(update, dataObject, pluginExecutionDto, args); } catch (Exception e) { errors.AddError(e.Message.Contains("Cannot convert given JSON to target type") ? ErrorResource.JSONIncompatibleConversionError + Environment.NewLine + e.Message : e.Message); } }
void RegularConstructorExecution(IDSFDataObject dataObject, Isolated <PluginRuntimeHandler> appDomain, ref PluginExecutionDto pluginExecutionDto) { pluginExecutionDto = PluginServiceExecutionFactory.ExecuteConstructor(appDomain, pluginExecutionDto); ObjectResult = pluginExecutionDto.ObjectString; if (!string.IsNullOrEmpty(ObjectName) && !pluginExecutionDto.IsStatic) { var jToken = JToken.Parse(ObjectResult) as JContainer ?? ObjectResult.DeserializeToObject(); dataObject.Environment.AddToJsonObjects(ObjectName, jToken); } }
static void MockConstructorExecution(IDSFDataObject dataObject, IServiceTestStep serviceTestStep, ref PluginExecutionDto pluginExecutionDto) { if (!string.IsNullOrEmpty(serviceTestStep.StepOutputs?[0].Variable)) { try { var languageExpression = EvaluationFunctions.parseLanguageExpression(serviceTestStep.StepOutputs?[0].Variable, 0); if (languageExpression.IsJsonIdentifierExpression) { var jToken = JToken.Parse(serviceTestStep.StepOutputs?[0].Value) as JContainer ?? serviceTestStep.StepOutputs?[0].Value.DeserializeToObject(); dataObject.Environment.AddToJsonObjects(serviceTestStep.StepOutputs[0].Variable, jToken); pluginExecutionDto.ObjectString = serviceTestStep.StepOutputs[0].Value; } } catch (Exception e) { dataObject.Environment.Errors.Add(e.Message); } } }
private void ExecuteService(int update, out ErrorResultTO errors, IPluginConstructor constructor, INamespaceItem namespaceItem, IDSFDataObject dataObject) { _childStatesToDispatch = new List <IDebugState>(); errors = new ErrorResultTO(); PluginExecutionDto pluginExecutionDto; if (Constructor.IsExistingObject) { var existingObj = DataListUtil.AddBracketsToValueIfNotExist(Constructor.ConstructorName); var warewolfEvalResult = dataObject.Environment.EvalForJson(existingObj); var existingObject = ExecutionEnvironment.WarewolfEvalResultToString(warewolfEvalResult); pluginExecutionDto = new PluginExecutionDto(existingObject); ObjectResult = pluginExecutionDto.ObjectString; } else { pluginExecutionDto = new PluginExecutionDto(string.Empty); } constructor.Inputs = new List <IConstructorParameter>(); foreach (var parameter in ConstructorInputs) { var resultToString = GetEvaluatedResult(dataObject, parameter.Value, parameter.EmptyIsNull, update); constructor.Inputs.Add(new ConstructorParameter() { TypeName = parameter.TypeName, Name = parameter.Name, Value = resultToString, EmptyToNull = parameter.EmptyIsNull, IsRequired = parameter.RequiredField }); } var args = BuidlPluginInvokeArgs(update, constructor, namespaceItem, dataObject); pluginExecutionDto.Args = args; try { using (var appDomain = PluginServiceExecutionFactory.CreateAppDomain()) { if (dataObject.IsServiceTestExecution) { var serviceTestStep = dataObject.ServiceTest?.TestSteps?.Flatten(step => step.Children)?.FirstOrDefault(step => step.UniqueId == pluginExecutionDto.Args.PluginConstructor.ID); if (serviceTestStep != null && serviceTestStep.Type == StepType.Mock) { var start = DateTime.Now; MockConstructorExecution(dataObject, serviceTestStep, ref pluginExecutionDto); DebugStateForConstructorInputsOutputs(dataObject, update, true, start); } else { var start = DateTime.Now; RegularConstructorExecution(dataObject, appDomain, ref pluginExecutionDto); DebugStateForConstructorInputsOutputs(dataObject, update, false, start); } } else { var start = DateTime.Now; RegularConstructorExecution(dataObject, appDomain, ref pluginExecutionDto); DebugStateForConstructorInputsOutputs(dataObject, update, false, start); } var index = 0; foreach (var dev2MethodInfo in args.MethodsToRun) { if (dataObject.IsServiceTestExecution) { var serviceTestStep = dataObject.ServiceTest?.TestSteps?.Flatten(step => step.Children)?.FirstOrDefault(step => step.UniqueId == dev2MethodInfo.ID); if (serviceTestStep != null) { if (serviceTestStep.Type == StepType.Mock) { MockMethodExecution(dataObject, serviceTestStep, dev2MethodInfo, index); } else { RegularMethodExecution(appDomain, pluginExecutionDto, dev2MethodInfo, index, update, dataObject); } } else { RegularMethodExecution(appDomain, pluginExecutionDto, dev2MethodInfo, index, update, dataObject); } } else { RegularMethodExecution(appDomain, pluginExecutionDto, dev2MethodInfo, index, update, dataObject); } if (dev2MethodInfo.HasError) { break; } index++; } } } catch (Exception e) { if (e.Message.Contains("Cannot convert given JSON to target type")) { errors.AddError(ErrorResource.JSONIncompatibleConversionError + Environment.NewLine + e.Message); } else { errors.AddError(e.Message); } } }