private static void ProcessChildrenConstraints(object item, IList<ValidationConstraint> constraints, ValidationConstraint parentConstraint, out string message)
 {
     StringBuilder sb = new StringBuilder();
     message = null;
     var parentConstraintIsRespected = ProcessConstraint(item, parentConstraint);
     if (!parentConstraintIsRespected) return;
     foreach (var source in constraints)
     {
         var constraintIsRespected = ProcessConstraint(item, source);
         var childConstraints = SessionManagement.Db.GetAllValidationConstraints().Where(c => c.ParentConstraint == source).ToList();
         if (constraintIsRespected && childConstraints.Any())
         {
             //Go down the tree
             string subMessage;
             ProcessChildrenConstraints(item, childConstraints, source, out subMessage);
             if (!string.IsNullOrEmpty(subMessage)) sb.AppendLine(subMessage);
         }
         else if (!constraintIsRespected && !childConstraints.Any())
         {
             //if all parents are true and the final child is false, the constraint is not respected.
             sb.AppendLine(string.Format("{0} is not respected", source));
         }
     }
     message = sb.ToString();
 }
Ejemplo n.º 2
0
 public ValidationConstraint SaveOrUpdateInstance(ValidationConstraint validationConstraint)
 {
     var oldItem = _cachedValidationConstraints.FirstOrDefault(vc => vc.Equals(validationConstraint));
     if (oldItem == null)
         _cachedValidationConstraints.Add(validationConstraint);
     else
     {
         _cachedValidationConstraints.Remove(oldItem);
         _cachedValidationConstraints.Add(validationConstraint);
     }
     return validationConstraint;
 }
        public void ValidationConstraints_CheckThatArgumentsProperlyParsed()
        {
            var newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Equal,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1"
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Equal,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "NotAnInt"
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Between,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1",
                SecondaryArgument = "2"
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Between,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1",
                SecondaryArgument = "NotAnInt"
            };
            TrySaveAndFail(newConstraint);
        }
        public void ValidationConstraints_Validate()
        {
            const string errorMessage = "The object should not have been saved given its constraints";
            //var transac = SessionManagement.Db.StartTransaction();
            IList<ValidationConstraint> testConstraints = new List<ValidationConstraint>();
            try
            {
                var accor = SessionManagement.Db.GetAllAssets().SingleOrDefault(a => a.Name == "ACCOR");
                var total = SessionManagement.Db.GetAllAssets().SingleOrDefault(a => a.Name == "TOTAL");
                Assert.IsNotNull(total);
                var nameConstraint = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.Equal,
                    ObjectType = _typeOfAssetEditable,
                    Property = "Name",
                    PropertyType = _typeOfString,
                    MainArgument = "ACCOR"
                };
                testConstraints.Add(nameConstraint);
                var savedNameConstraint = ValidationConstraintService.Instance.SaveOrUpdateList(testConstraints).SingleOrDefault();
                //Should succeed
                ValidationConstraintService.Instance.ValidateObject(accor);
                //Should fail
                try
                {
                    ValidationConstraintService.Instance.ValidateObject(total);
                    Assert.Fail(errorMessage);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                //Second Level
                var idConstraint1 = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.Greater,
                    ObjectType = _typeOfAssetEditable,
                    Property = "Id",
                    PropertyType = _typeOfString,
                    MainArgument = "0",
                    ParentConstraint = savedNameConstraint
                };
                testConstraints.Add(idConstraint1);
                ValidationConstraintService.Instance.SaveOrUpdateList(testConstraints);
                //Should succeed
                ValidationConstraintService.Instance.ValidateObject(accor);

                //Should Fail (Id = 122)
                var idConstraint2 = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.Lower,
                    ObjectType = _typeOfAssetEditable,
                    Property = "Id",
                    PropertyType = _typeOfString,
                    MainArgument = "30",
                    ParentConstraint = savedNameConstraint
                };
                testConstraints.Add(idConstraint2);
                testConstraints = ValidationConstraintService.Instance.SaveOrUpdateList(testConstraints);
                try
                {
                    ValidationConstraintService.Instance.ValidateObject(accor);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                //Remove it now that it has been tested
                testConstraints.ToList().ForEach(c =>
                {
                    if (c == idConstraint2)
                        c.RequiresDeletion = true;
                });

                var handlingQuotesConstraint = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.True,
                    ObjectType = _typeOfAssetEditable,
                    Property = "HandlingQuotes",
                    PropertyType = _typeOfBool,
                    ParentConstraint = savedNameConstraint
                };
                testConstraints.Add(handlingQuotesConstraint);
                var thirdLevel = ValidationConstraintService.Instance.SaveOrUpdateList(testConstraints).SingleOrDefault(c => c.ToString() == handlingQuotesConstraint.ToString());
                //Should succeed
                ValidationConstraintService.Instance.ValidateObject(accor);

                var betweenConstraint = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.Between,
                    ObjectType = _typeOfAssetEditable,
                    Property = "Id",
                    PropertyType = _typeOfInt,
                    MainArgument = "0",
                    SecondaryArgument = "100000000",
                    ParentConstraint = thirdLevel
                };
                testConstraints.Add(betweenConstraint);
                ValidationConstraintService.Instance.SaveOrUpdateList(testConstraints);
                //Should succeed
                ValidationConstraintService.Instance.ValidateObject(accor);

                var notNullConstraint = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.NotNull,
                    ObjectType = _typeOfAssetEditable,
                    Property = "Status",
                    PropertyType = _typeOfAssetStatus,
                    ParentConstraint = thirdLevel
                };
                testConstraints.Add(notNullConstraint);
                ValidationConstraintService.Instance.SaveOrUpdateList(testConstraints);

                //Should succeed
                ValidationConstraintService.Instance.ValidateObject(accor);

                var differentConstraint = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.Different,
                    ObjectType = _typeOfAssetEditable,
                    Property = "Id",
                    PropertyType = _typeOfInt,
                    MainArgument = "1",
                    ParentConstraint = thirdLevel
                };

                testConstraints.Add(differentConstraint);
                ValidationConstraintService.Instance.SaveOrUpdateList(testConstraints);
                //Should succeed
                ValidationConstraintService.Instance.ValidateObject(accor);

                var nullConstraint = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.Null,
                    ObjectType = _typeOfAssetEditable,
                    Property = "AssetType",
                    PropertyType = _typeOfAssetType,
                    ParentConstraint = thirdLevel
                };
                testConstraints.Add(nullConstraint);
                ValidationConstraintService.Instance.SaveOrUpdateList(testConstraints);
                try
                {
                    //Should fail.
                    ValidationConstraintService.Instance.ValidateObject(accor);
                    Assert.Fail(errorMessage);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                //If you want to add constraint remove the top one from the list.
            }
            finally
            {
                //SessionManagement.Db.RollbackTrans(transac);
            }
        }
 public void ValidationConstraints_Update()
 {
     var constraintsToSave = new List<ValidationConstraint>();
     var newConstraint = new ValidationConstraint
     {
         ConstraintType = trkValidationConstraintType.False,
         ObjectType = _typeOfAssetEditable,
         Property = "HandlingQuotes",
         PropertyType = _typeOfBool,
     };
     constraintsToSave.Add(newConstraint);
     var savedItem = ValidationConstraintService.Instance.SaveOrUpdateList(constraintsToSave).Single();
     constraintsToSave.Clear();
     savedItem.ConstraintType = trkValidationConstraintType.True;
     constraintsToSave.Add(savedItem);
     savedItem = ValidationConstraintService.Instance.SaveOrUpdateList(constraintsToSave).Single();
     constraintsToSave.Clear();
     Assert.AreEqual(savedItem.ConstraintType, trkValidationConstraintType.True);
     savedItem.RequiresDeletion = true;
     constraintsToSave.Add(newConstraint);
     ValidationConstraintService.Instance.SaveOrUpdateList(constraintsToSave);
     var supposedToHaveBeenErased = ValidationConstraintService.Instance.GetValidationConstraintById(savedItem.ToString());
     if (supposedToHaveBeenErased != null)
     {
         Assert.Fail(supposedToHaveBeenErased.ToString());
     }
 }
        public void ValidationConstraints_SecondGreaterThanMainArgument()
        {
            var newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Between,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1",
                SecondaryArgument = "2"
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Between,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1",
                SecondaryArgument = "0"
            };
            TrySaveAndFail(newConstraint);
        }
        public void ValidationConstraints_SaveSomeConstraints()
        {
            var portfolioType = new PortfolioEditable().GetType().AssemblyQualifiedName;
            var userInfoType = new UserInfo().GetType().AssemblyQualifiedName;

            var newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.NotNull,
                ObjectType = portfolioType,
                Property = "Comment",
                PropertyType = portfolioType
            };
            var newConstraint2 = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.NotNull,
                ObjectType = portfolioType,
                Property = "Manager",
                PropertyType = userInfoType
            };

            var constraintsToSave = new List<ValidationConstraint> { newConstraint, newConstraint2 };
            ValidationConstraintService.Instance.SaveOrUpdateList(constraintsToSave);
        }
        public void ValidationConstraints_Save()
        {
            var newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.NotNull,
                ObjectType = _typeOfAssetEditable,
                Property = "Code",
                PropertyType = _typeOfString
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Null,
                ObjectType = _typeOfAssetEditable,
                Property = "Code",
                PropertyType = _typeOfString
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.NotNull,
                ObjectType = _typeOfAssetEditable,
                Property = "Status",
                PropertyType = _typeOfAssetStatus
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Null,
                ObjectType = _typeOfAssetEditable,
                Property = "Status",
                PropertyType = _typeOfAssetStatus
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Equal,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1"
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Different,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1"
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Greater,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1"
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Lower,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1"
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.True,
                ObjectType = _typeOfAssetEditable,
                Property = "HandlingQuotes",
                PropertyType = _typeOfBool,
            };
            TrySaveAndSucceed(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.False,
                ObjectType = _typeOfAssetEditable,
                Property = "HandlingQuotes",
                PropertyType = _typeOfBool,
            };
            TrySaveAndSucceed(newConstraint);
        }
 /*override*/
 protected ValidationConstraint SaveOrUpdateObject(ValidationConstraint validationConstraint)
 {
     //return validationConstraint.IsNew ? SessionManagement.Db.SaveNew(validationConstraint) : SessionManagement.Db.Update(validationConstraint);
     return SessionManagement.Db.SaveOrUpdateInstance(validationConstraint);
 }
        public void ValidationConstraints_DeleteParentFails()
        {
            //var transaction = SessionManagement.Db.StartTransaction();
            try
            {
                //Check that a validation constraint cannot reference itself
                var parentConstraint = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.NotNull,
                    ObjectType = _typeOfAssetEditable,
                    Property = "Id",
                    PropertyType = _typeOfInt,
                };

                var childConstraint = new ValidationConstraint
                {
                    ConstraintType = trkValidationConstraintType.NotNull,
                    ObjectType = _typeOfAssetEditable,
                    Property = "Code",
                    PropertyType = _typeOfString,
                };

                var constraintsToSave = new List<ValidationConstraint> { parentConstraint, childConstraint };
                var savedConstraints = ValidationConstraintService.Instance.SaveOrUpdateList(constraintsToSave);
                Assert.IsNotNull(savedConstraints);
                savedConstraints[1].ParentConstraint = savedConstraints[0];
                savedConstraints = ValidationConstraintService.Instance.SaveOrUpdateList(constraintsToSave);
                savedConstraints[0].RequiresDeletion = true;
                TrySaveAndFail(savedConstraints);
            }
            finally
            {
                //SessionManagement.Db.RollbackTrans(transaction);
            }
        }
 /*override*/
 protected void DeleteObject(ValidationConstraint obj)
 {
     string message;
     if (CanBeDeleted(obj, out message))
         SessionManagement.Db.Delete(obj);
     else
         throw new Exception(message);
 }
        /*override*/
        protected bool CanBeSaved(ValidationConstraint constraint, out string message)
        {
            StringBuilder sb = new StringBuilder();
            if (constraint.ParentConstraint != null && Equals(constraint, constraint.ParentConstraint))
                sb.AppendLine("A constraint cannot reference itself as parent");
            var test = Type.GetType(constraint.PropertyType);
            if (test == null)
                sb.AppendLine("The Object Type associated to the constraint could not be found");
            if (test != null && test.IsSubclassOf(typeof(ObjectBase)))
                switch (constraint.ConstraintType)
                {
                    case trkValidationConstraintType.False:
                    case trkValidationConstraintType.True:
                    case trkValidationConstraintType.Greater:
                    case trkValidationConstraintType.Lower:
                    case trkValidationConstraintType.Between:
                    case trkValidationConstraintType.Equal:
                        sb.AppendLine(string.Format("The constraint type specified cannot be applied to the selected parameter {0}", constraint));
                        break;
                }
            if (string.IsNullOrEmpty(sb.ToString()) && constraint.MainArgument != null || constraint.SecondaryArgument != null)
            {
                switch (constraint.ConstraintType)
                {
                    case trkValidationConstraintType.Null:
                    case trkValidationConstraintType.NotNull:
                        sb.AppendLine(string.Format(string.Format("The arguments you specified are incompatible with the constraint type {0}"),constraint));
                        break;
                    case trkValidationConstraintType.Between:
                        if (!(constraint.MainArgument != null && constraint.SecondaryArgument != null))
                            sb.AppendLine("Both arguments must be specified");
                        break;
                }
                try
                {
                    Object mainArgument = null;
                    Object secondArgument = null;
                    if (constraint.MainArgument != null)
                    {
                        MethodInfo m = test.GetMethod("Parse", new[] { typeof(string) });
                        if (m != null)
                        {
                            mainArgument = m.Invoke(null, new[] { constraint.MainArgument });
                            if (mainArgument == null) throw new Exception();
                        }
                    }
                    if (constraint.SecondaryArgument != null && constraint.ConstraintType == trkValidationConstraintType.Between)
                    {
                        MethodInfo m = test.GetMethod("Parse", new[] { typeof(string) });
                        if (m != null)
                        {
                            secondArgument = m.Invoke(null, new[] { constraint.SecondaryArgument });
                            if (secondArgument == null) throw new Exception();
                        }
                        // MainArgument < SecondArgument when it is a between constraint.
                        if (ValidationConstraintHelper.IsGreater(secondArgument, mainArgument))
                            sb.AppendLine("The second argument must be greater than the first");
                    }
                    else if (constraint.SecondaryArgument != null)
                        sb.AppendLine("The second argument is only used in the Between constraint and will therefore be ignored");
                }
                catch (Exception)
                {
                    throw new Exception();
                    //throw new FunctionalException(string.Format(ExceptionDictionary.The_value_of_the_argument_could_not_be_correctly_translated_into_the_specified_type, constraint));
                }
            }

            if ((constraint.ConstraintType == trkValidationConstraintType.True || constraint.ConstraintType == trkValidationConstraintType.False) && constraint.PropertyType != typeof(bool).AssemblyQualifiedName)
            {
                sb.AppendLine(string.Format("{0} is not a boolean and_therefore not compatible with_a {1} constraint", constraint.Property, constraint.ConstraintType));
                //sb.AppendLine(string.Format(ExceptionDictionary._0__is_not_a_boolean_and_therefore_not_compatible_with_a__1__constraint, constraint.Property, constraint.ConstraintType));
            }
            message = sb.ToString();
            return string.IsNullOrEmpty(message);
        }
 /*override*/
 protected bool CanBeDeleted(ValidationConstraint obj, out string message)
 {
     message = string.Empty;
     StringBuilder sb = new StringBuilder();
     var children = SessionManagement.Db.GetAllValidationConstraints().Where(c => Equals(c.ParentConstraint, obj)).ToList();
     if (children.Any())
         sb.AppendLine(string.Format("The constraint {0} cannot be deleted because it is linked to child constraints", obj));
     message = sb.ToString();
     return string.IsNullOrEmpty(message);
 }
        public void ValidationConstraints_ValidateDeepTree()
        {
            var asset = SessionManagement.Db.GetAllAssets().FirstOrDefault(a => a.Name == "ACCOR");
            var firstConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Equal,
                ObjectType = _typeOfAssetEditable,
                Property = "Name",
                PropertyType = _typeOfString,
                MainArgument = "ACCOR"
            };

            var secondConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Equal,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                ParentConstraint = firstConstraint,
                MainArgument = "124"
            };

            var thirdConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.NotNull,
                ObjectType = _typeOfAssetEditable,
                Property = "Code",
                ParentConstraint = secondConstraint,
                PropertyType = _typeOfString,
            };
            ValidationConstraintService.Instance.SaveOrUpdateList(new List<ValidationConstraint>{firstConstraint, secondConstraint, thirdConstraint});
            ValidationConstraintService.Instance.ValidateObject(asset);
        }
        public void ValidationConstraints_SaveFailed()
        {
            var newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.False,
                ObjectType = _typeOfAssetEditable,
                Property = "Status",
                PropertyType = _typeOfAssetStatus
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.True,
                ObjectType = _typeOfAssetEditable,
                Property = "Status",
                PropertyType = _typeOfAssetStatus
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Greater,
                ObjectType = _typeOfAssetEditable,
                Property = "Status",
                PropertyType = _typeOfAssetStatus
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Lower,
                ObjectType = _typeOfAssetEditable,
                Property = "Status",
                PropertyType = _typeOfAssetStatus
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Between,
                ObjectType = _typeOfAssetEditable,
                Property = "Status",
                PropertyType = _typeOfAssetStatus
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Equal,
                ObjectType = _typeOfAssetEditable,
                Property = "Status",
                PropertyType = _typeOfAssetStatus
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.True,
                ObjectType = _typeOfAssetEditable,
                Property = "Code",
                PropertyType = _typeOfString
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.False,
                ObjectType = _typeOfAssetEditable,
                Property = "Code",
                PropertyType = _typeOfString
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.False,
                ObjectType = _typeOfAssetEditable,
                Property = "UnknownProperty",
                PropertyType = _typeOfString
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Between,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                MainArgument = "1"
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Between,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                SecondaryArgument = "1"
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.NotNull,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                SecondaryArgument = "1"
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.Null,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
                SecondaryArgument = "1"
            };
            TrySaveAndFail(newConstraint);

            newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.NotNull,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = "RandomTypeThatDoesNotExist",
            };
            TrySaveAndFail(newConstraint);
        }
 private static void TrySaveAndSucceed(ValidationConstraint constraintsToSave)
 {
     var constraintsList = new List<ValidationConstraint> { constraintsToSave };
     TrySaveAndSucceed(constraintsList);
 }
        public void ValidationConstraints_SaveItselfAsParentFails()
        {
            //Check that a validation constraint cannot reference itself
            var newConstraint = new ValidationConstraint
            {
                ConstraintType = trkValidationConstraintType.NotNull,
                ObjectType = _typeOfAssetEditable,
                Property = "Id",
                PropertyType = _typeOfInt,
            };

            var constraintsToSave = new List<ValidationConstraint> { newConstraint };
            var savedConstraint = ValidationConstraintService.Instance.SaveOrUpdateList(constraintsToSave).SingleOrDefault();
            Assert.IsNotNull(savedConstraint);
            constraintsToSave.Clear();
            savedConstraint.ParentConstraint = savedConstraint;
            TrySaveAndFail(newConstraint);

            ////Finally delete the constraint that had to be saved.
            //savedConstraint.RequiresDeletion = true;
            //constraintsToSave.Add(newConstraint);
            //ValidationConstraintService.Instance.SaveOrUpdateList(constraintsToSave);
            //Assert.IsNull(ValidationConstraintService.Instance.GetValidationConstraintById(savedConstraint.ToString()));
            //constraintsToSave.Clear();
        }
 private static bool ProcessConstraint(object item, ValidationConstraint constraint)
 {
     var argumentType = item.GetType();
     object mainArgument = null;
     object secondArgument = null;
     if (constraint.ObjectType == argumentType.AssemblyQualifiedName)
     {
         var evaluator = new ExpressionEvaluator();
         var propertyValue = evaluator.GetValue(item, constraint.Property);
         if (constraint.MainArgument != null) mainArgument = GetValue(constraint.MainArgument, propertyValue.GetType());
         if (constraint.SecondaryArgument != null) secondArgument = GetValue(constraint.SecondaryArgument, propertyValue.GetType());
         switch (constraint.ConstraintType)
         {
             case trkValidationConstraintType.Null:
                 if (propertyValue is string)
                     return string.IsNullOrEmpty(propertyValue.ToString());
                 return (propertyValue == null);
             case trkValidationConstraintType.NotNull:
                 if (propertyValue is string)
                     return !string.IsNullOrEmpty((string)propertyValue.ToString());
                 return (propertyValue != null);
             case trkValidationConstraintType.Greater:
                 if (mainArgument == null) return false;
                 return IsGreater(mainArgument, propertyValue);
             case trkValidationConstraintType.Lower:
                 if (mainArgument == null) return false;
                 return IsGreater(propertyValue, mainArgument);
             case trkValidationConstraintType.Equal:
                 if (mainArgument == null) return false;
                 return AreEqual(propertyValue, mainArgument);
             case trkValidationConstraintType.Between:
                 if (mainArgument == null) return false;
                 if (secondArgument == null) return false;
                 return (IsGreater(mainArgument, propertyValue) && IsGreater(propertyValue, secondArgument));
             case trkValidationConstraintType.Different:
                 return !AreEqual(propertyValue, mainArgument);
             case trkValidationConstraintType.False:
                 return !((bool)propertyValue);
             case trkValidationConstraintType.True:
                 return ((bool)propertyValue);
             default:
                 throw new ArgumentOutOfRangeException();
         }
     }
     return false;
 }
Ejemplo n.º 19
0
 public void Delete(ValidationConstraint validationConstraint)
 {
     if (_cachedValidationConstraints.FirstOrDefault(vc => vc.Equals(validationConstraint)) != null)
         _cachedValidationConstraints.Remove(validationConstraint);
 }