// ReSharper disable UnusedParameter.Global protected static IDictionary <string, object> RunActivity(ActivityImplementationBase activity, IDictionary <string, object> inputs, bool expectedToComplete = true) // ReSharper restore UnusedParameter.Global { Assert.IsFalse(activity is WorkflowImplementation, "Dont use RunActivity to run a workflow, use WorkflowRunner.Instance."); var invoker = new WorkflowInvoker(); // invoker.RunStateFactory = new TestRunStateFactory(); var workflowRun = ActivityTestHelper.CreateWfRunForActivity(activity); workflowRun.Name = "Dummy Run" + DateTime.Now; var convertedInputs = new ActivityInputs(activity.ActivityInstance.GetInputArguments(), inputs); var factory = new TestRunStateFactory(); // var runState = Factory.Current.Resolve<IRunStateFactory>().CreateRunState(new WorkflowMetadata(), workflowRun); var runState = factory.CreateRunState(new WorkflowMetadata(), workflowRun); runState.CurrentActivity = activity.ActivityInstance; bool hasCompleted = activity.Execute(runState, convertedInputs); if (expectedToComplete && !hasCompleted) { Assert.Fail("Unfinished activity"); } return(runState.GetResult(activity.ActivityInstance.GetOutputArguments())); }
public void GetResourceByType( ) { var getResources = new GetResourcesActivity( ); getResources.Save( ); var getResourcesAs = getResources.As <WfActivity>( ); ActivityImplementationBase nextActivity = getResourcesAs.CreateWindowsActivity( ); var args = new Dictionary <string, object> { { "Object", TriggeredOnEnum.TriggeredOnEnum_Type }, }; IDictionary <string, object> result = RunActivity(nextActivity, args); var list = result["List"] as IEnumerable <IEntity>; var first = result["First"]; int countFields = TriggeredOnEnum.TriggeredOnEnum_Type.GetDescendantsAndSelf().Select(t => t.InstancesOfType.Count()).Sum(); Assert.IsNotNull(list); Assert.AreEqual(list.Count(), countFields, "The count of fields is correct"); }
public void GetResourceByReport( ) { var personReport = CodeNameResolver.GetInstance("AA_Person", "Report"); var getResources = new GetResourcesActivity( ); getResources.Save( ); //_toDelete.Add(getResources.Id); var getResourcesAs = getResources.As <WfActivity>( ); ActivityImplementationBase nextActivity = getResourcesAs.CreateWindowsActivity( ); var args = new Dictionary <string, object> { { "Report", personReport }, }; IDictionary <string, object> result = RunActivity(nextActivity, args); var list = result["List"] as IEnumerable <IEntity>; var first = result["First"]; var personType = CodeNameResolver.GetTypeByName("AA_Person").As <EntityType>(); long countPeople = personType.GetDescendantsAndSelf().SelectMany(t => t.InstancesOfType).Select(i => i.Id).Distinct().Count(); Assert.IsNotNull(list); Assert.AreEqual(list.Count( ), countPeople, "The count of people is correct"); }
public void UpdateRel() { var employeeType = Entity.Get <EntityType>("test:employee"); var managerType = Entity.Get <EntityType>("test:manager"); var reportsToRel = Entity.Get <Relationship>("test:reportsTo"); var bob = new Entity(employeeType); var bobManager = new Entity(employeeType); bob.Save(); bobManager.Save(); ToDelete.Add(bob.Id); ToDelete.Add(bobManager.Id); var updateActivity = new UpdateFieldActivity(); updateActivity.InputArguments.Add(new ResourceArgument { Name = "1" }.Cast <ActivityArgument>()); updateActivity.InputArguments.Add(new ResourceArgument { Name = "1_value_" }.Cast <ActivityArgument>()); updateActivity.InputArguments.Add(new BoolArgument { Name = "1_reverse" }.Cast <ActivityArgument>()); updateActivity.InputArguments.Add(new BoolArgument { Name = "1_replace" }.Cast <ActivityArgument>()); updateActivity.Save(); ToDelete.Add(updateActivity.Id); var updateActionAs = updateActivity.As <WfActivity>(); ActivityImplementationBase nextActivity = updateActionAs.CreateWindowsActivity(); var args = new Dictionary <string, object> { { "Record", bob }, { "1_value_", bobManager }, { "1", reportsToRel }, { "1_reverse", false } }; RunActivity(nextActivity, args); var bob2 = Entity.Get(bob); var reportsTo = bob2.GetRelationships(reportsToRel); Assert.AreEqual(1, reportsTo.Count(), "Relationship set"); Assert.AreEqual(bobManager.Id, reportsTo.First().Entity.Id, "Manager is correct"); }
public void SetChoice( ) { var trigger = new WfTriggerUserUpdatesResource( ); trigger.Save( ); ToDelete.Add(trigger.Id); var setAction = new SetChoiceActivity( ); setAction.Save( ); ToDelete.Add(setAction.Id); var setChoiceAs = setAction.As <WfActivity>( ); ActivityImplementationBase nextActivity = setChoiceAs.CreateWindowsActivity( ); var args = new Dictionary <string, object> { { "Resource to Update", trigger }, { "Field to Update", new EntityRef("core:triggeringCondition").Entity }, { "New Value", new EntityRef("core:triggeredOnEnumCreate").Entity }, }; RunActivity(nextActivity, args); trigger = Entity.Get <WfTriggerUserUpdatesResource>(trigger.Id); Assert.IsNotNull(trigger.TriggeringCondition, "Triggering condition should be set"); Assert.AreEqual("core:triggeredOnEnumCreate", trigger.TriggeringCondition.Alias); args = new Dictionary <string, object> { { "Resource to Update", trigger }, { "Field to Update", new EntityRef("core:triggeringCondition").Entity }, { "New Value", new EntityRef("core:triggeredOnEnumUpdate").Entity }, { "Replace Existing Values", false } // should ignore this value }; RunActivity(nextActivity, args); Assert.IsNotNull(trigger.TriggeringCondition, "Triggering condition should be set"); Assert.AreEqual("core:triggeredOnEnumUpdate", trigger.TriggeringCondition.Alias); }
public void EnsureBadExpressionGeneratesEventLog( ) { Workflow wf = CreateLoggingWorkflow("{{EnsureBadExpressionGeneratesEventLog - This is an expected error in the log}}"); wf.Save( ); ToDelete.Add(wf.Id); ActivityImplementationBase nextActivity = wf.Cast <WfActivity>( ).CreateWindowsActivity( ); var input = new Dictionary <string, object>( ); var run = RunWorkflow(wf); Assert.IsNotNull(run.ErrorLogEntry, "A log entry exists"); }
public void TestRun( ) { var wf = new Workflow { Name = "Wf" }; wf.AddDefaultExitPoint( ); var l1 = new LogActivity { Name = "l1" }.Cast <WfActivity>( ); var l2 = new LogActivity { Name = "12" }.Cast <WfActivity>( ); // wf.FirstActivity = l1; //wf.ContainedActivities.Add(l1); wf.ContainedActivities.Add(l2); ActivityTestHelper.AddFirstActivityWithMapping(wf, l1, null); ActivityTestHelper.AddExpressionToActivityArgument(wf, l1, "Message", "'Message 1'", false); ActivityTestHelper.AddTransition(wf, l1, l2); ActivityTestHelper.AddExpressionToActivityArgument(wf, l2, "Message", "'Message 2'", false); ActivityTestHelper.AddTermination(wf, l2, l2.GetDefaultExitPoint(), CreateDefaultExitPoint()); wf.Save( ); ActivityImplementationBase nextActivity = wf.Cast <WfActivity>( ).CreateWindowsActivity( ); l1.Save( ); l2.Save( ); ToDelete.Add(wf.Id); ToDelete.Add(l1.Id); ToDelete.Add(l2.Id); var run = (RunWorkflow(wf)); Assert.AreEqual(WorkflowRunState_Enumeration.WorkflowRunCompleted, run.WorkflowRunStatus_Enum, "Finished without errors"); // run using service }
public void UpdateField() { var employeeType = CodeNameResolver.GetTypeByName("AA_Person").As <EntityType>(); var ageField = employeeType.Fields.First(f => f.Name == "Age"); var bob = new Entity(employeeType); bob.Save(); ToDelete.Add(bob.Id); var updateActivity = new UpdateFieldActivity(); updateActivity.InputArguments.Add(new ResourceArgument { Name = "1" }.Cast <ActivityArgument>()); updateActivity.InputArguments.Add(new IntegerArgument { Name = "1_value" }.Cast <ActivityArgument>()); updateActivity.Save(); ToDelete.Add(updateActivity.Id); var updateActionAs = updateActivity.As <WfActivity>(); ActivityImplementationBase nextActivity = updateActionAs.CreateWindowsActivity(); var args = new Dictionary <string, object> { { "Record", bob }, { "1_value", 32 }, { "1", (new EntityRef(ageField.Id)).Entity } }; RunActivity(nextActivity, args); Entity.Get(bob); var age = (int?)bob.GetField(ageField); Assert.AreEqual(32, age); }
public static WorkflowRun CreateWfRunForActivity(ActivityImplementationBase activity) { //TODO: change to using CreateWorkflowRun in WorkflowRunner.Instance var run = new WorkflowRun(); var workflow = activity.ActivityInstance.As <Workflow>(); if (workflow == null) { workflow = new Workflow { }; workflow.Save(); } run.WorkflowBeingRun = workflow; run.TriggeringUser = Entity.Get <UserAccount>(RequestContext.GetContext().Identity.Id); return(run); }
public void TestInputVarNotSet() { using (new WorkflowRunContext { RunTriggersInCurrentThread = true }) { var wf = new Workflow(); wf.AddDefaultExitPoint(); var forEach1 = new ForEachResource { Name = "foreach1" + DateTime.Now }; var forEach1As = forEach1.As <WfActivity>(); wf.FirstActivity = forEach1As; wf.ContainedActivities.Add(forEach1As); var loopExitPoint = Entity.Get <ExitPoint>(ForeachImplementation.LoopExitPointAlias); ActivityTestHelper.AddTransition(wf, forEach1As, forEach1As, loopExitPoint); ActivityTestHelper.AddTermination(wf, forEach1As); //ActivityHelper.AddMissingExpressionParametersToWorkflow(wf); wf.Save(); ToDelete.Add(wf.Id); ActivityImplementationBase nextActivity = forEach1As.Cast <WfActivity>().CreateWindowsActivity(); var metaData = new WorkflowMetadata(); nextActivity.Validate(metaData); Assert.IsTrue(metaData.ValidationMessages.Count() == 1, "There is only one validation message"); Assert.IsTrue(metaData.ValidationMessages.First().StartsWith("Mandatory argument"), "Validation is 'Mandatory argument'"); } }
public void SetChoiceMultiple( ) { var sch = new ScheduleDailyRepeat { Name = "Test sch" + DateTime.Now }; sch.Save( ); // _toDelete.Add(sch.Id); var setAction = new SetChoiceActivity( ); setAction.Save( ); ToDelete.Add(setAction.Id); var setChoiceAs = setAction.As <WfActivity>( ); ActivityImplementationBase nextActivity = setChoiceAs.CreateWindowsActivity( ); var dayOfWeekRef = ( EntityRef )"core:sdrDayOfWeek"; var args = new Dictionary <string, object> { { "Resource to Update", sch }, { "Field to Update", dayOfWeekRef.Entity }, { "New Value", new EntityRef("core:dowSunday").Entity }, }; RunActivity(nextActivity, args); sch = Entity.Get <ScheduleDailyRepeat>(sch.Id); IEntityRelationshipCollection <IEntity> dowRefs = sch.GetRelationships(dayOfWeekRef); Assert.AreEqual(1, dowRefs.Count( ), "has been set"); Assert.IsTrue(dowRefs.Any(w => w.Entity.Alias == "dowSunday")); args = new Dictionary <string, object> { { "Resource to Update", sch }, { "Field to Update", dayOfWeekRef.Entity }, { "New Value", new EntityRef("core:dowMonday").Entity }, { "Replace Existing Values", false } }; RunActivity(nextActivity, args); sch = Entity.Get <ScheduleDailyRepeat>(sch.Id); dowRefs = sch.GetRelationships(dayOfWeekRef); Assert.AreEqual(2, dowRefs.Count( ), "has been added"); Assert.IsTrue(dowRefs.Any(w => w.Entity.Alias == "dowMonday")); args = new Dictionary <string, object> { { "Resource to Update", sch }, { "Field to Update", dayOfWeekRef.Entity }, { "New Value", new EntityRef("core:dowTuesday").Entity }, { "Replace Existing Values", true } }; RunActivity(nextActivity, args); sch = Entity.Get <ScheduleDailyRepeat>(sch.Id); dowRefs = sch.GetRelationships(dayOfWeekRef); Assert.AreEqual(1, dowRefs.Count( ), "has been reset"); Assert.IsTrue(dowRefs.Any(w => w.Entity.Alias == "dowTuesday")); }
public void SetRelationship( ) { var employeeType = Entity.Get <EntityType>("test:employee"); var managerType = Entity.Get <EntityType>("test:manager"); var nameField = Entity.Get <StringField>("core:name"); IEntity emp = new Entity(employeeType); emp.SetField(nameField, "Test Employee"); IEntity emp2 = new Entity(employeeType); emp.SetField(nameField, "Test Employee 2"); IEntity mgr = (new Entity(employeeType)); emp.SetField(nameField, "Test Manager"); mgr.As <Resource>( ).IsOfType.Add(managerType); emp.Save( ); emp2.Save( ); mgr.Save( ); ToDelete.Add(emp.Id); ToDelete.Add(emp2.Id); ToDelete.Add(mgr.Id); var setRel = new SetRelationshipActivity( ); setRel.Save( ); ToDelete.Add(setRel.Id); var setRelAs = setRel.As <WfActivity>( ); ActivityImplementationBase nextActivity = setRelAs.CreateWindowsActivity( ); var args = new Dictionary <string, object> { { "Origin", emp }, { "Relationship", new EntityRef("test:reportsTo").Entity }, { "Destination", mgr }, }; RunActivity(nextActivity, args); emp = Entity.Get(emp.Id); IEntityRelationshipCollection <IEntity> rels = emp.GetRelationships("test:reportsTo"); Assert.AreEqual(1, rels.Count( ), "Ensure the manager has been set"); Assert.AreEqual(rels.First( ).Entity.Id, mgr.Id, "Ensure the manager has been set to the correct value"); // clear relationships args = new Dictionary <string, object> { { "Origin", emp }, { "Relationship", new EntityRef("test:reportsTo").Entity }, { "Destination", null }, { "Replace Existing Destination", true } }; RunActivity(nextActivity, args); emp = Entity.Get(emp.Id); rels = emp.GetRelationships("test:reportsTo"); Assert.AreEqual(0, rels.Count( ), "Ensure the manager has been cleared"); // set the reverse relationship args = new Dictionary <string, object> { { "Origin", mgr }, { "Relationship", new EntityRef("test:directReports").Entity }, { "Destination", emp }, { "(Internal) Is this a reverse relationship", true } }; RunActivity(nextActivity, args); mgr = Entity.Get(mgr.Id); rels = mgr.GetRelationships("test:directReports"); Assert.AreEqual(1, rels.Count( ), "Ensure the employee has been set"); Assert.AreEqual(rels.First( ).Entity.Id, emp.Id, "Ensure the employee has been set to the correct value"); // add a second relationship, clearing the first args = new Dictionary <string, object> { { "Origin", mgr }, { "Relationship", new EntityRef("test:directReports").Entity }, { "Destination", emp2 }, { "(Internal) Is this a reverse relationship", true }, { "Replace Existing Destination", true } }; RunActivity(nextActivity, args); mgr = Entity.Get(mgr.Id); rels = mgr.GetRelationships("test:directReports"); Assert.AreEqual(1, rels.Count( ), "Ensure the new employee has been set and the old cleared"); Assert.AreEqual(rels.First( ).Entity.Id, emp2.Id, "Ensure the manager has been set to the correct value"); // add the first back in args = new Dictionary <string, object> { { "Origin", mgr }, { "Relationship", new EntityRef("test:directReports").Entity }, { "Destination", emp }, { "(Internal) Is this a reverse relationship", true }, { "Replace Existing Destination", false } }; RunActivity(nextActivity, args); mgr = Entity.Get(mgr.Id); rels = mgr.GetRelationships("test:directReports"); Assert.AreEqual(2, rels.Count( ), "Add a second relationship"); }
public void PassingValuesBetweenActivities( ) { // This test creates a person with an age of ten and a workflow that takes the persons resource id as inputs, reads that persons age and writes it out to the log embedded in a message. // Testing: // Mapping workflow input arguments. // Mapping an output argument to a variable. // Using an expression that contains an input parameter. var personType = CodeNameResolver.GetTypeByName("AA_Person").As <EntityType>(); var ageField = personType.Fields.First(f => f.Name == "Age"); var peter = Entity.Create(personType).As <Resource>(); peter.Name = "Peter" + DateTime.Now; peter.SetField(ageField, 10); peter.Save( ); ToDelete.Add(peter.Id); var workflow = new Workflow { Name = "Wf" + DateTime.Now }; workflow.AddDefaultExitPoint( ); var resourceIdArg = new ResourceArgument { Name = "ResourceId", ConformsToType = personType }; var resourceIdArgAs = resourceIdArg.As <ActivityArgument>( ); workflow.InputArguments.Add(resourceIdArg.As <ActivityArgument>( )); //workflow.Save( ); var workflowAs = workflow.As <WfActivity>( ); // log activity var log = new LogActivity { Name = "log" + DateTime.Now }; var logAs = log.As <WfActivity>( ); workflow.ContainedActivities.Add(logAs); workflow.FirstActivity = logAs; ActivityTestHelper.AddExpressionToActivityArgument(workflow, logAs, "Message", "'Peters age is ' + ResourceId.Age", false); ActivityTestHelper.AddTermination(workflow, logAs); workflow.Save( ); ToDelete.Add(workflow.Id); ActivityImplementationBase nextActivity = workflow.Cast <WfActivity>( ).CreateWindowsActivity( ); var input = new Dictionary <string, object> { { "ResourceId", peter } }; var run = (RunWorkflow(workflow, input)); Assert.AreEqual(WorkflowRunState_Enumeration.WorkflowRunCompleted, run.WorkflowRunStatus_Enum, "The workflow run and completed without error"); //RunActivity( nextActivity, input ); }