Пример #1
0
        public static void LoadTest(string loadTestFilePath = null)
        {
            if (string.IsNullOrEmpty(loadTestFilePath))
            {
                loadTestFilePath = Environment.CurrentDirectory + "\\" + ConfigurationManager.AppSettings["loadTestFile"];
            }
            _testConfigurationFile = loadTestFilePath;

            var sr = new StreamReader(_testConfigurationFile);
            var testXml = sr.ReadToEnd();
            sr.Close();

            TestPackage = new Package();

            var xDoc = XDocument.Parse(testXml);

            if (xDoc.Root != null)
            {
                TestPackage.ResultFileName = Convert.ToString(xDoc.Root.Attribute("resultFileName").Value);

                if (xDoc.Root.Attributes("intervalBetweenScenarios").Any())
                {
                    TestPackage.IntervalBetweenScenarios =
                        Convert.ToInt32(xDoc.Root.Attribute("intervalBetweenScenarios").Value);
                }

                TestPackage.Duration = Convert.ToInt32(xDoc.Root.Attribute("duration").Value);
                TestPackage.Clients = Convert.ToInt32(xDoc.Root.Attribute("clients").Value);

                TestPackage.DelayRangeStart = Convert.ToInt32(xDoc.Root.Attribute("delayRangeStart").Value);
                TestPackage.DelayRangeEnd = Convert.ToInt32(xDoc.Root.Attribute("delayRangeEnd").Value);

                TestPackage.ResultFileName = Convert.ToString(xDoc.Root.Attribute("resultFileName").Value);
                if (string.IsNullOrEmpty(TestPackage.ResultFileName))
                {
                    TestPackage.ResultFileName = "PerfResults.txt";
                }

                if (File.Exists(TestPackage.ResultFileName))
                {
                    File.Delete(TestPackage.ResultFileName);
                }

                if (xDoc.Root.Elements("nodes").Any())
                {
                    TestPackage.Nodes = new Nodes { NodeList = new List<string>() };

                    XElement nsE = xDoc.Root.Elements("nodes").ElementAt(0);
                    if (nsE.Attributes("noOfClientsPerNode").Any())
                    {
                        TestPackage.Nodes.NoOfClientsPerNode = Convert.ToInt32(nsE.Attribute("noOfClientsPerNode").Value);
                    }
                    foreach (XElement nE in nsE.Elements("node"))
                    {
                        TestPackage.Nodes.NodeList.Add(nE.Attribute("name").Value);
                    }
                }

                if (xDoc.Root.Elements("scenarios").Any())
                {
                    TestPackage.Scenarios = new List<Scenario>();

                    XElement scenariosE = xDoc.Root.Elements("scenarios").ElementAt(0);
                    foreach (XElement scenarioE in scenariosE.Elements("scenario"))
                    {
                        Scenario scen = new Scenario();
                        foreach (XElement sOrderE in scenarioE.Elements("order"))
                        {
                            ScenarioOrder sorder = new ScenarioOrder
                            {
                                MethodName = Convert.ToString(sOrderE.Attribute("methodName").Value),
                                Order = Convert.ToInt32(sOrderE.Attribute("order").Value),
                                MethodGuid =
                                    sOrderE.Attributes("methodGuid").Any()
                                        ? Convert.ToString(sOrderE.Attribute("methodGuid").Value)
                                        : string.Empty,
                                AssemblyGuid = sOrderE.Attributes("assemblyGuid").Any()
                                        ? Convert.ToString(sOrderE.Attribute("assemblyGuid").Value)
                                        : string.Empty,
                                IsRest = sOrderE.Attributes("isRest").Any() && Convert.ToBoolean(sOrderE.Attribute("isRest").Value)
                            };
                            scen.ScenarioOrder.Add(sorder);
                        }
                        TestPackage.Scenarios.Add(scen);
                    }
                }

                foreach (var suite in xDoc.Root.Elements("testSuite"))
                {
                    var newSuite = new TestSuite();
                    TestPackage.Suites.Add(newSuite);

                    newSuite.Guid = Convert.ToString(suite.Attribute("__guid__").Value);
                    newSuite.ServiceUrl = Convert.ToString(suite.Attribute("serviceUrl").Value);
                    newSuite.BaseUrl = newSuite.ServiceUrl;
                    newSuite.Wsdl = newSuite.ServiceUrl + "?wsdl";

                    if (suite.Attributes("bindingToTest").Any())
                        newSuite.BindingToTest = Convert.ToString(suite.Attribute("bindingToTest").Value);

                    if (suite.Attributes("configuration").Any())
                        newSuite.Configuration = Convert.ToString(suite.Attribute("configuration").Value);

                    foreach (XElement test in suite.Elements("test"))
                    {
                        LoadServiceElement(test, newSuite.Tests, false);
                    }

                    foreach (XElement test in suite.Elements("functionalTest"))
                    {
                        LoadServiceElement(test, newSuite.FunctionalTests, true);
                    }
                    GenerateProxyAssembly(newSuite.Wsdl, newSuite.Guid);
                }

                #region rest api
                foreach (var restNodeX in xDoc.Root.Elements("restApis"))
                {
                    foreach (XElement restMethodX in restNodeX.Elements("restMethod"))
                    {
                        RestMethod restMethod = new RestMethod()
                        {
                            Guid = Convert.ToString(restMethodX.Attribute("__guid__").Value),
                            Url = Convert.ToString(restMethodX.Attribute("url").Value),
                            SelectedHeaderTab = Convert.ToInt32(restMethodX.Attribute("selectedHeaderTab").Value),
                            SelectedPayloadTab = Convert.ToInt32(restMethodX.Attribute("selectedPayloadTab").Value),
                            Headers = (List<KeyValue>)TestHelper.Deserialize(restMethodX.Attribute("headers").Value, typeof(List<KeyValue>)),
                            HeaderText = Convert.ToString(restMethodX.Attribute("headerText").Value),
                            PayloadValues = (List<KeyValue>)TestHelper.Deserialize(restMethodX.Attribute("payload").Value, typeof(List<KeyValue>)),
                            PayloadText = Convert.ToString(restMethodX.Attribute("payloadText").Value),
                            ContentType = Convert.ToString(restMethodX.Attribute("contentType").Value),
                            Type = (RequestType)Enum.Parse(typeof(RequestType), Convert.ToString(restMethodX.Attribute("type").Value)),
                            IsAddedToFunctional = restMethodX.Attributes("isAddedToFunctional").Any() && Convert.ToBoolean(restMethodX.Attribute("isAddedToFunctional").Value),
                            MethodOutput = restMethodX.Attributes("methodOutput").Any() ?
                                Convert.ToString(restMethodX.Attribute("methodOutput").Value)
                                : string.Empty

                        };

                        TestPackage.RestMethods.Add(restMethod);
                    }
                }
                #endregion
            }
        }
Пример #2
0
        public static void GenerateProxyAssembly(string url, string guid, bool fromUi = false)
        {
            TestSuite suite = TestPackage.Suites.Find(s => s.Guid == guid);

            if (suite == null && fromUi)
            {
                suite = new TestSuite()
                {
                    ServiceUrl = url,
                    BaseUrl = url,
                    Wsdl = url + "?wsdl",
                    Guid = guid
                };
                TestPackage.Suites.Add(suite);
                url = suite.Wsdl;
            }

            StreamReader lResponseStream = GetHttpWebResponse(url);

            XmlTextReader xmlreader = new XmlTextReader(lResponseStream);

            //read the downloaded WSDL file
            ServiceDescription desc = ServiceDescription.Read(xmlreader);

            MetadataSection section = MetadataSection.CreateFromServiceDescription(desc);
            MetadataSet metaDocs = new MetadataSet(new[] { section });
            WsdlImporter wsdlimporter = new WsdlImporter(metaDocs);

            //Add any imported files
            foreach (XmlSchema wsdlSchema in desc.Types.Schemas)
            {
                foreach (XmlSchemaObject externalSchema in wsdlSchema.Includes)
                {
                    var import = externalSchema as XmlSchemaImport;
                    if (import != null)
                    {
                        if (suite != null)
                        {
                            Uri baseUri = new Uri(suite.BaseUrl);
                            if (string.IsNullOrEmpty(import.SchemaLocation)) continue;
                            Uri schemaUri = new Uri(baseUri, import.SchemaLocation);
                            StreamReader sr = GetHttpWebResponse(schemaUri.ToString());
                            XmlSchema schema = XmlSchema.Read(sr, null);
                            wsdlimporter.XmlSchemas.Add(schema);
                        }
                    }
                }
            }

            //Additional check in case some services do not generate end points using generator
            for (int w = 0; w < wsdlimporter.WsdlDocuments.Count; w++)
            {
                for (int se = 0; se < wsdlimporter.WsdlDocuments[w].Services.Count; se++)
                {
                    for (int po = 0; po < wsdlimporter.WsdlDocuments[w].Services[se].Ports.Count; po++)
                    {
                        // ReSharper disable once ForCanBeConvertedToForeach
                        for (int ext = 0; ext < wsdlimporter.WsdlDocuments[w].Services[se].Ports[po].Extensions.Count; ext++)
                        {

                            switch (wsdlimporter.WsdlDocuments[w].Services[se].Ports[po].Extensions[ext].GetType().Name)
                            {
                                //BasicHttpBinding
                                case "SoapAddressBinding":
                                    _endPointUrls.Add(((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name, ((SoapAddressBinding)(wsdlimporter.WsdlDocuments[w].Services[se].Ports[po].Extensions[ext])).Location);
                                    if (suite != null &&
                                        !suite.EndPoints.ContainsKey(
                                            ((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name))
                                    {
                                        suite.EndPoints.Add(
                                            ((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name,
                                            ((SoapAddressBinding)
                                                (wsdlimporter.WsdlDocuments[w].Services[se].Ports[po].Extensions[ext]))
                                                .Location);
                                        suite.EndPointType.Add(((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name,
                                            "BasicHttpBinding");
                                    }
                                    break;
                                //WSHttpBinding
                                case "Soap12AddressBinding":
                                    _endPointUrls.Add(((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name, ((SoapAddressBinding)(wsdlimporter.WsdlDocuments[w].Services[se].Ports[po].Extensions[ext])).Location);
                                    if (suite != null &&
                                        !suite.EndPoints.ContainsKey(
                                            ((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name))
                                    {
                                        if (((SoapAddressBinding)
                                            (wsdlimporter.WsdlDocuments[w].Services[se].Ports[po].Extensions[ext]))
                                            .Location.ToLower().StartsWith("net.tcp"))
                                        {
                                            suite.EndPoints.Add(
                                               ((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name,
                                               ((SoapAddressBinding)
                                                   (wsdlimporter.WsdlDocuments[w].Services[se].Ports[po].Extensions[ext
                                                       ]))
                                                   .Location);
                                            suite.EndPointType.Add(
                                                ((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name,
                                                "NetTcpBinding");
                                        }
                                        else
                                        {
                                            suite.EndPoints.Add(
                                                ((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name,
                                                ((SoapAddressBinding)
                                                    (wsdlimporter.WsdlDocuments[w].Services[se].Ports[po].Extensions[ext
                                                        ]))
                                                    .Location);
                                            suite.EndPointType.Add(
                                                ((wsdlimporter.WsdlDocuments[w].Services[se].Ports[po])).Binding.Name,
                                                "WSHttpBinding");
                                        }
                                    }
                                    break;
                                case "XmlElement":
                                    break;
                            }
                        }
                    }
                }
            }

            foreach (Import import in wsdlimporter.WsdlDocuments[0].Imports)
            {
                GenerateProxyAssembly(import.Location, guid);
                return;
            }

            XsdDataContractImporter xsd = new XsdDataContractImporter
            {
                Options = new ImportOptions
                {
                    ImportXmlType = true,
                    GenerateSerializable = true
                }
            };
            xsd.Options.ReferencedTypes.Add(typeof(KeyValuePair<string, string>));
            xsd.Options.ReferencedTypes.Add(typeof(List<KeyValuePair<string, string>>));

            wsdlimporter.State.Add(typeof(XsdDataContractImporter), xsd);

            Collection<ContractDescription> contracts = wsdlimporter.ImportAllContracts();
            ServiceEndpointCollection allEndpoints = wsdlimporter.ImportAllEndpoints();
            // Generate type information for each contract.
            ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator();

            foreach (var contract in contracts)
            {
                serviceContractGenerator.GenerateServiceContractType(contract);
                // Keep a list of each contract's endpoints.
                if (suite != null)
                {
                    if (suite.EndpointsForContracts == null)
                        suite.EndpointsForContracts = new Dictionary<string, List<EndpointsForContractsClass>>();
                    suite.EndpointsForContracts[contract.Name] =
                        allEndpoints.Where(ep => ep.Contract.Name == contract.Name)
                            .Select(ep => new EndpointsForContractsClass()
                            {
                                Uri = ep.Address.Uri.ToString(),
                                Binding = ep.Binding
                            }).ToList();
                }
            }

            if (serviceContractGenerator.Errors.Count != 0)
                throw new Exception("There were errors during code compilation.");

            //Initialize the CODE DOM tree in which we will import the ServiceDescriptionImporter
            CodeNamespace nm = new CodeNamespace();
            CodeCompileUnit unit = new CodeCompileUnit();
            unit.Namespaces.Add(nm);

            CodeDomProvider compiler = CodeDomProvider.CreateProvider("C#");
            // include the assembly references needed to compile
            var references = new[] { "System.Web.Services.dll", "System.Xml.dll", "System.ServiceModel.dll", "System.configuration.dll", "System.Runtime.Serialization.dll" };
            var parameters = new CompilerParameters(references) { GenerateInMemory = true };
            var results = compiler.CompileAssemblyFromDom(parameters, serviceContractGenerator.TargetCompileUnit);

            if (results.Errors.Cast<CompilerError>().Any())
            {
                throw new Exception("Compilation Error Creating Assembly");
            }

            // all done....
            if (_assembly.ContainsKey(guid))
                _assembly[guid] = results.CompiledAssembly;
            else
                _assembly.Add(guid, results.CompiledAssembly);

            if (_serviceInterface.ContainsKey(guid))
                _serviceInterface[guid] = _assembly[guid].GetTypes()[0];
            else
                _serviceInterface.Add(guid, _assembly[guid].GetTypes()[0]);
        }