public void ThenVerwerkEenBestellingInHetSysteemMetHetKlantnummerBesteldatumEnBestelregels()
        {
            var bestellingDataMapper = new BestellingDataMapper(_context);
            var artikelDataMapper    = new ArtikelDataMapper(_context);
            var eventPublisherMock   = new Mock <IEventPublisher>(MockBehavior.Strict);
            var commandSenderMock    = new Mock <ICommandSender>(MockBehavior.Strict);

            var response = new ResponseCommandMessage(JsonConvert.SerializeObject(true), "type", "correlationId");

            eventPublisherMock.Setup(p => p.Publish(It.IsAny <DomainEvent>()));
            commandSenderMock.Setup(sendr => sendr.SendCommandAsync(It.IsAny <RequestCommandMessage>())).ReturnsAsync(response);

            var controller = new BestellingController(bestellingDataMapper, artikelDataMapper, eventPublisherMock.Object, new LoggerFactory());

            var bestelling = new BestellingCM
            {
                Klantnummer  = _klantnummer,
                BestelRegels = new List <BestelRegelCM>()
            };

            foreach (var regel in _bestelregels)
            {
                bestelling.BestelRegels.Add(new BestelRegelCM()
                {
                    Artikelnummer = regel.Item1,
                    Aantal        = regel.Item5
                });
            }

            var result = controller.HandlePlaatsBestelling(new PlaatsBestellingCommand(bestelling, ""));

            eventPublisherMock.VerifyAll();
            Assert.AreEqual(1, result);
        }
        private void MapBestelRegel(BestellingCM bestellingCM, Bestelling bestelling)
        {
            var bestelRegels = bestellingCM?.BestelRegels ?? new List <BestelRegelCM>();

            foreach (var regel in bestelRegels)
            {
                var artikel = _artikelDataMapper.GetById(regel.Artikelnummer);

                var regelResult = Mapper.Map <BestelRegel>(artikel);
                regelResult.Aantal       = regel.Aantal;
                regelResult.PrijsExclBtw = artikel.Prijs;
                CalculateRegelSum(ref regelResult);

                bestelling.BestelRegels.Add(regelResult);
            }
        }
        public void HandlePlaatsBestelling_ShouldCallInsertOnDataMapper()
        {
            var artikel = new ArtikelBuilder()
                          .SetArtikelNummer(1)
                          .SetLeveranciercode("1-abc-xyz")
                          .SetNaam("Fietsband")
                          .SetPrijs(12.99m)
                          .Create();

            var bestellingCM = new BestellingCM
            {
                Klantnummer  = 1234,
                ContactInfo  = new ContactInfoCM(),
                Afleveradres = new AfleveradresCM(),
                BestelRegels = new List <BestelRegelCM>
                {
                    new BestelRegelCM {
                        Artikelnummer = 1, Aantal = 2
                    },
                }
            };

            Bestelling bestelling = null;

            _artikelDataMapperMock.Setup(a => a.GetById(1)).Returns(artikel);

            _bestellingDataMapperMock.Setup(m => m.Insert(It.IsAny <Bestelling>()))
            .Callback <Bestelling>(b => { b.Id = 42; bestelling = b; })
            .Returns(bestelling);

            _bestellingDataMapperMock.Setup(m => m.Update(It.IsAny <Bestelling>()))
            .Callback <Bestelling>(b => bestelling = b);

            _eventPublisherMock.Setup(p => p.Publish(It.IsAny <DomainEvent>()));

            var requestCommand = new PlaatsBestellingCommand(bestellingCM, NameConstants.BestelServicePlaatsBestellingCommandQueue);
            var result         = _target.HandlePlaatsBestelling(requestCommand);

            _bestellingDataMapperMock.VerifyAll();
            _artikelDataMapperMock.VerifyAll();
            _eventPublisherMock.VerifyAll();

            Assert.AreEqual(42, result);
            Assert.AreEqual(42, bestelling.Factuurnummer);
            Assert.AreEqual(DateTime.Now.Date, bestelling.Besteldatum.Date);
            Assert.AreEqual(BestelStatus.Geplaatst, bestelling.BestelStatus);
        }
        public void HandlePlaatsBestelling_ShouldThrowDatabaseExceptionWhenInsertFailed()
        {
            var bestelling = new BestellingCM();

            _bestellingDataMapperMock.Setup(b => b.Insert(It.IsAny <Bestelling>())).Throws(new DatabaseException("error"));

            var requestCommand = new PlaatsBestellingCommand(bestelling, NameConstants.BestelServicePlaatsBestellingCommandQueue);

            Action action = () => _target.HandlePlaatsBestelling(requestCommand);

            _bestellingDataMapperMock.Verify(d => d.Insert(It.IsAny <Bestelling>()), Times.Never);
            _eventPublisherMock.Verify(e => e.Publish(It.IsAny <DomainEvent>()), Times.Never);

            var ex = Assert.ThrowsException <DatabaseException>(action);

            Assert.AreEqual("Something unexpected happend while inserting into the database", ex.Message);
        }
        public void HandlePlaatsBestelling_ShouldPublishBestellingGeplaatstEvent()
        {
            var artikel = new ArtikelBuilder()
                          .SetArtikelNummer(1)
                          .SetLeveranciercode("1-abc-xyz")
                          .SetNaam("Fietsband")
                          .SetPrijs(12.99m)
                          .Create();

            var bestellingCM = new BestellingCM
            {
                Klantnummer  = 1234,
                ContactInfo  = new ContactInfoCM(),
                Afleveradres = new AfleveradresCM(),
                BestelRegels = new List <BestelRegelCM>
                {
                    new BestelRegelCM {
                        Artikelnummer = 1, Aantal = 2
                    },
                }
            };

            Bestelling bestelling = null;

            _artikelDataMapperMock.Setup(a => a.GetById(1)).Returns(artikel);

            _bestellingDataMapperMock.Setup(m => m.Insert(It.IsAny <Bestelling>()))
            .Callback <Bestelling>(b => { b.Id = 42; bestelling = b; })
            .Returns(bestelling);

            _bestellingDataMapperMock.Setup(m => m.Update(It.IsAny <Bestelling>()));

            _eventPublisherMock.Setup(e => e.Publish(It.Is <BestellingGeplaatstEvent>(evt =>
                                                                                      evt.RoutingKey == NameConstants.BestelServiceBestellingGeplaatstEvent &&
                                                                                      evt.Bestelling == bestelling
                                                                                      ))).Verifiable("Publish event should be called");

            var requestCommand = new PlaatsBestellingCommand(bestellingCM, NameConstants.BestelServicePlaatsBestellingCommandQueue);

            _target.HandlePlaatsBestelling(requestCommand);

            _bestellingDataMapperMock.VerifyAll();
            _artikelDataMapperMock.VerifyAll();
            _eventPublisherMock.VerifyAll();
        }
        private Bestelling MapBestelling(BestellingCM bestellingCM)
        {
            var contactInfo  = Mapper.Map <ContactInfo>(bestellingCM.ContactInfo);
            var afleveradres = Mapper.Map <Afleveradres>(bestellingCM.Afleveradres);

            var bestelling = new Bestelling
            {
                Klantnummer  = bestellingCM.Klantnummer,
                ContactInfo  = contactInfo,
                Afleveradres = afleveradres,
                BestelStatus = BestelStatus.Geplaatst,
                Besteldatum  = DateTime.Now,
                BestelRegels = new List <BestelRegel>()
            };

            MapBestelRegel(bestellingCM, bestelling);
            CalculateBestellingSum(ref bestelling);
            return(bestelling);
        }
Example #7
0
        public void ShouldHandlePlaatsBestellingCommand()
        {
            var queueName = "PlaatsOrderQueue";

            _nijnHost.EventBus.DeclareQueue(queueName, new List <string> {
                NameConstants.BestelServiceBestellingGeplaatstEvent
            });

            var bestellingCM = new BestellingCM
            {
                Klantnummer = 1234,
                ContactInfo = new ContactInfoCM
                {
                    Naam           = "Pieter Pas",
                    Email          = "*****@*****.**",
                    Telefoonnummer = "1234567890"
                },
                Afleveradres = new AfleveradresCM
                {
                    Adres    = "Straat 1",
                    Plaats   = "Plaats",
                    Postcode = "1234 AB",
                    Land     = "Nederland"
                },
                BestelRegels = new List <BestelRegelCM>
                {
                    new BestelRegelCM
                    {
                        Artikelnummer = 1,
                        Aantal        = 42,
                    }
                }
            };

            var receiver = _nijnHost.CreateCommandReceiver(NameConstants.MagazijnServiceCommandQueue);

            receiver.DeclareCommandQueue();
            receiver.StartReceivingCommands(request => new ResponseCommandMessage(JsonConvert.SerializeObject(true), "boolean", request.CorrelationId));

            var requestCommand = new PlaatsBestellingCommand(bestellingCM, NameConstants.BestelServicePlaatsBestellingCommandQueue);

            _target.HandlePlaatsBestelling(requestCommand);

            var result = _context.Bestellingen.Include(b => b.BestelRegels).SingleOrDefault(b => b.Id == 1);

            Assert.IsNotNull(result, "result != null");
            Assert.AreEqual(1234, result.Klantnummer);
            Assert.AreEqual(DateTime.Now.Date, result.Besteldatum.Date);
            Assert.AreEqual(1, result.Factuurnummer);
            Assert.AreEqual(BestelStatus.Geplaatst, result.BestelStatus);

            Assert.AreEqual(bestellingCM.ContactInfo.Naam, result.ContactInfo.Naam);
            Assert.AreEqual(bestellingCM.ContactInfo.Email, result.ContactInfo.Email);
            Assert.AreEqual(bestellingCM.ContactInfo.Telefoonnummer, result.ContactInfo.Telefoonnummer);

            Assert.AreEqual(bestellingCM.Afleveradres.Adres, result.Afleveradres.Adres);
            Assert.AreEqual(bestellingCM.Afleveradres.Postcode, result.Afleveradres.Postcode);
            Assert.AreEqual(bestellingCM.Afleveradres.Plaats, result.Afleveradres.Plaats);
            Assert.AreEqual(bestellingCM.Afleveradres.Land, result.Afleveradres.Land);

            Assert.AreEqual(1, result.BestelRegels.Count);
            Assert.IsTrue(result.BestelRegels.Any(b => b.Id == 1 && b.Artikelnummer == 1 && b.Naam == "Fietsband" && b.PrijsExclBtw == 12.95m), "Should contain BestelRegel");

            Assert.AreEqual(1, _nijnHost.EventBus.Queues[queueName].MessageQueueLength);
        }
 public PlaatsBestellingCommand(BestellingCM bestelling, string routingKey) : base(routingKey)
 {
     Bestelling = bestelling;
 }
        public void HandlePlaatsBestelling_ShouldCalculateBestellingTotaal()
        {
            var artikel1 = new ArtikelBuilder()
                           .SetArtikelNummer(1)
                           .SetLeveranciercode("1-abc-xyz")
                           .SetNaam("Fietsband")
                           .SetPrijs(12.99m)
                           .Create();

            var artikel2 = new ArtikelBuilder()
                           .SetArtikelNummer(2)
                           .SetLeveranciercode("2-abc-xyz")
                           .SetNaam("Ventieldopje")
                           .SetPrijs(.99m)
                           .Create();

            _artikelDataMapperMock.Setup(a => a.GetById(1)).Returns(artikel1);
            _artikelDataMapperMock.Setup(a => a.GetById(2)).Returns(artikel2);

            Bestelling bestelling = null;

            _bestellingDataMapperMock.Setup(m => m.Insert(It.IsAny <Bestelling>()))
            .Callback <Bestelling>(b => { b.Id = 42; bestelling = b; })
            .Returns(bestelling);

            _bestellingDataMapperMock.Setup(m => m.Update(It.IsAny <Bestelling>()));

            BestellingGeplaatstEvent result = null;

            _eventPublisherMock.Setup(p => p.Publish(It.IsAny <DomainEvent>()))
            .Callback <DomainEvent>(b => result = (BestellingGeplaatstEvent)b);

            var bestellingCM = new BestellingCM
            {
                Klantnummer  = 1234,
                ContactInfo  = new ContactInfoCM(),
                Afleveradres = new AfleveradresCM(),
                BestelRegels = new List <BestelRegelCM>
                {
                    new BestelRegelCM {
                        Artikelnummer = 1, Aantal = 2
                    },
                    new BestelRegelCM {
                        Artikelnummer = 2, Aantal = 1
                    }
                }
            };

            var requestCommand = new PlaatsBestellingCommand(bestellingCM, NameConstants.BestelServicePlaatsBestellingCommandQueue);

            _target.HandlePlaatsBestelling(requestCommand);

            _bestellingDataMapperMock.VerifyAll();
            _artikelDataMapperMock.VerifyAll();
            _eventPublisherMock.VerifyAll();

            Assert.AreEqual(25.98m, result.Bestelling.BestelRegels[0].RegelTotaalExclBtw);
            Assert.AreEqual(31.44m, result.Bestelling.BestelRegels[0].RegelTotaalInclBtw);

            Assert.AreEqual(26.97m, result.Bestelling.FactuurTotaalExclBtw);
            Assert.AreEqual(32.64m, result.Bestelling.FactuurTotaalInclBtw);
        }