/// <summary> /// Initializes the method proxy. /// </summary> /// <param name="callDefinition"> /// The definition of the web method call. /// </param> /// <exception cref="ArgumentNullException"> /// The <paramref name="callDefinition"/> parameter is null. /// </exception> public void Initialize(WebMethodCallDefinition callDefinition) { if (callDefinition == null) throw new ArgumentNullException("callDefinition"); var proxy = WebServiceProxyAssemblyManager.GetProxyAssembly(callDefinition.ServiceDescriptionId); ContractType = TypeResolver.FindType(callDefinition.ContractTypeName); ProxyType = FindProxyType(ContractType); var endpointConfiguration = callDefinition.EndpointConfiguration; if (string.IsNullOrEmpty(endpointConfiguration)) endpointConfiguration = proxy.EndpointConfiguration; var endpoints = ServiceConfigurationImporter.GetServiceEndpoints(endpointConfiguration, typeName => proxy.Assembly.GetType(typeName)); Endpoint = endpoints.First(); if (Endpoint.Behaviors.Find<IntegrationServiceBehavior>() == null) Endpoint.Behaviors.Add(new IntegrationServiceBehavior()); if (!string.IsNullOrEmpty(callDefinition.Username)) { Username = callDefinition.Username; Password = callDefinition.Password; var basicHttpBinding = Endpoint.Binding as BasicHttpBinding; if (basicHttpBinding != null && basicHttpBinding.Security != null) { if (basicHttpBinding.Security.Mode == BasicHttpSecurityMode.None) basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; if (basicHttpBinding.Security.Transport.ClientCredentialType == HttpClientCredentialType.None) basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; } var customBinding = new CustomBinding(Endpoint.Binding); var transportElement = customBinding.Elements.OfType<HttpTransportBindingElement>().FirstOrDefault(); if (transportElement != null) { transportElement.KeepAliveEnabled = false; } Endpoint.Binding = customBinding; } Method = GetMethod(ContractType, callDefinition.MethodName); RequestType = GetRequestType(Method); }
/// <summary> /// Initializes the service caller. /// </summary> /// <param name="callDefinition">The call definition.</param> /// <exception cref="ArgumentNullException"> /// The <paramref name="callDefinition"/> parameter is null. /// </exception> public void Initialize(WebMethodCallDefinition callDefinition) { if (callDefinition == null) throw new ArgumentNullException("callDefinition"); ProcessName = callDefinition.ProcessName; ProcessDisplayName = callDefinition.ProcessDisplayName; ServiceDescriptionId = callDefinition.ServiceDescriptionId; Name = callDefinition.Name; Guid = callDefinition.Guid; MethodProxy.Initialize(callDefinition); RequestPropertySetters.Clear(); foreach (var mappingDefinition in callDefinition.ParameterMappings) { RequestPropertySetters.Add(ParameterCalculatorFactory.CreatePropertySetter(MethodProxy.RequestType, mappingDefinition)); } HeaderAttributesCalculators.Clear(); foreach (var headerMapping in callDefinition.HeaderMappings) { HeaderAttributesCalculators.Add(HeaderAttributesCalculatorFactory.CreateCalculator(MethodProxy.RequestType, headerMapping)); if (headerMapping.ValueMapping != null) RequestPropertySetters.Add(ParameterCalculatorFactory.CreatePropertySetter(MethodProxy.RequestType, headerMapping.ValueMapping)); } ResultMappings.Clear(); ResultMappings.AddRange(callDefinition.ResultMappings.Select(m => ResultMappingFactory.CreateMapping(callDefinition.ProcessName, m)).SortByExecutionOrder()); }
public void EnqueueServiceCall_InsertsScheduledCall() { const int ServiceDescriptionId = 123; const string ServiceName = "TestService"; const string SerializedRequest = "TestData"; const string SerializedOptions = "TestOptions"; var serviceGuid = new Guid("{298F9413-AB7F-4DCC-A3E0-9B5E5640FC68}"); const string ProcessName = "TestProcess"; const int ItemId = 456; var headerValueMappingDefinition = new WebMethodParameterMappingDefinition(); var headerDefinition = new WebMethodHeaderMappingDefinition { ValueMapping = headerValueMappingDefinition }; var parameterDefinition = new WebMethodParameterMappingDefinition(); var callDefinition = new WebMethodCallDefinition { ServiceDescriptionId = ServiceDescriptionId, Name = ServiceName, Guid = serviceGuid }; callDefinition.HeaderMappings.Add(headerDefinition); callDefinition.ParameterMappings.Add(parameterDefinition); var editableRoot = Mock.Create<IEditableRoot>(Behavior.Loose); Mock.Arrange(() => editableRoot.Id).Returns(ItemId); Mock.Arrange(() => editableRoot.ProcessName).Returns(ProcessName); var callContext = Mock.Create<IDataContext>(Behavior.Loose); var callContextFactory = Mock.Create<IEditableRootDataContextFactory>(Behavior.Loose); Mock.Arrange(() => callContextFactory.CreateDataContext(editableRoot)).Returns(callContext); var soapHeaderAttributes = new SoapHeaderAttributes { HeaderName = "Test Header", HeaderNamespace = "Test Namespace", Actor = "Test Actor", MustUnderstand = true, Relay = true }; var headerAttributesCalculator = Mock.Create<ISoapHeaderAttributesCalculator>(Behavior.Loose); Mock.Arrange(() => headerAttributesCalculator.GetHeaderAttributes(callContext)).Returns(soapHeaderAttributes); var headerValue = new object(); var headerSetter = Mock.Create<IPropertySetter>(Behavior.Loose); Mock.Arrange(() => headerSetter.Update(callContext, Arg.IsAny<InputMessage>())) .DoInstead<IDataContext, InputMessage>((dc, target) => target.Header = headerValue); var parameterValue = new object(); var parameterSetter = Mock.Create<IPropertySetter>(Behavior.Loose); Mock.Arrange(() => parameterSetter.Update(callContext, Arg.IsAny<InputMessage>())) .DoInstead<IDataContext, InputMessage>((dc, target) => target.Parameter = parameterValue); var mappingFactory = Mock.Create<IWebMethodParameterCalculatorFactory>(Behavior.Loose); Mock.Arrange(() => mappingFactory.CreatePropertySetter(typeof(InputMessage), headerValueMappingDefinition)).Returns(headerSetter); Mock.Arrange(() => mappingFactory.CreatePropertySetter(typeof(InputMessage), parameterDefinition)).Returns(parameterSetter); var headerMappingFactory = Mock.Create<ISoapHeaderAttributesCalculatorFactory>(Behavior.Loose); Mock.Arrange(() => headerMappingFactory.CreateCalculator(typeof(InputMessage), headerDefinition)).Returns(headerAttributesCalculator); var methodProxy = Mock.Create<IWebMethodProxy>(Behavior.Loose); Mock.Arrange(() => methodProxy.RequestType).Returns(typeof(InputMessage)); Mock.Arrange(() => methodProxy.ContractType).Returns(typeof(ITestService)); Mock.Arrange(() => methodProxy.Method).Returns(typeof(ITestService).GetMethod("TestMethod")); var requestSerializer = Mock.Create<IWebMethodRequestSerializer>(Behavior.Loose); Mock.Arrange(() => requestSerializer.SerializeRequest(Arg.IsAny<object>())).Returns(SerializedRequest); Mock.Arrange(() => requestSerializer.SerializeOptions(Arg.IsAny<WebMethodCallOptions>())).Returns(SerializedOptions); WebMethodScheduledCall scheduledCall = null; var callScheduler = Mock.Create<IServiceCallScheduler>(Behavior.Loose); Mock.Arrange(() => callScheduler.InsertScheduledCall(Arg.IsAny<ServiceScheduledCall>())) .DoInstead<ServiceScheduledCall>(sc => scheduledCall = (WebMethodScheduledCall)sc); var utils = Mock.Create<IUtils>(Behavior.Loose); var eventLogger = Mock.Create<IIntegrationEventLogger>(Behavior.Loose); var serviceCaller = new WebMethodServiceCaller { ParameterCalculatorFactory = mappingFactory, HeaderAttributesCalculatorFactory = headerMappingFactory, MethodProxy = methodProxy, EditableRootDataContextFactory = callContextFactory, RequestSerializer = requestSerializer, CallScheduler = callScheduler, Utils = utils, IntegrationEventLogger = eventLogger }; serviceCaller.Initialize(callDefinition); // Act. serviceCaller.EnqueueServiceCall(editableRoot); // Assert. Mock.Assert(() => callScheduler.InsertScheduledCall(Arg.IsAny<ServiceScheduledCall>()), Occurs.Once()); Assert.IsNotNull(scheduledCall); Assert.AreEqual(serviceGuid, scheduledCall.IntegrationServiceGuid); Assert.AreEqual(ProcessName, scheduledCall.ProcessName); Assert.AreEqual(ItemId, scheduledCall.ItemId); Assert.AreEqual(0, scheduledCall.CallCount); Assert.AreEqual(ServiceCallStatus.Scheduled, scheduledCall.Status); Assert.AreEqual(ServiceDescriptionId, scheduledCall.ServiceDescriptionId); Assert.AreEqual(typeof(ITestService).AssemblyQualifiedName, scheduledCall.ContractTypeName); Assert.AreEqual("TestMethod", scheduledCall.MethodName); Assert.AreEqual(SerializedRequest, scheduledCall.Data); Assert.AreEqual(SerializedOptions, scheduledCall.Options); }
public void ExecuteTest() { // Arrange. const int ServiceDescriptionId = 123; const string ServiceName = "TestService"; const string Username = "******"; const string ProcessDisplayName = "Test Process"; const string RequestXml = "Test Request"; const string ResponseXml = "Test Response"; var serviceGuid = new Guid("{298F9413-AB7F-4DCC-A3E0-9B5E5640FC68}"); var headerValueMappingDefinition = new WebMethodParameterMappingDefinition(); var headerDefinition = new WebMethodHeaderMappingDefinition { ValueMapping = headerValueMappingDefinition }; var parameterDefinition = new WebMethodParameterMappingDefinition(); var callDefinition = new WebMethodCallDefinition { ServiceDescriptionId = ServiceDescriptionId, Name = ServiceName, Guid = serviceGuid, ProcessDisplayName = ProcessDisplayName }; callDefinition.HeaderMappings.Add(headerDefinition); callDefinition.ParameterMappings.Add(parameterDefinition); var editableRoot = Mock.Create<IEditableRoot>(Behavior.Loose); var callContext = Mock.Create<IDataContext>(Behavior.Loose); var callContextFactory = Mock.Create<IEditableRootDataContextFactory>(Behavior.Loose); Mock.Arrange(() => callContextFactory.CreateDataContext(editableRoot)).Returns(callContext); var soapHeaderAttributes = new SoapHeaderAttributes { HeaderName = "Test Header", HeaderNamespace = "Test Namespace", Actor = "Test Actor", MustUnderstand = true, Relay = true }; var headerAttributesCalculator = Mock.Create<ISoapHeaderAttributesCalculator>(Behavior.Loose); Mock.Arrange(() => headerAttributesCalculator.GetHeaderAttributes(callContext)).Returns(soapHeaderAttributes); var headerValue = new object(); var headerSetter = Mock.Create<IPropertySetter>(Behavior.Loose); Mock.Arrange(() => headerSetter.Update(callContext, Arg.IsAny<InputMessage>())) .DoInstead<IDataContext, InputMessage>((dc, target) => target.Header = headerValue); var parameterValue = new object(); var parameterSetter = Mock.Create<IPropertySetter>(Behavior.Loose); Mock.Arrange(() => parameterSetter.Update(callContext, Arg.IsAny<InputMessage>())) .DoInstead<IDataContext, InputMessage>((dc, target) => target.Parameter = parameterValue); var mappingFactory = Mock.Create<IWebMethodParameterCalculatorFactory>(Behavior.Loose); Mock.Arrange(() => mappingFactory.CreatePropertySetter(typeof(InputMessage), headerValueMappingDefinition)).Returns(headerSetter); Mock.Arrange(() => mappingFactory.CreatePropertySetter(typeof(InputMessage), parameterDefinition)).Returns(parameterSetter); var headerMappingFactory = Mock.Create<ISoapHeaderAttributesCalculatorFactory>(Behavior.Loose); Mock.Arrange(() => headerMappingFactory.CreateCalculator(typeof(InputMessage), headerDefinition)).Returns(headerAttributesCalculator); WebMethodCallData callData = null; var methodProxy = Mock.Create<IWebMethodProxy>(); Mock.Arrange(() => methodProxy.RequestType).Returns(typeof(InputMessage)); Mock.Arrange(() => methodProxy.Invoke(Arg.IsAny<WebMethodCallData>())).DoInstead<WebMethodCallData>( data => { callData = data; if (IntegrationServiceCallScope.Current != null) { IntegrationServiceCallScope.Current.Request = RequestXml; IntegrationServiceCallScope.Current.Response = ResponseXml; } }); var utils = Mock.Create<IUtils>(Behavior.Loose); Mock.Arrange(() => utils.CurrentUserName).Returns(Username); var events = new List<IntegrationEvent>(); var eventLogger = Mock.Create<IIntegrationEventLogger>(Behavior.Loose); Mock.Arrange(() => eventLogger.Log(Arg.IsAny<IntegrationEvent>())).DoInstead<IntegrationEvent>(events.Add); var serviceCaller = new WebMethodServiceCaller { ParameterCalculatorFactory = mappingFactory, HeaderAttributesCalculatorFactory = headerMappingFactory, MethodProxy = methodProxy, EditableRootDataContextFactory = callContextFactory, Utils = utils, IntegrationEventLogger = eventLogger }; serviceCaller.Initialize(callDefinition); // Act. serviceCaller.Execute(editableRoot); // Assert. Assert.IsNotNull(callData); var request = callData.Message as InputMessage; Assert.IsNotNull(request); Assert.AreSame(headerValue, request.Header); Assert.AreSame(parameterValue, request.Parameter); Assert.AreEqual(1, callData.Options.HeaderAttributes.Count); Assert.IsTrue(callData.Options.HeaderAttributes.Contains(soapHeaderAttributes)); Mock.Assert(() => methodProxy.Invoke(Arg.IsAny<WebMethodCallData>()), Occurs.Once()); Assert.AreEqual(1, events.Count); Assert.IsTrue(events[0].IsSuccessful); Assert.AreEqual(ProcessDisplayName, events[0].Process); Assert.AreEqual(ServiceName, events[0].Service); Assert.AreEqual(Username, events[0].Username); Assert.AreEqual(RequestXml, events[0].Request); Assert.AreEqual(ResponseXml, events[0].Response); // Exceptions. TestsHelper.VerifyThrow<ArgumentNullException>(() => serviceCaller.Execute(null)); }
public void WhenExecutionFails_ServiceCallExceptionIsThrown() { // Arrange. const int ServiceDescriptionId = 123; const string ServiceName = "TestService"; const string SerializedRequest = "TestData"; const string SerializedOptions = "TestOptions"; var serviceGuid = new Guid("{298F9413-AB7F-4DCC-A3E0-9B5E5640FC68}"); const string ProcessName = "TestProcess"; const int ItemId = 456; var headerValueMappingDefinition = new WebMethodParameterMappingDefinition(); var headerDefinition = new WebMethodHeaderMappingDefinition { ValueMapping = headerValueMappingDefinition }; var parameterDefinition = new WebMethodParameterMappingDefinition(); var callDefinition = new WebMethodCallDefinition { ServiceDescriptionId = ServiceDescriptionId, Name = ServiceName, Guid = serviceGuid }; callDefinition.HeaderMappings.Add(headerDefinition); callDefinition.ParameterMappings.Add(parameterDefinition); var editableRoot = Mock.Create<IEditableRoot>(Behavior.Loose); Mock.Arrange(() => editableRoot.Id).Returns(ItemId); Mock.Arrange(() => editableRoot.ProcessName).Returns(ProcessName); var callContext = Mock.Create<IDataContext>(Behavior.Loose); var callContextFactory = Mock.Create<IEditableRootDataContextFactory>(Behavior.Loose); Mock.Arrange(() => callContextFactory.CreateDataContext(editableRoot)).Returns(callContext); var soapHeaderAttributes = new SoapHeaderAttributes { HeaderName = "Test Header", HeaderNamespace = "Test Namespace", Actor = "Test Actor", MustUnderstand = true, Relay = true }; var headerAttributesCalculator = Mock.Create<ISoapHeaderAttributesCalculator>(Behavior.Loose); Mock.Arrange(() => headerAttributesCalculator.GetHeaderAttributes(callContext)).Returns(soapHeaderAttributes); var headerValue = new object(); var headerSetter = Mock.Create<IPropertySetter>(Behavior.Loose); Mock.Arrange(() => headerSetter.Update(callContext, Arg.IsAny<InputMessage>())) .DoInstead<IDataContext, InputMessage>((dc, target) => target.Header = headerValue); var parameterValue = new object(); var parameterSetter = Mock.Create<IPropertySetter>(Behavior.Loose); Mock.Arrange(() => parameterSetter.Update(callContext, Arg.IsAny<InputMessage>())) .DoInstead<IDataContext, InputMessage>((dc, target) => target.Parameter = parameterValue); var mappingFactory = Mock.Create<IWebMethodParameterCalculatorFactory>(Behavior.Loose); Mock.Arrange(() => mappingFactory.CreatePropertySetter(typeof(InputMessage), headerValueMappingDefinition)).Returns(headerSetter); Mock.Arrange(() => mappingFactory.CreatePropertySetter(typeof(InputMessage), parameterDefinition)).Returns(parameterSetter); var headerMappingFactory = Mock.Create<ISoapHeaderAttributesCalculatorFactory>(Behavior.Loose); Mock.Arrange(() => headerMappingFactory.CreateCalculator(typeof(InputMessage), headerDefinition)).Returns(headerAttributesCalculator); var methodProxy = Mock.Create<IWebMethodProxy>(Behavior.Loose); Mock.Arrange(() => methodProxy.RequestType).Returns(typeof(InputMessage)); Mock.Arrange(() => methodProxy.Invoke(Arg.IsAny<WebMethodCallData>())).Throws(new CommunicationException()); Mock.Arrange(() => methodProxy.ContractType).Returns(typeof(ITestService)); Mock.Arrange(() => methodProxy.Method).Returns(typeof(ITestService).GetMethod("TestMethod")); var requestSerializer = Mock.Create<IWebMethodRequestSerializer>(Behavior.Loose); Mock.Arrange(() => requestSerializer.SerializeRequest(Arg.IsAny<object>())).Returns(SerializedRequest); Mock.Arrange(() => requestSerializer.SerializeOptions(Arg.IsAny<WebMethodCallOptions>())).Returns(SerializedOptions); var callScheduler = Mock.Create<IServiceCallScheduler>(Behavior.Loose); var utils = Mock.Create<IUtils>(Behavior.Loose); var eventLogger = Mock.Create<IIntegrationEventLogger>(Behavior.Loose); var serviceCaller = new WebMethodServiceCaller { ParameterCalculatorFactory = mappingFactory, HeaderAttributesCalculatorFactory = headerMappingFactory, MethodProxy = methodProxy, EditableRootDataContextFactory = callContextFactory, RequestSerializer = requestSerializer, CallScheduler = callScheduler, Utils = utils, IntegrationEventLogger = eventLogger }; serviceCaller.Initialize(callDefinition); // Act / Assert. TestsHelper.VerifyThrow<ServiceCallException>(() => serviceCaller.Execute(editableRoot)); }