Esempio 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 navigPropName = string.Empty;
            string entityTypeShortName;
            var    entityTypeShortNames    = new List <string>();
            Tuple <string, string> keyProp = null;

            do
            {
                var navigPropNames = MetadataHelper.GetNavigationPropertyNames(out entityTypeShortName, entityTypeShortNames);
                if (null == navigPropNames || !navigPropNames.Any())
                {
                    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))
            {
                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)
            {
                return(passed);
            }

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

            if (!entities.Any())
            {
                return(passed);
            }

            var    entity     = entities.First();
            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);

            info = new ExtensionRuleViolationInfo(new Uri(url), string.Empty, detail);
            if (null != resp && HttpStatusCode.OK == resp.StatusCode)
            {
                entities = JsonParserHelper.GetEntities(resp.ResponsePayload);
                if (!entities.Any())
                {
                    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;
            }
            else
            {
                passed = false;
            }

            return(passed);
        }