Exemple #1
0
        public long HandleVoegKlantToe(VoegKlantToeCommand request)
        {
            var klant = request.Klant;

            try
            {
                _klantDataMapper.Insert(klant);
            }
            catch (DbUpdateException ex)
            {
                _logger.LogError("Email allready exists with email: {0}", klant.Email);
                _logger.LogDebug(
                    "DB exception occured with klant {}, it threw exception: {}. Inner exception: {}",
                    klant, ex.Message, ex.InnerException?.Message);
                throw new EmailAllreadyExistsException("Something unexpected happened whule inserting into the database");
            }
            catch (Exception ex)
            {
                _logger.LogError("DB exception occured with klantnummer: {0}", klant?.Id);
                _logger.LogDebug(
                    "DB exception occured with klant {}, it threw exception: {}. Inner exception: {}",
                    klant, ex.Message, ex.InnerException?.Message
                    );
                throw new DatabaseException("Something unexpected happend while inserting into the database");
            }

            _eventPublish.Publish(new KlantToegevoegdEvent(klant, NameConstants.KlantBeheerKlantToegevoegdEvent));
            return(klant.Id);
        }
        public void HandleVoegKlantToe_ShouldReturnExistingKlantIdWhenEmailExists()
        {
            var eventQueueName = "TestEventQueue";
            var klant          = _klantBuilder.SetDummy().SetEmail("*****@*****.**").Create();
            var command        = new VoegKlantToeCommand(klant, NameConstants.KlantBeheerVoegKlantToeCommand);

            _nijnContext.EventBus.DeclareQueue(eventQueueName, new List <string> {
                NameConstants.KlantBeheerKlantToegevoegdEvent
            });

            Action action = () => _target.HandleVoegKlantToe(command);

            var eventQueue = _nijnContext.EventBus.Queues[eventQueueName];

            Assert.AreEqual(0, eventQueue.MessageQueueLength);
            Assert.ThrowsException <EmailAllreadyExistsException>(action);
        }
        public void HandleVoegKlantToe_ShouldReturnExistingKlantId_WhenEmailExists()
        {
            // Arrange
            long klantId = 42;
            var  klant   = new KlantBuilder().SetDummy().SetId(klantId).SetEmail("*****@*****.**").Create();
            var  klanten = new List <Klant> {
                klant
            };
            var command = new VoegKlantToeCommand(klant, "");

            _klantDataMapperMock.Setup(k => k.Insert(It.IsAny <Klant>())).Throws(new DbUpdateException("", new Exception()));

            // Act
            Action result = () => _target.HandleVoegKlantToe(command);

            // Assert
            Assert.ThrowsException <EmailAllreadyExistsException>(result);
        }
        public void HandleVoegKlantToe_ShouldThrowException_When_IncorrectInput()
        {
            // Arrange
            var klant   = new KlantBuilder().SetDummy().Create();
            var command = new VoegKlantToeCommand(klant, "");


            _klantDataMapperMock.Setup(repo => repo.Insert(It.IsAny <Klant>())).Throws <Exception>();

            // Act
            Action act = () =>
            {
                _target.HandleVoegKlantToe(command);
            };

            // Assert
            var exception = Assert.ThrowsException <DatabaseException>(act);

            Assert.AreEqual("Something unexpected happend while inserting into the database", exception.Message);
        }
        public void HandleVoegKlantToe_ShouldInsertKlantIntoTheDatabase()
        {
            var eventQueueName = "TestEventQueue";
            var klant          = _klantBuilder.SetDummy().SetId(3).SetEmail("*****@*****.**").Create();
            var command        = new VoegKlantToeCommand(klant, NameConstants.KlantBeheerVoegKlantToeCommand);

            _nijnContext.EventBus.DeclareQueue(eventQueueName, new List <string> {
                NameConstants.KlantBeheerKlantToegevoegdEvent
            });

            var result = _target.HandleVoegKlantToe(command);

            var eventQueue = _nijnContext.EventBus.Queues[eventQueueName];

            Assert.AreEqual(1, eventQueue.MessageQueueLength);

            var dbResult = _dbContext.Klanten.Include(k => k.Adres).SingleOrDefault(k => k.Id == 3);

            Assert.IsNotNull(dbResult);
            Assert.AreEqual(klant.Email, dbResult.Email);
        }
        public void HandleVoegKlantToe_ShouldCallDataMapperInsert()
        {
            // Arrange
            Klant klant   = new KlantBuilder().SetDummy().Create();
            var   command = new VoegKlantToeCommand(klant, "");

            Klant insertParam = null;

            _klantDataMapperMock.Setup(repo => repo.Insert(It.IsAny <Klant>())).Returns(klant)
            .Callback <Klant>(entity =>
            {
                insertParam = entity;
            });
            _eventPublisherMock.Setup(p => p.Publish(It.IsAny <KlantToegevoegdEvent>()));

            // Act
            var result = _target.HandleVoegKlantToe(command);

            // Assert
            _klantDataMapperMock.VerifyAll();
            Assert.IsNotNull(result);
            Assert.IsTrue(klant.IsEqual(klant));
        }
        public async Task <ActionResult> Register([FromBody] KlantRegistration registration)
        {
            if (string.IsNullOrWhiteSpace(registration.Email) || string.IsNullOrWhiteSpace(registration.Password))
            {
                return(BadRequest("Invalid post parameters"));
            }

            //TODO Check if email and password are valid

            try
            {
                var registerCommand = new RegisterCommand(
                    new Credential()
                {
                    Email = registration.Email, Password = registration.Password
                },
                    NameConstants.AuthenticationServiceRegisterCommand
                    );

                RegisterResult registerResult = await _commandPublisher.Publish <RegisterResult>(registerCommand);

                if (registerResult != RegisterResult.Ok)
                {
                    return(Conflict("Account already exists"));
                }
            }
            catch (TimeoutException)
            {
                _logger.LogError("RegisterCommand resulted in a TimeoutException.");
                return(StatusCode((int)HttpStatusCode.RequestTimeout, "Aanvraag kan niet worden verwerkt"));
            }
            catch (Exception ex)
            {
                _logger.LogError("RegisterCommand resulted in an error.");
                _logger.LogDebug(
                    "Exception occured during execution of RegisterCommand, it threw exception: {}. Inner exception: {}",
                    ex.Message, ex.InnerException?.Message
                    );
                return(BadRequest("Failed to register due to an internal error"));
            }
            try
            {
                var klant        = Mapper.Map <Klant>(registration.Klant);
                var klantCommand = new VoegKlantToeCommand(klant, NameConstants.KlantBeheerVoegKlantToeCommand);
                await _commandPublisher.Publish <long>(klantCommand);

                return(Ok());
            }
            catch (TimeoutException)
            {
                _logger.LogError("KlantCommand resulted in a TimeoutException.");
                return(StatusCode((int)HttpStatusCode.RequestTimeout, "Aanvraag kan niet worden verwerkt"));
            }
            catch (Exception ex)
            {
                _logger.LogError("KlantCommand resulted in an error.");
                _logger.LogDebug(
                    "Exception occured during execution of KlantCommand, it threw exception: {}. Inner exception: {}",
                    ex.Message, ex.InnerException?.Message
                    );
                return(BadRequest("Failed to create klant due to an internal error"));
            }
        }