public void TestControlName()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.ValidateRule1.json");
            ValidateTruthCondition condition = JsonConvert.DeserializeObject<ValidateTruthCondition>(json);

            ControlList controlList = new ControlList();
            RepeaterControl repeater = new RepeaterControl
                                       { Id = 3, Name = "Repeater" };
            controlList.Add(repeater);
            repeater.Controls.Add(new TextControl { Id = 1, Name = "Field1", ParentId = 3 });
            repeater.Controls.Add(new TextControl { Id = 2, Name = "Field2", ParentId = 3 });

            ApplicationData appData = new ApplicationData();
            Dictionary<string, object>[] repeaterData = new Dictionary<string, object>[2];
            appData.Add("Repeater", repeaterData);
            repeaterData[0] = new Dictionary<string, object>
                              {
                                  { "Field1", "Is Valid" },
                                  { "Field2", "When Combined With This" }
                              };
            repeaterData[1] = new Dictionary<string, object>
                              {
                                  { "Field1", "Not Valid" },
                                  { "Field2", "When Combined With This" }
                              };

            TruthConditionEvaluatorFactory evaluatorFactory = new TruthConditionEvaluatorFactory();

            ITruthConditionValidator validator = new ITruthConditionValidator(condition, controlList, evaluatorFactory);
            ValidationResults results = validator.Validate(appData);
            Assert.AreEqual("Repeater[1].Field1", results.ToArray()[0].Key);
        }
        public void TestEmptyRepeaterBusinessRule()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.ValidateRule1.json");
            ValidateTruthCondition condition = JsonConvert.DeserializeObject<ValidateTruthCondition>(json);

            ControlList controlList = new ControlList();
            RepeaterControl repeater = new RepeaterControl
                                       { Id = 3, Name = "Repeater" };
            controlList.Add(repeater);
            repeater.Controls.Add(new TextControl { Id = 1, Name = "Field1", ParentId = 3 });
            repeater.Controls.Add(new TextControl { Id = 2, Name = "Field2", ParentId = 3 });

            ApplicationData appData = new ApplicationData();

            TruthConditionEvaluatorFactory evaluatorFactory = new TruthConditionEvaluatorFactory();

            ITruthConditionValidator validator = new ITruthConditionValidator(condition, controlList, evaluatorFactory);
            ValidationResults results = validator.Validate(appData);
            Assert.AreEqual(true, results.IsValid);
        }
        public void TestFailBusinessRule()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.ValidateRule1.json");
            ValidateTruthCondition condition = JsonConvert.DeserializeObject<ValidateTruthCondition>(json);

            ControlList controlList = new ControlList
                                      {
                                          new TextControl
                                          {
                                              Id = 1,
                                              Name = "Field1"
                                          },
                                          new TextControl
                                          {
                                              Id = 2,
                                              Name = "Field2"
                                          }
                                      };

            ApplicationData appData = new ApplicationData
                                      {
                                          { "Field1", "Not Valid" },
                                          { "Field2", "When Combined With This" }
                                      };

            TruthConditionEvaluatorFactory evaluatorFactory = new TruthConditionEvaluatorFactory();

            ITruthConditionValidator validator = new ITruthConditionValidator(condition, controlList, evaluatorFactory);
            ValidationResults results = validator.Validate(appData);
            Assert.AreEqual(false, results.IsValid);
        }
        public void TestNestedRepeaterPassValueBusinessRule()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.ValidateRule1.json");
            ValidateTruthCondition condition = JsonConvert.DeserializeObject<ValidateTruthCondition>(json);

            ControlList controlList = new ControlList();
            RepeaterControl repeater = new RepeaterControl { Id = 3, Name = "Repeater" };
            controlList.Add(repeater);

            RepeaterControl nestedRepeater = new RepeaterControl { Id = 4, Name = "NestedRepeater", ParentId = 3 };
            repeater.Controls.Add(nestedRepeater);

            nestedRepeater.Controls.Add(new TextControl { Id = 1, Name = "Field1", ParentId = 4 });
            nestedRepeater.Controls.Add(new TextControl { Id = 2, Name = "Field2", ParentId = 4 });

            ApplicationData appData = new ApplicationData();

            var validExample = new Dictionary<string, object>
                              {
                                  { "Field1", "Is Valid" },
                                  { "Field2", "When Combined With This" }
                              };

            var anotherValidExample = new Dictionary<string, object>
                              {
                                  { "Field1", "And So Is This" },
                                  { "Field2", "When Combined With This" }
                              };

            Dictionary<string, object>[] repeaterData = new Dictionary<string, object>[2];
            appData.Add("Repeater", repeaterData);
            repeaterData[0] = new Dictionary<string, object>()
            {
                { "NestedRepeater", new[] { validExample, anotherValidExample } }
            };
            repeaterData[1] = new Dictionary<string, object>()
            {
                { "NestedRepeater", new[] { anotherValidExample, validExample } }
            };

            TruthConditionEvaluatorFactory evaluatorFactory = new TruthConditionEvaluatorFactory();

            ITruthConditionValidator validator = new ITruthConditionValidator(condition, controlList, evaluatorFactory);
            ValidationResults results = validator.Validate(appData);
            Assert.AreEqual(true, results.IsValid);
        }
        public void TestNestedRepeaterFailRootCompareBusinessRule()
        {
            string json = AssemblyResourceReader.ReadAsString("Test_Data.ValidateRule2.json");
            ValidateTruthCondition condition = JsonConvert.DeserializeObject<ValidateTruthCondition>(json);

            ControlList controlList = new ControlList();
            RepeaterControl repeater = new RepeaterControl { Id = 3, Name = "Repeater" };
            controlList.Add(repeater);

            RepeaterControl nestedRepeater = new RepeaterControl { Id = 4, Name = "NestedRepeater", ParentId = 3 };
            repeater.Controls.Add(nestedRepeater);

            RepeaterControl nestedNestedRepeater = new RepeaterControl { Id = 5, Name = "NestedNestedRepeater", ParentId = 4 };
            nestedRepeater.Controls.Add(nestedNestedRepeater);

            nestedNestedRepeater.Controls.Add(new TextControl { Id = 1, Name = "Field1", ParentId = 5 });

            // Compare to root level
            controlList.Add(new TextControl { Id = 2, Name = "Field2" });

            var validExample = new Dictionary<string, object>
                        {
                            { "Field1", "Is Valid" }
                        };

            var invalidExample = new Dictionary<string, object>
                        {
                            { "Field1", "Is Not Valid" }
                        };

            ApplicationData appData = new ApplicationData();
            Dictionary<string, object>[] repeaterData = new Dictionary<string, object>[2];
            appData.Add("Field2", "Is Not Valid");
            appData.Add("Repeater", repeaterData);
            repeaterData[0] = new Dictionary<string, object>()
            {
                {
                    "NestedRepeater", new[]
                    {
                        new Dictionary<string, object>()
                        {
                            { "NestedNestedRepeater", new[] { invalidExample, validExample } }
                        },
                        new Dictionary<string, object>()
                        {
                            { "NestedNestedRepeater", new[] { invalidExample, invalidExample } }
                        },
                    }
                }
            };
            repeaterData[1] = new Dictionary<string, object>()
            {
                {
                    "NestedRepeater", new[]
                    {
                        new Dictionary<string, object>()
                        {
                            { "NestedNestedRepeater", new[] { invalidExample } }
                        },
                        new Dictionary<string, object>()
                        {
                            { "NestedNestedRepeater", new[] { validExample } }
                        },
                    }
                }
            };

            TruthConditionEvaluatorFactory evaluatorFactory = new TruthConditionEvaluatorFactory();

            ITruthConditionValidator validator = new ITruthConditionValidator(condition, controlList, evaluatorFactory);
            ValidationResults results = validator.Validate(appData);
            Assert.AreEqual(false, results.IsValid);
            Assert.AreEqual(4, results.Count);
            Assert.AreEqual("Repeater[0].NestedRepeater[0].NestedNestedRepeater[0].Field1", results.ToArray()[0].Key);
            Assert.AreEqual("Repeater[0].NestedRepeater[1].NestedNestedRepeater[0].Field1", results.ToArray()[1].Key);
            Assert.AreEqual("Repeater[0].NestedRepeater[1].NestedNestedRepeater[1].Field1", results.ToArray()[2].Key);
            Assert.AreEqual("Repeater[1].NestedRepeater[0].NestedNestedRepeater[0].Field1", results.ToArray()[3].Key);
        }