Beispiel #1
0
        public IActionResult CreateUserCustomerRelationship(
            [FromBody] CreateUserCustomerRelationshipEvent userCustomerRelationship)
        {
            try
            {
                userCustomerRelationship.ReceivedUTC = DateTime.UtcNow;
                if (customerService.GetCustomer(userCustomerRelationship.CustomerUID) == null)
                {
                    logger.LogInformation(Messages.CustomerDoesntExist);
                    return(BadRequest(Messages.CustomerDoesntExist));
                }

                if (userCustomerService.GetCustomerUser(userCustomerRelationship.CustomerUID,
                                                        userCustomerRelationship.UserUID) != null)
                {
                    logger.LogInformation(Messages.CustomerUserAlreadyExists);
                    return(BadRequest(Messages.CustomerUserAlreadyExists));
                }

                if (customerService.CreateUserCustomerRelationship(userCustomerRelationship))
                {
                    return(Ok());
                }

                logger.LogWarning(Messages.UnableToSaveToDb);
                return(BadRequest(Messages.UnableToSaveToDb));
            }
            catch (Exception ex)
            {
                logger.LogError(string.Format(Messages.ExceptionOccured, ex.Message, ex.StackTrace));
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Beispiel #2
0
        public void CreateUserCutsomerRelationship_GivenPayload_ExpectedStatus(
            CreateUserCustomerRelationshipEvent relationshipEvent, bool hasValidCustomer, bool transactionStatus,
            int upsertCalls, int publishCalls, bool hasException)
        {
            //Arrange
            DbCustomer customerData = hasValidCustomer
                                ? new DbCustomer()
            {
                CustomerID = 109, CustomerUID = relationshipEvent.CustomerUID
            } : null;

            if (hasException)
            {
                transaction.Get <DbCustomer>(Arg.Any <string>()).Returns(e => throw new Exception());
            }
            else
            {
                transaction.Get <DbCustomer>(Arg.Any <string>()).Returns(new List <DbCustomer> {
                    customerData
                });
            }
            transaction.Execute(Arg.Any <List <Action> >())
            .Returns(a =>
            {
                a.Arg <List <Action> >().ForEach(action => action.Invoke());
                return(true);
            });

            if (hasException)
            {
                Assert.Throws <Exception>(() => customerService.CreateUserCustomerRelationship(relationshipEvent));
            }
            else
            {
                var resultData = customerService.CreateUserCustomerRelationship(relationshipEvent);


                Assert.Equal(transactionStatus, resultData);
            }

            //Assert
            transaction.Received(upsertCalls).Upsert(
                Arg.Is <DbUserCustomer>(userCust =>
                                        ValidateCustomerUserObject(relationshipEvent, userCust, customerData)));
            transaction.Received(publishCalls).Publish(
                Arg.Is <List <KafkaMessage> >(messages => messages
                                              .TrueForAll(m => ValidateCustomerUserKafkaObject(false, false, m, relationshipEvent))));
        }