Beispiel #1
0
        public void ItShouldBeCreatedOnTheApi()
        {
            int customerId = new Random(DateTime.Now.Millisecond).Next(15000, 35000);

            var newOrder = new StandardOrder();

            var dto = new OrderBuilder()
                      .With(newOrder)
                      .WithCustomerId(customerId)
                      .BuildDTO();

            var jsonContent = base.CreateJsonContentFor <CreateOrderRequestDTO>(dto);

            var response = _apiClient.PostAsync(string.Empty, jsonContent).Result;

            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);

            var deserializedResponse = JsonConvert.DeserializeObject <Order>(response.Content.ReadAsStringAsync().Result);

            Assert.AreEqual(dto.CustomerId, deserializedResponse.CustomerId);
            Assert.AreEqual(dto.ProductId, deserializedResponse.ProductId);
            Assert.AreEqual(dto.Quantity, deserializedResponse.Quantity);
            Assert.AreEqual(dto.UnitPrice, deserializedResponse.UnitPrice);
            Assert.AreEqual(dto.DeliveryAddress, deserializedResponse.DeliveryAddress);
        }
        public async Task GivenASearchByIdItShouldReturnAnOrder()
        {
            var newOrder = new StandardOrder();

            var dto = new OrderBuilder()
                .With(newOrder)
                .WithCustomerId(5000)
                .BuildDTO();

            var assumptionHttpResponse = await AssumeOrderCreatedOnApi(dto);
            var orderOnApi = JsonConvert.DeserializeObject<Order>(await assumptionHttpResponse.Content.ReadAsStringAsync());

            var requestUrl = $"/{orderOnApi.Id}";
            var response = await _apiClient.Get(requestUrl);

            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);

            var orderFromRequest = JsonConvert.DeserializeObject<OrderQueryResponseDTO>(await response.Content.ReadAsStringAsync());

            Assert.AreEqual(orderOnApi.Id, orderFromRequest.OrderId);
            Assert.AreEqual(orderOnApi.OrderStatus, orderFromRequest.OrderStatus);
            Assert.AreEqual(orderOnApi.ProductId, orderFromRequest.ProductId);
            Assert.AreEqual(orderOnApi.Quantity, orderFromRequest.Quantity);
            Assert.AreEqual(orderOnApi.UnitPrice, orderFromRequest.UnitPrice);
            Assert.AreEqual(orderOnApi.UnitPrice * orderOnApi.Quantity, orderFromRequest.OrderTotal);
        }
Beispiel #3
0
        public void GivenQueryForClientIdAndProductIdItShouldReturnASingleOrder()
        {
            int clientId  = 10;
            int productId = 20;

            var existingOrder = new StandardOrder();

            var order = new OrderBuilder()
                        .With(existingOrder)
                        .WithCustomerId(clientId)
                        .WithProductId(productId)
                        .BuildEntity();

            AssumeOrderExists(order);

            var orderOnDatabase = _orderService.GetOrderByQuery(new OrderQueryRequestDTO
            {
                OrderId    = order.Id,
                CustomerId = clientId,
                ProductId  = productId
            }).FirstOrDefault(x => x.OrderId == order.Id);

            Assert.IsNotNull(orderOnDatabase);
            Assert.AreEqual(order.OrderStatus, orderOnDatabase.OrderStatus);
            Assert.AreEqual(order.ProductId, orderOnDatabase.ProductId);
            Assert.AreEqual(order.Quantity, orderOnDatabase.Quantity);
            Assert.AreEqual(order.UnitPrice, orderOnDatabase.UnitPrice);
            Assert.AreEqual(order.CustomerId, orderOnDatabase.CustomerId);
            Assert.AreEqual(order.DeliveryAddress, orderOnDatabase.DeliveryAddress);
        }
Beispiel #4
0
        public void GivenTheApiRequestHasMissingRequiredFieldsItShouldReturnError500()
        {
            var newOrder = new StandardOrder();

            var dto = new OrderBuilder()
                      .WithEmptyObject()
                      .BuildDTO();

            var jsonContent = base.CreateJsonContentFor <CreateOrderRequestDTO>(dto);

            var response = _apiClient.PostAsync(string.Empty, jsonContent).Result;

            Assert.IsTrue(response.StatusCode == HttpStatusCode.InternalServerError);
        }
        public void GivenAnOrderHasNoProductIdItShouldThrowAnException()
        {
            var newOrder = new StandardOrder();

            var order = new OrderBuilder()
                        .With(newOrder)
                        .WithProductId(0)
                        .BuildDTO();

            string expectedErrorMessage = $"Product ID is required";

            var exception = Assert.ThrowsException <Exception>(() => { _orderService.CreateOrder(order); });

            Assert.AreEqual(expectedErrorMessage, exception.Message);
        }
        public void GivenAnOrderContainMoreThan10OfAProductItShouldBeRejected()
        {
            var newOrder = new StandardOrder();

            var order = new OrderBuilder()
                        .With(newOrder)
                        .WithQuantity(11)
                        .BuildDTO();

            string expectedErrorMessage = $"Order was rejected because it contains more than 10 units of product { order.ProductId }";


            var exception = Assert.ThrowsException <Exception>(() => { _orderService.CreateOrder(order); });

            Assert.AreEqual(expectedErrorMessage, exception.Message);
        }
        public void GivenAnOrderHasEmptyOrInvalidRequiredFieldsItShouldThrowAnException()
        {
            var newOrder = new StandardOrder();

            var order = new OrderBuilder()
                        .With(newOrder)
                        .WithProductId(0)
                        .WithCustomerId(0)
                        .WithQuantity(0)
                        .WithUnitPrice(0)
                        .BuildDTO();

            order.DeliveryAddress = null;

            Assert.ThrowsException <Exception>(() => { _orderService.CreateOrder(order); });
        }
        public void GivenAClientWithOutstandingOrdersExceeding100EuroItShouldBeRejected()
        {
            var newOrder = new StandardOrder();

            var order = new OrderBuilder()
                        .With(newOrder)
                        .IgnoreId()
                        .BuildDTO();

            string expectedErrorMessage = $"Order was rejected because customer with id {order.CustomerId} has outstanding orders with a total value in excess of one hundred Euro";

            this.AssumeOrdersCreatedWithUnitPriceAndQuantityForClient(5, order.CustomerId, 25, 2);

            var exception = Assert.ThrowsException <Exception>(() => { _orderService.CreateOrder(order); });

            Assert.AreEqual(expectedErrorMessage, exception.Message);
        }
        public void ItShouldBeCreated()
        {
            var newOrder = new StandardOrder();

            var order = new OrderBuilder()
                        .With(newOrder)
                        .BuildDTO();

            var orderOnDatabase = _orderService.CreateOrder(order);

            Assert.IsNotNull(orderOnDatabase);
            Assert.AreEqual(OrderStatus.Pending, orderOnDatabase.OrderStatus);
            Assert.AreEqual(order.ProductId, orderOnDatabase.ProductId);
            Assert.AreEqual(order.Quantity, orderOnDatabase.Quantity);
            Assert.AreEqual(order.UnitPrice, orderOnDatabase.UnitPrice);
            Assert.AreEqual(order.CustomerId, orderOnDatabase.CustomerId);
            Assert.AreEqual(order.DeliveryAddress, orderOnDatabase.DeliveryAddress);
        }
Beispiel #10
0
        private void AssumeOrdersCreatedWithUnitPriceAndQuantityForClient(int numberOfOrders, int clientId, decimal unitPrice, int quantity)
        {
            for (int i = 0; i < numberOfOrders; i++)
            {
                var newOrder = new StandardOrder();

                var dto = new OrderBuilder()
                          .With(newOrder)
                          .WithCustomerId(clientId)
                          .WithUnitPrice(unitPrice)
                          .WithQuantity(quantity)
                          .BuildDTO();

                var jsonContent = base.CreateJsonContentFor <CreateOrderRequestDTO>(dto);

                _apiClient.PostAsync(string.Empty, jsonContent).Wait();
            }
        }
        private void AssumeOrdersCreatedWithUnitPriceAndQuantityForClient(int numberOfOrders, int clientId, decimal unitPrice, int quantity)
        {
            var newExpensiveOrder = new StandardOrder();

            for (int i = 0; i < numberOfOrders; i++)
            {
                var order = new OrderBuilder()
                            .With(newExpensiveOrder)
                            .WithCustomerId(clientId)
                            .WithQuantity(quantity)
                            .WithUnitPrice(unitPrice)
                            .IgnoreId()
                            .BuildEntity();

                _context.Orders.Add(order);
            }

            _context.SaveChanges();
        }
        public async Task GivenASearchCriteriaItShouldReturnAnOrder()
        {
            var newOrder = new StandardOrder();
            int productId = 25;
            int customerId = new Random(DateTime.Now.Millisecond).Next(15000, 35000);

            var dto = new OrderBuilder()
                .With(newOrder)
                .WithProductId(productId)
                .WithCustomerId(customerId)
                .BuildDTO();

            for (int i = 0; i < 2; i++)
            {
                var assumptionHttpResponse = await AssumeOrderCreatedOnApi(dto);
            }

            var queryRequest = new StandardQueryRequest();

            var queryDto = new OrderQueryRequestBuilder()
                .With(queryRequest)
                .WithCustomerId(customerId)
                .WithProductId(productId)
                .Build();

            var jsonContent = base.CreateJsonContentFor<OrderQueryRequestDTO>(queryDto);

            string queryUrl = "query";
            var response = await _apiClient.PostAsync(queryUrl, jsonContent);

            Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);

            var deserializedResponse = JsonConvert.DeserializeObject<IEnumerable<OrderQueryResponseDTO>>(await response.Content.ReadAsStringAsync());

            Assert.IsTrue(deserializedResponse.Count() >= 2);

            foreach (var item in deserializedResponse)
            {
                Assert.AreEqual(dto.DeliveryAddress, item.DeliveryAddress);
                Assert.AreEqual(dto.CustomerId, item.CustomerId);
                Assert.AreEqual(dto.ProductId, item.ProductId);
            }
        }
Beispiel #13
0
        public void GivenAnOrderHasMoreThan10OfAnyProductItShouldBeRejected()
        {
            var newOrder = new StandardOrder();

            var dto = new OrderBuilder()
                      .With(newOrder)
                      .WithCustomerId(new Random(DateTime.Now.Millisecond).Next(15000, 35000))
                      .WithQuantity(11)
                      .BuildDTO();

            string expectedErrorMessage = $"Order was rejected because it contains more than 10 units of product {dto.ProductId}";

            var jsonContent = base.CreateJsonContentFor <CreateOrderRequestDTO>(dto);

            var response = _apiClient.PostAsync(string.Empty, jsonContent).Result;

            Assert.IsTrue(response.StatusCode == HttpStatusCode.InternalServerError);
            Assert.AreEqual(response.Content.ReadAsStringAsync().Result.Replace("\"", ""), expectedErrorMessage);
        }
        public void GivenAUserHasCompletedOrdersExceedingTheValueOfOneHundredEuroItShouldNotBeRejected()
        {
            var newOrder = new StandardOrder();

            var order = new OrderBuilder()
                        .With(newOrder)
                        .BuildDTO();

            AssumeOrdersCreatedWithCompletedStatusExceeds100EuroForClient(order.CustomerId);

            var orderOnDatabase = _orderService.CreateOrder(order);

            Assert.IsNotNull(orderOnDatabase);
            Assert.AreEqual(OrderStatus.Pending, orderOnDatabase.OrderStatus);
            Assert.AreEqual(order.ProductId, orderOnDatabase.ProductId);
            Assert.AreEqual(order.Quantity, orderOnDatabase.Quantity);
            Assert.AreEqual(order.UnitPrice, orderOnDatabase.UnitPrice);
            Assert.AreEqual(order.CustomerId, orderOnDatabase.CustomerId);
            Assert.AreEqual(order.DeliveryAddress, orderOnDatabase.DeliveryAddress);
        }
        private void AssumeOrdersCreatedWithCompletedStatusExceeds100EuroForClient(int clientId)
        {
            var newExpensiveOrder = new StandardOrder();

            for (int i = 0; i < 5; i++)
            {
                var order = new OrderBuilder()
                            .With(newExpensiveOrder)
                            .WithCustomerId(clientId)
                            .WithQuantity(5)
                            .WithUnitPrice(25)
                            .WithOrderStatus(OrderStatus.Completed)
                            .IgnoreId()
                            .BuildEntity();

                _context.Orders.Add(order);
            }

            _context.SaveChanges();
        }
Beispiel #16
0
        public void GivenAClientWithOutstandingOrdersExceeding100EuroItShouldBeRejected()
        {
            var newOrder = new StandardOrder();

            var dto = new OrderBuilder()
                      .With(newOrder)
                      .WithCustomerId(new Random(DateTime.Now.Millisecond).Next(15000, 35000))
                      .BuildDTO();

            string expectedErrorMessage = $"Order was rejected because customer with id {dto.CustomerId} has outstanding orders with a total value in excess of one hundred Euro";

            this.AssumeOrdersCreatedWithUnitPriceAndQuantityForClient(1, dto.CustomerId, 51, 2);

            var jsonContent = base.CreateJsonContentFor <CreateOrderRequestDTO>(dto);

            var response = _apiClient.PostAsync(string.Empty, jsonContent).Result;

            Assert.IsTrue(response.StatusCode == HttpStatusCode.InternalServerError);
            Assert.AreEqual(response.Content.ReadAsStringAsync().Result.Replace("\"", ""), expectedErrorMessage);
        }