示例#1
0
        private void ExportToJson(TextEditorToolRoutedEventArgs Args)
        {
            Compile(Args);
            if (!string.IsNullOrEmpty(Args.ErrorMessage))
            {
                return;
            }

            string SaveToPath = string.Empty;

            if (!OpenFolderDialog("Select folder for saving the created Json file", ref SaveToPath))
            {
                Args.ErrorMessage = "Export To Json Aborted";
                return;
            }

            List <ProviderServiceInteraction> PSIList = ParsePACT(Args.txt);
            string ServiceConsumer   = ParseProperty(Args.txt, "Consumer");
            string HasPactWith       = ParseProperty(Args.txt, "Provider");
            ServiceVirtualization SV = new ServiceVirtualization(0, SaveToPath, ServiceConsumer, HasPactWith);

            try
            {
                // TODO: use simple json save obj to file - might be faster - but will be good to comapre with below PACT lib
                //TODO: reuse the same code port is dummy, need to create SV constructor for creating json
                foreach (ProviderServiceInteraction PSI in PSIList)
                {
                    SV.AddInteraction(PSI);
                }
                SV.PactBuilder.Build();  // will save it in C:\temp\pacts - TODO: config
                SV.MockProviderService.Stop();
                Args.SuccessMessage = "Json File Exported Successfully";
                Process.Start(SaveToPath);
            }
            catch (Exception ex)
            {
                Args.ErrorMessage = "Json File Export Failed" + Environment.NewLine + ex.Message;
                SV.MockProviderService.Stop();
            }
        }
示例#2
0
        public override void RunAction(GingerAction act)
        {
            switch (act.ID)
            {
            case cStart:
                if (SV != null)
                {
                    SV.MockProviderService.Stop();
                }
                string port = "0";
                if (!Boolean.Parse(act.GetOrCreateParam("DynamicPort").Value.ToString()))
                {
                    port = act.GetOrCreateParam("Port").GetValueAsString();
                }
                SV = new ServiceVirtualization(int.Parse(port));

                act.ExInfo = "Mock Service Started: " + SV.MockProviderServiceBaseUri;

                break;

            case cStop:
                if (SV == null)
                {
                    act.Error += "Error: Service Virtualization not started yet";
                    return;
                }

                SV.MockProviderService.Stop();
                SV          = null;
                act.ExInfo += "Mock Server stopped";
                break;

            case cLoadInteractionsFile:
                if (SV == null)
                {
                    act.Error += "Error: Service Virtualization not started yet";
                    return;
                }
                string s = act.GetOrCreateParam("FileName").GetValueAsString();
                if (s.StartsWith("~"))
                {
                    s = Path.GetFullPath(s.Replace("~", act.SolutionFolder));
                }
                if (File.Exists(s))
                {
                    int count = SV.LoadInteractions(s);
                    act.ExInfo += "Interaction file loaded: '" + s + "', " + count + " Interactions loaded";
                    act.AddOutput("Interaction Loaded", count + "");
                }
                else
                {
                    act.Error += "Interaction file not found - " + s;
                }
                break;

            case cLoadInteractionsFolder:
                if (SV == null)
                {
                    act.Error += "Error: Service Virtualization not started yet";
                    return;
                }
                break;

            case cClearInteractions:
                if (SV == null)
                {
                    act.Error += "Error: Service Virtualization not started yet";
                    return;
                }
                SV.ClearInteractions();
                act.ExInfo += "Interactions cleared";
                break;

            case cAddInteraction:
                if (SV == null)
                {
                    act.Error += "Error: Service Virtualization not started yet";
                    return;
                }

                //TODO: get it from the edit page
                ProviderServiceInteraction PSI = new ProviderServiceInteraction();
                PSI.ProviderState = act.GetOrCreateParam("ProviderState").GetValueAsString();
                PSI.Description   = act.GetOrCreateParam("Description").GetValueAsString();
                PSI.Request       = new ProviderServiceRequest();


                string HTTPType = act.GetOrCreateParam("RequestType").GetValueAsString();
                switch (HTTPType)
                {
                case "Get":
                    PSI.Request.Method = HttpVerb.Get;
                    break;

                case "Post":
                    PSI.Request.Method = HttpVerb.Post;
                    break;

                case "PUT":
                    PSI.Request.Method = HttpVerb.Put;
                    break;

                    //TODO: add all the rest and include default for err
                }

                PSI.Request.Path = act.GetOrCreateParam("Path").GetValueAsString();
                Dictionary <string, string> d = new Dictionary <string, string>();
                d.Add("Accept", "application/json");      //TODO: fixme
                PSI.Request.Headers = d;
                PSI.Response        = new ProviderServiceResponse();
                PSI.Response.Status = 200;                                //TODO: fixme
                Dictionary <string, string> r = new Dictionary <string, string>();
                r.Add("Content-Type", "application/json; charset=utf-8"); //TODO: fixme
                PSI.Response.Headers = r;
                PSI.Response.Body    = act.GetOrCreateParam("ResposneBody").GetValueAsString();

                SV.AddInteraction(PSI);

                act.ExInfo += "Interaction added";

                break;

            case cSaveInteractions:
                SV.SaveInteractions();
                act.ExInfo += "Interactions saved to file";
                // TODO: add file name to ex info
                break;

            default:
                act.Error += "Unknown PlugIn action ID - " + act.ID;
                break;
            }
        }
示例#3
0
        public void Compile()
        {
            ServiceVirtualization SV = new ServiceVirtualization();

            try
            {
                List <int> BodyLines        = new List <int>();
                List <int> InteractionLines = new List <int>();
                string     EditorText       = txt;
                ErrorLines = new List <int>();
                string[] lines = EditorText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                for (int i = 0; i < lines.Length; i++)
                {
                    string st = lines[i].TrimStart();

                    if (st.StartsWith("PACT:"))
                    {
                    }
                    else if (string.IsNullOrEmpty(st))
                    {
                    }
                    else if (st.StartsWith("@"))
                    {
                        string[] Tags = st.Split(new string[] { " " }, StringSplitOptions.None);
                        foreach (string tag in Tags)
                        {
                            if (!tag.TrimStart().StartsWith("@"))
                            {
                                ErrorLines.Add(i + 2);
                                ErrorMessage = ErrorMessage + "Line number: " + (i + 2) + "-Tags must start with @" + Environment.NewLine;
                            }
                        }
                    }
                    else if (st.StartsWith("Interaction") || st.StartsWith("Interaction Outline"))
                    {
                        InteractionLines.Add(i);
                        if (lines[i + 1].TrimStart().StartsWith("Given"))
                        {
                            if (string.IsNullOrEmpty(GetStringBetween(lines[i + 1], "\"", "\"")))
                            {
                                ErrorLines.Add(i + 2);
                                ErrorMessage = ErrorMessage + "Line number: " + (i + 2) + "-Given value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            ErrorLines.Add(i + 2);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 2) + "-Given is missing." + Environment.NewLine;
                            i            = i - 1;
                        }

                        if (lines[i + 2].TrimStart().StartsWith("Upon Receiving"))
                        {
                            if (string.IsNullOrEmpty(GetStringBetween(lines[i + 2], "\"", "\"")))
                            {
                                ErrorLines.Add(i + 3);
                                ErrorMessage = ErrorMessage + "Line number: " + (i + 3) + "-Upon Receiving value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            ErrorLines.Add(i + 3);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 3) + "-Upon Receiving is missing." + Environment.NewLine;
                            i            = i - 1;
                        }

                        if (lines[i + 3].TrimStart().StartsWith("Method"))
                        {
                            if (string.IsNullOrEmpty(GetStringBetween(lines[i + 3], "\"", "\"")))
                            {
                                ErrorLines.Add(i + 4);
                                ErrorMessage = ErrorMessage + "Line number: " + (i + 4) + "-Method value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            ErrorLines.Add(i + 4);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 4) + "-Method is missing." + Environment.NewLine;
                            i            = i - 1;
                        }

                        if (lines[i + 4].TrimStart().StartsWith("Path"))
                        {
                            if (string.IsNullOrEmpty(GetStringBetween(lines[i + 4], "\"", "\"")))
                            {
                                ErrorLines.Add(i + 5);
                                ErrorMessage = ErrorMessage + "Line number: " + (i + 5) + "-Path value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            ErrorLines.Add(i + 5);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 5) + "-Path is missing." + Environment.NewLine;
                            i            = i - 1;
                        }

                        if (lines[i + 5].TrimStart().StartsWith("Headers:"))
                        {
                            string HeadersColumns = lines[i + 6].TrimStart();
                            Dictionary <string, object> HeadersDict = GetDictBetween(HeadersColumns, "|", "|", true);
                            if (!HeadersDict.ContainsKey("Key") || HeadersDict["Key"] != "Value")
                            {
                                ErrorLines.Add(i + 7);
                                ErrorMessage = ErrorMessage + "Line number: " + (i + 7) + "-Headers Columns has to be Key and Value" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            ErrorLines.Add(i + 7);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 7) + "-Headers definition is missing." + Environment.NewLine;
                            i            = i - 1;
                        }
                        int j = i + 7;

                        while (lines[j].TrimStart().StartsWith("|"))
                        {
                            string line = lines[j].TrimStart();
                            Dictionary <string, object> ValuesDict = GetDictBetween(line, "|", "|");
                            if (ValuesDict.ContainsKey("") || ValuesDict.ContainsValue(""))
                            {
                                ErrorLines.Add(j + 1);
                                ErrorMessage = ErrorMessage + "Line number: " + (j + 1) + "-Headers Values have to be wrapped by quotes and cannot be empty" + Environment.NewLine;
                            }
                            j = j + 1;
                        }
                        i = j;
                        if (lines[i].TrimStart().StartsWith("Body"))
                        {
                            BodyLines.Add(i + 2);
                        }
                        else
                        {
                            ErrorLines.Add(i + 1);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 1) + "-Body definition is missing.";
                            i            = i - 1;
                        }

                        j = i + 1;

                        while (lines[j].TrimStart().StartsWith("{"))
                        {
                            string line = lines[j].TrimStart();
                            //TODO: Add code for validating Body
                            j = j + 1;
                        }

                        i = j;

                        if (lines[i].TrimStart().StartsWith("Will Respond With"))
                        {
                        }
                        else
                        {
                            ErrorLines.Add(i + 1);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 1) + "-Will Respond With is missing." + Environment.NewLine;
                            i            = i - 1;
                        }

                        if (lines[i + 1].TrimStart().StartsWith("Status"))
                        {
                            if (string.IsNullOrEmpty(GetStringBetween(lines[i + 1], "\"", "\"")))
                            {
                                ErrorLines.Add(i + 2);
                                ErrorMessage = ErrorMessage + "Line number: " + (i + 2) + "-Status value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            ErrorLines.Add(i + 3);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 3) + "-Status is missing." + Environment.NewLine;
                            i            = i - 1;
                        }

                        if (lines[i + 2].TrimStart().StartsWith("Headers:"))
                        {
                            string HeadersColumns = lines[i + 3].TrimStart();
                            Dictionary <string, object> HeadersDict = GetDictBetween(HeadersColumns, "|", "|", true);
                            if (!HeadersDict.ContainsKey("Key") || HeadersDict["Key"] != "Value")
                            {
                                ErrorLines.Add(i + 4);
                                ErrorMessage = ErrorMessage + "Line number: " + (i + 4) + "-Headers Columns has to be Key and Value" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            ErrorLines.Add(i + 4);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 4) + "-Headers definition is missing." + Environment.NewLine;
                        }

                        j = i + 4;

                        while (lines[j].TrimStart().StartsWith("|"))
                        {
                            string line = lines[j].TrimStart();
                            Dictionary <string, object> ValuesDict = GetDictBetween(line, "|", "|");
                            if (ValuesDict.ContainsKey("") || ValuesDict.ContainsValue(""))
                            {
                                ErrorLines.Add(j + 1);
                                ErrorMessage = ErrorMessage + "Line number: " + (j + 1) + "-Headers Values have to be wrapped by quotes and cannot be empty" + Environment.NewLine;
                            }
                            j = j + 1;
                        }
                        i = j;

                        if (lines[i].TrimStart().StartsWith("Body"))
                        {
                            BodyLines.Add(i + 2);
                        }
                        else
                        {
                            ErrorLines.Add(i + 1);
                            ErrorMessage = ErrorMessage + "Line number: " + (i + 1) + "-Body definition is missing." + Environment.NewLine;
                            i            = i - 1;
                        }

                        j = i + 1;

                        while (j < lines.Length && lines[j].TrimStart().StartsWith("{"))
                        {
                            string line = lines[j].TrimStart();
                            //TODO: Add code for validating Body
                            j = j + 1;
                        }
                        i = j;
                    }
                }

                if (!string.IsNullOrEmpty(ErrorMessage))
                {
                    SV.MockProviderService.Stop();
                    return;
                }

                List <ProviderServiceInteraction> PSIList = ParsePACT(txt);
                //TODO: reuse the same code port is dummy, need to create SV constructor for creating json
                int InteractionsIndexes = 0;
                try
                {
                    foreach (ProviderServiceInteraction PSI in PSIList)
                    {
                        SV.AddInteraction(PSI);
                        InteractionsIndexes = InteractionsIndexes + 1;
                    }
                }
                catch (Exception ex)
                {
                    ErrorLines.Add(InteractionLines[InteractionsIndexes] + 1);
                    ErrorMessage = ErrorMessage + "Failed to Build Interaction no- " + (InteractionsIndexes + 1) + Environment.NewLine;
                    throw ex;
                }

                //Validate Json Body
                int BodysIndexes = 0;
                foreach (ProviderServiceInteraction PSI in PSIList)
                {
                    try
                    {
                        KeyValuePair <string, object> KVP = new KeyValuePair <string, object>("Content-Type", "application/json");

                        if (PSI.Request.Headers.Contains(KVP))
                        {
                            JObject BodyJsonObj = null;
                            Dictionary <string, string> BodydictObj = null;
                            BodyJsonObj = JObject.Parse(PSI.Request.Body);
                            BodydictObj = BodyJsonObj.ToObject <Dictionary <string, string> >();
                        }
                        BodysIndexes = BodysIndexes + 1;
                        if (PSI.Response.Headers.Contains(KVP))
                        {
                            JObject BodyJsonObj = null;
                            Dictionary <string, string> BodydictObj = null;
                            BodyJsonObj = JObject.Parse(PSI.Response.Body);
                            BodydictObj = BodyJsonObj.ToObject <Dictionary <string, string> >();
                        }
                        BodysIndexes = BodysIndexes + 1;
                    }
                    catch (Exception)
                    {
                        ErrorLines.Add(BodyLines[BodysIndexes]);
                        ErrorMessage = ErrorMessage + "Body's Json is not in the correct Json format" + Environment.NewLine;
                    }
                }

                SV.MockProviderService.Stop();
                if (string.IsNullOrEmpty(ErrorMessage))
                {
                    MessageBox.Show("Compilation Passed Successfully");
                }
            }
            catch (Exception ex)
            {
                ErrorMessage = "Compilation Failed" + Environment.NewLine + ErrorMessage + ex.Message + Environment.NewLine + ex.InnerException;
                SV.MockProviderService.Stop();
                throw new Exception(ErrorMessage);
            }
        }