public async Task CommandPublisherPublishExceptionTest()
        {
            var sender          = new Mock <ICommandSender>(MockBehavior.Strict);
            var responseCommand = new TestCommand()
            {
                Message = "message2"
            };

            sender.Setup(m => m.SendCommandAsync(It.IsAny <CommandRequestMessage>(), "queue")).Returns(
                Task.FromResult(new CommandResponseMessage("error", typeof(ArgumentException).FullName, null)));

            var iBusContextMock = new Mock <IBusContext <IConnection> >(MockBehavior.Strict);

            iBusContextMock.Setup(m => m.CreateCommandSender()).Returns(sender.Object);

            var target      = new CommandPublisher(iBusContextMock.Object);
            var testCommand = new TestCommand();

            var exception = await Assert.ThrowsExceptionAsync <ArgumentException>(async() =>
            {
                await target.Publish <TestCommand>(testCommand, "queue");
            });

            Assert.AreEqual("error", exception.Message);
        }
        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 async Task CommandPublisherPublishUnknownExceptionThrowsInvalidCastException()
        {
            var sender          = new Mock <ICommandSender>(MockBehavior.Strict);
            var responseCommand = new TestCommand()
            {
                Message = "message2"
            };

            sender.Setup(m => m.SendCommandAsync(It.IsAny <CommandRequestMessage>(), "queue")).Returns(
                Task.FromResult(new CommandResponseMessage("error", "RandomException", null)));

            var iBusContextMock = new Mock <IBusContext <IConnection> >(MockBehavior.Strict);

            iBusContextMock.Setup(m => m.CreateCommandSender()).Returns(sender.Object);

            var target      = new CommandPublisher(iBusContextMock.Object);
            var testCommand = new TestCommand();

            var exception = await Assert.ThrowsExceptionAsync <InvalidCastException>(async() =>
            {
                await target.Publish <TestCommand>(testCommand, "queue");
            });

            Assert.AreEqual("an unknown exception occured (message error), exception type was RandomException", exception.Message);
        }
Example #4
0
        public async Task FinishBestellingLowersVoorraad()
        {
            _magazijnCalled = 0;

            using (var context = new BeheerContext(options))
            {
                var bestelling = new Bestelling
                {
                    KlantId           = "1",
                    AdresRegel1       = "Laagstraat 11",
                    Plaats            = "Laaghoven",
                    Postcode          = "1234FG",
                    BesteldeArtikelen = new List <BestellingItem>
                    {
                        new BestellingItem(1, 3),
                        new BestellingItem(2, 5)
                    }
                };
                var datamapper = new BestellingDatamapper(context);
                await datamapper.Insert(bestelling);
            }


            var commandPublisher = new CommandPublisher(_context);
            var result           = await commandPublisher.Publish <int>(new BestellingAfrondenCommand { Id = 1 },
                                                                        NameConstants.FinishBestellingCommandQueue);

            Thread.Sleep(1000);

            Assert.AreEqual(2, _magazijnCalled);
        }
            public void CommandCannotBeNull()
            {
                var messageFactory = new Mock <ICreateMessages>();
                var messageBus     = new Mock <ISendMessages <CommandEnvelope> >();
                var publisher      = new CommandPublisher(messageFactory.Object, messageBus.Object);

                var ex = Assert.Throws <ArgumentNullException>(() => publisher.Publish(GuidStrategy.NewGuid(), null, HeaderCollection.Empty));

                Assert.Equal("command", ex.ParamName);
            }
 protected virtual void GenerateCommand()
 {
     if (HelloWorldWasSaid && HelloWorldWasRepliedTo && ConversationWasEnded)
     {
         CommandPublisher.Publish
         (
             new UpdateCompletedConversationReportCommand()
         );
     }
 }
            public void CommandCannotBeNull()
            {
                var messageFactory = new Mock<ICreateMessages>();
                var messageBus = new Mock<ISendMessages<CommandEnvelope>>();
                var publisher = new CommandPublisher(messageFactory.Object, messageBus.Object);

                var ex = Assert.Throws<ArgumentNullException>(() => publisher.Publish(GuidStrategy.NewGuid(), null, HeaderCollection.Empty));

                Assert.Equal("command", ex.ParamName);
            }
Example #8
0
 public void Handle(TestAggregateDidSomethingElse2 message)
 {
     // This could happen out of order in this test
     if (DidSomething)
     {
         CommandPublisher.Publish(new TestAggregateDoSomething3());
         // This is a testing variable
         Responded = true;
     }
     ApplyChange(message);
 }
            public void HeadersCanBeNull()
            {
                var messageFactory = new Mock <ICreateMessages>();
                var messageBus     = new Mock <ISendMessages <CommandEnvelope> >();
                var publisher      = new CommandPublisher(messageFactory.Object, messageBus.Object);
                var payload        = new FakeCommand();

                publisher.Publish(GuidStrategy.NewGuid(), payload, null);

                messageFactory.Verify(mock => mock.Create(null, It.IsAny <CommandEnvelope>()), Times.Once);
                messageBus.Verify(mock => mock.Send(It.IsAny <Message <CommandEnvelope> >()), Times.Once);
            }
            public void HeadersCanBeNull()
            {
                var messageFactory = new Mock<ICreateMessages>();
                var messageBus = new Mock<ISendMessages<CommandEnvelope>>();
                var publisher = new CommandPublisher(messageFactory.Object, messageBus.Object);
                var payload = new FakeCommand();

                publisher.Publish(GuidStrategy.NewGuid(), payload, null);

                messageFactory.Verify(mock => mock.Create(null, It.IsAny<CommandEnvelope>()), Times.Once);
                messageBus.Verify(mock => mock.Send(It.IsAny<Message<CommandEnvelope>>()), Times.Once);
            }
Example #11
0
        // Run every 15 minutes in release mode
        public static void Run([TimerTrigger("0 1/15 * * * *")] TimerInfo myTimer, Microsoft.Extensions.Logging.ILogger _logger, ExecutionContext context)
#endif
        {
            if (CommandPublisher == null)
            {
                PreapreOnce(context);
            }

            CorrelationIdHelper.SetCorrelationId(Guid.NewGuid());
            CommandPublisher.Publish(new PublishTimeZonesCommand());
            Console.WriteLine($"Published.");
            Logger.LogInfo($"Published.");
        }
            public void WrapCommandInMessageEnvelope()
            {
                var command        = new FakeCommand() as Command;
                var messageFactory = new Mock <ICreateMessages>();
                var messageBus     = new Mock <ISendMessages <CommandEnvelope> >();
                var publisher      = new CommandPublisher(messageFactory.Object, messageBus.Object);
                var message        = new Message <CommandEnvelope>(GuidStrategy.NewGuid(), HeaderCollection.Empty, new CommandEnvelope(GuidStrategy.NewGuid(), command));

                messageFactory.Setup(mock => mock.Create(HeaderCollection.Empty, It.Is <CommandEnvelope>(envelope => ReferenceEquals(command, envelope.Command)))).Returns(message);

                publisher.Publish(GuidStrategy.NewGuid(), command, HeaderCollection.Empty);

                messageBus.Verify(mock => mock.Send(message), Times.Once());
            }
Example #13
0
        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);
        }
Example #14
0
        private static async Task Test(IBusContext <IConnection> context, int multiply)
        {
            CommandPublisher commandPublisher = new CommandPublisher(context);
            var testcommand = new TestCommand()
            {
                i = new Random().Next(99, 100) * multiply
            };

            Console.WriteLine($"{multiply} sending");


            var result1 = await commandPublisher.Publish <int>(testcommand, "TestjeAsync");

            Console.WriteLine($"{multiply} result:" + result1);
        }
Example #15
0
        public async Task KeurBestellingGoedChangesBestellingToGereedVoorBehandeling()
        {
            using (var context = new BeheerContext(options))
            {
                var bestelling = new Bestelling
                {
                    Id                = 1,
                    KlantId           = "1",
                    AdresRegel1       = "Laagstraat 11",
                    Plaats            = "Laaghoven",
                    Postcode          = "1234FG",
                    BestellingStatus  = BestellingStatus.TerControleVoorSales,
                    BesteldeArtikelen = new List <BestellingItem>
                    {
                        new BestellingItem(1, 3)
                        {
                            Id = 1
                        },
                        new BestellingItem(2, 5)
                        {
                            Id = 2
                        }
                    }
                };
                var datamapper = new BestellingDatamapper(context);
                await datamapper.Insert(bestelling);
            }

            var commandPublisher = new CommandPublisher(_context);
            await commandPublisher.Publish <int>(new BestellingGoedkeurenCommand { Id = 1 },
                                                 NameConstants.BestellingGoedKeurenCommandQueue);

            Thread.Sleep(1000);

            using (var context = new BeheerContext(options))
            {
                var datamapper = new BestellingDatamapper(context);
                var result     = await datamapper.GetBestelling(1);

                Assert.AreEqual(BestellingStatus.GereedVoorBehandeling, result.BestellingStatus);

                var klantmapper = new KlantDatamapper(context);
                var klant       = await klantmapper.GetKlant("1");
            }
        }
Example #16
0
        public virtual void Publish <TCommand>(TCommand command)
            where TCommand : ICommand <TAuthenticationToken>
        {
            RouteHandlerDelegate commandHandler;

            if (!PrepareAndValidateCommand(command, out commandHandler))
            {
                return;
            }

            // This could be null if Akka won't handle the command and something else will.
            if (commandHandler != null)
            {
                commandHandler.Delegate(command);
            }

            // Let everything else know about the command (usually double handling a command is bad... but sometimes it might be useful... like pushing from AWS to Azure so both systems handle it... although an event really is the proper pattern to use here.
            CommandPublisher.Publish(command);
        }
Example #17
0
        public async Task NieuweBestellingThatGoesAboveKredietLimitGoesToSale()
        {
            var bestelling = new NieuweBestellingCommand
            {
                KlantId           = "1",
                AdresRegel1       = "Laagstraat 11",
                Plaats            = "Laaghoven",
                Postcode          = "1234FG",
                BesteldeArtikelen = new List <BestellingItem>
                {
                    new BestellingItem(1, 3),
                    new BestellingItem(2, 3)
                }
            };


            var commandPublisher = new CommandPublisher(_context);
            await commandPublisher.Publish <bool>(bestelling, NameConstants.NieuweBestellingCommandQueue);

            Thread.Sleep(500);

            using (var context = new BeheerContext(options))
            {
                var datamapper = new BestellingDatamapper(context);
                var result     = await datamapper.GetBestelling(1);

                Assert.AreEqual("Hans", result.Klant.Voornaam);
                Assert.AreEqual("Van Huizen", result.Klant.Achternaam);
                Assert.AreEqual("1", result.KlantId);
                Assert.AreEqual("Laagstraat 11", result.AdresRegel1);
                Assert.AreEqual("Laaghoven", result.Plaats);
                Assert.AreEqual("1234FG", result.Postcode);
                Assert.AreEqual(2, result.BesteldeArtikelen.Count);
                Assert.AreEqual(BestellingStatus.TerControleVoorSales, result.BestellingStatus);
                Assert.IsTrue(result.BesteldeArtikelen.Any(b => b.Artikel.Naam == "Fiets" && b.Aantal == 3));

                var klantMapper = new KlantDatamapper(context);
                var klant       = await klantMapper.GetKlant("1");

                //Krediet should not be added yet
                Assert.AreEqual(508.2m, klant.KredietMetSales);
            }
        }
Example #18
0
        public async Task InsertNieuweBestellingUnder500EuroFromEventSetsStatusToGereed()
        {
            var bestelling = new NieuweBestellingCommand
            {
                KlantId           = "1",
                AdresRegel1       = "Laagstraat 11",
                Plaats            = "Laaghoven",
                Postcode          = "1234FG",
                BesteldeArtikelen = new List <BestellingItem>
                {
                    new BestellingItem(1, 1),
                    new BestellingItem(2, 1)
                }
            };


            var commandPublisher = new CommandPublisher(_context);
            await commandPublisher.Publish <bool>(bestelling, NameConstants.NieuweBestellingCommandQueue);

            Thread.Sleep(500);

            using (var context = new BeheerContext(options))
            {
                var datamapper = new BestellingDatamapper(context);
                var result     = await datamapper.GetBestelling(1);

                Assert.AreEqual("Hans", result.Klant.Voornaam);
                Assert.AreEqual("Van Huizen", result.Klant.Achternaam);
                Assert.AreEqual("1", result.KlantId);
                Assert.AreEqual("Laagstraat 11", result.AdresRegel1);
                Assert.AreEqual("Laaghoven", result.Plaats);
                Assert.AreEqual("1234FG", result.Postcode);
                Assert.AreEqual(2, result.BesteldeArtikelen.Count);
                Assert.AreEqual(BestellingStatus.GereedVoorBehandeling, result.BestellingStatus);
                Assert.IsTrue(result.BesteldeArtikelen.Any(b => b.Artikel.Naam == "Fiets" && b.Aantal == 1));

                var klantMapper = new KlantDatamapper(context);
                var klant       = await klantMapper.GetKlant("1");

                Assert.AreEqual(169.4m, klant.KredietMetSales);
            }
        }
Example #19
0
        public void Handle(RebuildProjectionVersion @event)
        {
            if (Player.RebuildIndex() == false)
            {
                RequestTimeout(new RebuildProjectionVersion(@event.ProjectionVersionRequest, DateTime.UtcNow.AddSeconds(30)));
                return;
            }
            var rebuildUntil = @event.ProjectionVersionRequest.Timebox.RebuildFinishUntil;

            if (rebuildUntil < DateTime.UtcNow)
            {
                return;
            }

            var theType           = @event.ProjectionVersionRequest.ProjectionVersion.ProjectionName.GetTypeByContract();
            var rebuildTimesOutAt = @event.ProjectionVersionRequest.Timebox.RebuildFinishUntil;

            if (Player.Rebuild(theType, @event.ProjectionVersionRequest.ProjectionVersion, rebuildTimesOutAt))
            {
                var command = new FinalizeProjectionVersionRequest(@event.ProjectionVersionRequest.Id, @event.ProjectionVersionRequest.ProjectionVersion);
                CommandPublisher.Publish(command);
            }
        }
        public async Task CommandPublisherPublishTest()
        {
            var sender          = new Mock <ICommandSender>(MockBehavior.Strict);
            var responseCommand = new TestCommand()
            {
                Message = "message2"
            };

            sender.Setup(m => m.SendCommandAsync(It.IsAny <CommandRequestMessage>(), "queue")).Returns(
                Task.FromResult(new CommandResponseMessage(JsonConvert.SerializeObject(responseCommand), typeof(TestCommand).FullName, null)));

            var iBusContextMock = new Mock <IBusContext <IConnection> >(MockBehavior.Strict);

            iBusContextMock.Setup(m => m.CreateCommandSender()).Returns(sender.Object);

            var target      = new CommandPublisher(iBusContextMock.Object);
            var testCommand = new TestCommand();

            TestCommand result = await target.Publish <TestCommand>(testCommand, "queue");

            Assert.IsInstanceOfType(result, typeof(TestCommand));
            Assert.AreEqual("message2", (result as TestCommand).Message);
        }
Example #21
0
        public async Task RegistreerKlantCommandAddsKlantToDatabaseAndThrowsEvent()
        {
            _context.DeclareQueue("klantGeregistreedQueue", new List <string> {
                NameConstants.KlantGeregistreerdEvent
            });
            var command = new RegistreerKlantCommand
            {
                AccountId      = "1",
                Voornaam       = "Hans",
                Achternaam     = "van Huizen",
                AdresRegel     = "Voorstraat 8",
                Plaats         = "Groningen",
                Postcode       = "1345df",
                Telefoonnummer = "0665234365"
            };

            var publisher = new CommandPublisher(_context);
            var result    = await publisher.Publish <int>(command, NameConstants.RegistreerKlantCommandQueue);

            Thread.Sleep(1000);

            Assert.AreEqual(1, result);
            Assert.AreEqual(1, _context.TestQueues["klantGeregistreedQueue"].Queue.Count);
        }
Example #22
0
 /// <summary>
 /// Publish the <paramref name="commands"/>.
 /// </summary>
 protected virtual void PublishCommand(IEnumerable <ICommand <TAuthenticationToken> > commands)
 {
     CommandPublisher.Publish(commands);
 }
            public void WrapCommandInMessageEnvelope()
            {
                var command = new FakeCommand() as Command;
                var messageFactory = new Mock<ICreateMessages>();
                var messageBus = new Mock<ISendMessages<CommandEnvelope>>();
                var publisher = new CommandPublisher(messageFactory.Object, messageBus.Object);
                var message = new Message<CommandEnvelope>(GuidStrategy.NewGuid(), HeaderCollection.Empty, new CommandEnvelope(GuidStrategy.NewGuid(), command));

                messageFactory.Setup(mock => mock.Create(HeaderCollection.Empty, It.Is<CommandEnvelope>(envelope => ReferenceEquals(command, envelope.Command)))).Returns(message);

                publisher.Publish(GuidStrategy.NewGuid(), command, HeaderCollection.Empty);

                messageBus.Verify(mock => mock.Send(message), Times.Once());
            }
Example #24
0
        public async Task VolgendeBestellingInpakkenGivesVolgendeBestellingSetsStatusToInbehandeling()
        {
            using (var context = new BeheerContext(options))
            {
                var bestelling = new Bestelling
                {
                    BestellingStatus  = BestellingStatus.GereedVoorBehandeling,
                    KlantId           = "1",
                    AdresRegel1       = "Laagstraat 11",
                    Plaats            = "Laaghoven",
                    Postcode          = "1234FG",
                    BesteldeArtikelen = new List <BestellingItem>
                    {
                        new BestellingItem(1, 3),
                        new BestellingItem(2, 5)
                    }
                };
                var bestelling2 = new Bestelling
                {
                    BestellingStatus  = BestellingStatus.GereedVoorBehandeling,
                    KlantId           = "1",
                    AdresRegel1       = "Hoogstraat 77",
                    Plaats            = "Hooghoven",
                    Postcode          = "4321FE",
                    BesteldeArtikelen = new List <BestellingItem>
                    {
                        new BestellingItem(1, 3),
                        new BestellingItem(2, 5)
                    }
                };

                var datamapper = new BestellingDatamapper(context);
                await datamapper.Insert(bestelling);

                await datamapper.Insert(bestelling2);
            }

            var commandPublisher = new CommandPublisher(_context);
            var result           = await commandPublisher.Publish <int>(new VolgendeBestellingCommand(),
                                                                        NameConstants.VolgendeBestellingCommandQueue);

            Assert.AreEqual(1, result);

            var result2 = await commandPublisher.Publish <int>(new VolgendeBestellingCommand(),
                                                               NameConstants.VolgendeBestellingCommandQueue);

            Assert.AreEqual(2, result2);

            var resultShouldbeZero = await commandPublisher.Publish <int>(new VolgendeBestellingCommand(),
                                                                          NameConstants.VolgendeBestellingCommandQueue);

            Assert.AreEqual(0, resultShouldbeZero);


            using (var context = new BeheerContext(options))
            {
                var bestellingDatamapper = new BestellingDatamapper(context);
                var bestelling           = await bestellingDatamapper.GetBestelling(1);

                Assert.AreEqual(BestellingStatus.InBehandelingDoorMagazijn, bestelling.BestellingStatus);
            }
        }
Example #25
0
        public void Handle(ProjectionVersionRebuildTimedout sagaTimeout)
        {
            var timedout = new TimeoutProjectionVersionRequest(sagaTimeout.ProjectionVersionRequest.Id, sagaTimeout.ProjectionVersionRequest.ProjectionVersion, sagaTimeout.ProjectionVersionRequest.Timebox);

            CommandPublisher.Publish(timedout);
        }