public void TestCreateOrder() { Moip.Models.OrderRequest orderRequest = Helpers.RequestsCreator.createOrderRequest(); Moip.Models.OrderResponse orderResponse = controller.CreateOrder(orderRequest); Assert.NotNull(orderResponse.Id, "Id should not be null"); Assert.AreEqual("my_own_id", orderResponse.OwnId, "Should match exactly (string literal match)"); Assert.AreEqual("Bicicleta Specialized Tarmac 26 Shimano Alivio", orderResponse.Items[0].Product, "Should match exactly (string literal match)"); Assert.AreEqual(1, orderResponse.Items[0].Quantity, "Should match exactly (string literal match)"); Assert.AreEqual("uma linda bicicleta", orderResponse.Items[0].Detail, "Should match exactly (string literal match)"); Assert.AreEqual(2000, orderResponse.Items[0].Price, "Should match exactly (string literal match)"); Assert.AreEqual("BRL", orderResponse.Amount.Currency, "Should match exactly (string literal match)"); Assert.AreEqual("Fulano de Tal", orderResponse.Customer.Fullname, "Should match exactly (string literal match)"); Assert.AreEqual("OFulanoDeTal", orderResponse.Customer.OwnId, "Should match exactly (string literal match)"); Assert.AreEqual("1990-01-01", orderResponse.Customer.BirthDate, "Should match exactly (string literal match)"); Assert.AreEqual("*****@*****.**", orderResponse.Customer.Email, "Should match exactly (string literal match)"); Assert.AreEqual("Rua test", orderResponse.ShippingAddress.Street, "Should match exactly (string literal match)"); Assert.AreEqual("123", orderResponse.ShippingAddress.StreetNumber, "Should match exactly (string literal match)"); Assert.AreEqual("Ap test", orderResponse.ShippingAddress.Complement, "Should match exactly (string literal match)"); Assert.AreEqual("Bairro test", orderResponse.ShippingAddress.District, "Should match exactly (string literal match)"); Assert.AreEqual("TestCity", orderResponse.ShippingAddress.City, "Should match exactly (string literal match)"); Assert.AreEqual("SP", orderResponse.ShippingAddress.State, "Should match exactly (string literal match)"); Assert.AreEqual("BRA", orderResponse.ShippingAddress.Country, "Should match exactly (string literal match)"); Assert.AreEqual("01234000", orderResponse.ShippingAddress.ZipCode, "Should match exactly (string literal match)"); Assert.AreEqual("55", orderResponse.Customer.Phone.CountryCode, "Should match exactly (string literal match)"); Assert.AreEqual("11", orderResponse.Customer.Phone.AreaCode, "Should match exactly (string literal match)"); Assert.AreEqual("66778899", orderResponse.Customer.Phone.Number, "Should match exactly (string literal match)"); Assert.AreEqual("CPF", orderResponse.Customer.TaxDocument.Type, "Should match exactly (string literal match)"); Assert.AreEqual("22222222222", orderResponse.Customer.TaxDocument.Number, "Should match exactly (string literal match)"); Assert.AreEqual(1500, orderResponse.Amount.Subtotals.Shipping, "Should match exactly (string literal match)"); Assert.AreEqual(20, orderResponse.Amount.Subtotals.Addition, "Should match exactly (string literal match)"); Assert.AreEqual(10, orderResponse.Amount.Subtotals.Discount, "Should match exactly (string literal match)"); }
public void CreateOrder_ValidOrderCreated_ReturnCreated() { int orderAmount = 4; Product product = new Product() { ProductId = 1, Price = 240.34m }; Customer customer = new Customer() { CustomerId = 2 }; decimal checkTotalPrice = orderAmount * product.Price; Order order = new Order() { CustomerId = customer.CustomerId, ProductId = product.ProductId, Quantity = orderAmount, TotalPrice = 0 }; HttpMethod test = new HttpMethod("Test"); _controller.Request = new HttpRequestMessage(test, "/"); _controller.Configuration = new HttpConfiguration(); _customerRepository.Setup(c => c.GetCustomerById(2)).Returns(customer); _productRepository.Setup(p => p.GetProductById(1)).Returns(product); _mockRepository.Setup(o => o.CalculateTotalOrderValue(product, orderAmount)).Returns(orderAmount * product.Price); var result = _controller.CreateOrder(order) as CreatedNegotiatedContentResult <Order>; var totalPrice = result.Content.TotalPrice; Assert.AreEqual(checkTotalPrice, totalPrice); }
public async Task CreatePurchaseOrder_ValidOrder_Success() { // Arrange _mockOrdersRepository.Setup(s => s.CreateOrder(It.IsAny <PurchaseOrderDto>())) .ReturnsAsync(Guid.NewGuid()); _mockOrdersFactory.Setup(s => s.Create(It.IsAny <string>())).Returns(_mockOrdersService.Object); _mockOrdersService.Setup(s => s.CreateOrder(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>())) .ReturnsAsync(_stubOrderCreatedDto); _mockOrdersRepository.Setup(m => m.UpdateOrderAsync(It.IsAny <Guid>(), It.IsAny <OrderCreatedDto>(), It.IsAny <string>())) .ReturnsAsync(true); _ordersController.ControllerContext = new ControllerContext(); _ordersController.ControllerContext.HttpContext = new DefaultHttpContext(); _ordersController.ControllerContext.HttpContext.Request.Headers["Authorization"] = "token"; // Act var result = await _ordersController.CreateOrder(_stubPurchaseOrderDto) as OkObjectResult; // Assert Assert.AreEqual(result.StatusCode, 200); Assert.IsNotNull(result.Value); Assert.AreEqual(result.Value, true); _mockOrdersRepository.Verify(m => m.CreateOrder(It.IsAny <PurchaseOrderDto>()), Times.Once); _mockOrdersFactory.Verify(m => m.Create(It.IsAny <string>()), Times.Once); _mockOrdersService.Verify(m => m.CreateOrder(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()), Times.Once); _mockOrdersRepository.Verify(m => m.UpdateOrderAsync(It.IsAny <Guid>(), It.IsAny <OrderCreatedDto>(), It.IsAny <string>()), Times.Once); }
public async Task CreateOrder_NewOrderCreated_ReturnsCreatedResult() { _orderServiceMock.Setup(x => x.CreateOrderAsync()).ReturnsAsync(new Order { Id = "test" }); var order = await _ordersController.CreateOrder(); order.Result.Should().NotBeNull().And.BeOfType <CreatedResult>(); }
public void CreateOrder_ShouldReturn_BadRequestWhenNewOrderModelIsInvalid() { var newOrderBm = new Mock <NewOrderBm>(); newOrderBm.Object.PaperKg = 9; this._controller.Validate(newOrderBm.Object); var result = _controller.CreateOrder(newOrderBm.Object) as StatusCodeResult; Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); }
public void OrderCreationAndUpdate() { Mock <IHubClients> mockClients = new Mock <IHubClients>(); Mock <IClientProxy> mockClientProxy = new Mock <IClientProxy>(); mockClients.Setup(clients => clients.All).Returns(mockClientProxy.Object); Mock <IHubContext <OrderHub> > mockHubContext = new Mock <IHubContext <OrderHub> >(); mockHubContext.Setup(c => c.Clients).Returns(() => mockClients.Object); DateTimeOffset startTime = DateTimeOffset.UtcNow; Thread.Sleep(10); using var context = GetContext("blah"); OrdersController oc = new OrdersController(context, mockHubContext.Object); var account = context.Accounts.FirstOrDefault(); Order order = new Order { ClientOrderId = "Test Order2", Quantity = 100, Symbol = "ABC", Account = account, OrderType = OrderType.FillOrKill, Side = Side.Buy, LimitPrice = 100m }; oc.CreateOrder(order); TimeSpan difference = order.Created - startTime; Assert.True(difference.TotalMilliseconds > 0); mockClients.Verify(clients => clients.All, Times.Once); }
public void CreateOrder_ReturnsBadResponse() { // Arrange var context = new TestOrderContext(); var testProducts = new List <Product>(); foreach (var product in GetTestProducts()) { testProducts.Add(context.Products.Add(product)); } context.Orders.Add(new Order { Id = 1, Email = "", DeliveryAddress = "Ukraine, Chernivtsi, Ruska287", Products = testProducts }); var controller = new OrdersController(context); // Act var createdResponse = controller.CreateOrder(context.Orders.FirstOrDefault()) as InvalidModelStateResult; // Assert Assert.AreEqual(false, createdResponse.ModelState.IsValid); }
public void CreateOrder_ReturnsCreatedResponse() { // Arrange var context = new TestOrderContext(); var testProducts = new List <Product>(); foreach (var product in GetTestProducts()) { testProducts.Add(context.Products.Add(product)); } context.Orders.Add(new Order { Id = 1, Email = "*****@*****.**", DeliveryAddress = "Ukraine, Chernivtsi, Ruska287", Products = testProducts }); var controller = new OrdersController(context); // Act var createdResponse = controller.CreateOrder(context.Orders.FirstOrDefault()) as NegotiatedContentResult <string>; // Assert Assert.AreEqual(HttpStatusCode.Created, createdResponse.StatusCode); }
public async Task Create_Order_With_Valid_Request() { //Arrange request var rq = new CreateOrderRequest { CustomerId = "CustomerId" }; //response var resDateTime = DateTime.Now.ToUniversalTime(); var expectedResponse = new OrderInformation { OrderId = 1, OrderRef = "ORDERREF", CreatedDateTime = resDateTime }; _orderServiceMock.Setup(os => os.CreateOrder(rq)).ReturnsAsync(expectedResponse); var sut = new OrdersController(_orderServiceMock.Object, _loggerMock.Object); //Act var result = await sut.CreateOrder(rq); //Assert Assert.IsAssignableFrom <ObjectResult>(result); Assert.That(expectedResponse == ((ObjectResult)result).Value); }
public void CreateOrder_ProductNames_ReturnsObjectResult(string productName, ObjectResult expectedObjectResult) { var controller = new OrdersController(); var actual = controller.CreateOrder(productName); var expectedResult = expectedObjectResult.GetType(); Assert.IsInstanceOfType(actual, expectedResult); }
public void CreateOrder_CreatesNewOrder() { var orderList = new List <Order>(); var orderController = new OrdersController(new FakeOrderRepository(orderList)); var order = new Order("Company 1", 10); orderController.CreateOrder(order); Assert.AreSame(orderList[0], order); }
public void OrdersController_CreateOrder_ReturnsCorrectView() { var factoryMock = new Mock <ComessaEntitiesFactory>(); var controller = new OrdersController(factoryMock.Object); var result = controller.CreateOrder(2) as PartialViewResult; //the old way //Assert.IsNotNull(result); //Assert.AreEqual(result.ViewName, "CreateOrderView"); Assert.That(result, Is.Not.Null); Assert.That(result.ViewName, Is.EqualTo("CreateOrderView")); }
public async Task CreatingACustomer_Really_CreatesAndSavesTheCustomer() { var result = await _subject.CreateOrder("Json Order Data Here"); var createdId = result.Value; var orderFromDb = await _container.OrdersFacade.GetOrderById(createdId); Assert.NotNull(orderFromDb); // TODO: Compare fields to sent JSON, make sure they match }
public async Task Create_order_bad_request() { //Arrange _mediatorMock.Setup(x => x.SendAsync(It.IsAny <IdentifiedCommand <CreateOrderCommand, bool> >())) .Returns(Task.FromResult(true)); //Act var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object); var actionResult = await orderController.CreateOrder(new CreateOrderCommand(), String.Empty) as BadRequestResult; //Assert Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.BadRequest); }
public async Task Create_order_with_requestId_success() { //Arrange _mediatorMock.Setup(x => x.Send(It.IsAny <IdentifiedCommand <CreateOrderCommand, bool> >(), default(System.Threading.CancellationToken))) .Returns(Task.FromResult(true)); //Act var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object); var actionResult = await orderController.CreateOrder(new CreateOrderCommand(), Guid.NewGuid().ToString()) as OkResult; //Assert Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK); }
public void CreateOrder_CreateOrderSuccesfully_ReturnCreated() { using (new TransactionScope()) { Product product = CreateTestObjects.CreateNewProduct("G15", "Keyboard for gaming.", "Logitech", 125.4m, 2); Customer customer = CreateTestObjects.CreateNewCustomer("Kia", "Hankola", "Helsinki", "Helsingintie 16"); _context.Products.Add(product); _context.Customers.Add(customer); _context.SaveChanges(); Order order = CreateTestObjects.CreateNewOrder(customer.CustomerId, product.ProductId, 4); HttpMethod test = new HttpMethod("Test"); _controller.Request = new HttpRequestMessage(test, "/"); _controller.Configuration = new HttpConfiguration(); var result = _controller.CreateOrder(order) as CreatedNegotiatedContentResult <Order>; var ordersResult = result.Content; Assert.IsNotNull(ordersResult); } }
public async Task Throw_Error_When_CreatingOrder_With_Missing_OrderId() { //Arrange request var rq = new CreateOrderRequest { CustomerId = "CustomerId" }; //response var resDateTime = DateTime.Now; var expectedResponse = new ErrorResponse(ErrorCode.ModelBindingException, "Bad Request"); _orderServiceMock.Setup(os => os.CreateOrder(rq)).ThrowsAsync(new HttpStatusCodeException(HttpStatusCode.BadRequest, expectedResponse)); var sut = new OrdersController(_orderServiceMock.Object, _loggerMock.Object); //Act var result = await sut.CreateOrder(rq); //Assert Assert.IsAssignableFrom <ObjectResult>(result); Assert.That(expectedResponse == ((ObjectResult)result).Value); }
public async Task CreateOrder_Success() { // Arrange var loggerRepository = Loggers.OrderRepositoryLogger(); var mapper = Mapper.Get(); var dbContext = _fixture.Context; var category = NewDatas.NewCategory(); var user = NewDatas.NewUser(); await dbContext.Categories.AddAsync(category); await dbContext.Users.AddAsync(user); await dbContext.SaveChangesAsync(); var product1 = NewDatas.NewProduct(); product1.CategoryId = category.CategoryId; var product2 = NewDatas.NewProduct(); product2.CategoryId = category.CategoryId; await dbContext.Products.AddRangeAsync(product1, product2); await dbContext.SaveChangesAsync(); var contextAccessor = ContextAccesser.mockHttpAccessor(user.Id); var orderCartRepository = new OrderCartRepository(loggerRepository, contextAccessor, mapper, dbContext); var orderRequest = new List <CartOrderRequest> { new CartOrderRequest { ProductId = product1.ProductId, Quantity = 3, }, new CartOrderRequest { ProductId = product2.ProductId, Quantity = 2, }, }; // Act var orderController = new OrdersController(orderCartRepository); var result = await orderController.CreateOrder(orderRequest); // Assert var orderResultType = Assert.IsType <ActionResult <IEnumerable <CartOrderRespone> > >(result); var getOrdersResultValue = Assert.IsType <CreatedResult>(result.Result); var ordersValue = getOrdersResultValue.Value as List <CartOrderRespone>; Assert.NotEmpty(ordersValue); var totalPriceReq = (product1.Price * orderRequest[0].Quantity) + (product2.Price * orderRequest[1].Quantity); var totalPriceRes = 0; ordersValue.ForEach(order => totalPriceRes += order.Product.Price * order.Quantity); Assert.Equal(totalPriceReq, totalPriceRes); }