Exemplo n.º 1
0
        /// <summary>
        /// Validates the property.
        /// </summary>
        /// <param name="validation">The validation.</param>
        /// <param name="itemResult">The item result.</param>
        /// <returns><c>true</c> if the validation is successful, <c>false</c> otherwise.</returns>
        private bool ValidateProperty(ItemValidation validation, ValidationItemResult itemResult)
        {
            IPropertyData propertyData;

            if (!this.ElementLocator.TryGetProperty(validation.FieldName, out propertyData))
            {
                if (validation.Comparer is DoesNotExistComparer)
                {
                    itemResult.NoteValidationResult(validation, successful: true, actualValue: null);
                    return(true);
                }

                itemResult.NoteMissingProperty(validation);
                return(false);
            }


            string actualValue = null;
            bool?  successful  = null;

            this.DoValidate <IPropertyData>(propertyData, e =>
            {
                successful = e.ValidateItem(validation, out actualValue);
                return(successful.Value);
            });

            itemResult.NoteValidationResult(validation, successful.Value, actualValue);
            return(successful.Value);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks the item.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="validations">The validations.</param>
        /// <param name="validationResult">The validation tracker.</param>
        /// <returns><c>true</c> if the item is valid; otherwise <c>false</c>.</returns>
        private bool CheckItem(TElement element, IEnumerable <ItemValidation> validations, ValidationResult validationResult)
        {
            var page = this.ElementHandler.GetPageFromElement(element);

            var validationItemResult = new ValidationItemResult();

            validationResult.CheckedItems.Add(validationItemResult);

            var result = true;

            foreach (var itemValidation in validations)
            {
                IPropertyData property;
                if (!page.TryGetProperty(itemValidation.FieldName, out property))
                {
                    validationItemResult.NoteMissingProperty(itemValidation);
                    result = false;
                    continue;
                }

                string actualValue;
                var    successful = true;
                if (!property.ValidateItem(itemValidation, out actualValue))
                {
                    successful = false;
                    result     = false;
                }

                validationItemResult.NoteValidationResult(itemValidation, successful, actualValue);
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Validates the token.
        /// </summary>
        /// <param name="validation">The validation.</param>
        /// <param name="itemResult">The validation item result.</param>
        /// <returns><c>true</c> if the validation is successful, <c>false</c> otherwise.</returns>
        private bool ValidateToken(ItemValidation validation, ValidationItemResult itemResult)
        {
            var tokenValue = this.tokenManager.GetTokenByKey(validation.FieldName);
            var successful = validation.Compare(null, tokenValue);

            itemResult.NoteValidationResult(validation, successful, tokenValue);

            return(successful);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Validates the property.
        /// </summary>
        /// <param name="validation">The validation.</param>
        /// <param name="itemResult">The item result.</param>
        /// <returns><c>true</c> if the validation is successful, <c>false</c> otherwise.</returns>
        private bool ValidateProperty(ItemValidation validation, ValidationItemResult itemResult)
        {
            IPropertyData propertyData;

            if (!this.ElementLocator.TryGetProperty(validation.FieldName, out propertyData))
            {
                itemResult.NoteMissingProperty(validation);
                return(false);
            }

            string actualValue;
            var    successful = propertyData.ValidateItem(validation, out actualValue);

            itemResult.NoteValidationResult(validation, successful, actualValue);
            return(successful);
        }
Exemplo n.º 5
0
        public void TestGetComparisonTableByRuleWithInvalidFieldValue()
        {
            var validation       = ItemValidationHelper.Create("MyField", "Something");
            var validationResult = new ValidationResult(new[] { validation });

            var itemResult = new ValidationItemResult();

            itemResult.NoteValidationResult(validation, false, "Nothing");

            validationResult.CheckedItems.Add(itemResult);

            var result = validationResult.GetComparisonTableByRule();

            var expectedTable = new StringBuilder()
                                .AppendLine("| Field   | Rule   | Value               |")
                                .Append("| MyField | equals | Something [Nothing] |");

            Assert.AreEqual(expectedTable.ToString(), result);
        }
Exemplo n.º 6
0
        public void TestGetComparisonTableWithInvalidNullFields()
        {
            var validation       = ItemValidationHelper.Create("MyField", "Something");
            var validationResult = new ValidationResult(new[] { validation });

            var itemResult = new ValidationItemResult();

            itemResult.NoteValidationResult(validation, false, null);

            validationResult.CheckedItems.Add(itemResult);

            var result = validationResult.GetComparisonTable();

            var expectedTable = new StringBuilder()
                                .AppendLine("| MyField equals Something |")
                                .Append("| <EMPTY>                  |");

            Assert.AreEqual(expectedTable.ToString(), result);
        }
Exemplo n.º 7
0
        public void TestExecuteWhenPropertyValidationReturnsErrorsReturnsFailureResult()
        {
            var table = new ValidationTable();

            table.AddValidation("name", "Hello", "equals");
            table.Process();

            var itemResult = new ValidationItemResult();

            itemResult.NoteValidationResult(table.Validations.First(), false, "World");

            var validationResult = new ValidationResult(table.Validations)
            {
                IsValid = false, ItemCount = 1
            };

            validationResult.CheckedItems.Add(itemResult);

            var propData = new Mock <IPropertyData>(MockBehavior.Strict);

            propData.SetupGet(p => p.IsList).Returns(true);
            propData.SetupGet(p => p.Name).Returns("MyProperty");
            propData.Setup(p => p.ValidateList(ComparisonType.Equals, It.Is <ICollection <ItemValidation> >(c => c.Count == 1))).Returns(validationResult);

            var locator = new Mock <IElementLocator>(MockBehavior.Strict);

            locator.Setup(p => p.GetProperty("myproperty")).Returns(propData.Object);

            var buttonClickAction = new ValidateListAction
            {
                ElementLocator = locator.Object
            };

            var context = new ValidateListAction.ValidateListContext("myproperty", ComparisonType.Equals, table);
            var result  = buttonClickAction.Execute(context);

            Assert.AreEqual(false, result.Success);
            Assert.IsNotNull(result.Exception);

            locator.VerifyAll();
            propData.VerifyAll();
        }