internal override bool Eval(string left, Func <string, string, string, IEnumerable <string[]> > getArgumentsFunc, bool hasError) { var factory = Dev2DecisionFactory.Instance(); if (MatchType == enDecisionType.IsError) { return(hasError); } if (MatchType == enDecisionType.IsNotError) { return(!hasError); } IList <bool> ret = new List <bool>(); var items = getArgumentsFunc(left, From, To); foreach (var arguments in items) { try { ret.Add(factory.FetchDecisionFunction(MatchType).Invoke(arguments)); } catch (Exception) { ret.Add(false); } } return(ret.All(o => o)); }
public void Dev2DecisionFactory_Instance_ShouldHaveAStaticInstance() { //------------Setup for test-------------------------- //------------Execute Test--------------------------- //------------Assert Results------------------------- Assert.IsNotNull(Dev2DecisionFactory.Instance()); }
private IServiceTestModelTO Eval(Guid resourceId, IDSFDataObject dataObject, IServiceTestModelTO test) { Dev2Logger.Debug("Getting Resource to Execute"); var resourceCatalog = ResourceCat ?? ResourceCatalog.Instance; IDev2Activity resource = resourceCatalog.Parse(TheWorkspace.ID, resourceId); Dev2JsonSerializer serializer = new Dev2JsonSerializer(); var execPlan = serializer.SerializeToBuilder(resource); var clonedExecPlan = serializer.Deserialize <IDev2Activity>(execPlan); Dev2Logger.Debug("Got Resource to Execute"); if (test != null) { var testPassed = true; var canExecute = true; var failureMessage = new StringBuilder(); if (ServerAuthorizationService.Instance != null) { var authorizationService = ServerAuthorizationService.Instance; var hasView = authorizationService.IsAuthorized(AuthorizationContext.View, DataObject.ResourceID.ToString()); var hasExecute = authorizationService.IsAuthorized(AuthorizationContext.Execute, DataObject.ResourceID.ToString()); canExecute = hasExecute && hasView; } if (!canExecute) { dataObject.Environment.AllErrors.Add("Unauthorized to execute this resource."); } else { if (!dataObject.StopExecution) { dataObject.ServiceTest = test; UpdateToPending(test.TestSteps); EvalInner(dataObject, clonedExecPlan, dataObject.ForEachUpdateValue, test.TestSteps); if (test.Outputs != null) { var dev2DecisionFactory = Dev2DecisionFactory.Instance(); var testRunResults = test.Outputs.SelectMany(output => GetTestRunResults(dataObject, output, dev2DecisionFactory)).ToList(); testPassed = testRunResults.All(result => result.RunTestResult == RunResult.TestPassed); if (!testPassed) { failureMessage = failureMessage.Append(string.Join("", testRunResults.Select(result => result.Message).Where(s => !string.IsNullOrEmpty(s)).ToList())); } } } } ValidateError(test, testPassed, failureMessage); test.FailureMessage = failureMessage.ToString(); return(test); } throw new Exception($"Test {dataObject.TestName} for Resource {dataObject.ServiceName} ID {resourceId}"); }
private static void UpdateDebugStateWithAssertion(IDSFDataObject dataObject, IServiceTestStep stepToBeAsserted, IDebugState debugState) { if (debugState != null) { var factory = Dev2DecisionFactory.Instance(); var res = stepToBeAsserted.StepOutputs.SelectMany(output => GetTestRunResults(dataObject, output, factory, debugState)); var testRunResults = res as IList <TestRunResult> ?? res.ToList(); var testPassed = testRunResults.All(result => result.RunTestResult == RunResult.TestPassed || result.RunTestResult == RunResult.None); var serviceTestFailureMessage = string.Join("", testRunResults.Select(result => result.Message)); UpdateBasedOnFinalResult(dataObject, stepToBeAsserted, testPassed, testRunResults, serviceTestFailureMessage); } }
static void GetTestResults(IDSFDataObject dataObject, IServiceTestModelTO test, ref bool testPassed, ref StringBuilder failureMessage) { if (test.Outputs is null) { return; } var dev2DecisionFactory = Dev2DecisionFactory.Instance(); var testRunResults = test.Outputs.SelectMany(output => GetTestRunResults(dataObject, output, dev2DecisionFactory)).ToList(); testPassed = testRunResults.All(result => result.RunTestResult == RunResult.TestPassed); if (!testPassed) { failureMessage = failureMessage.Append(string.Join("", testRunResults.Select(result => result.Message).Where(s => !string.IsNullOrEmpty(s)).ToList())); } }
public bool ExecuteDecisionStack(string decisionDataPayload, IList <string> oldAmbientData, int update) { var dlId = FetchDataListID(oldAmbientData); var newDecisionData = Dev2DecisionStack.FromVBPersitableModelToJSON(decisionDataPayload); var dds = EvaluateRegion(newDecisionData, dlId, update); var env = Dev2DataListDecisionHandler._environments[dlId]; if (dds != null) { if (dlId != GlobalConstants.NullDataListID) { try { if (dds.TheStack != null) { for (int i = 0; i < dds.TotalDecisions; i++) { var dd = dds.GetModelItem(i); var typeOf = dd.EvaluationFn; // Treat Errors special if (typeOf == enDecisionType.IsError || typeOf == enDecisionType.IsNotError) { dd.Col1 = env.FetchErrors(); } var op = Dev2DecisionFactory.Instance().FetchDecisionFunction(typeOf); if (op != null) { try { var result = op.Invoke(dds.GetModelItem(i).FetchColsAsArray()); if (!result && dds.Mode == Dev2DecisionMode.AND) { // Naughty stuff, we have a false in AND mode... break return(false); } if (result && dds.Mode == Dev2DecisionMode.OR) { return(true); } } catch (Exception e) { // An error, push into the DL env.AddError(e.Message); return(false); } } else { throw new InvalidExpressionException(string.Format(ErrorResource.CouldNotEvaluateDecisionData, typeOf)); } } // else we are in AND mode and all have passed ;) if (dds.Mode == Dev2DecisionMode.AND) { return(true); } //finally, it must be OR mode with no matches ;( return(false); } throw new InvalidExpressionException(ErrorResource.InvalidModelDataSent); } catch { // all hell has broken loose... ;) throw new InvalidExpressionException(ErrorResource.NoModelDataSent); } } throw new InvalidExpressionException(ErrorResource.NoDataListIDsent); } throw new InvalidExpressionException(ErrorResource.DataListErrors); }
public override IDev2Activity Execute(IDSFDataObject dataObject, int update) { ErrorResultTO allErrors = new ErrorResultTO(); try { InitializeDebug(dataObject); if (dataObject.IsDebugMode()) { _debugInputs = CreateDebugInputs(dataObject.Environment); DispatchDebugState(dataObject, StateType.Before, 0); } var stack = Conditions.TheStack.Select(a => ParseDecision(dataObject.Environment, a)); var factory = Dev2DecisionFactory.Instance(); var res = stack.SelectMany(a => { if (a.EvaluationFn == enDecisionType.IsError) { return(new[] { dataObject.Environment.AllErrors.Count > 0 }); } if (a.EvaluationFn == enDecisionType.IsNotError) { return(new[] { dataObject.Environment.AllErrors.Count == 0 }); } IList <bool> ret = new List <bool>(); var iter = new WarewolfListIterator(); var c1 = new WarewolfAtomIterator(a.Cols1); var c2 = new WarewolfAtomIterator(a.Cols2); var c3 = new WarewolfAtomIterator(a.Cols3); iter.AddVariableToIterateOn(c1); iter.AddVariableToIterateOn(c2); iter.AddVariableToIterateOn(c3); while (iter.HasMoreData()) { ret.Add(factory.FetchDecisionFunction(a.EvaluationFn).Invoke(new[] { iter.FetchNextValue(c1), iter.FetchNextValue(c2), iter.FetchNextValue(c3) })); } return(ret); }); var resultval = And ? res.Aggregate(true, (a, b) => a && b) : res.Any(a => a); if (dataObject.IsDebugMode()) { _debugOutputs = GetDebugOutputs(resultval.ToString()); } if (resultval) { if (TrueArm != null) { var activity = TrueArm.FirstOrDefault(); return(activity); } } else { if (FalseArm != null) { var activity = FalseArm.FirstOrDefault(); return(activity); } } } catch (Exception e) { allErrors.AddError(e.Message); } finally { // Handle Errors var hasErrors = allErrors.HasErrors(); if (hasErrors) { DisplayAndWriteError("DsfDeleteRecordsActivity", allErrors); var errorString = allErrors.MakeDisplayReady(); dataObject.Environment.AddError(errorString); } } if (dataObject.IsDebugMode()) { _debugOutputs = new List <DebugItem>(); _debugOutputs = new List <DebugItem>(); DispatchDebugState(dataObject, StateType.Duration, update); } return(null); }
#pragma warning disable S1541 // Methods and properties should not be too complex #pragma warning disable S3776 // Cognitive Complexity of methods should not be too high IDev2Activity ExecuteDecision(IDSFDataObject dataObject) #pragma warning restore S3776 // Cognitive Complexity of methods should not be too high #pragma warning restore S1541 // Methods and properties should not be too complex { InitializeDebug(dataObject); if (dataObject.IsDebugMode()) { _debugInputs = CreateDebugInputs(dataObject.Environment); DispatchDebugState(dataObject, StateType.Before, 0); } var errorIfNull = !Conditions.TheStack.Any(decision => decision.EvaluationFn == enDecisionType.IsNull || decision.EvaluationFn == enDecisionType.IsNotNull); var stack = Conditions.TheStack.Select(a => ParseDecision(dataObject.Environment, a, errorIfNull)); var factory = Dev2DecisionFactory.Instance(); var res = stack.SelectMany(a => { if (a.EvaluationFn == enDecisionType.IsError) { return(new[] { dataObject.Environment.AllErrors.Count > 0 }); } if (a.EvaluationFn == enDecisionType.IsNotError) { return(new[] { dataObject.Environment.AllErrors.Count == 0 }); } IList <bool> ret = new List <bool>(); var iter = new WarewolfListIterator(); var c1 = new WarewolfAtomIterator(a.Cols1); var c2 = new WarewolfAtomIterator(a.Cols2); var c3 = new WarewolfAtomIterator(a.Cols3); iter.AddVariableToIterateOn(c1); iter.AddVariableToIterateOn(c2); iter.AddVariableToIterateOn(c3); while (iter.HasMoreData()) { try { ret.Add(factory.FetchDecisionFunction(a.EvaluationFn).Invoke(new[] { iter.FetchNextValue(c1), iter.FetchNextValue(c2), iter.FetchNextValue(c3) })); } catch (Exception) { if (errorIfNull) { throw; } ret.Add(false); } } return(ret); }); var results = res as IList <bool> ?? res.ToList(); var resultval = true; if (results.Any()) { if (And) { if (results.Any(b => !b)) { resultval = false; } } else { resultval = results.Any(b => b); } } Result = GetResultString(resultval.ToString(), Conditions); if (dataObject.IsDebugMode()) { _debugOutputs = GetDebugOutputs(resultval.ToString()); } if (resultval) { if (TrueArm != null) { var activity = TrueArm.FirstOrDefault(); return(activity); } } else { if (FalseArm != null) { var activity = FalseArm.FirstOrDefault(); return(activity); } } return(null); }
// Guid dlID /// <summary> /// Executes the decision stack. /// </summary> /// <param name="decisionDataPayload">The decision data payload.</param> /// <param name="oldAmbientData">The old ambient data.</param> /// <returns></returns> /// <exception cref="System.Data.InvalidExpressionException">Could not evaluate decision data - No decision function found for [ + typeOf + ]</exception> public bool ExecuteDecisionStack(string decisionDataPayload, IList <string> oldAmbientData) { Guid dlId = FetchDataListID(oldAmbientData); // if(dlId == GlobalConstants.NullDataListID) throw new InvalidExpressionException("Could not evaluate decision data - no DataList ID sent!"); string newDecisionData = Dev2DecisionStack.FromVBPersitableModelToJSON(decisionDataPayload); var dds = EvaluateRegion(newDecisionData, dlId); var env = _environments[dlId]; if (dds != null) { if (dlId != GlobalConstants.NullDataListID) { try { if (dds.TheStack != null) { for (int i = 0; i < dds.TotalDecisions; i++) { Dev2Decision dd = dds.GetModelItem(i); enDecisionType typeOf = dd.EvaluationFn; // Treat Errors special if (typeOf == enDecisionType.IsError || typeOf == enDecisionType.IsNotError) { dd.Col1 = String.Join("", env.Errors); } IDecisionOperation op = Dev2DecisionFactory.Instance().FetchDecisionFunction(typeOf); if (op != null) { try { bool result = op.Invoke(dds.GetModelItem(i).FetchColsAsArray()); if (!result && dds.Mode == Dev2DecisionMode.AND) { // Naughty stuff, we have a false in AND mode... break return(false); } if (result && dds.Mode == Dev2DecisionMode.OR) { return(true); } } catch (Exception e) { // An error, push into the DL env.AddError(e.Message); return(false); } } else { throw new InvalidExpressionException("Could not evaluate decision data - No decision function found for [ " + typeOf + " ]"); } } // else we are in AND mode and all have passed ;) if (dds.Mode == Dev2DecisionMode.AND) { return(true); } //finally, it must be OR mode with no matches ;( return(false); } throw new InvalidExpressionException("Could not evaluate decision data - Invalid model data sent!"); } catch { // all hell has broken loose... ;) throw new InvalidExpressionException("Could not evaluate decision data - No model data sent!"); } } throw new InvalidExpressionException("Could not evaluate decision data - no DataList ID sent!"); } throw new InvalidExpressionException("Could not populate decision model - DataList Errors!"); }
// Guid dlID /// <summary> /// Executes the decision stack. /// </summary> /// <param name="decisionDataPayload">The decision data payload.</param> /// <param name="oldAmbientData">The old ambient data.</param> /// <returns></returns> /// <exception cref="System.Data.InvalidExpressionException">Could not evaluate decision data - No decision function found for [ + typeOf + ]</exception> public bool ExecuteDecisionStack(string decisionDataPayload, IList <string> oldAmbientData) { // Evaluate decisionDataPayload through the EvaluateFunction ;) Guid dlId = FetchDataListID(oldAmbientData); if (dlId == GlobalConstants.NullDataListID) { throw new InvalidExpressionException("Could not evaluate decision data - no DataList ID sent!"); } // Swap out ! with a new internal token to avoid nasty issues with string newDecisionData = Dev2DecisionStack.FromVBPersitableModelToJSON(decisionDataPayload); var dds = EvaluateRegion(newDecisionData, dlId); ErrorResultTO errors = new ErrorResultTO(); if (dds != null) { if (dlId != GlobalConstants.NullDataListID) { try { if (dds.TheStack != null) { for (int i = 0; i < dds.TotalDecisions; i++) { Dev2Decision dd = dds.GetModelItem(i); enDecisionType typeOf = dd.EvaluationFn; // Treat Errors special if (typeOf == enDecisionType.IsError || typeOf == enDecisionType.IsNotError) { dd.Col1 = Compiler.EvaluateSystemEntry(dlId, enSystemTag.Dev2Error, out errors); } IDecisionOperation op = Dev2DecisionFactory.Instance().FetchDecisionFunction(typeOf); if (op != null) { try { bool result = op.Invoke(dds.GetModelItem(i).FetchColsAsArray()); if (!result && dds.Mode == Dev2DecisionMode.AND) { // Naughty stuff, we have a false in AND mode... break return(false); } if (result && dds.Mode == Dev2DecisionMode.OR) { return(true); } } catch (Exception e) { // An error, push into the DL ErrorResultTO errorErrors; errors.AddError(e.Message); Compiler.UpsertSystemTag(dlId, enSystemTag.Dev2Error, errors.MakeDataListReady(), out errorErrors); return(false); } } else { throw new InvalidExpressionException("Could not evaluate decision data - No decision function found for [ " + typeOf + " ]"); } } // else we are in AND mode and all have passed ;) if (dds.Mode == Dev2DecisionMode.AND) { return(true); } //finally, it must be OR mode with no matches ;( return(false); } throw new InvalidExpressionException("Could not evaluate decision data - Invalid model data sent!"); } catch { // all hell has broken loose... ;) throw new InvalidExpressionException("Could not evaluate decision data - No model data sent!"); } } throw new InvalidExpressionException("Could not evaluate decision data - no DataList ID sent!"); } throw new InvalidExpressionException("Could not populate decision model - DataList Errors!"); }