public void ShouldExecuteTheCorrectBaseMessageHandlers() { // Arrange var messageProcessor = new MessageHandlerProcessor(_mockContainer.Object); var message1HandlerReference = new HandlerReference { HandlerType = typeof(FakeBaseMessageHandler1), MessageType = typeof(FakeBaseMessage1) }; _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler<FakeBaseMessage1>))).Returns(new List<HandlerReference> { message1HandlerReference }); var fakeHandler = new FakeBaseMessageHandler1(); _mockContainer.Setup(x => x.GetInstance(typeof(FakeBaseMessageHandler1))).Returns(fakeHandler); // Act var message = new FakeDerivedMessage1(Guid.NewGuid()) { Status = "Test" }; messageProcessor.ProcessMessage<FakeDerivedMessage1>(JsonConvert.SerializeObject(message), null); // Assert Assert.Equal(message.CorrelationId, fakeHandler.Command.CorrelationId); Assert.Equal(message.Username, fakeHandler.Command.Username); _mockContainer.Verify(x => x.GetInstance(typeof(FakeBaseMessageHandler1)), Times.Once); }
public void ShouldStartAggregatorTimerIfAggregatorTimeoutIsSet() { // Arrange var mockContainer = new Mock<IBusContainer>(); var mockAggregatorPersistor = new Mock<IAggregatorPersistor>(); var mockAggregatorProcessor = new Mock<IAggregatorProcessor>(); var mockConfiguration = new Mock<IConfiguration>(); var handlerRef = new HandlerReference() { HandlerType = typeof(FakeAggregator), MessageType = typeof(FakeMessage1) }; mockContainer.Setup(x => x.GetHandlerTypes()).Returns(new List<HandlerReference> { handlerRef }); var timeout = new TimeSpan(0, 0, 0, 1); mockContainer.Setup(x => x.GetInstance(typeof(FakeAggregator))).Returns(new FakeAggregator { Time = timeout }); mockConfiguration.Setup(x => x.GetAggregatorProcessor(It.IsAny<IAggregatorPersistor>(), mockContainer.Object, typeof(FakeAggregator))).Returns(mockAggregatorProcessor.Object); mockConfiguration.Setup(x => x.GetAggregatorPersistor()).Returns(mockAggregatorPersistor.Object); mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object); mockConfiguration.Setup(x => x.AutoStartConsuming).Returns(true); mockConfiguration.Setup(x => x.TransportSettings).Returns(new TransportSettings()); mockConfiguration.Setup(x => x.GetConsumer()).Returns(new Mock<IConsumer>().Object); // Act new Bus(mockConfiguration.Object); // Assert mockAggregatorProcessor.Verify(x => x.StartTimer<FakeMessage1>(timeout), Times.Once); }
public void ShouldExecuteTheCorrectHandlers() { // Arrange var bus = new MessageBus.Bus(_mockConfiguration.Object); var message1HandlerReference = new HandlerReference { HandlerType = typeof (FakeHandler1), MessageType = typeof (FakeMessage1) }; var handlerReferences = new List<HandlerReference> { message1HandlerReference, new HandlerReference { HandlerType = typeof (FakeHandler2), MessageType = typeof (FakeMessage2) } }; _mockContainer.Setup(x => x.GetHandlerTypes()).Returns(handlerReferences); _mockConsumer.Setup(x => x.StartConsuming(It.Is<ConsumerEventHandler>(y => AssignEventHandler(y)), It.IsAny<string>(), It.IsAny<string>())); _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler<FakeMessage1>))).Returns(new List<HandlerReference> { message1HandlerReference }); var fakeHandler = new FakeHandler1(); _mockContainer.Setup(x => x.GetInstance(typeof (FakeHandler1))).Returns(fakeHandler); bus.StartConsuming(); // Act var message1 = new FakeMessage1(Guid.NewGuid()) { Username = "******" }; _fakeEventHandler(Encoding.UTF8.GetBytes(_serializer.Serialize(message1))); var message2 = new FakeMessage2(Guid.NewGuid()) { DisplayName = "Tim Watson" }; _fakeEventHandler(Encoding.UTF8.GetBytes(_serializer.Serialize(message2))); // Assert Assert.Equal(message1.CorrelationId, fakeHandler.Command.CorrelationId); Assert.Equal(message1.Username, fakeHandler.Command.Username); _mockContainer.Verify(x => x.GetInstance(typeof (FakeHandler2)), Times.Never); }
public void ShouldExecuteTheCorrectHandlerWithRoutingKeyAttribute() { // Arrange var messageProcessor = new MessageHandlerProcessor(_mockContainer.Object); var message1HandlerReference = new HandlerReference { HandlerType = typeof(FakeHandlerWithAttr1), MessageType = typeof(FakeMessage1), RoutingKeys = new List<string> { "Test"} }; _mockContainer.Setup(x => x.GetHandlerTypes(typeof(IMessageHandler<FakeMessage1>))).Returns(new List<HandlerReference> { message1HandlerReference }); var fakeHandler = new FakeHandlerWithAttr1(); _mockContainer.Setup(x => x.GetInstance(typeof(FakeHandlerWithAttr1))).Returns(fakeHandler); // Act var message1 = new FakeMessage1(Guid.NewGuid()) { Username = "******" }; messageProcessor.ProcessMessage<FakeMessage1>(JsonConvert.SerializeObject(message1), new ConsumeContext {Headers = new Dictionary<string, object> {{"RoutingKey", Encoding.ASCII.GetBytes("Test") }}}); // Assert Assert.Equal(message1.CorrelationId, fakeHandler.Command.CorrelationId); Assert.Equal(message1.Username, fakeHandler.Command.Username); _mockContainer.Verify(x => x.GetInstance(typeof(FakeHandlerWithAttr1)), Times.Once); }