Ejemplo n.º 1
0
        public IActionResult DissociateCustomerAsset([FromBody] DissociateCustomerAssetEvent customerAsset)
        {
            try
            {
                customerAsset.ReceivedUTC = DateTime.UtcNow;
                if (customerService.GetCustomer(customerAsset.CustomerUID) == null)
                {
                    logger.LogInformation(Messages.CustomerDoesntExist);
                    return(BadRequest(Messages.CustomerDoesntExist));
                }

                var customerAssetInDb
                    = customerAssetService.GetAssetCustomer(customerAsset.CustomerUID, customerAsset.AssetUID);
                if (customerAssetInDb == null)
                {
                    logger.LogInformation(Messages.CustomerAssetDoesntExist);
                    return(BadRequest(Messages.CustomerAssetDoesntExist));
                }

                if (customerAssetService.DissociateCustomerAsset(customerAsset))
                {
                    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));
            }
        }
Ejemplo n.º 2
0
        public bool DissociateCustomerAsset(DissociateCustomerAssetEvent dissociateCustomerAsset)
        {
            try
            {
                var messages = CustomerTopics
                               ?.Select(topic => new KafkaMessage
                {
                    Key     = dissociateCustomerAsset.CustomerUID.ToString(),
                    Message = new { DissociateCustomerAssetEvent = dissociateCustomerAsset },
                    Topic   = topic
                })
                               ?.ToList();

                var deleteQuery = string.Format("DELETE FROM md_customer_CustomerAsset " +
                                                "WHERE fk_CustomerUID = {0} AND fk_AssetUID = {1};",
                                                dissociateCustomerAsset.CustomerUID.ToStringAndWrapWithUnhex(),
                                                dissociateCustomerAsset.AssetUID.ToStringAndWrapWithUnhex());

                var actions = new List <Action>()
                {
                    () => transaction.Delete(deleteQuery),
                    () => transaction.Publish(messages)
                };
                return(transaction.Execute(actions));
            }
            catch (Exception ex)
            {
                logger.LogError($"Error while dissociating asset from customer : {ex.Message}, {ex.StackTrace}");
                throw;
            }
        }
Ejemplo n.º 3
0
        public static DissociateCustomerAssetEvent GetDefaultValidDissociateCustomerAssetServiceRequest()
        {
            DissociateCustomerAssetEvent defaultValidDissociateCustomerAssetServiceModel = new DissociateCustomerAssetEvent();

            defaultValidDissociateCustomerAssetServiceModel.CustomerUID = customerServiceSupport.CreateCustomerModel.CustomerUID;
            defaultValidDissociateCustomerAssetServiceModel.AssetUID    = customerAssetServiceSupport.AssociateCustomerAssetModel.AssetUID;
            defaultValidDissociateCustomerAssetServiceModel.ActionUTC   = DateTime.UtcNow;
            defaultValidDissociateCustomerAssetServiceModel.ReceivedUTC = null;
            return(defaultValidDissociateCustomerAssetServiceModel);
        }
Ejemplo n.º 4
0
        public void DissociateCustomerAsset_ValidPayload_ExpectedTransactionStatus(
            bool transactionStatus, int deleteCalls, int publishCalls, bool hasException)
        {
            //Arrange
            var customerUid        = Guid.NewGuid();
            var assetUid           = Guid.NewGuid();
            var assetCustomerEvent = new DissociateCustomerAssetEvent
            {
                CustomerUID = customerUid,
                AssetUID    = assetUid,
                ActionUTC   = DateTime.UtcNow,
                ReceivedUTC = DateTime.UtcNow
            };
            var deleteQuery = $"DELETE FROM md_customer_CustomerAsset " +
                              $"WHERE fk_CustomerUID = UNHEX('{assetCustomerEvent.CustomerUID.ToString("N")}')" +
                              $" AND fk_AssetUID = UNHEX('{assetCustomerEvent.AssetUID.ToString("N")}');";

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

            //Act
            if (hasException)
            {
                Assert.Throws <Exception>(() => customerAssetService.DissociateCustomerAsset(assetCustomerEvent));
            }
            else
            {
                var resultData = customerAssetService.DissociateCustomerAsset(assetCustomerEvent);
                Assert.Equal(transactionStatus, resultData);
            }

            //Assert
            transaction.Received(deleteCalls).Delete(Arg.Is(deleteQuery));
            transaction.Received(publishCalls).Publish(
                Arg.Is <List <KafkaMessage> >(messages => messages
                                              .TrueForAll(m => ValidateCustomerAssetKafkaObject(true, m, assetCustomerEvent))));
        }