Beispiel #1
0
        private static bool ValidateSection(string responde, ValidationCollections validations)
        {
            var body   = ValidateResponse(responde, validations.ValidationsBody);
            var header = ValidateResponse(responde, validations.ValidationsHeader);
            var fault  = ValidateResponse(responde, validations.ValidationsFault);

            return(body && header && fault);
        }
Beispiel #2
0
        public bool ValidateSection(string response, ValidationCollections validations)
        {
            XDocument xml = null;

            try
            {
                xml = XDocument.Parse(response);
            }
            catch (Exception ex)
            {
                Console.WriteLine("La respuesta del servicio no se reconoce como un XML válido.\n{0}", ex.Message);
            }

            var body   = true;
            var header = true;
            var fault  = true;

            if (validations?.ValidationsBody?.Count > 0)
            {
                if (ExtractValue(xml, "Envelope.Body").IsExist&& !ExtractValue(xml, "Envelope.Body.Fault").IsExist)
                {
                    body = ValidateResponse(xml, validations.ValidationsBody);
                }
                else
                {
                    body = true;
                }
            }
            if (validations?.ValidationsHeader?.Count > 0)
            {
                if (ExtractValue(xml, "Envelope.Header").IsExist)
                {
                    header = ValidateResponse(xml, validations.ValidationsHeader);
                }
                else
                {
                    header = true;
                }
            }

            if (validations?.ValidationsFault?.Count == 0)
            {
                if (ExtractValue(xml, "Envelope.Body.Fault").IsExist)
                {
                    fault = false;
                }
            }
            else
            {
                if (ExtractValue(xml, "Envelope.Body.Fault").IsExist)
                {
                    fault = ValidateResponse(xml, validations.ValidationsFault);
                }
                else
                {
                    fault = true;
                }
            }

            return(body && header && fault);
        }
Beispiel #3
0
        public Config(string path, string url)
        {
            try
            {
                XDocument xml = XDocument.Load(path);

                if (xml != null)
                {
                    //Description
                    Description = xml.Elements("Config")?
                                  .Elements("Description")?
                                  .FirstOrDefault()?
                                  .Value;
                    if (string.IsNullOrWhiteSpace(Description) || Description.Trim() == "?")
                    {
                        throw new NullReferenceException("La variable 'Description' no se encuentra definida.");
                    }

                    //AutomaticSpecialTest
                    bool   SpecialTest;
                    string textoAutomaticSpecialTest = xml.Elements("Config")?
                                                       .Elements("AutomaticSpecialTest")?
                                                       .FirstOrDefault()?
                                                       .Value;
                    AutomaticSpecialTest = true;
                    if (bool.TryParse(textoAutomaticSpecialTest, out SpecialTest))
                    {
                        AutomaticSpecialTest = SpecialTest;
                    }

                    //TypeProtocol
                    TypeProtocol protocolo;
                    string       textoProtocolo = xml.Elements("Config")?
                                                  .Elements("EndPoint")?
                                                  .Elements("Type")?
                                                  .FirstOrDefault()?
                                                  .Value;
                    if (string.IsNullOrWhiteSpace(textoProtocolo))
                    {
                        Type = TypeProtocol.SOAP;
                    }
                    else if (Enum.TryParse <TypeProtocol>(textoProtocolo, true, out protocolo))
                    {
                        Type = protocolo;
                    }
                    if (Type == TypeProtocol.None)
                    {
                        throw new NullReferenceException("La variable 'Type' no se encuentra definida.");
                    }

                    //Method
                    TypeMethod metodo;
                    string     textoMetodo = xml.Elements("Config")?
                                             .Elements("EndPoint")?
                                             .Elements("Method")?
                                             .FirstOrDefault()?
                                             .Value;
                    if (string.IsNullOrWhiteSpace(textoMetodo))
                    {
                        if (Type == TypeProtocol.SOAP)
                        {
                            Method = TypeMethod.POST;
                        }
                        else
                        {
                            Method = TypeMethod.GET;
                        }
                    }
                    else if (Enum.TryParse <TypeMethod>(textoMetodo, true, out metodo))
                    {
                        Method = metodo;
                    }
                    if (Method == TypeMethod.None)
                    {
                        throw new NullReferenceException("La variable 'Method' no se encuentra definida.");
                    }


                    if (Type == TypeProtocol.REST)
                    {
                        //Parameters
                        Parameters = xml.Elements("Config")?
                                     .Elements("EndPoint")?
                                     .Elements("RESTParameters")?
                                     .FirstOrDefault()?
                                     .Value;
                    }


                    //Url
                    if (string.IsNullOrWhiteSpace(url))
                    {
                        url = xml.Elements("Config")?
                              .Elements("EndPoint")?
                              .Elements("Url")?
                              .FirstOrDefault()?
                              .Value;
                    }
                    if (string.IsNullOrWhiteSpace(url) || url.Trim() == "?")
                    {
                        throw new NullReferenceException("La variable 'Url' no se encuentra definida.");
                    }
                    Url = new Uri(url);



                    //encabezados
                    Headers = new Dictionary <string, string>();
                    var encabezados = xml.Elements("Config")?
                                      .Elements("HeaderCollection")?
                                      .Elements("Header");
                    foreach (var item in encabezados)
                    {
                        Headers.Add(item.Element("Code")?.Value, item.Element("Value")?.Value);
                    }

                    if (Type == TypeProtocol.SOAP && string.IsNullOrWhiteSpace(Headers["SOAPAction"]))
                    {
                        throw new NullReferenceException("La variable 'SOAPAction' no se encuentra definida.");
                    }
                    var enc = Headers.FirstOrDefault(x => string.IsNullOrWhiteSpace(x.Value) || x.Value.Equals("?"));
                    if (enc.Key != null)
                    {
                        throw new NullReferenceException(string.Format("La variable '{0}' no se encuentra definida correctamente.", enc.Key));
                    }

                    //validaciones
                    Validations = new ValidationCollections();
                    var validaciones = xml.Elements("Config")?
                                       .Elements("ValidationCollection").FirstOrDefault();
                    if (validaciones != null)
                    {
                        Validations.ValidationsFault  = ValidationSection("Envelope.Body.Fault", validaciones?.Elements("Envelope.Body.Fault")?.Elements("Validation"));
                        Validations.ValidationsHeader = ValidationSection("Envelope.Header", validaciones?.Elements("Envelope.Header")?.Elements("Validation"));
                        Validations.ValidationsBody   = ValidationSection("Envelope.Body", validaciones?.Elements("Envelope.Body")?.Elements("Validation"));
                        if (Validations.ValidationsBody?.Count == 0)
                        {
                            Validations.ValidationsBody = ValidationSection("Envelope.Body", validaciones?.Elements("Validation"));
                        }
                    }
                }
            }
            catch (NullReferenceException ex)
            {
                throw new NullReferenceException("El archivo .config no es correcto.", ex);
            }
            catch (Exception ex)
            {
                throw new Exception("El archivo.config no es correcto.", ex);
            }
        }