Ejemplo n.º 1
0
        /// <summary>
        /// Verifies the service implementation feature.
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out parameter to return violation information when rule does not pass</param>
        /// <returns>true if the service implementation feature passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;
            var    svcStatus     = ServiceStatus.GetInstance();
            string navigPropName = string.Empty;
            string entityTypeShortName;
            var    entityTypeShortNames            = new List <string>();
            Tuple <string, string>    keyProp      = null;
            ExtensionRuleResultDetail detailglobal = new ExtensionRuleResultDetail(this.Name);

            info         = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detailglobal);
            detailglobal = info.Details[0];

            do
            {
                var navigPropNames = MetadataHelper.GetNavigationPropertyNames(out entityTypeShortName, entityTypeShortNames);
                if (null == navigPropNames || !navigPropNames.Any())
                {
                    detailglobal.ErrorMessage = "No navigation properties available to test";
                    return(passed);
                }

                navigPropName = navigPropNames[0];
                entityTypeShortNames.Add(entityTypeShortName);
                keyProp = MetadataHelper.GetKeyProperty(entityTypeShortName);
            }while (null == keyProp);

            string keyPropName  = keyProp.Item1;
            string keyPropType  = keyProp.Item2;
            var    entitySetUrl = entityTypeShortName.GetAccessEntitySetURL();

            if (string.IsNullOrEmpty(entitySetUrl))
            {
                detailglobal.ErrorMessage = "Unable to get a proper Entity URL";
                return(passed);
            }

            string url  = svcStatus.RootURL.TrimEnd('/') + "/" + entitySetUrl;
            var    resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);

            if (null == resp || HttpStatusCode.OK != resp.StatusCode)
            {
                detailglobal.ErrorMessage = resp.StatusCode + "status code returned for " + url;
                return(passed);
            }

            var entities = JsonParserHelper.GetEntities(resp.ResponsePayload);

            if (!entities.Any())
            {
                detailglobal.ErrorMessage = "No entities returned:  " + resp.ResponsePayload;
                return(passed);
            }

            var entity = entities.First();

            if (!entity[keyPropName].HasValues)
            {
                detailglobal.ErrorMessage = keyPropName + " have no values to test";
                return(passed);
            }
            string keyPropVal = entity[keyPropName].ToString();
            string pattern    = "Edm.String" == keyPropType ? "{0}('{1}')/{2}/$ref" : "{0}({1})/{2}/$ref";

            url  = string.Format(pattern, url, keyPropVal, navigPropName);
            resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
            var detail = new ExtensionRuleResultDetail("ServiceImpl_SystemQueryOptionRef", url, HttpMethod.Get, string.Empty);

            detail.URI                = url;
            detail.ResponsePayload    = resp.ResponsePayload;
            detail.ResponseHeaders    = resp.ResponseHeaders;
            detail.HTTPMethod         = "GET";
            detail.ResponseStatusCode = resp.StatusCode.ToString();


            info = new ExtensionRuleViolationInfo(new Uri(url), string.Empty, detailglobal);
            info.AddDetail(detail);
            if (null != resp && HttpStatusCode.OK == resp.StatusCode)
            {
                entities = JsonParserHelper.GetEntities(resp.ResponsePayload);
                if (!entities.Any())
                {
                    detail.ErrorMessage = "No entities available to test";
                    return(false);
                }

                entity = entities.First();
                var odataId = entity[Constants.V4OdataId].ToString();
                resp   = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
                passed = null != resp && HttpStatusCode.OK == resp.StatusCode;
                if (passed == false)
                {
                    detail.URI                = url;
                    detail.ResponsePayload    = resp.ResponsePayload;
                    detail.ResponseHeaders    = resp.ResponseHeaders;
                    detail.HTTPMethod         = "GET";
                    detail.ResponseStatusCode = resp.StatusCode.ToString();
                    detail.ErrorMessage       = "Response return an error message:  " + resp.StatusCode.ToString();
                }
            }
            else
            {
                detail.URI                = url;
                detail.ResponsePayload    = resp.ResponsePayload;
                detail.ResponseHeaders    = resp.ResponseHeaders;
                detail.HTTPMethod         = "GET";
                detail.ResponseStatusCode = resp.StatusCode.ToString();

                detail.ErrorMessage = "Response return an error message:  " + resp.StatusCode.ToString();
                passed = false;
            }

            return(passed);
        }
        /// <summary>
        /// Verifies the service implementation feature.
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out parameter to return violation information when rule does not pass</param>
        /// <returns>true if the service implementation feature passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;
            var    svcStatus = ServiceStatus.GetInstance();
            string entityTypeShortName;
            var    propNames    = MetadataHelper.GetNumericPropertyNames(out entityTypeShortName);
            string propName     = propNames[0];
            string entitySetUrl = entityTypeShortName.GetAccessEntitySetURL();

            if (string.IsNullOrEmpty(entitySetUrl))
            {
                return(passed);
            }

            string url  = svcStatus.RootURL.TrimEnd('/') + '/' + entitySetUrl;
            var    resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);

            if (null != resp && HttpStatusCode.OK == resp.StatusCode)
            {
                JObject jObj    = JObject.Parse(resp.ResponsePayload);
                JArray  jArr    = jObj.GetValue(Constants.Value) as JArray;
                var     entity  = jArr.First as JObject;
                string  propVal = entity[propName].ToString();
                url  = string.Format("{0}?$filter=cast({1}, 'Edm.String') eq '{2}'", url, propName, propVal);
                resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
                var detail = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Get, resp.ResponseHeaders, resp);
                detail.URI                = url;
                detail.ResponsePayload    = resp.ResponsePayload;
                detail.ResponseHeaders    = resp.ResponseHeaders;
                detail.HTTPMethod         = "GET";
                detail.ResponseStatusCode = resp.StatusCode.ToString();

                info = new ExtensionRuleViolationInfo(new Uri(url), string.Empty, detail);
                if (null != resp && HttpStatusCode.OK == resp.StatusCode)
                {
                    jObj = JObject.Parse(resp.ResponsePayload);
                    jArr = jObj.GetValue(Constants.Value) as JArray;
                    foreach (JObject et in jArr)
                    {
                        if (et[propName].ToString() == propVal)
                        {
                            passed = true;
                        }
                        else
                        {
                            detail.ErrorMessage = et[propName].ToString() + " does not equal " + propVal;
                            info.AddDetail(detail);
                            passed = false;
                            break;
                        }
                    }
                }
                else
                {
                    detail.ErrorMessage = "The server returned an error reponse:  " + detail.ResponseStatusCode;
                    passed = false;
                }
            }

            return(passed);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Verifies the service implementation feature.
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out parameter to return violation information when rule does not pass</param>
        /// <returns>true if the service implementation feature passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;
            var    svcStatus = ServiceStatus.GetInstance();
            string entityTypeShortName;
            var    propNames = MetadataHelper.GetPropertyNames(
                "Edm.String",
                out entityTypeShortName,
                2,
                (elem) => {
                return(!(null != elem.Attribute("MaxLength") && elem.GetAttributeValue("MaxLength") == "max"));
            });

            if (null == propNames || propNames.Count < 2)
            {
                return(passed);
            }

            string propName1    = propNames[0];
            string propName2    = propNames[1];
            var    entitySetUrl = entityTypeShortName.MapEntityTypeShortNameToEntitySetURL();
            string url          = svcStatus.RootURL.TrimEnd('/') + "/" + entitySetUrl;
            var    resp         = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);

            if (null != resp && HttpStatusCode.OK == resp.StatusCode)
            {
                JObject jObj     = JObject.Parse(resp.ResponsePayload);
                JArray  jArr     = jObj.GetValue(Constants.Value) as JArray;
                var     entity   = jArr.First as JObject;
                var     propVal1 = entity[propName1].ToString();
                var     propVal2 = entity[propName2].ToString();
                url  = string.Format("{0}?$filter=concat(concat({1}, ': '), {2}) eq '{3}: {4}'", url, propName1, propName2, propVal1, propVal2);
                resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
                var detail = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Get, resp.ResponseHeaders, resp);
                detail.URI                = url;
                detail.ResponsePayload    = resp.ResponsePayload;
                detail.ResponseHeaders    = resp.ResponseHeaders;
                detail.HTTPMethod         = "GET";
                detail.ResponseStatusCode = resp.StatusCode.ToString();

                info = new ExtensionRuleViolationInfo(new Uri(url), string.Empty, detail);
                if (null != resp && HttpStatusCode.OK == resp.StatusCode)
                {
                    jObj = JObject.Parse(resp.ResponsePayload);
                    jArr = jObj.GetValue(Constants.Value) as JArray;
                    bool passtest = false;
                    foreach (JObject et in jArr)
                    {
                        string test1 = string.Format("{0}: {1}", et[propName1].ToString(), et[propName2].ToString());
                        string test2 = string.Format("{0}: {1}", propVal1, propVal2);

                        passtest = (string.Format("{0}: {1}", et[propName1].ToString(), et[propName2].ToString()) == string.Format("{0}: {1}", propVal1, propVal2));
                        if (!passtest)
                        {
                            var errordetail = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Get, resp.ResponseHeaders, resp, "The concatination did not match: " + test1 + " not equal to " + test2);
                            info.AddDetail(errordetail);
                            passed = false;
                        }
                    }
                }
                else
                {
                    detail.ErrorMessage = "The server returned an error response:  " + detail.ResponseStatusCode;
                    passed = false;
                }
            }

            return(passed);
        }
        /// <summary>
        /// Verifies the service implementation feature.
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out parameter to return violation information when rule does not pass</param>
        /// <returns>true if the service implementation feature passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            info = null;
            var    svcStatus = ServiceStatus.GetInstance();
            string entityTypeShortName;
            var    propTypes = new string[1] {
                "Edm.DateTimeOffset"
            };
            var propNames = MetadataHelper.GetPropertyNames(propTypes, out entityTypeShortName);

            if (null == propNames || !propNames.Any())
            {
                return(passed);
            }

            string propName     = propNames[0];
            var    entitySetUrl = entityTypeShortName.GetAccessEntitySetURL();

            if (string.IsNullOrEmpty(entitySetUrl))
            {
                return(passed);
            }

            string url  = svcStatus.RootURL.TrimEnd('/') + "/" + entitySetUrl;
            var    resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);

            if (null != resp && HttpStatusCode.OK == resp.StatusCode)
            {
                JObject jObj   = JObject.Parse(resp.ResponsePayload);
                JArray  jArr   = jObj.GetValue(Constants.Value) as JArray;
                var     entity = jArr.First as JObject;
                //var propVal = Convert.ToDateTime(entity[propName]).Minute;
                var propVal = -1;
                for (int i = 0; i < propNames.Count - 1; i++)
                {
                    try
                    {
                        if (entity[propNames[i]] != null)
                        {
                            propVal = Convert.ToDateTime(entity[propNames[i]]).Minute;
                        }
                    }
                    catch
                    {
                    }
                    if (propVal >= 0)
                    {
                        propName = propNames[i].ToString();
                        break;
                    }
                }
                if (propVal < 0)
                {
                    passed = false;
                }

                url  = string.Format("{0}?$filter=totaloffsetminutes({1}) eq {2}", url, propName, propVal);
                resp = WebHelper.Get(new Uri(url), string.Empty, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, svcStatus.DefaultHeaders);
                var detail = new ExtensionRuleResultDetail(this.Name, url, HttpMethod.Get, resp.ResponseHeaders, resp);
                detail.URI                = url;
                detail.ResponsePayload    = resp.ResponsePayload;
                detail.ResponseHeaders    = resp.ResponseHeaders;
                detail.HTTPMethod         = "GET";
                detail.ResponseStatusCode = resp.StatusCode.ToString();

                info = new ExtensionRuleViolationInfo(new Uri(url), string.Empty, detail);
                if (null != resp && HttpStatusCode.OK == resp.StatusCode)
                {
                    jObj = JObject.Parse(resp.ResponsePayload);
                    jArr = jObj.GetValue(Constants.Value) as JArray;
                    foreach (JObject et in jArr)
                    {
                        bool passedtest = Convert.ToDateTime(et[propName]).Minute == propVal;
                        if (!passedtest)
                        {
                            detail.ErrorMessage = "Verification failed:  " + et[propName];
                            info.AddDetail(detail);
                            //passed = false;
                            break;
                        }
                    }
                }
                else
                {
                    passed = false;
                }
            }

            return(passed);
        }
        /// <summary>
        /// Verifies the service implementation feature.
        /// </summary>
        /// <param name="context">The Interop service context</param>
        /// <param name="info">out parameter to return violation information when rule does not pass</param>
        /// <returns>true if the service implementation feature passes; false otherwise</returns>
        public override bool?Verify(ServiceContext context, out ExtensionRuleViolationInfo info)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            bool?passed = null;

            Response resp = WebHelper.Get(new Uri(context.ServiceBaseUri.OriginalString.TrimEnd('/') + @"/" + "$all"), Constants.AcceptHeaderJson,
                                          RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);

            ExtensionRuleResultDetail detail = new ExtensionRuleResultDetail(this.Name, context.ServiceBaseUri.OriginalString.TrimEnd('/') + @"/" + "$all",
                                                                             HttpMethod.Get, context.RequestHeaders.ToString());

            info = new ExtensionRuleViolationInfo(context.Destination, context.ResponsePayload, detail);

            if (null == resp || HttpStatusCode.OK != resp.StatusCode)
            {
                return(passed = false);
            }

            JObject allFeed;

            resp.ResponsePayload.TryToJObject(out allFeed);

            if (allFeed == null || JTokenType.Object != allFeed.Type)
            {
                return(passed = false);
            }

            JArray allEntities = JsonParserHelper.GetEntries(allFeed);

            passed = true;

            List <string> entitySetURLs = MetadataHelper.GetEntitySetURLs();

            foreach (string entitySetUrl in entitySetURLs)
            {
                string entityTypeShortName = entitySetUrl.MapEntitySetNameToEntityTypeShortName();
                Tuple <string, string> key = MetadataHelper.GetKeyProperty(entityTypeShortName);

                resp = WebHelper.Get(new Uri(context.ServiceBaseUri.OriginalString.TrimEnd('/') + @"/" + entitySetUrl),
                                     Constants.AcceptHeaderJson, RuleEngineSetting.Instance().DefaultMaximumPayloadSize, context.RequestHeaders);
                detail.URI                = context.ServiceBaseUri.OriginalString.TrimEnd('/') + @"/" + entitySetUrl;
                detail.ResponsePayload    = resp.ResponsePayload;
                detail.ResponseHeaders    = resp.ResponseHeaders;
                detail.HTTPMethod         = "GET";
                detail.ResponseStatusCode = resp.StatusCode.ToString();

                if (null == resp || HttpStatusCode.OK != resp.StatusCode)
                {
                    continue;
                }

                JObject feed;
                resp.ResponsePayload.TryToJObject(out feed);

                if (feed == null || JTokenType.Object != feed.Type)
                {
                    continue;
                }

                JArray entities = JsonParserHelper.GetEntries(feed);
                foreach (JToken entity in entities)
                {
                    try
                    {
                        var test = entity;


                        if (!Find(allEntities, key.Item1, entity[key].ToString()))
                        {
                            passed = false;
                            //public ExtensionRuleResultDetail(string ruleName, string uri, string httpMethod, string requestHeaders, string responseStatusCode = "", string responseHeaders = "", string responsePayload = "", string errorMessage = "", string requestData = "")
                            ExtensionRuleResultDetail detail2 = new ExtensionRuleResultDetail(this.Name);
                            detail2.ErrorMessage = "Unable to find key / value in entity set using:  " + key.Item1 + "/" + entity[key].ToString();
                            info.AddDetail(detail2);
                        }
                        else
                        {
                            return(passed = true);
                        }
                    }
                    catch (Exception ex)
                    {
                        passed = false;
                        ExtensionRuleResultDetail detail3 = new ExtensionRuleResultDetail(this.Name);
                        detail3.ErrorMessage = "Exception occured try to find key / value in entity set:  " + ex.Message;
                        info.AddDetail(detail3);
                    }
                }
            }

            return(passed);
        }