Beispiel #1
0
        private void CreateCampaignInputValidate(CreateCampaignInput createCampaignInput)
        {
            if (string.IsNullOrWhiteSpace(createCampaignInput.Name))
            {
                throw new ValidationException(nameof(createCampaignInput.Name));
            }
            if (string.IsNullOrWhiteSpace(createCampaignInput.ProductCode))
            {
                throw new ValidationException(nameof(createCampaignInput.ProductCode));
            }
            if (createCampaignInput.TargetSalesCount <= 0)
            {
                throw new InvalidTargetSaleCount();
            }
            if (createCampaignInput.Duration <= 0)
            {
                throw new InvalidDurationException();
            }
            if (createCampaignInput.PriceManipulationLimit <= 0)
            {
                throw new InvalidPriceManipulationLimitException("Price manipulation limit cannot be equal to or less than zero");
            }
            if (createCampaignInput.PriceManipulationLimit > 100)
            {
                throw new InvalidPriceManipulationLimitException("Price manipulation limit cannot be higher than 100");
            }

            var campaign = _campaignRepository.GetCampaign(createCampaignInput.Name);

            if (campaign != null)
            {
                throw new BaseServiceException("This CampaignName is used.", (int)HttpStatusCode.BadRequest);
            }
        }
        public void When_GetCampaignInfo_IsSuccessful()
        {
            CreateProduct();

            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name                   = "C1",
                ProductCode            = "P1",
                TargetSalesCount       = 100,
                Duration               = 10,
                PriceManipulationLimit = 20
            };

            CreateCampaign(createCampaignInput);
            using (var client = new TestStart().Client)
            {
                client.DefaultRequestHeaders.Add("Accept", "application/json");

                string name     = "C1";
                var    response = client.GetAsync("/api/campaign?name=" + name);

                Assert.AreEqual(HttpStatusCode.OK, response.Result.StatusCode);

                var getCampaignInfoOutput = JsonConvert.DeserializeObject <GetCampaignInfoOutput>(response.Result.Content.ReadAsStringAsync().Result);

                Assert.IsNotNull(getCampaignInfoOutput);
                Assert.AreEqual(0, getCampaignInfoOutput.TotalSales);
                Assert.AreEqual(createCampaignInput.TargetSalesCount, getCampaignInfoOutput.TargetSales);
                Assert.AreEqual(0, getCampaignInfoOutput.Turnover);
            }
        }
Beispiel #3
0
        public void When_ProductCode_IsNull()
        {
            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name = "C1",
            };

            Assert.Throws <ValidationException>(() => campaignService.CreateCampaign(createCampaignInput));
        }
Beispiel #4
0
        public void When_TargetSalesCount_IsLessThanZero()
        {
            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name             = "C1",
                ProductCode      = "P1",
                TargetSalesCount = -10
            };

            Assert.Throws <InvalidTargetSaleCount>(() => campaignService.CreateCampaign(createCampaignInput));
        }
 private void CreateCampaign(CreateCampaignInput createCampaignInput)
 {
     using (var client = new TestStart().Client)
     {
         client.DefaultRequestHeaders.Add("Accept", "application/json");
         var json     = JsonConvert.SerializeObject(createCampaignInput);
         var data     = new StringContent(json, Encoding.UTF8, "application/json");
         var response = client.PostAsync("/api/campaign", data).Result;
         Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
     }
 }
Beispiel #6
0
        public void When_Duration_IsLessThanZero()
        {
            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name             = "C1",
                ProductCode      = "P1",
                TargetSalesCount = 100,
                Duration         = -10
            };

            Assert.Throws <InvalidDurationException>(() => campaignService.CreateCampaign(createCampaignInput));
        }
Beispiel #7
0
 public void CreateCampaign(CreateCampaignInput createCampaignInput)
 {
     CreateCampaignInputValidate(createCampaignInput);
     _campaignRepository.CreateCampaign(
         new Campaign(
             createCampaignInput.Name,
             createCampaignInput.ProductCode,
             createCampaignInput.Duration,
             createCampaignInput.PriceManipulationLimit,
             createCampaignInput.TargetSalesCount)
         );
 }
        private HttpResponseMessage CreateCampaign(CreateCampaignInput createCampaignInput)
        {
            using (var client = new TestStart().Client)
            {
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                var json = JsonConvert.SerializeObject(createCampaignInput);
                var data = new StringContent(json, Encoding.UTF8, "application/json");

                var response = client.PostAsync("/api/campaign", data).Result;
                return(response);
            }
        }
        public void When_CreateOrder_IsSuccessful()
        {
            CreateProductInput createProductInput = new CreateProductInput
            {
                Code  = "P1",
                Price = 100,
                Stock = 1000,
            };

            CreateProduct(createProductInput);

            CreateOrderInput createOrderInput = new CreateOrderInput
            {
                ProductCode = "P1",
                Quantity    = 10
            };
            var orderResponse = CreateOrder(createOrderInput);

            Assert.AreEqual(HttpStatusCode.OK, orderResponse.StatusCode);

            var getProduct = GetProduct();

            Assert.AreEqual(990, getProduct.Stock);

            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name                   = "C1",
                ProductCode            = "P1",
                TargetSalesCount       = 100,
                Duration               = 10,
                PriceManipulationLimit = 20
            };

            CreateCampaign(createCampaignInput);
            IncreaseTime();


            var orderResponse2 = CreateOrder(createOrderInput);

            Assert.AreEqual(HttpStatusCode.OK, orderResponse2.StatusCode);

            var getProduct2 = GetProduct();

            Assert.AreEqual(980, getProduct2.Stock);

            var campaignResponse = GetCampaign(createCampaignInput.Name);

            Assert.AreEqual(createCampaignInput.TargetSalesCount, campaignResponse.TargetSales);
            Assert.AreEqual(createOrderInput.Quantity, campaignResponse.TotalSales);
            Assert.AreEqual(980, campaignResponse.Turnover);
            Assert.AreEqual(98, campaignResponse.AverageItemPrice);
        }
Beispiel #10
0
        public void When_PriceManipulationLimit_IsZero()
        {
            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name                   = "C1",
                ProductCode            = "P1",
                TargetSalesCount       = 100,
                Duration               = 10,
                PriceManipulationLimit = 0
            };

            Assert.Throws <InvalidPriceManipulationLimitException>(() => campaignService.CreateCampaign(createCampaignInput));
        }
Beispiel #11
0
        public void When_CreateCampaign_IsSuccessful()
        {
            Product product = new Product("P1", 100, 1000);

            mockProductRepository.Setup(x => x.GetProduct("P1")).Returns(product);
            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name                   = "C1",
                ProductCode            = "P1",
                TargetSalesCount       = 100,
                Duration               = 10,
                PriceManipulationLimit = 20
            };

            Assert.DoesNotThrow(() => campaignService.CreateCampaign(createCampaignInput));
            mockCampaignRepository.Verify(x => x.CreateCampaign(It.IsAny <Campaign>()), Times.Once());
        }
        public void When_CreateCampaign_IsSuccessful()
        {
            CreateProduct();

            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name                   = "C1",
                ProductCode            = "P1",
                TargetSalesCount       = 100,
                Duration               = 10,
                PriceManipulationLimit = 20
            };

            var response = CreateCampaign(createCampaignInput);

            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
Beispiel #13
0
        public void When_Name_IsUsed()
        {
            Product product = new Product("P1", 100, 1000);

            mockProductRepository.Setup(x => x.GetProduct("P1")).Returns(product);
            Campaign campaign = new Campaign("C1", "P1", 10, 20, 100);

            mockCampaignRepository.Setup(x => x.GetCampaign("C1")).Returns(campaign);
            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name                   = "C1",
                ProductCode            = "P1",
                TargetSalesCount       = 100,
                Duration               = 10,
                PriceManipulationLimit = 20
            };

            Assert.Throws <BaseServiceException>(() => campaignService.CreateCampaign(createCampaignInput));
        }
Beispiel #14
0
        public void When_GetProductInfo_IsSuccessful()
        {
            CreateProductRequest input = new CreateProductRequest
            {
                Code  = "P1",
                Price = 100,
                Stock = 1000,
            };

            CreateProduct(input);

            CreateCampaignInput createCampaignInput = new CreateCampaignInput
            {
                Name                   = "C1",
                ProductCode            = "P1",
                TargetSalesCount       = 100,
                Duration               = 10,
                PriceManipulationLimit = 20
            };

            CreateCampaign(createCampaignInput);

            IncreaseTime();

            using (var client = new TestStart().Client)
            {
                client.DefaultRequestHeaders.Add("Accept", "application/json");

                string productCode = "P1";
                var    response    = client.GetAsync("/api/product?productCode=" + productCode);

                Assert.AreEqual(HttpStatusCode.OK, response.Result.StatusCode);

                var getProductInfoOutput = JsonConvert.DeserializeObject <GetProductInfoOutput>(response.Result.Content.ReadAsStringAsync().Result);
                Assert.IsNotNull(getProductInfoOutput);

                Assert.AreNotEqual(input.Price, getProductInfoOutput.Price);
                Assert.AreEqual(input.Stock, getProductInfoOutput.Stock);
            }
        }
Beispiel #15
0
        public void When_Name_IsNull()
        {
            CreateCampaignInput createCampaignInput = new CreateCampaignInput();

            Assert.Throws <ValidationException>(() => campaignService.CreateCampaign(createCampaignInput));
        }