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();
        }
        public long HandlePlaatsBestelling(PlaatsBestellingCommand request)
        {
            var bestelling = MapBestelling(request.Bestelling);

            try
            {
                _bestellingDataMapper.Insert(bestelling);
                bestelling.Factuurnummer = bestelling.Id;
                _bestellingDataMapper.Update(bestelling);
            }
            catch (Exception ex)
            {
                _logger.LogError("DB exception occured with klantnummer: {0}", bestelling.Klantnummer);
                _logger.LogDebug(
                    "DB exception occured with klant {}, it threw exception: {}. Inner exception: {}",
                    bestelling, ex.Message, ex.InnerException?.Message
                    );
                throw new DatabaseException("Something unexpected happend while inserting into the database");
            }

            _eventPublish.Publish(new BestellingGeplaatstEvent(bestelling, NameConstants.BestelServiceBestellingGeplaatstEvent));
            return(bestelling.Id);
        }
        public async Task <IActionResult> CreateBestelling([FromBody] Bestelling incomingBestelling)
        {
            var klantEmail = _jwtHelper.GetEmail(HttpContext);
            var klant      = _klantDataMapper.GetByEmail(klantEmail);

            var bestelling = Mapper.Map <BestellingCM>(incomingBestelling);

            bestelling.Klantnummer = klant.Id;

            try
            {
                var bestellingCommand = new PlaatsBestellingCommand(bestelling, NameConstants.BestelServicePlaatsBestellingCommandQueue);
                var result            = await _commandPublisher.Publish <long>(bestellingCommand);

                return(Created($"/api/bestelling/{result}", result));
            }
            catch (DatabaseException)
            {
                _logger.LogError("PlaatsBestellingCommand resulted in a DatabaseException.");
                return(BadRequest("Bestelling bevat incorrecte data"));
            }
            catch (TimeoutException)
            {
                _logger.LogError("PlaatsBestellingCommand resulted in a TimeoutException.");
                return(StatusCode((int)HttpStatusCode.RequestTimeout, "Aanvraag kon niet worden verwerkt"));
            }
            catch (Exception ex)
            {
                _logger.LogError("PlaatsBestellingCommand resulted in an internal server error.");
                _logger.LogDebug(
                    "Exception occured during execution of PlaatsBestellingCommand {}, it threw exception: {}. Inner exception: {}",
                    bestelling, ex.Message, ex.InnerException?.Message
                    );

                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }
Example #6
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 async Task CreateBestelling_ShouldCreateNewBestelling()
        {
            var klant = new Klant
            {
                Voornaam       = "Pieter",
                Achternaam     = "Pas",
                Email          = "*****@*****.**",
                Telefoonnummer = "06123456789",
                Adres          = new Adres
                {
                    Straatnaam = "Schepen Canishof",
                    Huisnummer = "71",
                    Postcode   = "6831 HJ",
                    Plaats     = "Arnhem",
                    Land       = "Nederland"
                }
            };

            _dbContext.KlantEntities.Add(klant);
            _dbContext.SaveChanges();

            var bestelling = new Bestelling
            {
                ContactInfo = new ContactInfo
                {
                    Naam           = $"{klant.Voornaam} {klant.Achternaam}",
                    Email          = klant.Email,
                    Telefoonnummer = klant.Telefoonnummer
                },
                Afleveradres = new Afleveradres
                {
                    Adres    = $"{klant.Adres.Straatnaam} {klant.Adres.Huisnummer}",
                    Plaats   = klant.Adres.Plaats,
                    Postcode = klant.Adres.Postcode,
                    Land     = klant.Adres.Land
                },
                BestelRegels = new List <BestelRegel>
                {
                    new BestelRegel
                    {
                        Artikelnummer = 10,
                        Aantal        = 5
                    }
                }
            };

            _jwtHelperMock.Setup(h => h.GetEmail(It.IsAny <HttpContext>())).Returns(klant.Email);

            PlaatsBestellingCommand commandResult = null;
            var receiver = _nijnContext.CreateCommandReceiver(NameConstants.BestelServicePlaatsBestellingCommandQueue);

            receiver.DeclareCommandQueue();
            receiver.StartReceivingCommands(request =>
            {
                commandResult = JsonConvert.DeserializeObject <PlaatsBestellingCommand>(request.Message);
                return(new ResponseCommandMessage("10", "Long", request.CorrelationId));
            });

            var result = await _target.CreateBestelling(bestelling);

            var queue = _nijnContext.CommandBus.Queues[NameConstants.BestelServicePlaatsBestellingCommandQueue];

            Assert.IsInstanceOfType(result, typeof(CreatedResult));
            Assert.AreEqual(1, queue.CalledTimes);

            Assert.IsNotNull(commandResult, "commandResult != null");
            var bestellingResult = commandResult.Bestelling;

            Assert.AreEqual(bestelling.ContactInfo.Naam, bestellingResult.ContactInfo.Naam);
            Assert.AreEqual(bestelling.ContactInfo.Email, bestellingResult.ContactInfo.Email);
            Assert.AreEqual(bestelling.ContactInfo.Telefoonnummer, bestellingResult.ContactInfo.Telefoonnummer);

            Assert.AreEqual(bestelling.Afleveradres.Adres, bestellingResult.Afleveradres.Adres);
            Assert.AreEqual(bestelling.Afleveradres.Plaats, bestellingResult.Afleveradres.Plaats);
            Assert.AreEqual(bestelling.Afleveradres.Postcode, bestellingResult.Afleveradres.Postcode);
            Assert.AreEqual(bestelling.Afleveradres.Land, bestellingResult.Afleveradres.Land);

            Assert.AreEqual(1, bestellingResult.BestelRegels.Count);
            Assert.IsTrue(bestellingResult.BestelRegels.Any(r => r.Artikelnummer == 10 && r.Aantal == 5));
        }
        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);
        }