public void CanConvertSingleAccountPayload()
        {
            //Arrange
            var fakeCostDto = new CostDto()
            {
                ResultsByTime = new[]
                {
                    new ResultByTimeDto()
                    {
                        Total = new[]
                        {
                            new KeyValuePair <string, MetricValueDto>("BlendedCost", new MetricValueDto()
                            {
                                Amount = "100", Unit = "USD"
                            }),
                            new KeyValuePair <string, MetricValueDto>("BlendedCost", new MetricValueDto()
                            {
                                Amount = "200", Unit = "DKK"
                            })
                        }
                    }
                }
            };

            var sut = new CostDtoToReportRootConverter();

            //Act
            var result = sut.Convert(fakeCostDto, null, null);

            //Assert
            Assert.NotNull(sut);
            Assert.True(result is ReportRoot);
            Assert.Equal("monthlyTotalCost", result.CostItems.First().Label);
            Assert.Equal("300", result.CostItems.First().Value);
        }
Example #2
0
        public void CanBeConstructed()
        {
            //Arrange
            CostDto sut;

            //Act
            sut = new CostDto();

            //Assert
            Assert.NotNull(sut);
        }
Example #3
0
        public async Task CreateCost(int?tenantId, long userId, CostDto data)
        {
            var cost = _mapper.Map <Cost>(data);

            if (!string.IsNullOrEmpty(data.CostDate))
            {
                cost.CostDate = Convert.ToDateTime(data.CostDate);
            }
            cost.Code           = Constants.GenerateCode();
            cost.CreatedDate    = DateTime.Now;
            cost.UserModifiedId = userId;
            cost.TenantId       = tenantId.Value;
            _unitOfWork.CostRepository.Create(cost);
            await _unitOfWork.Commit();
        }
Example #4
0
        public void CanBeSerialized()
        {
            //Arrange
            var sut = new CostDto()
            {
                DimensionValueAttributes = new List <DimensionValueAttributeDto>(),
                ResultsByTime            = new List <ResultByTimeDto>()
            };

            //Act
            var payload = JsonSerializer.Serialize(sut, new JsonSerializerOptions {
                IgnoreNullValues = true
            });

            //Assert
            Assert.NotNull(JsonDocument.Parse(payload));
        }
        public void CanConvertAllAccountsPayload()
        {
            //Arrange
            var fakeCostDto = new CostDto()
            {
                DimensionValueAttributes = new[] { new DimensionValueAttributeDto()
                                                   {
                                                       Attributes = new[] { new KeyValuePair <string, string>("description", "dfds-AWS_ACCOUNT_NAME") }, Value = "AWS_ACCOUNT_ID"
                                                   } },
                ResultsByTime = new[]
                {
                    new ResultByTimeDto()
                    {
                        Groups = new[]
                        {
                            new GroupDto()
                            {
                                Keys = new[] { "AWS_ACCOUNT_ID" }, Metrics = new[] { new KeyValuePair <string, MetricValueDto>("BlendedCost", new MetricValueDto()
                                    {
                                        Amount = "100", Unit = "USD"
                                    }) }
                            },
                            new GroupDto()
                            {
                                Keys = new[] { "AWS_ACCOUNT_ID" }, Metrics = new[] { new KeyValuePair <string, MetricValueDto>("BlendedCost", new MetricValueDto()
                                    {
                                        Amount = "200", Unit = "DKK"
                                    }) }
                            }
                        }
                    }
                }
            };

            var sut = new CostDtoToReportRootConverter();

            //Act
            var result = sut.Convert(fakeCostDto, null, null);

            //Assert
            Assert.NotNull(sut);
            Assert.True(result is ReportRoot);
            Assert.Equal("AWS_ACCOUNT_NAME", result.CostItems.First().CapabilityIdentifier);
            Assert.Equal("monthlyTotalCost", result.CostItems.First().Label);
            Assert.Equal("300", result.CostItems.First().Value);
        }
        public JsonResult Cost([Bind(Prefix = "InvestPart")] InvestPartBm bm)
        {
            var result = new CostDto();

            var carId = bm.CarId;

            if (carId < 1)
            {
                this.Response.StatusCode = 400;
                return(new JsonResult()
                {
                    Data = new CostDto()
                    {
                        ErrorMessage = "Cannot process request!"
                    }
                });
            }

            Cost investmentEntity = null;

            if (!string.IsNullOrWhiteSpace(bm.Name) && bm.Price > 0)// && (bm.DistanceTraveled > 0 || bm.MountedOnMi > 0 || bm.MountedOnKm > 0))
            {
                var newInvestment = Mapper.Map <InvestPartBm, CreateInvestmentVm>(bm);

                investmentEntity = this.carManager.AddNewInvestment(newInvestment, carId, this.GetAppUserId);

                var isInvestAdded = investmentEntity != null;

                if (isInvestAdded)
                {
                    result.HasInvest     = isInvestAdded;
                    result.InvestMessage = this.GetMessage(newInvestment.Name, isInvestAdded);
                }
                else
                {
                    result.HasInvest    = isInvestAdded;
                    result.HasError     = !isInvestAdded;
                    result.ErrorMessage = this.GetMessage(newInvestment.Name, isInvestAdded);
                }
            }
            else if ((string.IsNullOrWhiteSpace(bm.Name) && bm.Price > 0) || (!string.IsNullOrWhiteSpace(bm.Name) && bm.Price <= 0) || (string.IsNullOrWhiteSpace(bm.Name) && bm.Price < 0))
            {
                result.HasError      = true;
                result.HasInvest     = false;
                result.InvestMessage = string.Format("Cost/Investment contain invalid data!\r\nCost/Investment name: {0}\r\nCost/Investment Price: {1}", bm.Name, bm.Price);
            }

            if (!string.IsNullOrWhiteSpace(bm.PartName) && bm.PartPrice > 0)
            {
                var quantity = 1;

                if (bm.Quantity > 1)
                {
                    quantity = bm.Quantity;
                }

                var carPart = Mapper.Map <InvestPartBm, CreateCarPartVm>(bm);

                var isCarPartAdded = true;

                for (int i = 0; i < quantity; i++)
                {
                    isCarPartAdded = this.carManager.AddReplacedPart(carPart, carId, this.GetAppUserId);

                    if (!isCarPartAdded)
                    {
                        break;
                    }
                }

                if (isCarPartAdded)
                {
                    result.HasInvest     = isCarPartAdded;
                    result.InvestMessage = this.GetMessage(carPart.PartName, isCarPartAdded, quantity);
                }
                else
                {
                    this.Response.StatusCode = 400;

                    result.HasInvest    = isCarPartAdded;
                    result.HasError     = !isCarPartAdded;
                    result.ErrorMessage = this.GetMessage(carPart.PartName, isCarPartAdded);

                    return(new JsonResult()
                    {
                        Data = result
                    });
                }
            }
            else if ((string.IsNullOrWhiteSpace(bm.PartName) && bm.PartPrice > 0) || (!string.IsNullOrWhiteSpace(bm.PartName) && bm.PartPrice <= 0) || (string.IsNullOrWhiteSpace(bm.PartName) && bm.PartPrice < 0))
            {
                result.NewPartMessage = string.Format("New Part contain invalid data!\r\nPart name: {0}\r\nPart Price: {1}", bm.PartName, bm.PartPrice);
            }

            return(new JsonResult()
            {
                Data = result
            });
        }