Prerequisites: none
Namespaces: InRule.Repository, InRule.Repository.Regression
Classes: RuleApplicationDef, EntityDef, FieldDef, RuleSetDef, SimpleRuleDef, FireNotificationActionDef, SetValueActionDef, AddCollectionMemberActionDef, TestSuiteDef, DataStateDef, TestContextDef, DataStateMappingDef, ZipFileTestSuitePersistenceProvider
References: InRule.Repository.dll,
The following illustrates the creation of a Rule Application and Test Suite with irSDK. Both are saved as files to the C:\Temp directory. This directory should be created with write privileges before testing. The test suite generated by this sample can be used with the sample code for Executing a Simple Test Suite.
The Rule Application below performs 4 different actions if Field1 is greater than zero. Although Field1 is a String field, the rule uses implicit casting to compare it as an integer. The RuleSet is set to use the Auto fire mode, so ApplyRules is used to execute this rule. The Test Suite creation in the second method illustrates how the Test and Data State are set up, and also how the Assertions are authored to validate the 4 rules that fire, along with checking that the IfThen rule fired. The important points to note are:
- The RuleApplicationDef is assigned to the TestSuiteDef right after creating it. This should always be one of the first actions performed.
- The DataStateDef is assigned to both the TestSuiteDef.RootDataFolder and the TestDef’s DataStates collection.
- The TestDef is created by passing a TestContextDef instance to the constructor that was created from the EntityDef of the root entity.
- In this case, the TestDef is added to the TestSuiteDef.RootTestFolder, but it could have been added to a FolderDef hierarchy that we will see in the next example.
- The Assertions use object-relational path notation to address the Rule Application schema, as opposed to XPath notation.
- Not all Assertions require an ExpectedValue and ExpectedValueType.
The files generated from these two methods can be loaded into irAuthor and irVerify:
1.Load C:\Temp\SimpleRuleApp.ruleapp into irAuthor.
2.Launch irVerify from the Entity1 context.
3.Use the File menu to load the C:\Temp\SimpleTestSuite.testsuite file.
4.By clicking on the Test1 tree node, the 5 Assertions will be visible in the Business Language editor.
5.The Test control should show that DataState1 is being used, and that ApplyRules will be performed.
6.By clicking the ‘Run All’ button, the tests will execute, and all Assertions should pass.
7.By clicking the ‘Edit’ button next to the Data State drop-down menu on the Test control, the initial state that was stored in the Data State may be viewed, edited and tested in a familiar irVerify style window.
public static RuleApplicationDef CreateTestingRuleApp()
{
RuleApplicationDef ruleApp = new RuleApplicationDef("SetValueTest");
EntityDef entity = new EntityDef("Entity1");
ruleApp.Entities.Add(entity);
FieldDef field1 = new FieldDef("Field1", DataType.String);
FieldDef field2 = new FieldDef("Field2", DataType.Integer);
FieldDef collection1 = new FieldDef("Collection1", DataType.collection);
FieldDef field3 = new FieldDef("Field3", DataType.DateTime);
collection1.FieldDefType = FieldDefType.Collection;
collection1.Fields.Add(field3);
entity.Fields.Add(field1);
entity.Fields.Add(field2);
entity.Fields.Add(collection1);
RuleSetDef ruleSet = new RuleSetDef("RuleSet1");
ruleSet.FireMode = RuleSetFireMode.Auto;
ruleSet.RunMode = RuleSetRunMode.Sequential;
entity.RuleElements.Add(ruleSet);
SimpleRuleDef ifthen = new SimpleRuleDef("IfThen1", "Field1 > 0");
ifthen.SubRules.Add(new SetValueActionDef("Field2", "Field1 + 5"));
ifthen.SubRules.Add(new AddCollectionMemberActionDef("Collection1"));
ifthen.SubRules.Add(new SetValueActionDef("Collection1(1).Field3", "Now()"));
FireNotificationActionDef notification = new FireNotificationActionDef("Notification1");
notification.NotificationType = NotificationType.Informational;
notification.NotificationMessageText = "Test Notification";
ifthen.SubRules.Add(notification);
ruleSet.Rules.Add(ifthen);
ruleApp.SaveToFile(@"C:\Temp\SimpleRuleApp.ruleapp");
return (ruleApp);
}
public static TestSuiteDef WriteSimpleTestSuite()
{
// Create a new Test Suite
TestSuiteDef suite = TestSuiteDef.Create();
suite.Settings.Name = "SimpleTestSuite";
suite.ActiveRuleApplicationDef = CreateTestingRuleApp();
// Create an EntityState Data State with the initial state
DataStateDef dataState = new DataStateDef();
dataState.DisplayName = "DataState1";
dataState.RootEntityName = "Entity1";
dataState.DataStateType = DataStateType.EntityState;
dataState.StateXml = "<Entity1><Field1>2</Field1></Entity1>";
suite.RootDataFolder.Members.Add(dataState);
// Create a Test and set the Data State as the root mapping
EntityDef entity1 = suite.ActiveRuleApplicationDef.Entities["Entity1"];
TestDef test = new TestDef(TestContextDef.Create(entity1));
test.DisplayName = "Test1";
test.DataStates.Add(new DataStateMappingDef(dataState));
suite.RootTestsFolder.Members.Add(test);
// Create an Assertion to check that Field2 equals 7
AssertionDef assertion = new AssertionDef();
assertion.AssertionType = AssertionType.FieldIsEqualToX;
assertion.TargetElementPath = "Entity1.Field2";
assertion.ExpectedValue = "7";
assertion.ExpectedValueType = ExpectedValueDataType.Integer;
test.Assertions.Add(assertion);
// Create an Assertion to check that there is 1 collection memeber
assertion = new AssertionDef();
assertion.AssertionType = AssertionType.CollectionCountIsX;
assertion.TargetElementPath = "Entity1.Collection1";
assertion.ExpectedValue = "1";
assertion.ExpectedValueType = ExpectedValueDataType.Integer;
test.Assertions.Add(assertion);
// Create an Assertion to check the DateTime in Field3
assertion = new AssertionDef();
assertion.AssertionType = AssertionType.FieldIsAfterX;
assertion.TargetElementPath = "Entity1.Collection1(1).Field3";
assertion.ExpectedValue = "#01/01/2000 00:00#";
assertion.ExpectedValueType = ExpectedValueDataType.DateTime;
test.Assertions.Add(assertion);
// Create an Assertion to check a Notification fired
assertion = new AssertionDef();
assertion.AssertionType = AssertionType.NotificationFired;
assertion.TargetElementPath = "Entity1.RuleSet1.IfThen1.Notification1";
test.Assertions.Add(assertion);
// Create an Assertion to check that the IfThen rule fired
assertion = new AssertionDef();
assertion.AssertionType = AssertionType.RuleFired;
assertion.TargetElementPath = "Entity1.RuleSet1.IfThen1";
test.Assertions.Add(assertion);
// Persist Test Suite to file system
suite.SaveAs(new ZipFileTestSuitePersistenceProvider(@"C:\Temp\SimpleTestSuite.testsuite")
return (suite);
}
Comments
0 comments
Please sign in to leave a comment.