Esempio n. 1
0
        private static void AddPayloadLinesToJobInImmediateDBEnv(ExtValidationJobs job, ServiceContext ctx)
        {
            using (var x = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                var j = x.ExtValidationJobs;
                j.AddObject(job);

                if (ctx.PayloadType == PayloadType.RawValue && ctx.ContentType == PayloadFormat.Image)
                {
                    PayloadLine payloadLine = new PayloadLine();
                    payloadLine.ID         = Guid.NewGuid();
                    payloadLine.LineNumber = 1;
                    payloadLine.LineText   = "( Image data )";
                    job.PayloadLines.Add(payloadLine);
                }
                else
                {
                    PayloadLine payloadLine;
                    int         lineNumber = 0;
                    foreach (var responseLine in ctx.GetPayloadLines())
                    {
                        payloadLine            = new PayloadLine();
                        payloadLine.ID         = Guid.NewGuid();
                        payloadLine.LineNumber = ++lineNumber;
                        payloadLine.LineText   = responseLine;
                        job.PayloadLines.Add(payloadLine);
                    }
                }

                x.SaveChanges();
            }
        }
        /// <summary>
        /// Get all conformance rules from TestResult table.
        /// </summary>
        /// <param name="guid">The job id of conformance rules.</param>
        public static void GetAllConformanceLevelRules(Guid guid)
        {
            try
            {
                allRulesResult.Clear();

                using (var ctx = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
                {
                    var testResults = (from j in ctx.TestResults
                                       where guid == j.ValidationJobID
                                       orderby j.ID descending
                                       select j);

                    if (testResults != null)
                    {
                        foreach (TestResult result in testResults)
                        {
                            allRulesResult.Add(result);
                        }

                        pendingRulesResult = (from j in allRulesResult
                                              where j.Classification == "pending"
                                              select j).ToList();
                    }
                }
            }
            catch (System.Data.OptimisticConcurrencyException)
            {
                // error occurred while trying to mark operation as complete.  This is not a terminal error for this system and
                // this is on a threadpool thread so swallow the exception
            }
        }
Esempio n. 3
0
        public void Log(RuntimeException runtimeError)
        {
            if (runtimeError == null)
            {
                return;
            }

            try
            {
                using (var ctx = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
                {
                    var runtimeException = EngineRuntimeException.CreateEngineRuntimeException(
                        runtimeError.JobId,
                        runtimeError.RuleName,
                        runtimeError.Timestamp,
                        runtimeError.DestinationEndpoint,
                        0);
                    runtimeException.Message    = runtimeError.Message;
                    runtimeException.StackTrace = runtimeError.StackTrace;
                    runtimeException.Detail     = runtimeError.Detail;

                    ctx.AddToEngineRuntimeExceptions(runtimeException);
                    ctx.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                if (!RuleEngine.Common.ExceptionHelper.IsCatchableExceptionType(ex))
                {
                    throw;
                }

                // swallows the exception since logging is considered low-prio task
            }
        }
Esempio n. 4
0
        private void UpdateSimpleRerunTestResult(out List <TestResult> newTestResult)
        {
            using (var x = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                this.resultsToSave = this.resultsToSave.OrderBy(item => item.RuleName).ToList();
                var names = (from result in this.resultsToSave orderby result.RuleName select result.RuleName).ToList();

                newTestResult = (
                    from t in x.TestResults
                    where t.ValidationJobID == this.jobId && names.Contains(t.RuleName)
                    orderby t.RuleName
                    select t
                    ).ToList();

                for (int i = 0; i < newTestResult.Count; i++)
                {
                    var testResultInDB = newTestResult[i];
                    var testResultNew  = this.resultsToSave[i];
                    testResultInDB.Classification = testResultNew.Classification;
                    testResultInDB.AppliesTo      = testResultNew.AppliesTo;
                    testResultInDB.ErrorMessage   = testResultNew.ErrorMessage;
                }

                x.SaveChanges();
            }

            this.resultsToSave.Clear();
        }
Esempio n. 5
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);
        }
Esempio n. 6
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) });
                }
            }
        }
        /// <summary>
        /// Write conformance test result to database.
        /// </summary>
        /// <param name="testResultID">The updated rule's test result ID.</param>
        /// <param name="result">The result info.</param>
        /// <param name="errorMessage">The error message.</param>
        private static void WriteToTestResult(int testResultID, string result, string errorMessage)
        {
            try
            {
                var testResult = (from j in allRulesResult
                                  where j.ID == testResultID
                                  select j).FirstOrDefault();

                if (testResult != null)
                {
                    using (var ctx = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
                    {
                        var resultInTable = (from j in ctx.TestResults
                                             where j.ID == testResultID && j.ValidationJobID == testResult.ValidationJobID
                                             select j).FirstOrDefault();

                        if (resultInTable != null)
                        {
                            resultInTable.Classification = result;
                            resultInTable.ErrorMessage   = errorMessage;
                            ctx.SaveChanges();

                            var detailInTable = (from j in ctx.ResultDetails
                                                 where j.TestResultID == testResultID
                                                 select j);
                            if (detailInTable != null && detailInTable.Count() > 0)
                            {
                                detailInTable.FirstOrDefault().ErrorMessage = errorMessage;
                            }
                            else
                            {
                                ResultDetail resultDetailInDB = new ResultDetail();
                                resultDetailInDB.TestResultID       = resultInTable.ID;
                                resultDetailInDB.RuleName           = resultInTable.RuleName;
                                resultDetailInDB.URI                = "";
                                resultDetailInDB.HTTPMethod         = "";
                                resultDetailInDB.RequestHeaders     = "";
                                resultDetailInDB.RequestData        = "";
                                resultDetailInDB.ResponseStatusCode = "";
                                resultDetailInDB.ResponseHeaders    = "";
                                resultDetailInDB.ResponsePayload    = "";
                                resultDetailInDB.ErrorMessage       = errorMessage;
                                ctx.AddToResultDetails(resultDetailInDB);
                            }

                            ctx.SaveChanges();
                        }
                    }
                }
            }
            catch (System.Data.OptimisticConcurrencyException)
            {
                // error occurred while trying to mark operation as complete.  This is not a terminal error for this system and
                // this is on a thread-pool thread so swallow the exception
            }
        }
Esempio n. 8
0
        private void UpdateResultDetails(List <TestResult> newTestResult)
        {
            using (var ctx = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                foreach (var testResult in newTestResult)
                {
                    var updateExistDetails = (from t in ctx.ResultDetails
                                              where t.TestResultID == testResult.ID
                                              select t).ToList();

                    var newDetails = (from t in this.details
                                      where t.RuleName == testResult.RuleName
                                      select t).ToList();

                    int i = 0, j = 0;
                    for (; i < newDetails.Count; i++)
                    {
                        if (j < updateExistDetails.Count)
                        {
                            updateExistDetails[j].URI            = newDetails[i].URI;
                            updateExistDetails[j].HTTPMethod     = newDetails[i].HTTPMethod;
                            updateExistDetails[j].RequestHeaders = newDetails[i].RequestHeaders;
                            updateExistDetails[j].RequestData    = newDetails[i].RequestData;
                            if (string.IsNullOrEmpty(newDetails[i].ResponseStatusCode))
                            {
                                updateExistDetails[j].ResponseStatusCode = newDetails[i].ResponseStatusCode;
                            }
                            else
                            {
                                HttpStatusCode?responseStatusCode = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), newDetails[i].ResponseStatusCode);
                                int            statusCode         = Convert.ToInt32(responseStatusCode);
                                updateExistDetails[j].ResponseStatusCode = string.Format("{0},{1}", statusCode.ToString(), newDetails[i].ResponseStatusCode.ToString());
                            }
                            updateExistDetails[j].ResponseHeaders = newDetails[i].ResponseHeaders;
                            updateExistDetails[j].ResponsePayload = newDetails[i].ResponsePayload;
                            updateExistDetails[j].ErrorMessage    = newDetails[i].ErrorMessage;
                            j++;
                        }
                        else
                        {
                            AddTestResultDetailToDB(ctx, testResult.ID, newDetails[i]);
                        }
                    }
                    for (; j < updateExistDetails.Count; j++)
                    {
                        updateExistDetails[j].ErrorMessage = string.Empty;
                    }

                    ctx.SaveChanges();
                }
            }

            this.details.Clear();
        }
Esempio n. 9
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) });
            }
        }
Esempio n. 10
0
        public IEnumerable <JobGroup> Reload(string jobIdStr)
        {
            Guid jobId = new Guid(jobIdStr);

            using (var x = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                var jobs = (from j in x.JobGroups
                            where j.MasterJobId == jobId
                            select j);

                if (jobs.Count() != 0)
                {
                    return(jobs.ToArray());
                }

                var job = (from j in x.ExtValidationJobs
                           where j.ID == jobId
                           select j).FirstOrDefault();
                if (job != null)
                {
                    return(new JobGroup[]
                    {
                        new JobGroup()
                        {
                            Uri = job.Uri == null? "":job.Uri,
                            MasterJobId = job.ID,
                            DerivativeJobId = job.ID,
                            ResourceType = job.ResourceType == null? "":job.ResourceType,
                            RuleCount = job.RuleCount.Value,
                        }
                    });
                }
            }

            return(new JobGroup[]
            {
                new JobGroup()
                {
                    Uri = "Uri",
                    MasterJobId = Guid.Empty,
                    DerivativeJobId = Guid.Empty,
                    ResourceType = "ResourceType",
                    RuleCount = 0,
                    Issues = "Error: Cannot get the extension validation job information for the given job.",
                }
            });
        }
Esempio n. 11
0
        /// <summary>
        /// Logs response Http headers for the interop validation context to persistent storage
        /// </summary>
        /// <param name="job">The validation job object</param>
        /// <param name="ctx">The interop validation context</param>
        private static void LogJobRespHeaders(ExtValidationJobs job, ServiceContext ctx)
        {
            if (!string.IsNullOrEmpty(ctx.ResponseHttpHeaders))
            {
                using (var x = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
                {
                    var     j       = x.JobData;
                    JobData jobData = new JobData();
                    jobData.ID          = Guid.NewGuid();
                    jobData.RespHeaders = ctx.ResponseHttpHeaders;
                    jobData.JobID       = job.ID;

                    j.AddObject(jobData);
                    x.SaveChanges();
                }
            }
        }
Esempio n. 12
0
        /// <summary>Mark the ValidationJob complete</summary>
        /// <param name="errorOccurred">True of false if there's any Rule Engine exception</param>
        public void JobCompleted(bool errorOccurred)
        {
            using (var ctx = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
            {
                this.ProcessResultsBatchByJobCompleted(ctx, true);

                var currentJob = (from j in ctx.ExtValidationJobs
                                  where j.ID == this.jobId
                                  select j).FirstOrDefault();
                if (currentJob == null)
                {
                    return;
                }

                currentJob.ErrorOccurred = errorOccurred;
                currentJob.Complete      = true;
                currentJob.CompleteDate  = DateTime.Now;
                ctx.SaveChanges();
            }
        }
Esempio n. 13
0
 /// <summary>Set the Complete column to true for the row in ValidationJob table</summary>
 /// <param name="jobID">Guid</param>
 private static void MarkJobAsCompletedInError(Guid jobID)
 {
     try
     {
         using (var ctx = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
         {
             var job = (from j in ctx.ExtValidationJobs
                        where j.ID == jobID
                        select j).FirstOrDefault();
             if (job != null)
             {
                 job.Complete      = true;
                 job.ErrorOccurred = true;
                 ctx.SaveChanges();
             }
         }
     }
     catch (System.Data.OptimisticConcurrencyException)
     {
         // error occured while trying to mark operation as complete.  This is not a terminal error for this system and
         // this is on a threadpool thread so swallow the exception
     }
 }
Esempio n. 14
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) });
                }
            }
        }
Esempio n. 15
0
        /// <summary>Save the test results to TestResults table</summary>
        /// <param name="result">TestResult</param>
        public void Accept(RuleEngine.TestResult result)
        {
            if (result == null)
            {
                return;
            }

            TestResult testResult = new TestResult();

            testResult.ValidationJobID = this.jobId;
            testResult.RuleName        = result.RuleName;
            testResult.Description     = result.Description;

            // TODO: need ErrorMessage property on CheckResult
            testResult.ErrorMessage = result.ErrorDetail != null ? result.ErrorDetail : string.Empty;
            testResult.HelpUri      = result.HelpLink;

            testResult.SpecificationUri = "version:" + result.Version + ";";

            // TODO: need spec back in HTML form.
            if (result.SpecificationSection != null && result.V4SpecificationSection != null)
            {
                testResult.SpecificationUri += "V4SpecificationSection:" + result.V4SpecificationSection + "&SpecificationSection:" + result.SpecificationSection;
            }
            else if (result.SpecificationSection != null && result.V4SpecificationSection == null)
            {
                testResult.SpecificationUri += result.SpecificationSection;
            }
            else
            {
                testResult.SpecificationUri += result.V4SpecificationSection;
            }

            if (result.V4Specification != null)
            {
                testResult.SpecificationUri += ";V4Specification:" + result.V4Specification;
            }

            testResult.Classification    = result.Classification;
            testResult.ODataLevel        = result.Category;
            testResult.LineNumberInError = result.LineNumberInError.ToString(CultureInfo.CurrentCulture);

            this.resultsToSave.Add(testResult);

            if (result.Details != null)
            {
                foreach (ExtensionRuleResultDetail detail in result.Details)
                {
                    this.details.Add(detail.Clone());
                }
            }

            // save results to DB in batches of 5
            if (this.resultsToSave.Count >= ResultBatchSize)
            {
                using (var ctx = SuiteEntitiesUtility.GetODataValidationSuiteEntities())
                {
                    this.ProcessResultsBatchByJobCompleted(ctx, false);
                }
            }
        }