Пример #1
0
        public bool Compare(object expected, object result, object oSchema)
        {
            // this method can get 3 types:
            //1. Dictionary<string,object> when jsonObject
            //2. object[] (when jsonArray),
            //3. Primitive (string,int etc...),
            Type expValType = expected.GetType();



            //case when its a jsonObject.
            if (expected is Dictionary <string, object> )
            {
                Dictionary <string, object> dictExp = expected as Dictionary <string, object>;
                Dictionary <string, object> dictRes = result as Dictionary <string, object>;

                if (dictRes == null)
                {
                    var validatorResult = SchemaValidators.Validate(dictExp, dictRes, oSchema != null && oSchema.GetType() == typeof(String)? oSchema.ToString() : SchemaValidators.DefaultSchemaValidator);
                    if (!validatorResult.Success)
                    {
                        if (ThrowException)
                        {
                            throw new ApplicationException(validatorResult.Message);
                        }
                        else
                        {
                            Differences.Add(new CompareResultInfo
                            {
                                Field    = _lastKey,
                                Expected = SerializeObject(dictExp),
                                Result   = dictRes,
                                Schema   = SchemaValidators.DefaultSchemaValidator
                            });
                        }
                    }
                    return(false);
                }

                var schema = oSchema as Dictionary <string, object>;

                foreach (KeyValuePair <string, object> kvp in dictExp)
                {
                    _lastKey = kvp.Key;
                    object resValue = null;
                    if (dictRes != null && dictRes.ContainsKey(kvp.Key))
                    {
                        resValue = dictRes[kvp.Key];
                    }
                    else
                    {
                        resValue = null;
                    }

                    object expValue  = dictExp[kvp.Key];
                    var    validator = schema != null && schema.ContainsKey(kvp.Key) ? schema[kvp.Key].ToString() : SchemaValidators.DefaultSchemaValidator;
                    try
                    {
                        Compare(expValue, resValue, validator);
                    }
                    catch (Exception e)
                    {
                        if (ThrowException)
                        {
                            throw new ApplicationException(String.Format("The failed key: {0} , {1}", kvp.Key, e.Message));
                        }
                        else
                        {
                            Differences.Add(new CompareResultInfo
                            {
                                Field    = _lastKey,
                                Result   = resValue,
                                Expected = expValue,
                                Schema   = validator
                            });
                        }
                    }
                }
            }

            //case when jsonArray.
            else if (expected is object[])
            {
                var schema = oSchema as Dictionary <string, object>;

                IEnumerable enumerableExp = expected as IEnumerable;
                IEnumerator enuExp        = enumerableExp.GetEnumerator();

                IEnumerable enumerableRes = result as IEnumerable;
                IEnumerator enuRes        = enumerableRes == null ? null : enumerableRes.GetEnumerator();

                //there are 2 cases:
                // - one that should not happen ,
                //   when the items in the array are not primitive type we should do something else.

                // - if its primitive type than we should validate the array not the items in the array.
                while (enuExp.MoveNext())
                {
                    if (enuRes != null && enuRes.MoveNext())
                    {
                        Compare(enuExp.Current, enuRes.Current, schema);
                    }
                    else
                    {
                        Compare(enuExp.Current, null, schema);
                    }
                }
            }
            //probably primitive type.
            else if (expValType.IsPrimitive || expValType == typeof(String))
            {
                var schema = oSchema != null?oSchema.ToString() : SchemaValidators.DefaultSchemaValidator;

                var validatorResult = SchemaValidators.Validate(expected, result, schema);
                if (!validatorResult.Success)
                {
                    if (ThrowException)
                    {
                        throw new ApplicationException(validatorResult.Message);
                    }
                    else
                    {
                        Differences.Add(new CompareResultInfo
                        {
                            Field    = _lastKey,
                            Result   = result,
                            Expected = expected,
                            Schema   = schema
                        });
                    }
                }
            }
            else
            {
                throw new ApplicationException("Unknown compare senario!");
            }

            return(true);
        }
Пример #2
0
        public bool Compare(object oExpected, object oResult, object oSchema)
        {
            if (oExpected is XContainer)
            {
                var expected = oExpected as XContainer;
                var result   = oResult as XContainer;


                var eEnu = expected.Elements().GetEnumerator();
                var rEnu = result == null ? null : result.Elements().GetEnumerator();

                while (eEnu.MoveNext())
                {
                    if (rEnu != null)
                    {
                        rEnu.MoveNext();
                    }

                    var node    = eEnu.Current;
                    var resNode = rEnu == null ? null : rEnu.Current;

                    var name = node.Name.LocalName.ToString();
                    _lastKey = name;

                    //xml object
                    if (node.Elements().Count() > 0)
                    {
                        var schema    = oSchema as Dictionary <string, object>;
                        var validator = schema != null && schema.ContainsKey(name) ? schema[name] : null;
                        try
                        {
                            if (resNode == null)
                            {
                                var validatorResult = SchemaValidators.Validate((object)node, (object)resNode, oSchema != null && oSchema.GetType() == typeof(String) ? oSchema.ToString() : SchemaValidators.DefaultSchemaValidator);
                                if (!validatorResult.Success)
                                {
                                    if (ThrowException)
                                    {
                                        throw new ApplicationException(validatorResult.Message);
                                    }
                                    else
                                    {
                                        Differences.Add(new CompareResultInfo
                                        {
                                            Field    = _lastKey,
                                            Expected = node,
                                            Result   = resNode == null ? null : (resNode as XElement).Value,
                                            Schema   = SchemaValidators.DefaultSchemaValidator
                                        });
                                    }
                                }
                            }
                            else
                            {
                                Compare(node, resNode, validator);
                            }
                        }
                        catch (Exception e)
                        {
                            if (ThrowException)
                            {
                                throw new ApplicationException(String.Format("The failed key: {0} , {1}", node.Name, e.Message));
                            }
                            else
                            {
                                Differences.Add(new CompareResultInfo
                                {
                                    Field    = _lastKey,
                                    Result   = resNode,
                                    Expected = node,
                                    Schema   = SchemaValidators.DefaultSchemaValidator
                                });
                            }
                        }
                    }
                    //xml primitive
                    else
                    {
                        var nExpected       = node.Value;
                        var schemaDict      = oSchema as Dictionary <string, object>;
                        var schema          = schemaDict != null && schemaDict.ContainsKey(name) ? schemaDict[name].ToString() : SchemaValidators.DefaultSchemaValidator;
                        var validatorResult = SchemaValidators.Validate(nExpected, resNode == null ? null : (resNode as XElement).Value, schema);
                        if (!validatorResult.Success)
                        {
                            if (ThrowException)
                            {
                                throw new ApplicationException(validatorResult.Message);
                            }
                            else
                            {
                                Differences.Add(new CompareResultInfo
                                {
                                    Field    = _lastKey,
                                    Expected = nExpected,
                                    Result   = resNode == null ? null : (resNode as XElement).Value,
                                    Schema   = schema
                                });
                            }
                        }
                    }
                }
            }


            //xml primitive
            else
            {
                var expected = oExpected as XmlNode;
                var result   = oResult as XmlNode;
                var schema   = oSchema.ToString() == null ? SchemaValidators.DefaultSchemaValidator : oSchema.ToString();

                var validatorResult = SchemaValidators.Validate(expected.Value, result.Value, schema);
                if (!validatorResult.Success)
                {
                    if (ThrowException)
                    {
                        throw new ApplicationException(validatorResult.Message);
                    }
                    else
                    {
                        Differences.Add(new CompareResultInfo
                        {
                            Field    = _lastKey,
                            Result   = result,
                            Expected = expected,
                            Schema   = schema
                        });
                    }
                }
            }


            return(true);
        }