public void HandleCallsFindByOnRepositoryWithExpectedItems(string topics, string types, long fromTimestamp, long toTimeStamp) { // Arrange Mock <IAuditLogItemRepository> repositoryMock = new Mock <IAuditLogItemRepository>(); Mock <IEventPublisher> eventPublisherMock = new Mock <IEventPublisher>(); List <string> topicNames = topics.Split(',').ToList(); List <string> typeNames = types.Split(',').ToList(); ReplayCommandListener commandListener = new ReplayCommandListener(repositoryMock.Object, eventPublisherMock.Object, new LoggerFactory()); ReplayEventsCommand command = new ReplayEventsCommand(toTimeStamp) { Topics = topicNames, Types = typeNames, FromTimeStamp = fromTimestamp }; AuditLogItemCriteria criteria = null; repositoryMock.Setup(e => e.FindBy(It.IsAny <AuditLogItemCriteria>())) .Callback <AuditLogItemCriteria>(e => criteria = e); // Act commandListener.Handle(command); // Assert Assert.AreEqual(fromTimestamp, criteria.FromTimeStamp); Assert.AreEqual(toTimeStamp, criteria.ToTimeStamp); Assert.AreEqual(topicNames, criteria.Topics); Assert.AreEqual(typeNames, criteria.Types); }
public void FetchedLogItemsArePublishedProperly(string data, string topic, string type, long timestamp) { // Arrange Mock <IAuditLogItemRepository> repositoryMock = new Mock <IAuditLogItemRepository>(MockBehavior.Strict); Mock <IEventPublisher> eventPublisherMock = new Mock <IEventPublisher>(); AuditLogItem auditLogItem = new AuditLogItem { Id = Guid.NewGuid().ToString(), TimeStamp = timestamp, Data = data, Topic = topic, Type = type }; repositoryMock.Setup(e => e.FindBy(It.IsAny <AuditLogItemCriteria>())) .Returns(new List <AuditLogItem> { auditLogItem }); ReplayCommandListener commandListener = new ReplayCommandListener(repositoryMock.Object, eventPublisherMock.Object, new LoggerFactory()); ReplayEventsCommand command = new ReplayEventsCommand(0); // Act commandListener.Handle(command); Thread.Sleep(WaitTime); // Assert eventPublisherMock.Verify(e => e.PublishAsync(timestamp, $"replay_{topic}", It.IsAny <Guid>(), type, data)); }
public static async Task ScrapeAuditLog(IBusContext <IConnection> mainContext, ServiceCollection collection, DateTime startTime) { var exchangeName = "Audit_Bestelling " + Guid.NewGuid(); var connectionBuilder = new RabbitMQContextBuilder() .ReadFromEnvironmentVariables().WithExchange(exchangeName); var builder = new MicroserviceHostBuilder(); builder .RegisterDependencies(collection) .WithContext(connectionBuilder.CreateContext()) .ExitAfterIdleTime(new TimeSpan(0, 0, 2, 0)) .UseConventions(); builder.CreateHost().StartListeningInOtherThread(); var publisher = new CommandPublisher(mainContext); var replayEventsCommand = new ReplayEventsCommand { ToTimestamp = startTime.Ticks, ExchangeName = exchangeName }; var result = await publisher.Publish <bool>(replayEventsCommand, "AuditlogReplayService", "Minor.WSA.AuditLog.Commands.ReplayEventsCommand"); }
public void EnsureDestinationQueueIsProperlySet() { // Arct ReplayEventsCommand command = new ReplayEventsCommand(0); // Assert Assert.AreEqual("auditlog.replay", command.DestinationQueue); }
public void EnsureInitializingCommandSetsEmptyProcessId() { // Act ReplayEventsCommand command = new ReplayEventsCommand(0); // Assert Assert.AreEqual(command.ProcessId, Guid.Empty); }
public void EnsureToTimeStampIsProperlySet(long timeStamp) { // Act ReplayEventsCommand command = new ReplayEventsCommand(timeStamp); // Assert Assert.AreEqual(timeStamp, command.ToTimeStamp); }
/// <summary> /// Entrypoint /// </summary> static void Main(string[] args) { /** * Logging is important, so first set up a logger factory */ using var loggerFactory = LoggerFactory.Create(configure => { configure.AddConsole().SetMinimumLevel(LogLevel.Information); }); /** * Set up a context using environment variables */ using var context = new RabbitMqContextBuilder() .ReadFromEnvironmentVariables() .CreateContext(); /** * Build a host using the loggerfactory, context and register any event listeners in the package. */ using var hostBuilder = new MicroserviceReplayHostBuilder() .SetLoggerFactory(loggerFactory) .WithBusContext(context) .UseConventions(); /** * Now create the host and start it */ using IMicroserviceReplayHost host = (MicroserviceReplayHost)hostBuilder.CreateHost(); host.Start(); /** * Start spamming events to the auditlogger as a demonstration */ StartSpammingEvents(context); /** * Now let's start replaying, first create a replay command */ Guid processId = Guid.NewGuid(); ReplayEventsCommand replayEventsCommand = new ReplayEventsCommand(DateTime.Now.ToFileTimeUtc(), processId) { Types = new List <string> { "AnimalAddedEvent" } }; /** * Create the publishers */ ICommandPublisher publisher = new CommandPublisher(context); IReplayCommandPublisher replayCommandPublisher = new ReplayCommandPublisher(host, publisher, loggerFactory); /** * Commence a replay! */ replayCommandPublisher.Initiate(replayEventsCommand); }
public void EnsureProcessIdIsProperlySet(string guidString) { // Arrange Guid guid = Guid.Parse(guidString); // Act ReplayEventsCommand command = new ReplayEventsCommand(0, guid); // Assert Assert.AreEqual(guid, command.ProcessId); }
public ReplayEventsResult Handle(ReplayEventsCommand command) { AuditLogItemCriteria criteria = (AuditLogItemCriteria)command; _logger.LogInformation($"Received replaycommand with criteria: From: {criteria.FromTimeStamp}, " + $"to: {criteria.ToTimeStamp}, " + $"topics: {string.Join(',', criteria.Topics)} and " + $"types: {string.Join(',', criteria.Types)}"); IEnumerable <AuditLogItem> auditLogItems = _repository.FindBy(criteria).ToList(); _logger.LogDebug($"Found {auditLogItems.Count()} in the database"); Task.Run(() => { _logger.LogInformation($"Publishing start event with process id {command.ProcessId}"); _eventPublisher.Publish(new StartReplayEvent(command.ProcessId)); List <Task> tasks = new List <Task>(); foreach (AuditLogItem logItem in auditLogItems) { _logger.LogTrace($"Publishing logitem with id {logItem.Id}"); try { Task task = _eventPublisher.PublishAsync(logItem.TimeStamp, $"{ReplayTopicNames.ReplayEventTopicPrefix}{logItem.Topic}", new Guid(logItem.Id), logItem.Type, logItem.Data); tasks.Add(task); } catch (Exception e) { _logger.LogCritical($"A critical exception occured while asynchronously publishing event {logItem.Id} " + $"with exception {e.Message}"); throw; } } ; _logger.LogTrace("Waiting for all events to be published"); Task.WaitAll(tasks.ToArray()); _logger.LogInformation($"Publishing end event with process id {command.ProcessId}"); _eventPublisher.Publish(new EndReplayEvent(command.ProcessId)); }); _logger.LogDebug("Publishing ReplayEventsResult with auditlogitems count"); return(new ReplayEventsResult { AmountOfEvents = auditLogItems.Count() }); }
private async static Task Test(IBusContext <IConnection> context) { CommandPublisher commandPublisher = new CommandPublisher(context); ReplayEventsCommand replayEventsCommand = new ReplayEventsCommand() { ToTimestamp = DateTime.Now.Ticks, ExchangeName = "fddfgf", }; var result = commandPublisher.Publish <bool>(replayEventsCommand, "AuditlogReplayService", "Minor.WSA.AuditLog.Commands.ReplayEventsCommand").Result; //Console.WriteLine($"{multiply} result:" + result1); }
private void SendReplayCommand() { ICommandSender commandSender = _context.CreateCommandSender(); ReplayEventsCommand replayEventsCommand = new ReplayEventsCommand() { ExchangeName = NameConstants.BestelServiceEventReplayExchange, EventType = NameConstants.CatalogusServiceCategorieAanCatalogusToegevoegdEvent }; commandSender.SendCommandAsync(new RequestCommandMessage( message: JsonConvert.SerializeObject(replayEventsCommand), type: NameConstants.AuditlogReplayCommandType, correlationId: "", routingKey: NameConstants.AuditlogQueue )); }
private void SendReplayCommand(string eventType) { ICommandSender commandSender = _context.CreateCommandSender(); ReplayEventsCommand replayEventsCommand = new ReplayEventsCommand() { ExchangeName = NameConstants.BffWebshopEventReplayExchange, Topic = eventType }; commandSender.SendCommandAsync(new RequestCommandMessage( message: JsonConvert.SerializeObject(replayEventsCommand), type: NameConstants.AuditlogReplayCommandType, correlationId: "", routingKey: NameConstants.AuditlogQueue )); }
public void SerializeDeserializeTest() { using (var ms = new MemoryStream()) { var replayEventsCommand = new ReplayEventsCommand { Destination = "12312", From = new DateTime(2013, 12, 31, 23, 59, 59), SerializationFormat = "protobuf", Types = new Type[] { typeof(string), typeof(int), typeof(ReplayEventsCommandTests) } }; ProtoBuf.Serializer.Serialize(ms, replayEventsCommand); ms.Seek(0, SeekOrigin.Begin); var actual = ProtoBuf.Serializer.Deserialize <ReplayEventsCommand>(ms); Assert.AreEqual(replayEventsCommand.Destination, actual.Destination); Assert.AreEqual(replayEventsCommand.From, actual.From); Assert.AreEqual(replayEventsCommand.SerializationFormat, actual.SerializationFormat); CollectionAssert.AreEquivalent(replayEventsCommand.Types, actual.Types); } }
public void AmountOfEventsAreProperlyReturned(int amount) { // Arrange Mock <IAuditLogItemRepository> repositoryMock = new Mock <IAuditLogItemRepository>(); Mock <IEventPublisher> eventPublisherMock = new Mock <IEventPublisher>(); repositoryMock.Setup(e => e.FindBy(It.IsAny <AuditLogItemCriteria>())) .Returns(Enumerable.Range(0, amount).Select(e => new AuditLogItem())); ReplayCommandListener commandListener = new ReplayCommandListener(repositoryMock.Object, eventPublisherMock.Object, new LoggerFactory()); ReplayEventsCommand command = new ReplayEventsCommand(0); // Act int result = commandListener.Handle(command).AmountOfEvents; // Assert Assert.AreEqual(amount, result); }
public void ShouldCorrectlySetProperties() { // Arrange // Act var replayEventsCommand = new ReplayEventsCommand() { ExchangeName = "ExchangeName", FromTimestamp = null, ToTimestamp = null, EventType = "EventType", Topic = "Topic" }; // Assert Assert.AreEqual("ExchangeName", replayEventsCommand.ExchangeName); Assert.AreEqual(null, replayEventsCommand.FromTimestamp); Assert.AreEqual(null, replayEventsCommand.ToTimestamp); Assert.AreEqual("EventType", replayEventsCommand.EventType); Assert.AreEqual("Topic", replayEventsCommand.Topic); }
public void EndEventIsSentOnHandle() { // Arrange Mock <IAuditLogItemRepository> repositoryMock = new Mock <IAuditLogItemRepository>(MockBehavior.Strict); Mock <IEventPublisher> eventPublisherMock = new Mock <IEventPublisher>(); repositoryMock.Setup(e => e.FindBy(It.IsAny <AuditLogItemCriteria>())) .Returns(new List <AuditLogItem>()); ReplayCommandListener commandListener = new ReplayCommandListener(repositoryMock.Object, eventPublisherMock.Object, new LoggerFactory()); ReplayEventsCommand command = new ReplayEventsCommand(0); // Act commandListener.Handle(command); Thread.Sleep(WaitTime); // Assert eventPublisherMock.Verify(e => e.Publish(It.IsAny <EndReplayEvent>())); }
public void FetchedLogItemsArePublished(int amount) { // Arrange Mock <IAuditLogItemRepository> repositoryMock = new Mock <IAuditLogItemRepository>(MockBehavior.Strict); Mock <IEventPublisher> eventPublisherMock = new Mock <IEventPublisher>(); repositoryMock.Setup(e => e.FindBy(It.IsAny <AuditLogItemCriteria>())) .Returns(Enumerable.Range(0, amount).Select(e => new AuditLogItem { Id = Guid.NewGuid().ToString(), Data = "test", Topic = "TestTopic", Type = "TestType", TimeStamp = 10 })); ReplayCommandListener commandListener = new ReplayCommandListener(repositoryMock.Object, eventPublisherMock.Object, new LoggerFactory()); ReplayEventsCommand command = new ReplayEventsCommand(0); // Act commandListener.Handle(command); Thread.Sleep(WaitTime); // Assert eventPublisherMock.Verify(e => e.PublishAsync(It.IsAny <long>(), It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(amount)); }
public void CriteriaCanBeCreatedFromCastingReplayCommand(string topics, string types, long fromTimestamp, long toTimeStamp) { // Arrange List <string> topicNames = topics.Split(',').ToList(); List <string> typeNames = types.Split(',').ToList(); ReplayEventsCommand replayEventsCommand = new ReplayEventsCommand(toTimeStamp) { FromTimeStamp = fromTimestamp, Topics = topicNames, Types = typeNames }; // Act AuditLogItemCriteria criteria = (AuditLogItemCriteria)replayEventsCommand; // Assert Assert.AreEqual(topicNames, criteria.Topics); Assert.AreEqual(typeNames, criteria.Types); Assert.AreEqual(fromTimestamp, criteria.FromTimeStamp); Assert.AreEqual(toTimeStamp, criteria.ToTimeStamp); }
private ReplayEventsResponse ReplayEvents(ReplayEventsCommand command) { try { var criteria = new LogEntryCriteria { EventType = command.EventType, RoutingKey = command.RoutingKey, FromTimestamp = command.FromTimestamp, ToTimestamp = command.ToTimestamp }; var logEntries = _repository.FindBy(criteria).ToList(); _logger.LogTrace($"Found {logEntries.Count} log entries"); logEntries = logEntries .Where(entry => _routingKeyMatcher.IsMatch(criteria.RoutingKey, entry.RoutingKey)).ToList(); _logger.LogTrace($"Filtered log entries, which results in {logEntries.Count} log entries"); _eventReplayer.RegisterReplayExchange(command.ReplayExchangeName); _eventReplayer.ReplayLogEntries(logEntries); _logger.LogTrace($"Replayed {logEntries.Count} log entries"); } catch (Exception exception) { _logger.LogError($"Internal error occured, with exception: {exception.Message}"); return(new ReplayEventsResponse { Code = StatusCodes.Status500InternalServerError, Status = "Internal Error" }); } _logger.LogTrace("Sending response"); return(new ReplayEventsResponse { Code = StatusCodes.Status200OK, Status = "OK" }); }