public async Task CreateBestelling_ShouldSendAdres_WhenAdresIsFilled()
        {
            // Arrange
            var klant = new KlantBuilder().SetDummy().SetId(10).SetEmail("*****@*****.**").Create();

            var bestelling = new Bestelling
            {
                ContactInfo  = new ContactInfo(),
                Afleveradres = new Afleveradres(),
                BestelRegels = new List <BestelRegel>()
            };

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

            _commandPublisherMock.Setup(repo => repo.Publish <long>(It.IsAny <PlaatsBestellingCommand>()))
            .ReturnsAsync(1);

            // Act
            var result = await _target.CreateBestelling(bestelling);

            // Assert
            _commandPublisherMock.VerifyAll();

            var createdResult = result as CreatedResult;

            Assert.IsNotNull(createdResult);
            Assert.AreEqual(201, createdResult.StatusCode);
            Assert.AreEqual((long)1, createdResult.Value);
        }
        public async Task CreateBestelling_ShouldReturn201StatusCode()
        {
            // Arrange
            var klant = new KlantBuilder().SetDummy().SetId(10).SetEmail("*****@*****.**").Create();

            var bestelling = new Bestelling()
            {
                ContactInfo  = new ContactInfo(),
                Afleveradres = new Afleveradres(),
                BestelRegels = new List <BestelRegel>()
            };

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

            _commandPublisherMock.Setup(repo => repo.Publish <long>(It.Is <PlaatsBestellingCommand>(c =>
                                                                                                    c.RoutingKey == NameConstants.BestelServicePlaatsBestellingCommandQueue &&
                                                                                                    c.Bestelling.Klantnummer == klant.Id
                                                                                                    ))).ReturnsAsync(42);

            // Act
            var result = await _target.CreateBestelling(bestelling);

            // Assert
            _commandPublisherMock.VerifyAll();
            _klantDataMapperMock.VerifyAll();

            var createdResult = result as CreatedResult;

            Assert.IsNotNull(createdResult);
            Assert.AreEqual(201, createdResult.StatusCode);
            Assert.IsInstanceOfType(createdResult.Value, typeof(long));
            Assert.AreEqual(42L, createdResult.Value);
        }
        public async Task CreateBestelling_ShouldReturn500StatusCode_WhenUnknownExceptionOccured_InBestellingCommand()
        {
            // Arrange
            var klant = new KlantBuilder().SetDummy().SetId(10).SetEmail("*****@*****.**").Create();

            var bestelling = new Bestelling
            {
                ContactInfo  = new ContactInfo(),
                Afleveradres = new Afleveradres(),
                BestelRegels = new List <BestelRegel>()
            };

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

            _commandPublisherMock.Setup(repo => repo.Publish <long>(It.IsAny <PlaatsBestellingCommand>()))
            .ThrowsAsync(new Exception());

            // Act
            var result = await _target.CreateBestelling(bestelling);

            // Assert
            _commandPublisherMock.VerifyAll();

            var requestResult = result as StatusCodeResult;

            Assert.IsNotNull(requestResult);
            Assert.AreEqual(500, requestResult.StatusCode);
        }
        public async Task CreateBestelling_ShouldReturn400StatusCode_WhenBestellingIsInvalid()
        {
            // Arrange
            var klant = new KlantBuilder().SetDummy().SetId(10).SetEmail("*****@*****.**").Create();

            var bestelling = new Bestelling
            {
                ContactInfo  = new ContactInfo(),
                Afleveradres = new Afleveradres(),
                BestelRegels = new List <BestelRegel>()
            };

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

            _commandPublisherMock.Setup(repo => repo.Publish <long>(It.IsAny <PlaatsBestellingCommand>()))
            .ThrowsAsync(new DatabaseException("error"));

            // Act
            var result = await _target.CreateBestelling(bestelling);

            // Assert
            _commandPublisherMock.VerifyAll();

            var badRequestResult = result as BadRequestObjectResult;

            Assert.IsNotNull(badRequestResult);
            Assert.AreEqual("Bestelling bevat incorrecte data", badRequestResult.Value);
        }
        public async Task CreateBestelling_WithBestelRegels_ShouldReturn201StatusCode()
        {
            // Arrange
            var klant = new KlantBuilder().SetDummy().SetId(10).SetEmail("*****@*****.**").Create();

            var bestelling = new Bestelling()
            {
                ContactInfo  = new ContactInfo(),
                Afleveradres = new Afleveradres(),
                BestelRegels = new List <BestelRegel>()
                {
                    new BestelRegel()
                    {
                        Aantal        = 1,
                        Artikelnummer = 1254
                    }
                }
            };

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

            _commandPublisherMock.Setup(repo => repo.Publish <long>(It.Is <PlaatsBestellingCommand>(c =>
                                                                                                    c.Bestelling.BestelRegels.Any(r => r.Aantal == 1 && r.Artikelnummer == 1254)
                                                                                                    ))).ReturnsAsync(10);

            // Act
            var result = await _target.CreateBestelling(bestelling);

            // Assert
            _commandPublisherMock.VerifyAll();

            var createdResult = result as CreatedResult;

            Assert.IsNotNull(createdResult);
            Assert.AreEqual(201, createdResult.StatusCode);
            Assert.AreEqual((long)10, createdResult.Value);
        }
        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));
            }
        }
        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));
        }