public void HandleShouldCallBasicAck() { // Arrange var sender = new object(); var basicDeliverEventArgs = new BasicDeliverEventArgs { BasicProperties = new BasicProperties { Type = "SomeCommand", Timestamp = new AmqpTimestamp(new DateTime(2019, 6, 4).Ticks), }, Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new ReplayEventsCommand { EventType = "DomainEvent", RoutingKey = "Test.*", ReplayExchangeName = "AuditLog.TestExchange" })), RoutingKey = "TestQueue", }; var eventReplayerMock = new Mock <IEventReplayer>(); var repositoryMock = new Mock <IAuditLogRepository <LogEntry, long> >(); var repository = repositoryMock.Object; var eventReplayer = eventReplayerMock.Object; var routingKeyMatcherMock = new Mock <IRoutingKeyMatcher>(); var routingKeyMatcher = routingKeyMatcherMock.Object; var eventBusMock = new Mock <IEventBus>(); var eventBus = eventBusMock.Object; var modelMock = new Mock <IModel>(); var model = modelMock.Object; eventBusMock.Setup(mock => mock.Model).Returns(model); var commandListener = new AuditLogCommandListener(repository, eventReplayer, routingKeyMatcher, eventBus); repositoryMock.Setup(mock => mock.FindBy(It.IsAny <LogEntryCriteria>())) .Returns(new List <LogEntry> { new LogEntry { Timestamp = new DateTime(2019, 5, 8).Ticks, RoutingKey = "Test.*", EventJson = "{'title': 'Some title'}", EventType = "DomainEvent" }, new LogEntry { Timestamp = new DateTime(2019, 7, 2).Ticks, RoutingKey = "Test.#", EventJson = "{'title': 'Some title'}", EventType = "DomainEvent" } }); // Act commandListener.Handle(sender, basicDeliverEventArgs); // Assert modelMock.Verify(mock => mock.BasicAck(It.IsAny <ulong>(), false)); }
public void ReplayEventsReturnsReplayEventResponse500() { // Arrange var result = new ReplayEventsResponse(); var sender = new object(); var basicDeliverEventArgs = new BasicDeliverEventArgs { BasicProperties = new BasicProperties { Type = "SomeCommand", Timestamp = new AmqpTimestamp(new DateTime(2019, 6, 4).Ticks), }, Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new ReplayEventsCommand { EventType = "DomainEvent", RoutingKey = "Test.*" })), RoutingKey = "TestQueue" }; var eventReplayerMock = new Mock <IEventReplayer>(); var repositoryMock = new Mock <IAuditLogRepository <LogEntry, long> >(); var repository = repositoryMock.Object; var eventReplayer = eventReplayerMock.Object; var routingKeyMatcherMock = new Mock <IRoutingKeyMatcher>(); var routingKeyMatcher = routingKeyMatcherMock.Object; var eventBusMock = new Mock <IEventBus>(); var eventBus = eventBusMock.Object; var modelMock = new Mock <IModel>(); var model = modelMock.Object; eventBusMock.Setup(mock => mock.Model).Returns(model); eventBusMock.Setup(mock => mock.PublishCommand(It.IsAny <ReplayEventsResponse>())) .Callback((DomainCommand response) => result = response as ReplayEventsResponse); var commandListener = new AuditLogCommandListener(repository, eventReplayer, routingKeyMatcher, eventBus); repositoryMock.Setup(mock => mock.FindBy(It.IsAny <LogEntryCriteria>())) .Throws(new Exception()); // Act commandListener.Handle(sender, basicDeliverEventArgs); // Assert Assert.AreEqual(StatusCodes.Status500InternalServerError, result.Code); Assert.AreEqual("Internal Error", result.Status); }
public void ReplayEventCallsFindByOnRepositoryWithRightCriteria() { // Arrange var logEntryCriteria = new LogEntryCriteria(); var sender = new object(); var basicDeliverEventArgs = new BasicDeliverEventArgs { BasicProperties = new BasicProperties { Type = "SomeCommand", Timestamp = new AmqpTimestamp(new DateTime(2019, 6, 4).Ticks), }, Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new ReplayEventsCommand { EventType = "DomainEvent", RoutingKey = "Test.*" })), RoutingKey = "TestQueue" }; var eventReplayerMock = new Mock <IEventReplayer>(); var repositoryMock = new Mock <IAuditLogRepository <LogEntry, long> >(); var repository = repositoryMock.Object; var eventReplayer = eventReplayerMock.Object; var routingKeyMatcherMock = new Mock <IRoutingKeyMatcher>(); var routingKeyMatcher = routingKeyMatcherMock.Object; var eventBusMock = new Mock <IEventBus>(); var eventBus = eventBusMock.Object; var modelMock = new Mock <IModel>(); var model = modelMock.Object; eventBusMock.Setup(mock => mock.Model).Returns(model); var commandListener = new AuditLogCommandListener(repository, eventReplayer, routingKeyMatcher, eventBus); repositoryMock.Setup(mock => mock.FindBy(It.IsAny <LogEntryCriteria>())) .Callback((LogEntryCriteria criteria) => logEntryCriteria = criteria); // Act commandListener.Handle(sender, basicDeliverEventArgs); // Assert.AreEqual("DomainEvent", logEntryCriteria.EventType); Assert.AreEqual("Test.*", logEntryCriteria.RoutingKey); Assert.AreEqual(null, logEntryCriteria.FromTimestamp); Assert.AreEqual(null, logEntryCriteria.ToTimestamp); }
public void ReplayEventsReturnsReplayEventsResponse() { // Arrange var sender = new object(); var basicDeliverEventArgs = new BasicDeliverEventArgs { BasicProperties = new BasicProperties { Type = "SomeCommand", Timestamp = new AmqpTimestamp(new DateTime(2019, 6, 4).Ticks), }, Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new ReplayEventsCommand { EventType = "DomainEvent", RoutingKey = "Test.*" })), RoutingKey = "TestQueue" }; var eventReplayerMock = new Mock <IEventReplayer>(); var repositoryMock = new Mock <IAuditLogRepository <LogEntry, long> >(); var repository = repositoryMock.Object; var eventReplayer = eventReplayerMock.Object; var routingKeyMatcherMock = new Mock <IRoutingKeyMatcher>(); var routingKeyMatcher = routingKeyMatcherMock.Object; var eventBusMock = new Mock <IEventBus>(); var eventBus = eventBusMock.Object; var modelMock = new Mock <IModel>(); var model = modelMock.Object; eventBusMock.Setup(mock => mock.Model).Returns(model); var commandListener = new AuditLogCommandListener(repository, eventReplayer, routingKeyMatcher, eventBus); // Act commandListener.Handle(sender, basicDeliverEventArgs); // Assert eventBusMock.Verify(mock => mock.PublishCommand(It.IsAny <ReplayEventsResponse>())); }