private static RestResult CreateRestResultWhenSuccessful <T>(DispatcherResponse <T> data, object value, HttpStatusCode statusCode) where T : BaseResponseType
        {
            var warnings    = Enumerable.Empty <ValidationOutcome>();
            var infos       = Enumerable.Empty <ValidationOutcome>();
            var errors      = Enumerable.Empty <ValidationOutcome>();
            var ruleResults = data.Data.RuleResults;

            if (ruleResults != null)
            {
                warnings = ruleResults
                           .Where(rl => rl.ValidationOutcome.OutcomeStatus.Equals(BusinessRule.OutcomeStatus.Warning.ToString()))
                           .Select(p => p.ValidationOutcome).ToList();
                infos = ruleResults
                        .Where(rl => rl.ValidationOutcome.OutcomeStatus.Equals(BusinessRule.OutcomeStatus.Info.ToString()))
                        .Select(p => p.ValidationOutcome).ToList();

                errors = ruleResults
                         .Where(rl => rl.ValidationOutcome.OutcomeStatus.Equals(BusinessRule.OutcomeStatus.Error.ToString()))
                         .Select(p => p.ValidationOutcome).ToList();
            }

            var restContent = new RestContent(value,
                                              errors.Any() ? errors : null, warnings.Any() ? warnings : null, infos.Any() ? infos : null);

            return(new RestResult(restContent, statusCode));
        }
        public override void Run(DebugAdapterRunner runner)
        {
            // Send the request
            string request = CreateDispatcherRequest(runner);

            // VSCode doesn't send /n at the end. If this is writeline, then concord hangs
            runner.DebugAdapter.StandardInput.Write(request);

            // Process + validate responses
            List <object> responseList = new List <object>();
            int           currentExpectedResponseIndex = 0;

            // Loop until we have received as many expected responses as expected
            while (currentExpectedResponseIndex < this.ExpectedResponses.Count)
            {
                string    receivedMessage      = null;
                Exception getMessageExeception = null;
                try
                {
                    receivedMessage = this.GetMessage(
                        runner.DebugAdapter.StandardOutput.BaseStream,
                        runner.ResponseTimeout,
                        runner.DebugAdapter,
                        runner);
                }
                catch (Exception e)
                {
                    getMessageExeception = e;
                }

                if (getMessageExeception != null)
                {
                    if (!runner.DebugAdapter.HasExited)
                    {
                        // If it hasn't exited yet, wait a little bit longer to make sure it isn't just about to exit
                        try
                        {
                            runner.DebugAdapter.WaitForExit(500);
                        }
                        catch
                        { }
                    }

                    string messageStart;
                    if (runner.DebugAdapter.HasExited)
                    {
                        if (runner.HasAsserted())
                        {
                            messageStart = string.Format(CultureInfo.CurrentCulture, "The debugger process has asserted and exited with code '{0}' without sending all expected responses. See test log for assert details.", runner.DebugAdapter.ExitCode);
                        }
                        else
                        {
                            messageStart = string.Format(CultureInfo.CurrentCulture, "The debugger process has exited with code '{0}' without sending all expected responses.", runner.DebugAdapter.ExitCode);
                        }
                    }
                    else if (getMessageExeception is TimeoutException)
                    {
                        if (runner.HasAsserted())
                        {
                            messageStart = "The debugger process has asserted. See test log for assert details.";
                        }
                        else
                        {
                            messageStart = "Expected response not found before timeout.";
                        }
                    }
                    else
                    {
                        messageStart = "Exception while reading message from debug adpter. " + getMessageExeception.Message;
                    }

                    string expectedResponseText = JsonConvert.SerializeObject(this.ExpectedResponses[currentExpectedResponseIndex].Response);
                    string actualResponseText   = string.Empty;

                    for (int i = 0; i < responseList.Count; i++)
                    {
                        actualResponseText += string.Format(CultureInfo.CurrentCulture, "{0}. {1}\n", (i + 1), JsonConvert.SerializeObject(responseList[i]));
                    }

                    string errorMessage = string.Format(CultureInfo.CurrentCulture, "{0}\nExpected = {1}\nActual Responses =\n{2}",
                                                        messageStart, expectedResponseText, actualResponseText);

                    throw new DARException(errorMessage);
                }

                try
                {
                    DispatcherMessage dispatcherMessage = JsonConvert.DeserializeObject <DispatcherMessage>(receivedMessage);

                    if (dispatcherMessage.type == "event")
                    {
                        DispatcherEvent dispatcherEvent = JsonConvert.DeserializeObject <DispatcherEvent>(receivedMessage);
                        responseList.Add(dispatcherEvent);

                        if (dispatcherEvent.eventType == "stopped")
                        {
                            runner.CurrentThreadId = dispatcherEvent.body.threadId;
                        }

                        var expected = this.ExpectedResponses[currentExpectedResponseIndex];
                        if (Utils.CompareObjects(expected.Response, dispatcherEvent, expected.IgnoreOrder))
                        {
                            expected.Match = dispatcherEvent;
                            currentExpectedResponseIndex++;
                        }
                    }
                    else if (dispatcherMessage.type == "response")
                    {
                        DispatcherResponse dispatcherResponse = JsonConvert.DeserializeObject <DispatcherResponse>(receivedMessage);
                        responseList.Add(dispatcherResponse);

                        var expected = this.ExpectedResponses[currentExpectedResponseIndex];
                        if (Utils.CompareObjects(expected.Response, dispatcherResponse, expected.IgnoreOrder))
                        {
                            expected.Match = dispatcherResponse;
                            currentExpectedResponseIndex++;
                        }
                    }
                    else if (dispatcherMessage.type == "request")
                    {
                        runner.HandleCallbackRequest(receivedMessage);
                    }
                    else
                    {
                        throw new DARException(String.Format(CultureInfo.CurrentCulture, "Unknown Dispatcher Message type: '{0}'", dispatcherMessage.type));
                    }
                }
                catch (JsonReaderException)
                {
                    runner.AppendLineToDebugAdapterOutput("Response could not be parsed as json. This was the response:");
                    runner.AppendLineToDebugAdapterOutput(receivedMessage);
                    throw;
                }
            }
        }