Пример #1
0
        /// <summary>
        /// Generates the mock address from the provided mock settings and the endpoint name from the bindings
        /// </summary>
        /// <param name="mockSettings">The mock settings instance</param>
        /// <param name="bindingsEndpointName">The name of the endpoint as defined in the bindings file</param>
        /// <returns>A string containing the mock URL</returns>
        private string GenerateMockAddress(MockSettings mockSettings, string bindingsEndpointName)
        {
            string mockAddress = null;

            if (!string.IsNullOrEmpty(mockSettings.EndpointName))
            {
                mockAddress = string.Format(
                    CultureInfo.InvariantCulture,
                    MockAddressTemplate,
                    mockSettings.EndpointName);
            }
            else
            {
                mockAddress = string.Format(
                    CultureInfo.InvariantCulture,
                    MockAddressTemplate,
                    bindingsEndpointName);
            }

            if (!string.IsNullOrEmpty(mockSettings.Operation))
            {
                mockAddress += string.Format(
                    CultureInfo.InvariantCulture,
                    "/{0}",
                    mockSettings.Operation);
            }

            return(mockAddress);
        }
Пример #2
0
        /// <summary>
        /// Parses the send ports in the bindings for any mocking
        /// </summary>
        /// <param name="root">The XML root element containing all the send ports configurations</param>
        /// <param name="btsVersion">The version of the BizTalk server</param>
        /// <param name="unescape">A flag indicating whether to unescape the mock transport configuration</param>
        private void ParseSendPorts(XElement root, string btsVersion, bool unescape)
        {
            var sendPortsComments = root.DescendantNodes()
                                    .Where(n => n.NodeType == XmlNodeType.Comment && n.Parent.Name == "SendPort");

            foreach (var portComment in sendPortsComments)
            {
                System.Diagnostics.Debug.WriteLine("Iterating over comments");

                MockSettings mockSettings = this.ParseComment(portComment as XComment);

                if (mockSettings != null)
                {
                    // Check if the port is static
                    var  isDynamicAttribute = portComment.Parent.Attribute("IsStatic");
                    bool isStaticPort       = bool.Parse(isDynamicAttribute.Value);

                    if (isStaticPort)
                    {
                        // We fetch the adapter settings in the binding and replace those with the mock ones
                        this.ReplaceSendTransportConfiguration(
                            portComment.Parent.Element("PrimaryTransport"),
                            mockSettings,
                            btsVersion,
                            unescape);
                    }
                    else
                    {
                        // We process dynamic send ports in a different way
                        this.ProcessDynamicSendPort(
                            portComment.Parent);
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Parses an XML comment for instance of Mock tag
        /// </summary>
        /// <param name="comment">The instance of the XML comment to parse</param>
        /// <returns>An instance of the MockSettings class which represents the serialized object of the Mock tag</returns>
        private MockSettings ParseComment(XComment comment)
        {
            System.Diagnostics.Debug.WriteLine("Parsing a commented send port");
            MockSettings mockSettings = null;

            try
            {
                string commentContent = comment.Value;

                System.Diagnostics.Debug.WriteLine("Comment content is: " + commentContent);

                // Here we compare the content againse the predefined expected values
                XDocument mockSettingsDoc = XDocument.Parse(commentContent.Trim());

                // Validating the content of the Xml against the Mock schema
                XmlSchemaSet schemaSet = new XmlSchemaSet();
                schemaSet.Add(XmlSchema.Read(this.resourceReader.MockSchema, null));
                mockSettingsDoc.Validate(schemaSet, null);

                // Validation passed, we deserialize the object
                mockSettings = new MockSettings()
                {
                    EndpointName = mockSettingsDoc.Root.Attribute(XmlNodeNames.EndpointName) == null ?
                                   null : mockSettingsDoc.Root.Attribute(XmlNodeNames.EndpointName).Value,
                    Encoding = mockSettingsDoc.Root.Attribute(XmlNodeNames.Encoding) == null ?
                               null : mockSettingsDoc.Root.Attribute(XmlNodeNames.Encoding).Value,
                    Operation = mockSettingsDoc.Root.Attribute(XmlNodeNames.Operation) == null ?
                                null : mockSettingsDoc.Root.Attribute(XmlNodeNames.Operation).Value
                };

                // Parsing PromotedProperties collection
                XElement promotedPropertiesElement = mockSettingsDoc.Root.Element(XmlNodeNames.PromotedProperties);

                if (promotedPropertiesElement != null)
                {
                    var propertieElement = promotedPropertiesElement.Elements(XmlNodeNames.Property);

                    // Adding each property to the PromotedProperties collection
                    foreach (var propertyElement in propertieElement)
                    {
                        mockSettings.PromotedProperties.Add(
                            propertyElement.Attribute(XmlNodeNames.Name).Value,
                            propertyElement.Attribute("Value").Value);
                    }
                }

                System.Diagnostics.Debug.WriteLine("ParseComment succeeded. Returning a MockSettings object");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("ParseComments threw an exception: " + ex.Message);
            }

            return(mockSettings);
        }
Пример #4
0
        /// <summary>
        /// Parses the receive locations for any mocking
        /// </summary>
        /// <param name="root">The root element for the receive ports</param>
        /// <param name="btsVersion">The version of the BizTalk server</param>
        /// <param name="unescape">Indicates whether the mock transport configuration should be unescaped</param>
        private void ParseReceiveLocations(XElement root, string btsVersion, bool unescape)
        {
            var receiveLocationsWithComments = root.DescendantNodes()
                                               .Where(n => n.NodeType == XmlNodeType.Comment && n.Parent.Name == "ReceiveLocation");

            foreach (var transportComment in receiveLocationsWithComments)
            {
                System.Diagnostics.Debug.WriteLine("Iterating over comments");

                MockSettings mockSettings = this.ParseComment(transportComment as XComment);

                if (mockSettings != null)
                {
                    // We fetch the adapter settings in the binding and replace those with the mock ones
                    this.ReplaceReceiveTransportConfiguration(
                        transportComment.Parent,
                        mockSettings,
                        btsVersion,
                        unescape);
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Replaces the transport configuration for a receive location with the mock adapter transport
        /// </summary>
        /// <param name="receiveLocationElement">The XML element of the receive location</param>
        /// <param name="mockSettings">The instance of the mock adapter settings</param>
        /// <param name="btsVersion">the version of BizTalk server</param>
        /// <param name="unescape">A flag indicating whether to unescape the mock adapter transport configuration</param>
        private void ReplaceReceiveTransportConfiguration(
            XElement receiveLocationElement,
            MockSettings mockSettings,
            string btsVersion,
            bool unescape)
        {
            System.Diagnostics.Debug.WriteLine("Replacing the receive transport settings");

            // Find out if this is one or two way port
            var receivePortElement = receiveLocationElement.Parent.Parent;

            #region Setting the Address Element

            // Setting the address
            string receiveLocationName = receiveLocationElement.Attribute(XmlNodeNames.Name).Value;

            var addressElement = receiveLocationElement.Element(XmlNodeNames.Address);

            string mockAddress = this.GenerateMockAddress(mockSettings, receiveLocationName);

            addressElement.SetValue(mockAddress);

            System.Diagnostics.Debug.WriteLine("Mock address set to: " + mockAddress);
            #endregion

            #region Setting the ReceiveHandler.TransportType element
            var handlerElement = receiveLocationElement.Descendants()
                                 .Where(e => e.Name == XmlNodeNames.ReceiveHandler).First();

            // Setting the default host name
            handlerElement.Attribute("Name").Value = DefaultHostName;

            var handlerTransportTypeElement = receiveLocationElement.Descendants()
                                              .Where(e => e.Name == XmlNodeNames.TransportType && e.Parent.Name == XmlNodeNames.ReceiveHandler).First();

            handlerTransportTypeElement.SetAttributeValue(XmlNodeNames.Name, InprocTransportName);
            handlerTransportTypeElement.SetAttributeValue(XmlNodeNames.Capabilities, InprocTransportCapabilities);
            handlerTransportTypeElement.SetAttributeValue(XmlNodeNames.ConfigurationClsid, InprocTransportConfigurationClsid);

            System.Diagnostics.Debug.WriteLine("Transport type values set");
            #endregion

            #region Setting the TransportType element
            var transportTypeElement = receiveLocationElement.Element(XmlNodeNames.ReceiveLocationTransportType);

            transportTypeElement.SetAttributeValue(XmlNodeNames.Name, InprocTransportName);
            transportTypeElement.SetAttributeValue(XmlNodeNames.Capabilities, InprocTransportCapabilities);
            transportTypeElement.SetAttributeValue(XmlNodeNames.ConfigurationClsid, InprocTransportConfigurationClsid);

            System.Diagnostics.Debug.WriteLine("Transport type values for Inproc host set");
            #endregion

            bool isTwoWay = bool.Parse(receivePortElement.Attribute(XmlNodeNames.IsTwoWay).Value);

            // Adding the port name and mocked address to the dictionary
            this.mockedEndpointSettings.Add(receiveLocationName,
                                            new EndpointSettings
            {
                Address   = mockAddress,
                IsTwoWay  = isTwoWay,
                Direction = EndpointDirection.Receive
            });

            // Navigate to the element containing the transport configuration
            var transportInfo = receiveLocationElement.Element(XmlNodeNames.ReceiveLocationTransportTypeData);

            // Preparing the proper contents of the transport data element for the mocked transport
            string mockTransportData = null;
            if (isTwoWay)
            {
                mockTransportData = this.resourceReader.GetMockTransportConfig(
                    btsVersion,
                    ReceiveLocationTwoWayMockTransportKey);

                mockTransportData = mockTransportData.Replace(
                    "{Encoding}",
                    mockSettings.Encoding ?? "UTF-8");
            }
            else
            {
                mockTransportData = this.resourceReader.GetMockTransportConfig(
                    btsVersion,
                    ReceiveLocationOneWayMockTransportKey);

                mockTransportData = mockTransportData.Replace(
                    "{Encoding}",
                    mockSettings.Encoding ?? "UTF-8");
            }

            // Preparing the list of promoted properties if such are defined
            if (mockSettings.PromotedProperties.Count > 0)
            {
                string promotedPropertiesList = null;

                foreach (var promotedProperty in mockSettings.PromotedProperties)
                {
                    promotedPropertiesList += string.Format(
                        CultureInfo.InvariantCulture,
                        "{0}={1};",
                        promotedProperty.Key,
                        promotedProperty.Value);
                }

                mockTransportData = mockTransportData.Replace(
                    "{PromotedProperties}",
                    promotedPropertiesList);
            }
            else
            {
                mockTransportData = mockTransportData.Replace(
                    "{PromotedProperties}",
                    string.Empty);
            }

            // Parse the original transport info and extract any custom service behaviors
            string customServiceBehaviors = this.ExtractCustomServiceBehaviors(transportInfo.Value);

            // Place any existing custom servince behaviors in the mocked transport data, otherwise place the default behaviors
            mockTransportData = mockTransportData.Replace(
                "{ServiceBehavior}",
                !string.IsNullOrEmpty(customServiceBehaviors) ? customServiceBehaviors : DefaultServiceBehaviorConfig);

            // Parse the original transport info and extract any custom service behaviors
            string customEndpointBehaviors = this.ExtractCustomEndpointBehaviors(transportInfo.Value);

            // Place any existing custom endpoint behaviors in the mocked transport data, otherwise place the default behaviors
            mockTransportData = mockTransportData.Replace(
                "{EndpointBehavior}",
                !string.IsNullOrEmpty(customEndpointBehaviors) ? customEndpointBehaviors : DefaultEndpointBehaviorConfig);

            // In case unescaping was specified as an option
            if (unescape)
            {
                mockTransportData = UnescapeTransportConfig(mockTransportData);
            }

            // Finally replace the current transport config with the mocked ones
            transportInfo.Value = mockTransportData;
        }
Пример #6
0
        /// <summary>
        /// Replaces the transport configuration for a send port with the mock adapter configuration
        /// </summary>
        /// <param name="transportElement">The XML element of the send port transport configuration</param>
        /// <param name="mockSettings">The mock settings object</param>
        /// <param name="btsVersion">The version of BizTalk server</param>
        /// <param name="unescape">A flag indicating whether to unescape the mock transport configuration</param>
        private void ReplaceSendTransportConfiguration(
            XElement transportElement,
            MockSettings mockSettings,
            string btsVersion,
            bool unescape)
        {
            System.Diagnostics.Debug.WriteLine("Replacing the transport settings");

            // Find out if this is one or two way port
            var sendPortElement = transportElement.Parent;

            #region Setting the Address Element

            // Setting the address
            string sendPortName = sendPortElement.Attribute(XmlNodeNames.Name).Value;

            var addressElement = transportElement.Element(XmlNodeNames.Address);

            string mockAddress = this.GenerateMockAddress(mockSettings, sendPortName);

            addressElement.SetValue(mockAddress);

            System.Diagnostics.Debug.WriteLine("Mock address set to: " + mockAddress);
            #endregion

            #region Setting the TransportType element
            var transportTypeElement = transportElement.Element(XmlNodeNames.TransportType);

            transportTypeElement.SetAttributeValue(XmlNodeNames.Name, InprocTransportName);
            transportTypeElement.SetAttributeValue(XmlNodeNames.Capabilities, InprocTransportCapabilities);
            transportTypeElement.SetAttributeValue(XmlNodeNames.ConfigurationClsid, InprocTransportConfigurationClsid);

            System.Diagnostics.Debug.WriteLine("Transport type values set");
            #endregion

            #region Setting the SendHandler.TransportType element
            var sendHandlerElement = transportElement.Element(XmlNodeNames.SendHandler);

            // Set the host name
            sendHandlerElement.Attribute("Name").Value = DefaultHostName;

            var handlerTransportTypeElement = sendHandlerElement
                                              .Element(XmlNodeNames.TransportType);

            handlerTransportTypeElement.SetAttributeValue(XmlNodeNames.Name, InprocTransportName);
            handlerTransportTypeElement.SetAttributeValue(XmlNodeNames.Capabilities, InprocTransportCapabilities);
            handlerTransportTypeElement.SetAttributeValue(XmlNodeNames.ConfigurationClsid, InprocTransportConfigurationClsid);

            System.Diagnostics.Debug.WriteLine("Transport type values set");
            #endregion

            bool isTwoWay = bool.Parse(sendPortElement.Attribute(XmlNodeNames.IsTwoWay).Value);

            // Adding the port name and mocked address to the dictionary
            this.mockedEndpointSettings.Add(sendPortName,
                                            new EndpointSettings {
                Address   = mockAddress,
                IsTwoWay  = isTwoWay,
                Direction = EndpointDirection.Send
            });

            // We navigate to the element containing the transport configuration
            var transportInfo = transportElement.Element(XmlNodeNames.TransportTypeData);

            string mockTransportData = null;
            if (isTwoWay)
            {
                mockTransportData = this.resourceReader.GetMockTransportConfig(
                    btsVersion,
                    SendPortTwoWayMockTransportKey);

                mockTransportData = mockTransportData.Replace(
                    "{Encoding}",
                    mockSettings.Encoding ?? "UTF-8");
            }
            else
            {
                mockTransportData = this.resourceReader.GetMockTransportConfig(
                    btsVersion,
                    SendPortOneWayMockTransportKey);

                mockTransportData = mockTransportData.Replace(
                    "{Encoding}",
                    mockSettings.Encoding ?? "UTF-8");
            }

            // Parse the original transport info and extract any custom service behaviors
            string customEndpointBehaviors = this.ExtractCustomEndpointBehaviors(transportInfo.Value);

            // Place any existing custom endpoint behaviors in the mocked transport data, otherwise place the default behaviors
            mockTransportData = mockTransportData.Replace(
                "{EndpointBehavior}",
                !string.IsNullOrEmpty(customEndpointBehaviors) ? customEndpointBehaviors : DefaultEndpointBehaviorConfig);

            // In case unescaping was defined as an option
            if (unescape)
            {
                mockTransportData = UnescapeTransportConfig(mockTransportData);
            }

            // Finally replace the current transport config with the mocked ones
            transportInfo.Value = mockTransportData;
        }