Esempio n. 1
0
        private async Task <VendorModel> CreateVendor(string url)
        {
            var updateVendorModel = new SaveVendorModel
            {
                Name                 = $"TestVendor_{Guid.NewGuid()}",
                SapVendorCode        = $"SAP_{Interlocked.Increment(ref _counter)}",
                VendorCategoryModels = new[]
                {
                    new VendorCategoryModel
                    {
                        IsPreferredSupplier = false,
                        HasDirectPayment    = false,
                        DefaultCurrencyId   = _defaultCurrencyId,
                        Name = "ProductionCategory"
                    }
                }
            };
            var result = await Browser.Put(url, c =>
            {
                c.User(_user);
                c.JsonBody(updateVendorModel);
            });

            var vendorResult = Deserialize <VendorModel>(result, HttpStatusCode.OK);

            return(vendorResult);
        }
Esempio n. 2
0
        private async Task <VendorModel> UpdateVendor(SaveVendorModel vendorModel)
        {
            var result = await Browser.Put(BaseUrl, c =>
            {
                c.User(_user);
                c.JsonBody(vendorModel);
            });

            var vendorResult = Deserialize <VendorModel>(result, HttpStatusCode.OK);

            return(vendorResult);
        }
Esempio n. 3
0
            public async Task Vendors_CreateNewVendor_WhenCurrencyIsNotProvided()
            {
                var          url                = "v1/vendors";
                const string vendorName         = "Vendor_CreateNewVendor_WhenCurrencyIsNotProvide";
                const string productionCategory = "Production";
                const string sapVendorCode      = "S_ALR_87012088";

                var vendor = new SaveVendorModel
                {
                    Name                 = vendorName,
                    SapVendorCode        = sapVendorCode,
                    VendorCategoryModels = new[]
                    {
                        new VendorCategoryModel
                        {
                            IsPreferredSupplier = false,
                            HasDirectPayment    = true,
                            Name = productionCategory,
                        }
                    }
                };
                var result = await Browser.Put(url, c =>
                {
                    c.User(_user);
                    c.JsonBody(vendor);
                });

                result.StatusCode.Should().Be(HttpStatusCode.OK);

                var vendorResult = Deserialize <VendorModel>(result, HttpStatusCode.OK);

                vendorResult.Id.Should().NotBe(Guid.Empty);
                vendorResult.Name.Should().Be(vendorName);

                vendorResult.SapVendorCode.Should().Be(sapVendorCode);

                vendorResult.VendorCategoryModels.Count.Should().Be(1);
                vendorResult.VendorCategoryModels.First().Name.Should().Be(productionCategory);
                vendorResult.VendorCategoryModels.First().HasDirectPayment.Should().BeTrue();
                vendorResult.VendorCategoryModels.First().IsPreferredSupplier.Should().BeFalse();
                vendorResult.VendorCategoryModels.First().DefaultCurrencyId.Should().Be(_defaultCurrencyId, "default currency has to be used if currency is not provided in the request.");
            }
Esempio n. 4
0
        public void SaveVendorModel_To_Vendor_IsValid()
        {
            var expectedId            = Guid.NewGuid();
            var expectedName          = "Bradford";
            var expectedSapVendorCode = "SapCode";

            var model = new SaveVendorModel
            {
                Id            = expectedId,
                Name          = expectedName,
                SapVendorCode = expectedSapVendorCode
            };
            var result = _mapper.Map <SaveVendorModel, Vendor>(model);

            result.Should().NotBeNull();
            result.Id.Should().Be(model.Id);
            result.Id.Should().Be(expectedId);
            result.Name.Should().Be(expectedName);
            result.Name.Should().Be(model.Name);
        }
Esempio n. 5
0
            public async Task Vendors_CreateNewVendor()
            {
                const string url                = "v1/vendors";
                const string vendorName         = "Vendor1_CreateNewVendor";
                const string productionCategory = "Production";
                const string sapVendorCode      = "S_ALR_87012086";

                var vendor = new SaveVendorModel
                {
                    Name                 = vendorName,
                    SapVendorCode        = sapVendorCode,
                    VendorCategoryModels = new[]
                    {
                        new VendorCategoryModel
                        {
                            IsPreferredSupplier = false,
                            HasDirectPayment    = true,
                            Name = productionCategory,
                            DefaultCurrencyId = _defaultCurrencyId
                        }
                    }
                };
                var result = await Browser.Put(url, c =>
                {
                    c.User(_user);
                    c.JsonBody(vendor);
                });

                result.StatusCode.Should().Be(HttpStatusCode.OK);

                var vendorResult = Deserialize <VendorModel>(result, HttpStatusCode.OK);

                vendorResult.Name.Should().Be(vendorName);
                vendorResult.SapVendorCode.Should().Be(sapVendorCode);
                vendorResult.VendorCategoryModels.Count.Should().Be(1);
                vendorResult.VendorCategoryModels.First().Name.Should().Be(productionCategory);
                vendorResult.VendorCategoryModels.First().HasDirectPayment.Should().BeTrue();
                vendorResult.VendorCategoryModels.First().IsPreferredSupplier.Should().BeFalse();
                vendorResult.VendorCategoryModels.First().PaymentRules.Should().BeNullOrEmpty();
            }
        private async Task <OperationResponse> CreateSampleVendor(string vendorName = null)
        {
            var newVendor = new SaveVendorModel {
            };
            var dbVendor  = new Vendor()
            {
                Name = vendorName
            };

            _currencyServiceMock.Setup(c => c.GetDefaultCurrency()).ReturnsAsync(new Currency());

            _vendorRuleBuilderMock.Setup(b =>
                                         b.ValidateAndGetVendorRules(It.IsAny <VendorRuleModel[]>(), It.IsAny <Vendor>(), It.IsAny <Guid>()))
            .ReturnsAsync(new List <VendorRule> {
                new VendorRule {
                    VendorCategory = new VendorCategory
                    {
                        Name                = CategoryName,
                        HasDirectPayment    = false,
                        IsPreferredSupplier = false,
                        Vendor              = dbVendor
                    }, Rule = new dataAccess.Entity.Rule()
                }
            });

            _mapperMock.Setup(m => m.Map <SaveVendorModel, Vendor>(It.IsAny <SaveVendorModel>(), It.IsAny <Vendor>())).Returns(dbVendor);
            _mapperMock.Setup(m => m.Map(It.IsAny <Currency>(), It.IsAny <Vendor>())).Returns(new Vendor());
            _mapperMock.Setup(m => m.Map <VendorModel>(It.IsAny <Vendor>())).Returns(new VendorModel());
            _mapperMock.Setup(m => m.Map <VendorCategory>(It.IsAny <VendorCategoryModel>())).Returns(
                new VendorCategory
            {
                Name = CategoryName,
                IsPreferredSupplier = false,
                HasDirectPayment    = false
            }
                );
            return(await _service.Upsert(newVendor, new UserIdentity { BuType = BuType.Pg }));
        }
Esempio n. 7
0
            public async Task Vendors_UpdateExistingVendor_Correctly()
            {
                var url    = "v1/vendors";
                var vendor = await CreateVendor(url);

                var updateVendorModel = new SaveVendorModel
                {
                    Id                   = vendor.Id,
                    Name                 = "TestVendor1",
                    SapVendorCode        = "VendorCode1",
                    VendorCategoryModels = new[]
                    {
                        new VendorCategoryModel
                        {
                            IsPreferredSupplier = false,
                            HasDirectPayment    = false,
                            DefaultCurrencyId   = _defaultCurrencyId,
                            Name = "ProductionCategory1"
                        }
                    }
                };
                var result = await Browser.Put(url, c =>
                {
                    c.User(_user);
                    c.JsonBody(updateVendorModel);
                });

                result.StatusCode.Should().Be(HttpStatusCode.OK);

                url    = $"v1/vendors/{updateVendorModel.Name}/category/{updateVendorModel.VendorCategoryModels.First().Name}";
                result = await Browser.Get(url, c => { c.User(_user); });

                var vendors = JsonConvert.DeserializeObject <List <VendorModel> >(result.Body.AsString());

                result.StatusCode.Should().Be(HttpStatusCode.OK);
                vendors.Should().NotBeEmpty();
            }
Esempio n. 8
0
            public async Task Vendors_CreateNewVendor_withPaymentRules()
            {
                const string vendorName         = "Vendor1_CreateNewVendor_withPaymentRules";
                const string productionCategory = "Production";
                const string sapVendorCode      = "S_ALR_87012090";

                const string  ruleName                   = "test rule 1";
                const string  splitsKey                  = "splits";
                const string  stageSplitsKey             = "stageSplits";
                const string  costTotalTypeKey           = "costTotalType";
                const decimal originalEstimateSplitValue = (decimal)0.20;

                const string criteriaFieldName = nameof(PgPaymentRule.BudgetRegion);
                const string criteriaValue     = Constants.BudgetRegion.China;
                var          criteriaOperator  = ExpressionType.Equal.ToString();

                var vendor = new SaveVendorModel
                {
                    Name                 = vendorName,
                    SapVendorCode        = sapVendorCode,
                    VendorCategoryModels = new[]
                    {
                        new VendorCategoryModel
                        {
                            HasDirectPayment    = true,
                            IsPreferredSupplier = false,
                            Name = productionCategory,
                            DefaultCurrencyId = _defaultCurrencyId,
                            PaymentRules      = new[]
                            {
                                new VendorRuleModel
                                {
                                    Name     = ruleName,
                                    Criteria = new Dictionary <string, CriterionValueModel>
                                    {
                                        {
                                            criteriaFieldName,
                                            new CriterionValueModel
                                            {
                                                FieldName = criteriaFieldName,
                                                Value     = criteriaValue,
                                                Operator  = criteriaOperator
                                            }
                                        }
                                    },
                                    Definition = new Dictionary <string, dynamic>
                                    {
                                        {
                                            splitsKey,
                                            new []
                                            {
                                                new Dictionary <string, dynamic>
                                                {
                                                    {
                                                        costTotalTypeKey,
                                                        Constants.CostSection.CostTotal
                                                    },
                                                    {
                                                        stageSplitsKey,
                                                        new Dictionary <string, dynamic>
                                                        {
                                                            { CostStages.OriginalEstimate.ToString(), originalEstimateSplitValue }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                var vendorResult = await CreateVendor(vendor);

                vendorResult.VendorCategoryModels.Count.Should().Be(1);
                vendorResult.VendorCategoryModels.First().PaymentRules.Should().HaveCount(1);
                vendorResult.VendorCategoryModels.First().PaymentRules[0].Id.Should().NotBe(Guid.Empty);
                vendorResult.VendorCategoryModels.First().PaymentRules[0].Name.Should().Be(ruleName);
                vendorResult.VendorCategoryModels.First().PaymentRules[0].Definition.Should().ContainKey(splitsKey);
                ((decimal)vendorResult.VendorCategoryModels.First().PaymentRules[0].Definition[splitsKey][0][stageSplitsKey][CostStages.OriginalEstimate.ToString()])
                .Should().Be(originalEstimateSplitValue);

                vendorResult.VendorCategoryModels.First().PaymentRules[0].Criteria.Should().ContainKey(criteriaFieldName);
                vendorResult.VendorCategoryModels.First().PaymentRules[0].Criteria[criteriaFieldName].FieldName.Should().Be(criteriaFieldName);
                vendorResult.VendorCategoryModels.First().PaymentRules[0].Criteria[criteriaFieldName].Value.Should().Be(criteriaValue);
                vendorResult.VendorCategoryModels.First().PaymentRules[0].Criteria[criteriaFieldName].Operator.Should().Be(criteriaOperator);
            }
Esempio n. 9
0
            public void Vendors_update_withPaymentRules_shouldDeleteOldRulesAndCreateNewOnes()
            {
                const string vendorName         = "Vendor1_update_withPaymentRules_shouldDeleteOldRulesAndCreateNewOnces";
                const string productionCategory = "Production";
                const string sapVendorCode      = "S_ALR_87012089";

                const string  ruleName                   = "test rule 2";
                const string  splitsKey                  = "splits";
                const string  stageSplitsKey             = "stageSplits";
                const string  costTotalTypeKey           = "costTotalType";
                const decimal originalEstimateSplitValue = (decimal)0.20;

                const string criteriaFieldName = nameof(PgPaymentRule.BudgetRegion);
                const string criteriaValue     = Constants.BudgetRegion.China;
                var          criteriaOperator  = ExpressionType.Equal.ToString();

                var vendor = new SaveVendorModel
                {
                    Name                 = vendorName,
                    SapVendorCode        = sapVendorCode,
                    VendorCategoryModels = new[]
                    {
                        new VendorCategoryModel
                        {
                            HasDirectPayment    = true,
                            IsPreferredSupplier = false,
                            Name = productionCategory,
                            DefaultCurrencyId = _defaultCurrencyId,
                            PaymentRules      = new[]
                            {
                                new VendorRuleModel
                                {
                                    Name     = ruleName,
                                    Criteria = new Dictionary <string, CriterionValueModel>
                                    {
                                        {
                                            criteriaFieldName,
                                            new CriterionValueModel
                                            {
                                                FieldName = criteriaFieldName,
                                                Value     = criteriaValue,
                                                Operator  = criteriaOperator
                                            }
                                        }
                                    },
                                    Definition = new Dictionary <string, dynamic>
                                    {
                                        {
                                            splitsKey,
                                            new []
                                            {
                                                new Dictionary <string, dynamic>
                                                {
                                                    {
                                                        costTotalTypeKey,
                                                        Constants.CostSection.CostTotal
                                                    },
                                                    {
                                                        stageSplitsKey,
                                                        new Dictionary <string, dynamic>
                                                        {
                                                            { CostStages.OriginalEstimate.ToString(), originalEstimateSplitValue }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                var vendorResult = CreateVendor(vendor).Result;

                const string vendorNameUpdated = "Vendor1_update_withPaymentRules_shouldDeleteOldRulesAndCreateNewOnes_updated";

                const string  ruleNameUpdated   = "test rule 2_update";
                const decimal splitValueUpdated = 0.3m;

                var updateModel = new SaveVendorModel
                {
                    Id                   = vendorResult.Id,
                    Name                 = vendorNameUpdated,
                    SapVendorCode        = sapVendorCode,
                    VendorCategoryModels = new[]
                    {
                        new VendorCategoryModel
                        {
                            HasDirectPayment    = true,
                            IsPreferredSupplier = false,
                            Name = productionCategory,
                            DefaultCurrencyId = _defaultCurrencyId,
                            PaymentRules      = new[]
                            {
                                new VendorRuleModel
                                {
                                    Name     = ruleNameUpdated,
                                    Criteria = new Dictionary <string, CriterionValueModel>
                                    {
                                        {
                                            criteriaFieldName,
                                            new CriterionValueModel
                                            {
                                                FieldName = criteriaFieldName,
                                                Value     = criteriaValue,
                                                Operator  = criteriaOperator
                                            }
                                        }
                                    },
                                    Definition = new Dictionary <string, dynamic>
                                    {
                                        {
                                            splitsKey,
                                            new []
                                            {
                                                new Dictionary <string, dynamic>
                                                {
                                                    {
                                                        costTotalTypeKey,
                                                        Constants.CostSection.CostTotal
                                                    },
                                                    {
                                                        stageSplitsKey,
                                                        new Dictionary <string, dynamic>
                                                        {
                                                            { CostStages.OriginalEstimate.ToString(), splitValueUpdated }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                // Act
                var updateVendorResult = UpdateVendor(updateModel).Result;

                // Assert
                updateVendorResult.VendorCategoryModels.First().PaymentRules.Should().HaveCount(1);
                var definition = updateVendorResult.VendorCategoryModels.First().PaymentRules[0].Definition;

                definition.Should().HaveCount(1);
                definition.Should().ContainKey(splitsKey);
                ((decimal)definition[splitsKey][0][stageSplitsKey][CostStages.OriginalEstimate.ToString()]).Should().Be(splitValueUpdated);
            }
        public async Task Upsert_ifAnyPaymentRules_shouldPersistPaymentRules()
        {
            // Arrange
            var request = new SaveVendorModel
            {
                VendorCategoryModels = new[]
                {
                    new VendorCategoryModel
                    {
                        PaymentRules = new[]
                        {
                            new VendorRuleModel
                            {
                                Name     = RuleName,
                                Criteria = new Dictionary <string, CriterionValueModel>
                                {
                                    {
                                        nameof(PgPaymentRule.BudgetRegion),
                                        new CriterionValueModel
                                        {
                                            FieldName = nameof(PgPaymentRule.BudgetRegion),
                                            Value     = Constants.BudgetRegion.China,
                                            Operator  = ExpressionType.Equal.ToString()
                                        }
                                    }
                                }
                            }
                        },
                        Name                = CategoryName,
                        HasDirectPayment    = false,
                        IsPreferredSupplier = false
                    }
                }
            };
            var dbVendor = new Vendor();

            _currencyServiceMock.Setup(c => c.GetDefaultCurrency()).ReturnsAsync(new Currency());

            _vendorRuleBuilderMock.Setup(b =>
                                         b.ValidateAndGetVendorRules(It.IsAny <VendorRuleModel[]>(), It.IsAny <Vendor>(), It.IsAny <Guid>()))
            .ReturnsAsync(new List <VendorRule> {
                new VendorRule {
                    VendorCategory = new VendorCategory
                    {
                        Name                = CategoryName,
                        HasDirectPayment    = false,
                        IsPreferredSupplier = false,
                        Vendor              = dbVendor
                    }, Rule = new dataAccess.Entity.Rule()
                }
            });

            _mapperMock.Setup(m => m.Map <Vendor>(It.IsAny <SaveVendorModel>())).Returns(dbVendor);
            _mapperMock.Setup(m => m.Map(It.IsAny <Currency>(), It.IsAny <Vendor>())).Returns(new Vendor());
            _mapperMock.Setup(m => m.Map <VendorModel>(It.IsAny <Vendor>())).Returns(new VendorModel());
            _mapperMock.Setup(m => m.Map <VendorCategory>(It.IsAny <VendorCategoryModel>())).Returns(
                new VendorCategory
            {
                Name = CategoryName,
                IsPreferredSupplier = false,
                HasDirectPayment    = false
            }
                );

            // Act
            await _service.Upsert(request, new UserIdentity { BuType = BuType.Pg });

            // Assert
            _efContext.Vendor.Should().HaveCount(1);
            var vendor = _efContext.Vendor.First();

            //
            vendor.Categories.Should().HaveCount(1);
            vendor.Categories.First().VendorCategoryRules.Should().HaveCount(1);
            var rule = vendor.Categories.First().VendorCategoryRules.First().Rule;

            rule.Should().NotBeNull();
            _eventServiceMock.Verify(a => a.SendAsync(It.IsAny <VendorUpserted>()), Times.Once);
        }