示例#1
0
        public void AddContext()
        {
            {
                var provider = new ServiceCollection()
                               .BuildServiceProvider()
                               .RebuildFromFactory();

                Assert.Null(ServiceContext.Provider);
            }

            {
                var provider = new ServiceCollection()
                               .AddContext()
                               .BuildServiceProvider();

                Assert.Null(ServiceContext.Provider);
            }

            {
                var provider = new ServiceCollection()
                               .AddContext()
                               .BuildServiceProvider()
                               .RebuildFromFactory();
                Assert.NotNull(ServiceContext.Provider);
                Assert.Equal(ServiceContext.Provider, provider);
            }

            {
                var provider = ServiceContextFactory.Create(new ServiceCollection().BuildServiceProvider());

                Assert.NotNull(ServiceContext.Provider);
                Assert.Equal(ServiceContext.Provider, provider);
            }
        }
示例#2
0
        private IEnumerable <JobGroup> CreateCrawlingJobsByUri(string Uri, string Format, IEnumerable <KeyValuePair <string, string> > reqHeaders)
        {
            ServiceContext ctxMaster = ServiceContextFactory.Create(Uri, Format, Guid.NewGuid(), MaxPayloadByteCount, reqHeaders);

            // make sure the master target is a service doc resource
            if (ctxMaster.PayloadType != PayloadType.ServiceDoc)
            {
                return(new JobGroup[]
                {
                    new JobGroup()
                    {
                        Uri = Uri,
                        MasterJobId = Guid.Empty,
                        DerivativeJobId = Guid.Empty,
                        ResourceType = ctxMaster.PayloadType.ToString(),
                        RuleCount = 0,
                        Issues = ctxMaster.HttpStatusCode == System.Net.HttpStatusCode.UnsupportedMediaType
                            ? "Error: The input is not a supported service resource for selected version."
                            : "Error: The input is not an OData service document resource.",
                    }
                });
            }

            SimpleJobPlanner jobPlanner = new SimpleJobPlanner(ctxMaster, Format, MaxPayloadByteCount);
            List <KeyValuePair <string, string> > failures;
            var             jobs      = jobPlanner.GetPlannedJobs(out failures);
            List <JobGroup> jobGroups = new List <JobGroup>();

            foreach (var job in jobs)
            {
                jobGroups.Add(CreateJobGroupItem(Format, ctxMaster.JobId, job, JobType.Uri));
            }

            if (failures != null)
            {
                foreach (var fail in failures)
                {
                    var item = JobGroup.CreateJobGroup(ctxMaster.JobId, "0", 0, fail.Key);
                    item.Issues = fail.Value;
                    jobGroups.Add(item);
                }
            }

            using (var x = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                var j = x.JobGroups;

                foreach (var job in jobGroups)
                {
                    JobGroup jobGroup = JobGroup.CreateJobGroup(ctxMaster.JobId, job.ResourceType, job.RuleCount, job.Uri);
                    jobGroup.Issues          = job.Issues;
                    jobGroup.DerivativeJobId = job.DerivativeJobId;
                    j.AddObject(jobGroup);
                }
                x.SaveChanges();
            }

            return(jobGroups);
        }
示例#3
0
        public IEnumerable <JobGroup> ConformanceRerunJob(string jobIdStr, string testResultIdsStr)
        {
            Guid masterJobId   = new Guid(jobIdStr);
            var  testResultIds = testResultIdsStr.TrimEnd(';').Split(';').Select(item => int.Parse(item)).ToList();

            using (var x = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                JobType rerunType     = JobType.ConformanceRerun;
                var     validationJob = (from j in x.ExtValidationJobs where j.ID == masterJobId select j).FirstOrDefault();

                var           toUpdateTestResults = (from t in x.TestResults where testResultIds.Contains(t.ID) select t);
                List <string> ruleNameList        = (from t in toUpdateTestResults select t.RuleName).Distinct().ToList();

                string Uri = validationJob.Uri;
                if (validationJob.Complete != true)
                {
                    return(new JobGroup[]
                    {
                        new JobGroup()
                        {
                            Uri = Uri == null? "":Uri,
                            MasterJobId = Guid.Empty,
                            DerivativeJobId = Guid.Empty,
                            ResourceType = "Rerun Conformance Rules",
                            RuleCount = 0,
                            Issues = "Error: Job is not complete or somebody else is rerunning this job!",
                        }
                    });
                }

                validationJob.Complete = false;
                x.SaveChanges();

                string Format   = validationJob.Format;
                string category = "conformance;" + validationJob.ServiceType + ";" + validationJob.LevelTypes;
                List <KeyValuePair <string, string> > reqHeaders = ToHeaderCollection(validationJob.ReqHeaders);

                ServiceContext ctx = ServiceContextFactory.Create(Uri, Format, masterJobId, MaxPayloadByteCount, reqHeaders, category);

                // make sure the target is a service doc resource
                if (ctx.PayloadType != PayloadType.ServiceDoc)
                {
                    return(new JobGroup[] { new JobGroup()
                                            {
                                                Uri = Uri,
                                                MasterJobId = Guid.Empty,
                                                DerivativeJobId = Guid.Empty,
                                                ResourceType = string.Empty,
                                                RuleCount = 0,
                                                Issues = "Error: The input is not an OData service document resource.",
                                            } });
                }
                else
                {
                    return(new JobGroup[] { CreateJobGroupItem(Format, masterJobId, ctx, rerunType, ruleNameList, false) });
                }
            }
        }
        private ServiceContext CreateMetadataJob(ref List <KeyValuePair <string, Exception> > failedTargets)
        {
            Func <ServiceContext> f = () => ServiceContextFactory.CreateMetadataContext(new Uri(this.metaResource),
                                                                                        this.rootCtx.ServiceBaseUri,
                                                                                        this.rootCtx.ServiceDocument,
                                                                                        this.rootCtx.MetadataDocument,
                                                                                        Guid.NewGuid(),
                                                                                        this.rootCtx.RequestHeaders,
                                                                                        this.rootCtx.ResponseHttpHeaders,
                                                                                        this.category);

            return(this.SetupCrawlSubJob(f, this.metaResource, ref failedTargets));
        }
示例#5
0
        public IEnumerable <JobGroup> SimpleRerunJob(string jobIdStr, string testResultIdsStr)
        {
            Guid masterJobId   = new Guid(jobIdStr);
            var  testResultIds = testResultIdsStr.TrimEnd(';').Split(';').Select(item => int.Parse(item)).ToList();

            using (var x = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                JobType rerunType     = JobType.None;
                var     validationJob = (from j in x.ExtValidationJobs where j.ID == masterJobId select j).FirstOrDefault();

                var           toUpdateTestResults = (from t in x.TestResults where testResultIds.Contains(t.ID) select t);
                List <string> ruleNameList        = (from t in toUpdateTestResults select t.RuleName).Distinct().ToList();

                string Uri = validationJob.Uri;
                if (validationJob.Complete != true)
                {
                    return(new JobGroup[]
                    {
                        new JobGroup()
                        {
                            Uri = Uri == null? "":Uri,
                            MasterJobId = Guid.Empty,
                            DerivativeJobId = Guid.Empty,
                            ResourceType = "Rerun Simple Rules",
                            RuleCount = 0,
                            Issues = "Error: Job is not complete or somebody else is rerunning this job!",
                        }
                    });
                }

                validationJob.Complete = false;
                x.SaveChanges();

                ServiceContext ctxMaster = null;
                string         Format    = validationJob.Format;
                List <KeyValuePair <string, string> > reqHeaders = ToHeaderCollection(validationJob.ReqHeaders);

                if (string.IsNullOrEmpty(Uri))
                {
                    rerunType = JobType.PayloadRerun;
                    ctxMaster = CreateRuleEngineContext(validationJob);
                }
                else
                {
                    rerunType = JobType.UriRerun;
                    ctxMaster = ServiceContextFactory.Create(Uri, Format, masterJobId, MaxPayloadByteCount, reqHeaders);
                }

                return(new JobGroup[] { CreateJobGroupItem(Format, masterJobId, ctxMaster, rerunType, ruleNameList, false) });
            }
        }
        private ServiceContext SetupCrawlSubJob(Uri targetUri, ref List <KeyValuePair <string, Exception> > failedTargets)
        {
            Func <ServiceContext> f = () => ServiceContextFactory.Create(targetUri,
                                                                         this.acceptHeaderValue,
                                                                         this.rootCtx.ServiceBaseUri,
                                                                         this.rootCtx.ServiceDocument,
                                                                         this.rootCtx.MetadataDocument,
                                                                         Guid.NewGuid(),
                                                                         this.maxPayloadSize,
                                                                         this.rootCtx.RequestHeaders,
                                                                         this.category);

            return(SetupCrawlSubJob(f, targetUri.AbsoluteUri, ref failedTargets));
        }
示例#7
0
 /// <summary>Create the ServiceContext based on the ValidationJob</summary>
 /// <param name="job">Row of ValidationJob in the SQL table</param>
 /// <returns>ServiceContext class</returns>
 private static ServiceContext CreateRuleEngineContext(ExtValidationJobs job)
 {
     if (!string.IsNullOrEmpty(job.Uri))
     {
         // an online validation
         return(ServiceContextFactory.Create(job.Uri, job.Format, job.ID, MaxPayloadByteCount, ToHeaderCollection(job.ReqHeaders)));
     }
     else
     {
         // an offline validation
         if (job.PayloadText.Length > MaxPayloadByteCount || job.MetadataText.Length > MaxPayloadByteCount)
         {
             throw new DataServiceException(509, "Content exceeds the allowed maximum size. Please copy and paste smaller content and retry.");
         }
         string payloadText  = SanitizeXmlLiteral(job.PayloadText);
         string metadataText = SanitizeXmlLiteral(job.MetadataText);
         return(ServiceContextFactory.Create(payloadText, metadataText, job.ID, job.ReqHeaders, ToHeaderCollection(job.ReqHeaders)));
     }
 }
示例#8
0
        private IEnumerable <JobGroup> CreateSimpleValidationJobByUri(string Uri, string Format, IEnumerable <KeyValuePair <string, string> > reqHeaders, bool isMetadataValidation = false, bool serviceImplementation = false)
        {
            ServiceContext ctx;

            if (serviceImplementation)
            {
                ctx = ServiceContextFactory.Create(Uri, Format, Guid.NewGuid(), MaxPayloadByteCount, reqHeaders, "ServiceImpl");
            }
            else
            {
                ctx = ServiceContextFactory.Create(Uri, Format, Guid.NewGuid(), MaxPayloadByteCount, reqHeaders);
            }

            bool isNotVerifyMetadata   = isMetadataValidation ^ (ctx != null && ctx.PayloadType == PayloadType.Metadata);
            bool isVersionNotSupported = ctx != null && !ctx.IsRequestVersion() && (ctx.HttpStatusCode == System.Net.HttpStatusCode.UnsupportedMediaType || ctx.HttpStatusCode == System.Net.HttpStatusCode.BadRequest);

            if (ctx == null || ctx.PayloadType == PayloadType.None || isNotVerifyMetadata || isVersionNotSupported)
            {
                return(new JobGroup[] { new JobGroup()
                                        {
                                            Uri = Uri,
                                            MasterJobId = Guid.Empty,
                                            DerivativeJobId = Guid.Empty,
                                            ResourceType = string.Empty,
                                            RuleCount = 0,
                                            Issues = isVersionNotSupported
                        ? "Error: The input is not a supported service resource for selected version."
                        : isNotVerifyMetadata
                        ? isMetadataValidation ? "Error: The input is not an OData metadata resource."
                        : "Error: Not support for OData metadata resource."
                        : "Error: The input is not a valid OData service endpoint.",
                                        } });
            }
            else
            {
                return(new JobGroup[] { CreateJobGroupItem(Format, ctx.JobId, ctx, JobType.Normal) });
            }
        }
示例#9
0
        private IEnumerable <JobGroup> CreateSimpleConformanceValidationJobByUri(string Uri, string Format, IEnumerable <KeyValuePair <string, string> > reqHeaders, string isConformance, string levelTypes)
        {
            string         category = "conformance;" + isConformance + ";" + levelTypes;
            ServiceContext ctx      = ServiceContextFactory.Create(Uri, Format, Guid.NewGuid(), MaxPayloadByteCount, reqHeaders, category);

            // make sure the target is a service doc resource
            if (ctx.PayloadType != PayloadType.ServiceDoc)
            {
                return(new JobGroup[] { new JobGroup()
                                        {
                                            Uri = Uri,
                                            MasterJobId = Guid.Empty,
                                            DerivativeJobId = Guid.Empty,
                                            ResourceType = string.Empty,
                                            RuleCount = 0,
                                            Issues = "Error: The input is not an OData service document resource.",
                                        } });
            }
            else
            {
                return(new JobGroup[] { CreateJobGroupItem(Format, ctx.JobId, ctx, JobType.Conformance) });
            }
        }
示例#10
0
        public IEnumerable <JobGroup> ConformanceRerunJob(string jobIdStr, string testResultIdsStr, string authorizationHeader)
        {
            Guid masterJobId   = new Guid(jobIdStr);
            var  testResultIds = testResultIdsStr.TrimEnd(';').Split(';').Select(item => int.Parse(item)).ToList();

            using (var x = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                JobType rerunType     = JobType.ConformanceRerun;
                var     validationJob = (from j in x.ExtValidationJobs where j.ID == masterJobId select j).FirstOrDefault();

                var           toUpdateTestResults = (from t in x.TestResults where testResultIds.Contains(t.ID) select t);
                List <string> ruleNameList        = (from t in toUpdateTestResults select t.RuleName).Distinct().ToList();

                string Uri = validationJob.Uri;
                if (validationJob.Complete != true)
                {
                    return(new JobGroup[]
                    {
                        new JobGroup()
                        {
                            Uri = Uri == null? "":Uri,
                            MasterJobId = Guid.Empty,
                            DerivativeJobId = Guid.Empty,
                            ResourceType = "Rerun Conformance Rules",
                            RuleCount = 0,
                            Issues = "Error: Job is not complete or somebody else is rerunning this job!",
                        }
                    });
                }

                validationJob.Complete = false;
                x.SaveChanges();

                string stringHeaders = validationJob.ReqHeaders;

                if (!string.IsNullOrEmpty(authorizationHeader))
                {
                    stringHeaders  = Regex.Replace(stringHeaders, "authorization:.*(;|$)", "", RegexOptions.IgnoreCase);
                    stringHeaders += authorizationHeader.Trim();
                }

                try
                {
                    // Try to get the necessary information for only one time.
                    RuleEngine.Common.ServiceStatus.GetInstance(Uri, stringHeaders);
                }
                catch (UnauthorizedAccessException)
                {
                    return(new JobGroup[] { new JobGroup()
                                            {
                                                Uri = Uri,
                                                MasterJobId = Guid.Empty,
                                                DerivativeJobId = Guid.Empty,
                                                ResourceType = string.Empty,
                                                RuleCount = 0,
                                                Issues = "Error: The current user is unauthorized to access the endpoint."
                                            } });
                }

                string Format   = validationJob.Format;
                string category = "conformance;" + validationJob.ServiceType + ";" + validationJob.LevelTypes;
                List <KeyValuePair <string, string> > reqHeaders = ToHeaderCollection(stringHeaders);

                ServiceContext ctx = ServiceContextFactory.Create(Uri, Format, masterJobId, MaxPayloadByteCount, reqHeaders, category);

                // make sure the target is a service doc resource
                if (ctx.PayloadType != PayloadType.ServiceDoc)
                {
                    return(new JobGroup[] { new JobGroup()
                                            {
                                                Uri = Uri,
                                                MasterJobId = Guid.Empty,
                                                DerivativeJobId = Guid.Empty,
                                                ResourceType = string.Empty,
                                                RuleCount = 0,
                                                Issues = "Error: The input is not an OData service document resource.",
                                            } });
                }
                else
                {
                    return(new JobGroup[] { CreateJobGroupItem(Format, masterJobId, ctx, rerunType, ruleNameList, false) });
                }
            }
        }