PageRenderTime 71ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/Release/02.00/src/Tests/RockBus.Routing.Tests/RouterTests.Shared.cs

#
C# | 327 lines | 255 code | 60 blank | 12 comment | 4 complexity | 59d96acb0f0567dff27aa91b30187449 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceModel;
  5. using System.ServiceModel.Channels;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Transactions;
  9. using Microsoft.VisualStudio.TestTools.UnitTesting;
  10. using Moq;
  11. using RockBus.DataContracts;
  12. using RockBus.Logging.Trace;
  13. using RockBus.Routing.Interfaces;
  14. using RockBus.Routing.MessageConverters;
  15. using RockBus.ServiceModel;
  16. using RockBus.Subscription.Interfaces;
  17. using RockBus.Threading;
  18. using RockBus.Pipeline.Interfaces;
  19. using RockBus.Utilities;
  20. using RockBus.Messages.Interfaces;
  21. using RockBus.Messages;
  22. namespace RockBus.Routing.Tests
  23. {
  24. public enum TestScenarioEnum
  25. {
  26. Success,
  27. ThrowException,
  28. SimulateException,
  29. SimulateFault,
  30. }
  31. [TestClass]
  32. public partial class RouterTests
  33. {
  34. private static Exception _testException = new Exception("Purposely thrown for testing.");
  35. private static Message _fault = Message.CreateMessage(MessageVersion.Default, FaultCode.CreateSenderFaultCode("RockBus", "http://rockbus.codeplex.com/"), "FaultReason", _testException, "http://FaultAction");
  36. private static IMessage _faultWrapper = new BufferedMessage(_fault, true);
  37. private static Message _message = RockBusAck.CreateMessage(MessageVersion.Default);
  38. private static IMessage _messageWrapper = new BufferedMessage(_message, true);
  39. // Vary/combine the following options:
  40. // - Publisher ExchangeType: OneWay, TwoWayAck, TwoWayRequestResponse
  41. // - Subscriber ExchangeType: OneWay, TwoWayAck, TwoWayRequestResponse
  42. // - Subscription Persistence: true, false
  43. // - Exceptions: Persistence, Delivery
  44. // - Responses: GenerateDefault, Advertised, RockBusAck
  45. // - Transformations: Advertisement/Subscription Request/Response
  46. // - ResponseManager:
  47. // TestMethod Name Format:
  48. // TestRouter_PublisherExchangeType_SubscriberExchangeType_SubscriberPersistent_TestScenario
  49. private void ExecuteRouter(ExchangeTypeEnum subscriberExchangeType, ExchangeTypeEnum publisherExchangeType, bool persistent, Guid correlationId, TestScenarioEnum ts, out TaskCompletionSource<Message> tcs, out Task<TaskResultList> task, out TaskStatus sts)
  50. {
  51. // Arrange
  52. var tl = new TraceLogger();
  53. var mc = new MessageConverter();
  54. var ai = this.CreateDefaultAdvertisementInfo();
  55. var ar = this.CreateDefaultAdvertisedResponse();
  56. var si = this.CreateDefaultSubscriptionInfo(persistent, subscriberExchangeType);
  57. var mw = this.CreateDefaultMessageWrapper();
  58. var tx = new CommittableTransaction();
  59. tcs = new TaskCompletionSource<Message>();
  60. var rc = this.CreateDefaultRoutingContextMock(
  61. correlationId,
  62. ai,
  63. persistent,
  64. mw,
  65. publisherExchangeType,
  66. tcs,
  67. tx);
  68. var rh = new RoutingHeader(correlationId, publisherExchangeType, si.Detail, si.EndpointInfo);
  69. var taskResult = this.CreateDefaultTaskResult(
  70. rc.Object,
  71. rh,
  72. (subscriberExchangeType == ExchangeTypeEnum.TwoWayRequestResponse) ? _messageWrapper : null);
  73. Mock<ISendPort> psp, dsp;
  74. if (persistent)
  75. {
  76. psp = this.CreateDefaultSendPortMock(ts, taskResult);
  77. dsp = this.CreateDefaultSendPortMock(ts);
  78. }
  79. else
  80. {
  81. psp = this.CreateDefaultSendPortMock(ts);
  82. dsp = this.CreateDefaultSendPortMock(ts, taskResult);
  83. }
  84. var em = this.CreateDefaultEvaluationManagerMock(ai, ar, si);
  85. var rm = this.CreateDefaultResponseManagerMock();
  86. var pm = this.CreateDefaultPipelineManagerMock();
  87. // Act
  88. Router r = new Router(
  89. tl,
  90. mc,
  91. psp.Object,
  92. dsp.Object,
  93. em.Object,
  94. rm.Object,
  95. pm.Object);
  96. task = r.RouteMessageAsync(mw.Message, tx, tcs, publisherExchangeType);
  97. sts = task.WaitForCompletionStatus();
  98. }
  99. #region IRoutingContext
  100. private IMessage CreateDefaultMessageWrapper()
  101. {
  102. return _messageWrapper;
  103. }
  104. private Mock<IRoutingContext> CreateDefaultRoutingContextMock(Guid correlationId, AdvertisementInfo ai, bool persistent, IMessage messageWrapper, ExchangeTypeEnum publisherExchangeType, TaskCompletionSource<Message> tcs, Transaction transaction)
  105. {
  106. var mock = new Mock<IRoutingContext>();
  107. mock.Setup(x => x.AdvertisementInfo).Returns(ai);
  108. mock.Setup(x => x.ContainsPersistentSubscriptions).Returns(persistent);
  109. mock.Setup(x => x.CorrelationId).Returns(correlationId);
  110. mock.Setup(x => x.MessageWrapper).Returns(messageWrapper);
  111. mock.Setup(x => x.PublisherExchangeType).Returns(publisherExchangeType);
  112. mock.Setup(x => x.RequestMessageVersion).Returns(messageWrapper.Version);
  113. mock.Setup(x => x.Task).Returns(tcs.Task);
  114. mock.Setup(x => x.Transaction).Returns(transaction);
  115. return mock;
  116. }
  117. #endregion
  118. #region ISendPort
  119. private TaskResult CreateDefaultTaskResult(IRoutingContext routingContext, RoutingHeader routingHeader, IMessage response = null)
  120. {
  121. return new TaskResult(routingContext, routingHeader, response);
  122. }
  123. private Mock<ISendPort> CreateDefaultSendPortMock(TestScenarioEnum ts, params TaskResult[] taskResults)
  124. {
  125. return CreateDefaultSendPortMock(ts, (IEnumerable<TaskResult>)taskResults);
  126. }
  127. private Mock<ISendPort> CreateDefaultSendPortMock(TestScenarioEnum ts, IEnumerable<TaskResult> taskResults)
  128. {
  129. var mock = new Mock<ISendPort>();
  130. mock.Setup(x => x.RouteMessageAsync(It.IsAny<IRoutingContext>(), It.IsAny<RoutingHeader[]>()))
  131. .Returns<IRoutingContext, RoutingHeader[]>((rc, rhs) =>
  132. {
  133. return this.ReturnDefaultTaskResultList(ts, taskResults);
  134. });
  135. mock.Setup(x => x.RouteMessageAsync(It.IsAny<IRoutingContext>(), It.IsAny<IEnumerable<RoutingHeader>>()))
  136. .Returns<IRoutingContext, IEnumerable<RoutingHeader>>((rc, rhs) =>
  137. {
  138. return this.ReturnDefaultTaskResultList(ts, taskResults);
  139. });
  140. return mock;
  141. }
  142. private Task<TaskResultList> ReturnDefaultTaskResultList(TestScenarioEnum ts, IEnumerable<TaskResult> taskResults)
  143. {
  144. TaskCompletionSource<TaskResultList> tcs = new TaskCompletionSource<TaskResultList>();
  145. var trl = new TaskResultList();
  146. switch (ts)
  147. {
  148. case TestScenarioEnum.Success:
  149. if (!taskResults.IsNullOrEmpty())
  150. {
  151. trl.AddRange(taskResults);
  152. }
  153. break;
  154. case TestScenarioEnum.ThrowException:
  155. throw _testException;
  156. case TestScenarioEnum.SimulateException:
  157. foreach (var tr in taskResults)
  158. {
  159. var mrr = new MapReduceResult<IRoutingContext, RoutingHeader, IMessage>(tr.RequestContext, tr.RoutingHeader, _testException);
  160. var exTr = new TaskResult(mrr);
  161. trl.Add(exTr);
  162. }
  163. break;
  164. case TestScenarioEnum.SimulateFault:
  165. foreach (var tr in taskResults)
  166. {
  167. tr.Response = _faultWrapper;
  168. trl.Add(tr);
  169. }
  170. break;
  171. default:
  172. break;
  173. }
  174. tcs.SetResult(trl);
  175. return tcs.Task;
  176. }
  177. #endregion
  178. #region ITransformationManager
  179. private Mock<IPipelineManager> CreateDefaultPipelineManagerMock()
  180. {
  181. var mock = new Mock<IPipelineManager>();
  182. mock.Setup(x => x.ExecuteInboundRequestPipeline(It.IsAny<IRoutingContext>(), It.IsAny<AdvertisementDetail>()))
  183. .Returns<IRoutingContext, AdvertisementDetail>((rc, ad) =>
  184. {
  185. return rc;
  186. });
  187. mock.Setup(x => x.ExecuteOutboundRequestPipeline(It.IsAny<IRoutingContext>(), It.IsAny<SubscriptionDetail>()))
  188. .Returns<IRoutingContext, SubscriptionDetail>((rc, sd) =>
  189. {
  190. return rc;
  191. });
  192. mock.Setup(x => x.ExecuteInboundResponsePipeline(It.IsAny<IMessage>(), It.IsAny<IRoutingContext>(), It.IsAny<SubscriptionDetail>()))
  193. .Returns<IMessage, IRoutingContext, SubscriptionDetail>((mw, rc, sd) =>
  194. {
  195. return mw;
  196. });
  197. mock.Setup(x => x.ExecuteOutboundResponsePipeline(It.IsAny<IMessage>(), It.IsAny<IRoutingContext>(), It.IsAny<AdvertisementDetail>()))
  198. .Returns<IMessage, IRoutingContext, AdvertisementDetail>((mw, rc, ad) =>
  199. {
  200. return mw;
  201. });
  202. return mock;
  203. }
  204. #endregion
  205. #region IResponseManager
  206. private Mock<IResponseManager> CreateDefaultResponseManagerMock()
  207. {
  208. var mock = new Mock<IResponseManager>();
  209. mock.Setup(x => x.CreateResponseHandler(It.IsAny<IRoutingContext>(), It.IsAny<AdvertisementInfo>()))
  210. .Callback<IRoutingContext, AdvertisementInfo>((rc, ai) =>
  211. {
  212. })
  213. .Returns(true);
  214. mock.Setup(x => x.HandleResult(It.IsAny<IRoutingContext>()))
  215. .Callback<IRoutingContext>((rc) =>
  216. {
  217. });
  218. return mock;
  219. }
  220. #endregion
  221. #region IEvaluationManager
  222. private Mock<IEvaluationManager> CreateDefaultEvaluationManagerMock(AdvertisementInfo ai, Message ar, SubscriptionInfo si)
  223. {
  224. var mock = new Mock<IEvaluationManager>();
  225. mock.Setup(x => x.EvaluateAdvertisements(It.IsAny<IMessage>(), It.IsAny<EvaluationResult>()))
  226. .Callback<IMessage, EvaluationResult>((mw, er) =>
  227. {
  228. er.AddAdvertisement(ai);
  229. er.AdvertisedResponse = ar;
  230. });
  231. mock.Setup(x => x.EvaluateSubscriptions(It.IsAny<IMessage>(), It.IsAny<EvaluationResult>()))
  232. .Callback<IMessage, EvaluationResult>((mw, er) =>
  233. {
  234. er.AddSubscription(si);
  235. });
  236. return mock;
  237. }
  238. private AdvertisementInfo CreateDefaultAdvertisementInfo()
  239. {
  240. var pi = new PublisherInfo("DefaultPublisher");
  241. var ad = new AdvertisementDetail(pi.Id, "DefaultAdvertisement");
  242. var ai = new AdvertisementInfo(pi, ad);
  243. ai.GenerateDefaultResponse = true;
  244. return ai;
  245. }
  246. private Message CreateDefaultAdvertisedResponse()
  247. {
  248. Message ar = RockBusAck.CreateMessage(MessageVersion.Default);
  249. return ar;
  250. }
  251. private SubscriptionInfo CreateDefaultSubscriptionInfo(bool persistent, ExchangeTypeEnum exchangeType)
  252. {
  253. var nis = new List<NamespaceInfo>();
  254. var fis = new List<FilterInfo>();
  255. var eis = new List<EndpointInfo>()
  256. {
  257. new EndpointInfo("Endpoint1", "http://FakeAddress", EndpointNameEnum.BasicHttpEndpoint, exchangeType)
  258. };
  259. var pis = new List<PipelineInfo>();
  260. var si = new SubscriberInfo("DefaultSubscriber", nis, fis, eis, pis, null, DateTime.Now);
  261. var sd = new SubscriptionDetail(si.Id, "DefaultSubscription", persistent);
  262. var spi = new SubscriptionInfo(si, sd, eis.First(), fis);
  263. return spi;
  264. }
  265. #endregion EvaluationManager
  266. }
  267. }