Esempio n. 1
0
        private void ToolBarButtonClick(object sender, RoutedEventArgs e)
        {
            // Call the text editor event which added this button, giving him all the data in args
            // since it can be implemented also as Plugin we need to avoid passing Avalon objects

            ToolClickRoutedEventHandler   clickHandler = (ToolClickRoutedEventHandler)((Button)sender).Tag;
            TextEditorToolRoutedEventArgs args         = new TextEditorToolRoutedEventArgs();

            args.CaretLocation = textEditor.CaretOffset;
            args.txt           = textEditor.Text;
            clickHandler.Invoke(args);

            BackgroundRenderer.Segments.Clear();
            if (!string.IsNullOrEmpty(args.ErrorMessage))
            {
                Reporter.ToUser(eUserMsgKey.StaticErrorMessage, args.ErrorMessage);

                if (args.ErrorLines != null)
                {
                    AddSegments(args.ErrorLines);
                }
            }
            else if (!string.IsNullOrEmpty(args.SuccessMessage))//succ
            {
                Reporter.ToUser(eUserMsgKey.StaticInfoMessage, args.SuccessMessage);
            }
            else if (!string.IsNullOrEmpty(args.WarnMessage))//warn
            {
                Reporter.ToUser(eUserMsgKey.StaticWarnMessage, args.WarnMessage);
            }
        }
Esempio n. 2
0
        private void ExportToJava(TextEditorToolRoutedEventArgs Args)
        {
            Compile(Args);
            if (!string.IsNullOrEmpty(Args.ErrorMessage))
            {
                return;
            }

            string SaveToPath = string.Empty;

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

            string JavaTemaplate = string.Empty;

            if (MessageBox.Show("Do you want to use a default Java template?", "Java template to be used", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes) == MessageBoxResult.No)
            {
                string SelectedFilePath = string.Empty;
                if (OpenFileDialog("Select Java Temaplate File", ref SelectedFilePath))
                {
                    JavaTemaplate = System.IO.File.ReadAllText(SelectedFilePath);
                }
                else
                {
                    Args.ErrorMessage = "Export To Java Aborted";
                    return;
                }
            }
            else
            {
                JavaTemaplate = Resources.JavaTemplate;
            }

            List <ProviderServiceInteraction> PSIList = ParsePACT(Args.txt);
            string Consumer = ParseProperty(Args.txt, "Consumer");
            string Provider = ParseProperty(Args.txt, "Provider");

            GenerateJava(JavaTemaplate, PSIList, SaveToPath, Consumer, Provider, Args);
            if (string.IsNullOrEmpty(Args.ErrorMessage))
            {
                Process.Start(SaveToPath);
                Args.SuccessMessage = "Export to Java finished successfully";
            }
        }
Esempio n. 3
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();
            }
        }
Esempio n. 4
0
 private void AddTable(TextEditorToolRoutedEventArgs Args)
 {
     this.GherkinTextEditor.textEditor.SelectedText = Environment.NewLine + "Examples:" + Environment.NewLine + "|A|B|C|" + Environment.NewLine + "|1|2|3|";
 }
Esempio n. 5
0
 private void Save_Click(TextEditorToolRoutedEventArgs Args)
 {
     Optimize();
     GherkinTextEditor.Save();
 }
Esempio n. 6
0
        //TODO: fix me and improve!
        private void GenerateJava(string JavaTemaplate, List <ProviderServiceInteraction> pSIList, string SaveToPath, string Consumer, string Provider, TextEditorToolRoutedEventArgs Args)
        {
            //TODO: create java code for Junit like: https://github.com/DiUS/pact-jvm/blob/master/pact-jvm-consumer-junit/src/test/java/au/com/dius/pact/consumer/examples/ExampleJavaConsumerPactRuleTest.java
            //TODO: use external file/resource with all the text and place holder to replace

            string txt  = string.Empty;
            string tab1 = "\t";
            string tab2 = "\t";

            int i = 1;

            foreach (ProviderServiceInteraction PSI in pSIList)
            {
                txt += tab2 + "//Data setup for Interaction: <<" + PSI.ProviderState + "  /  " + PSI.Description + ">> " + Environment.NewLine;
                txt += tab1 + "Map<String, String> requestHeaders" + i + " = new HashMap<>();" + Environment.NewLine;
                foreach (var header in PSI.Request.Headers)
                {
                    txt += tab1 + "requestHeaders" + i + ".put(\"" + header.Key + "\", \"" + header.Value + "\");" + Environment.NewLine;
                }
                txt += tab1 + "Map<String, String> responseHeaders" + i + " = new HashMap<>();" + Environment.NewLine;
                foreach (var header in PSI.Response.Headers)
                {
                    txt += tab1 + "responseHeaders" + i + ".put(\"" + header.Key + "\", \"" + header.Value + "\");" + Environment.NewLine;
                }
                txt += Environment.NewLine;
                i    = i + 1;
            }

            txt += tab2 + "return builder" + Environment.NewLine;
            i    = 1;
            foreach (ProviderServiceInteraction PSI in pSIList)
            {
                txt += tab2 + "//Add Interaction: <<" + PSI.Description + ">>" + Environment.NewLine;
                txt += tab2 + ".given(\"" + PSI.ProviderState + "\")" + Environment.NewLine;
                txt += tab1 + tab2 + ".uponReceiving(\"" + PSI.Description + "\")" + Environment.NewLine;
                txt += tab1 + tab2 + ".path(\"" + PSI.Request.Path + "\")" + Environment.NewLine;
                txt += tab1 + tab2 + ".method(\"" + PSI.Request.Method.ToString() + "\")" + Environment.NewLine;
                txt += tab1 + tab2 + ".headers(requestHeaders" + i + ")" + Environment.NewLine;
                txt += tab1 + tab2 + ".body(\"" + PSI.Request.Body + "\")" + Environment.NewLine;
                txt += tab2 + ".willRespondWith()" + Environment.NewLine;
                txt += tab1 + tab2 + ".status(" + PSI.Response.Status + ")" + Environment.NewLine;   //TODO: get status convert OK...
                txt += tab1 + tab2 + ".headers(responseHeaders" + i + ")" + Environment.NewLine;
                txt += tab1 + tab2 + ".body(\"" + PSI.Response.Body + "\")" + Environment.NewLine;
                i    = i + 1;
            }

            bool IsPlaceHolderExist = JavaTemaplate.Contains(InteractionData);

            if (!IsPlaceHolderExist)
            {
                Args.ErrorMessage = Args.ErrorMessage + "Place Holder " + InteractionData + " is missing from the customized Java template." + Environment.NewLine + "Export to Java Process Aborted.";
                return;
            }

            string JavaFileContent = JavaTemaplate.Replace(InteractionData, txt);

            JavaFileContent = JavaFileContent.Replace(ProviderData, Provider);
            JavaFileContent = JavaFileContent.Replace(ConsumerData, Consumer);

            String timeStamp = DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss");

            System.IO.File.WriteAllText(SaveToPath + @"\PactToJava" + timeStamp + ".Java", JavaFileContent);
        }
Esempio n. 7
0
        private void Compile(TextEditorToolRoutedEventArgs Args)
        {
            ServiceVirtualization SV = new ServiceVirtualization();

            try
            {
                List <int> BodyLines        = new List <int>();
                List <int> InteractionLines = new List <int>();
                string     EditorText       = Args.txt;
                Args.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("@"))
                            {
                                Args.ErrorLines.Add(i + 2);
                                Args.ErrorMessage = Args.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], "\"", "\"")))
                            {
                                Args.ErrorLines.Add(i + 2);
                                Args.ErrorMessage = Args.ErrorMessage + "Line number: " + (i + 2) + "-Given value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            Args.ErrorLines.Add(i + 2);
                            Args.ErrorMessage = Args.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], "\"", "\"")))
                            {
                                Args.ErrorLines.Add(i + 3);
                                Args.ErrorMessage = Args.ErrorMessage + "Line number: " + (i + 3) + "-Upon Receiving value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            Args.ErrorLines.Add(i + 3);
                            Args.ErrorMessage = Args.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], "\"", "\"")))
                            {
                                Args.ErrorLines.Add(i + 4);
                                Args.ErrorMessage = Args.ErrorMessage + "Line number: " + (i + 4) + "-Method value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            Args.ErrorLines.Add(i + 4);
                            Args.ErrorMessage = Args.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], "\"", "\"")))
                            {
                                Args.ErrorLines.Add(i + 5);
                                Args.ErrorMessage = Args.ErrorMessage + "Line number: " + (i + 5) + "-Path value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            Args.ErrorLines.Add(i + 5);
                            Args.ErrorMessage = Args.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, string> HeadersDict = GetDictBetween(HeadersColumns, "|", "|", true);
                            if (!HeadersDict.ContainsKey("Key") || HeadersDict["Key"] != "Value")
                            {
                                Args.ErrorLines.Add(i + 7);
                                Args.ErrorMessage = Args.ErrorMessage + "Line number: " + (i + 7) + "-Headers Columns has to be Key and Value" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            Args.ErrorLines.Add(i + 7);
                            Args.ErrorMessage = Args.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, string> ValuesDict = GetDictBetween(line, "|", "|");
                            if (ValuesDict.ContainsKey("") || ValuesDict.ContainsValue(""))
                            {
                                Args.ErrorLines.Add(j + 1);
                                Args.ErrorMessage = Args.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
                        {
                            Args.ErrorLines.Add(i + 1);
                            Args.ErrorMessage = Args.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
                        {
                            Args.ErrorLines.Add(i + 1);
                            Args.ErrorMessage = Args.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], "\"", "\"")))
                            {
                                Args.ErrorLines.Add(i + 2);
                                Args.ErrorMessage = Args.ErrorMessage + "Line number: " + (i + 2) + "-Status value cannot be empty" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            Args.ErrorLines.Add(i + 3);
                            Args.ErrorMessage = Args.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, string> HeadersDict = GetDictBetween(HeadersColumns, "|", "|", true);
                            if (!HeadersDict.ContainsKey("Key") || HeadersDict["Key"] != "Value")
                            {
                                Args.ErrorLines.Add(i + 4);
                                Args.ErrorMessage = Args.ErrorMessage + "Line number: " + (i + 4) + "-Headers Columns has to be Key and Value" + Environment.NewLine;
                            }
                        }
                        else
                        {
                            Args.ErrorLines.Add(i + 4);
                            Args.ErrorMessage = Args.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, string> ValuesDict = GetDictBetween(line, "|", "|");
                            if (ValuesDict.ContainsKey("") || ValuesDict.ContainsValue(""))
                            {
                                Args.ErrorLines.Add(j + 1);
                                Args.ErrorMessage = Args.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
                        {
                            Args.ErrorLines.Add(i + 1);
                            Args.ErrorMessage = Args.ErrorMessage + "Line number: " + (i + 1) + "-Body definition is missing." + Environment.NewLine;
                            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 (!string.IsNullOrEmpty(Args.ErrorMessage))
                {
                    SV.MockProviderService.Stop();
                    return;
                }

                List <ProviderServiceInteraction> PSIList = ParsePACT(Args.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)
                {
                    Args.ErrorLines.Add(InteractionLines[InteractionsIndexes] + 1);
                    Args.ErrorMessage = Args.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, string> KVP = new KeyValuePair <string, string>("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)
                    {
                        Args.ErrorLines.Add(BodyLines[BodysIndexes]);
                        Args.ErrorMessage = Args.ErrorMessage + "Body's Json is not in the correct Json format" + Environment.NewLine;
                    }
                }

                SV.MockProviderService.Stop();
                if (string.IsNullOrEmpty(Args.ErrorMessage))
                {
                    Args.SuccessMessage = "Compilation Passed Successfully";
                }
            }
            catch (Exception ex)
            {
                Args.ErrorMessage = "Compilation Failed" + Environment.NewLine + Args.ErrorMessage + ex.Message + Environment.NewLine + ex.InnerException;
                SV.MockProviderService.Stop();
            }
        }
Esempio n. 8
0
 private void AddNewInteractionTemplate(TextEditorToolRoutedEventArgs Args)
 {
     Args.txt = Args.txt.Substring(0, Args.CaretLocation) + Environment.NewLine + Resources.NewInteractionTemplate + Environment.NewLine + Args.txt.Substring(Args.CaretLocation);
 }