예제 #1
0
        private IEnumerable <EnumExtension.EnumMember> GetAllowedValueOperators(ExtendedAttributeType type)
        {
            if (AdvancedComparisonRuleViewModel.valueOperatorValues.Count == 0)
            {
                var enumValues = Enum.GetValues(typeof(ValueOperator));
                EnumExtension.EnumMember enumMember;

                foreach (var value in enumValues)
                {
                    enumMember             = new EnumExtension.EnumMember();
                    enumMember.Value       = value;
                    enumMember.Description = ((Enum)value).GetEnumDescription();
                    valueOperatorValues.Add(enumMember);
                }

                enumMember             = new EnumExtension.EnumMember();
                enumMember.Value       = ValueOperator.IsPresent;
                enumMember.Description = ((Enum)enumMember.Value).GetEnumDescription();
                valueOperatorValues.Add(enumMember);

                enumMember             = new EnumExtension.EnumMember();
                enumMember.Value       = ValueOperator.NotPresent;
                enumMember.Description = ((Enum)enumMember.Value).GetEnumDescription();
                valueOperatorValues.Add(enumMember);
            }

            foreach (var value in ComparisonEngine.GetAllowedValueOperators(type))
            {
                yield return(valueOperatorValues.FirstOrDefault(t => (int)t.Value == ((int)value)));
            }

            yield return(valueOperatorValues.FirstOrDefault(t => (ValueOperator)t.Value == ValueOperator.IsPresent));

            yield return(valueOperatorValues.FirstOrDefault(t => (ValueOperator)t.Value == ValueOperator.NotPresent));
        }
        /// <summary>
        /// Gets the matching single value from the multivalued attribute
        /// </summary>
        /// <param name="source">The source attribute array</param>
        /// <returns>An attribute value</returns>
        private DateTime GetValueFromMvDateTime(IList <object> source)
        {
            DateTime?lastValue = null;

            foreach (object value in source)
            {
                DateTime valueDateTime = TypeConverter.ConvertData <DateTime>(value);

                switch (this.SelectorOperator)
                {
                case ValueOperator.Equals:
                case ValueOperator.NotEquals:
                case ValueOperator.GreaterThan:
                case ValueOperator.LessThan:
                case ValueOperator.GreaterThanOrEq:
                case ValueOperator.LessThanOrEq:
                case ValueOperator.And:
                case ValueOperator.Or:
                    if (ComparisonEngine.CompareDateTime(valueDateTime, DateTime.Parse(this.SelectorValue), this.SelectorOperator))
                    {
                        return(valueDateTime);
                    }

                    break;

                case ValueOperator.Largest:
                    if (!lastValue.HasValue || valueDateTime > lastValue)
                    {
                        lastValue = valueDateTime;
                    }

                    break;

                case ValueOperator.Smallest:
                    if (!lastValue.HasValue || valueDateTime < lastValue)
                    {
                        lastValue = valueDateTime;
                    }

                    break;

                case ValueOperator.Contains:
                case ValueOperator.NotContains:
                case ValueOperator.StartsWith:
                case ValueOperator.EndsWith:
                case ValueOperator.IsPresent:
                case ValueOperator.NotPresent:
                default:
                    throw new UnknownOrUnsupportedValueOperatorException(this.SelectorOperator, typeof(long));
                }
            }

            if (lastValue.HasValue)
            {
                return(lastValue.Value);
            }

            throw new NotFoundException();
        }
예제 #3
0
        /// <summary>
        /// Determines if this AttributeValue object has the same value as another object
        /// </summary>
        /// <param name="obj">The value to compare</param>
        /// <returns>A boolean value indicating if the two values are the same</returns>
        public bool ValueEquals(object obj)
        {
            if (this.Value == null && obj == null)
            {
                return(true);
            }

            if (this.Value == null ^ obj == null)
            {
                return(false);
            }

            object comparisonValue;

            if (obj is AttributeValue)
            {
                AttributeValue otherValue = (AttributeValue)obj;

                if (this.IsNull && otherValue.IsNull)
                {
                    //// If both values are null
                    return(true);
                }
                else if (this.IsNull || otherValue.IsNull)
                {
                    //// If one of the two values are null
                    return(false);
                }
                else if (this.Attribute != otherValue.Attribute)
                {
                    //// If the types don't match
                    return(false);
                }

                comparisonValue = otherValue.Value;
            }
            else
            {
                comparisonValue = obj;
            }

            switch (this.attribute.Type)
            {
            case ExtendedAttributeType.Boolean:
            case ExtendedAttributeType.Integer:
            case ExtendedAttributeType.DateTime:
            case ExtendedAttributeType.Reference:
            case ExtendedAttributeType.Binary:
                return(ComparisonEngine.Compare(comparisonValue, this.Value, ValueOperator.Equals, this.Attribute.Type));

            case ExtendedAttributeType.String:
                return(ComparisonEngine.CompareString(TypeConverter.ConvertData <string>(comparisonValue), this.ValueString, ValueOperator.Equals, StringComparison.CurrentCulture));

            case ExtendedAttributeType.Undefined:
            default:
                throw new ArgumentException("Unknown or unsupported attribute type");
            }
        }
예제 #4
0
        public void CompareBinaryFromStringEqualsTest()
        {
            ValueOperator valueOperator    = ValueOperator.Equals;
            string        actualValue      = "AAECAwQ=";
            string        matchingValue    = "AAECAwQ=";
            string        nonMatchingValue = "AAACAwQ=";

            Assert.AreEqual(true, ComparisonEngine.CompareBinary(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareBinary(actualValue, nonMatchingValue, valueOperator));
        }
예제 #5
0
        public void CompareStringNotEqualsTest()
        {
            ValueOperator valueOperator    = ValueOperator.NotEquals;
            string        actualValue      = "abcd";
            string        matchingValue    = "defg";
            string        nonMatchingValue = "abcd";

            Assert.AreEqual(true, ComparisonEngine.CompareString(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareString(actualValue, nonMatchingValue, valueOperator));
        }
예제 #6
0
        public void CompareBooleanNotEqualsTest()
        {
            ValueOperator valueOperator    = ValueOperator.NotEquals;
            bool          actualValue      = true;
            bool          matchingValue    = false;
            bool          nonMatchingValue = true;

            Assert.AreEqual(true, ComparisonEngine.CompareBoolean(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareBoolean(actualValue, nonMatchingValue, valueOperator));
        }
예제 #7
0
        public void CompareBooleanFromStringEqualsTest()
        {
            ValueOperator valueOperator    = ValueOperator.Equals;
            string        actualValue      = "true";
            string        matchingValue    = "true";
            string        nonMatchingValue = "false";

            Assert.AreEqual(true, ComparisonEngine.CompareBoolean(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareBoolean(actualValue, nonMatchingValue, valueOperator));
        }
예제 #8
0
        public void CompareDateTimeLessThanTest()
        {
            ValueOperator valueOperator    = ValueOperator.LessThan;
            DateTime      actualValue      = DateTime.Parse("1/1/1999");
            DateTime      matchingValue    = DateTime.Parse("1/1/2000");
            DateTime      nonMatchingValue = DateTime.Parse("1/1/1989");

            Assert.AreEqual(true, ComparisonEngine.CompareDateTime(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareDateTime(actualValue, nonMatchingValue, valueOperator));
        }
예제 #9
0
        public void CompareLongOrTest()
        {
            ValueOperator valueOperator    = ValueOperator.Or;
            long          actualValue      = 514L;
            long          matchingValue    = 2L;
            long          nonMatchingValue = 5L;

            Assert.AreEqual(true, ComparisonEngine.CompareLong(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareLong(actualValue, nonMatchingValue, valueOperator));
        }
예제 #10
0
        public void CompareLongLessThanTest()
        {
            ValueOperator valueOperator    = ValueOperator.LessThan;
            long          actualValue      = 10L;
            long          matchingValue    = 20L;
            long          nonMatchingValue = 5L;

            Assert.AreEqual(true, ComparisonEngine.CompareLong(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareLong(actualValue, nonMatchingValue, valueOperator));
        }
예제 #11
0
        public void CompareLongNotEqualsTest()
        {
            ValueOperator valueOperator    = ValueOperator.NotEquals;
            long          actualValue      = 1234567890L;
            long          matchingValue    = 9876543210L;
            long          nonMatchingValue = 1234567890L;

            Assert.AreEqual(true, ComparisonEngine.CompareLong(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareLong(actualValue, nonMatchingValue, valueOperator));
        }
예제 #12
0
        public void CompareLongFromStringEqualsTest()
        {
            ValueOperator valueOperator    = ValueOperator.Equals;
            string        actualValue      = "1234567890";
            string        matchingValue    = "1234567890";
            string        nonMatchingValue = "9876543210";

            Assert.AreEqual(true, ComparisonEngine.CompareLong(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareLong(actualValue, nonMatchingValue, valueOperator));
        }
예제 #13
0
        public void CompareStringEndsWithTest()
        {
            ValueOperator valueOperator    = ValueOperator.EndsWith;
            string        actualValue      = "abcdefghijklmnop";
            string        matchingValue    = "mnop";
            string        nonMatchingValue = "zzz";

            Assert.AreEqual(true, ComparisonEngine.CompareString(actualValue, matchingValue, valueOperator));
            Assert.AreEqual(false, ComparisonEngine.CompareString(actualValue, nonMatchingValue, valueOperator));
        }
예제 #14
0
        public override bool Evaluate(CSEntry csentry, MVEntry mventry)
        {
            if (mventry == null)
            {
                throw new NotSupportedException(string.Format("The rule type {0} is not supported in this context", this.GetType().Name));
            }

            int actual = mventry.ConnectedMAs.OfType <ConnectedMA>().Count(t => t.Name == this.MAName);

            return(ComparisonEngine.CompareLong(actual, this.Count, this.Operator));
        }
예제 #15
0
        public void CompareStringNotPresentTest()
        {
            ValueOperator valueOperator    = ValueOperator.NotPresent;
            string        actualValue      = null;
            string        matchingValue    = null;
            string        nonMatchingValue = null;

            Assert.AreEqual(true, ComparisonEngine.CompareString(actualValue, matchingValue, valueOperator));

            actualValue = "abds";
            Assert.AreEqual(false, ComparisonEngine.CompareString(actualValue, nonMatchingValue, valueOperator));
        }
예제 #16
0
        public void CompareBinaryNoPresenceTest()
        {
            ValueOperator valueOperator = ValueOperator.NotPresent;

            byte[] actualValue      = null;
            byte[] matchingValue    = new byte[] { 0, 1, 2, 3 };
            byte[] nonMatchingValue = new byte[] { 0, 1, 2, 4 };

            Assert.AreEqual(true, ComparisonEngine.CompareBinary(actualValue, matchingValue, valueOperator));

            actualValue = new byte[] { 0, 1, 2, 3, 4 };
            Assert.AreEqual(false, ComparisonEngine.CompareBinary(actualValue, matchingValue, valueOperator));
        }
예제 #17
0
        public void CompareLongGreaterThanOrEqTest()
        {
            ValueOperator valueOperator    = ValueOperator.GreaterThanOrEq;
            long          actualValue      = 20L;
            long          matchingValue    = 10L;
            long          nonMatchingValue = 30L;

            Assert.AreEqual(true, ComparisonEngine.CompareLong(actualValue, matchingValue, valueOperator));

            matchingValue = 20L;
            Assert.AreEqual(true, ComparisonEngine.CompareLong(actualValue, matchingValue, valueOperator));

            Assert.AreEqual(false, ComparisonEngine.CompareLong(actualValue, nonMatchingValue, valueOperator));
        }
        /// <summary>
        /// Gets the matching single value from the multivalued attribute
        /// </summary>
        /// <param name="source">The source attribute array</param>
        /// <returns>An attribute value</returns>
        private Guid GetValueFromMvReference(IList <object> source)
        {
            foreach (object value in source)
            {
                Guid valueGuid = TypeConverter.ConvertData <Guid>(value);

                if (ComparisonEngine.CompareString(valueGuid.ToString(), this.SelectorValue, this.SelectorOperator))
                {
                    return(valueGuid);
                }
            }

            throw new NotFoundException();
        }
        /// <summary>
        /// Gets the matching single value from the multivalued attribute
        /// </summary>
        /// <param name="source">The source attribute array</param>
        /// <returns>An attribute value</returns>
        private string GetValueFromMvString(IList <object> source)
        {
            foreach (object value in source)
            {
                string valueString = TypeConverter.ConvertData <string>(value);

                if (ComparisonEngine.CompareString(valueString, this.SelectorValue, this.SelectorOperator))
                {
                    return(valueString);
                }
            }

            throw new NotFoundException();
        }
        /// <summary>
        /// Gets the matching single value from the multivalued attribute
        /// </summary>
        /// <param name="source">The source attribute array</param>
        /// <returns>An attribute value</returns>
        private byte[] GetValueFromMvBinary(IList <object> source)
        {
            foreach (object value in source)
            {
                byte[] valueByte = TypeConverter.ConvertData <byte[]>(value);

                if (ComparisonEngine.CompareBinary(valueByte, this.SelectorValue, this.SelectorOperator))
                {
                    return(valueByte);
                }
            }

            throw new NotFoundException();
        }
예제 #21
0
        /// <summary>
        /// Validates a change to a property
        /// </summary>
        /// <param name="propertyName">The name of the property that has changed</param>
        protected override void ValidatePropertyChange(string propertyName)
        {
            base.ValidatePropertyChange(propertyName);

            switch (propertyName)
            {
            case "SearchAttribute":
                if (this.SearchAttribute == null)
                {
                    this.AddError("SearchAttribute", "A search attribute must be provided");
                }
                else
                {
                    this.RemoveError("SearchAttribute");

                    if (!ComparisonEngine.IsAllowedOperator(this.Operator, this.SearchAttribute.Type) && (!ComparisonEngine.IsAllowedPresenceOperator(this.Operator)))
                    {
                        this.AddError("Operator", "The specified search operator is not valid for this data type");
                    }
                    else
                    {
                        this.RemoveError("Operator");
                    }
                }

                break;

            case "Operator":
                if (this.SearchAttribute != null)
                {
                    if (!ComparisonEngine.IsAllowedOperator(this.Operator, this.SearchAttribute.Type) && (!ComparisonEngine.IsAllowedPresenceOperator(this.Operator)))
                    {
                        this.AddError("Operator", "The specified search operator is not valid for this data type");
                    }
                    else
                    {
                        this.RemoveError("Operator");
                    }
                }

                break;

            default:
                break;
            }
        }
예제 #22
0
        /// <summary>
        /// Evaluates a specific value against the rule
        /// </summary>
        /// <param name="actualValue">The value to evaluate</param>
        /// <returns>A value indicating whether the rule conditions were met</returns>
        protected bool EvaluateAttributeValue(object actualValue, IList <object> expectedValues)
        {
            if (actualValue == null)
            {
                if (this.CompareAs == ExtendedAttributeType.Boolean)
                {
                    actualValue = false;
                }
                else
                {
                    this.RaiseRuleFailure(string.Format("The source value was null"));
                    return(false);
                }
            }

            bool result = false;

            foreach (object expectedValue in expectedValues)
            {
                switch (this.CompareAs)
                {
                case ExtendedAttributeType.Binary:
                    result = ComparisonEngine.CompareBinary(TypeConverter.ConvertData <byte[]>(actualValue), TypeConverter.ConvertData <byte[]>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.Boolean:
                    result = ComparisonEngine.CompareBoolean(TypeConverter.ConvertData <bool>(actualValue), TypeConverter.ConvertData <bool>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.Integer:
                    result = ComparisonEngine.CompareLong(TypeConverter.ConvertData <long>(actualValue), TypeConverter.ConvertData <long>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.DateTime:
                    result = ComparisonEngine.CompareDateTime(TypeConverter.ConvertData <DateTime>(actualValue), TypeConverter.ConvertData <DateTime>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.String:
                    result = ComparisonEngine.CompareString(TypeConverter.ConvertData <string>(actualValue), TypeConverter.ConvertData <string>(expectedValue), this.ValueOperator);
                    break;

                case ExtendedAttributeType.Reference:
                    result = ComparisonEngine.CompareString(TypeConverter.ConvertData <string>(actualValue), TypeConverter.ConvertData <string>(expectedValue), this.ValueOperator);
                    break;

                default:
                    throw new UnknownOrUnsupportedDataTypeException();
                }

                if (result)
                {
                    break;
                }
            }

            if (!result)
            {
                this.RaiseRuleFailure("Comparison failed\nComparison Operator: {0}\nExpected Values: {1}\nActual Value: {2}", this.ValueOperator.ToString(), expectedValues.Select(t => t.ToSmartStringOrNull()).ToCommaSeparatedString(), actualValue.ToSmartString());
            }

            return(result);
        }