You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
4.1 KiB

7 years ago
  1. using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
  2. using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
  3. using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
  4. using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
  5. using Moq;
  6. using System;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace UnitTest.Ordering.Application
  10. {
  11. using global::Ordering.API.Application.Models;
  12. using MediatR;
  13. using System.Collections;
  14. using System.Collections.Generic;
  15. using Xunit;
  16. using static Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands.CreateOrderCommand;
  17. public class NewOrderRequestHandlerTest
  18. {
  19. private readonly Mock<IOrderRepository> _orderRepositoryMock;
  20. private readonly Mock<IIdentityService> _identityServiceMock;
  21. private readonly Mock<IMediator> _mediator;
  22. public NewOrderRequestHandlerTest()
  23. {
  24. _orderRepositoryMock = new Mock<IOrderRepository>();
  25. _identityServiceMock = new Mock<IIdentityService>();
  26. _mediator = new Mock<IMediator>();
  27. }
  28. [Fact]
  29. public async Task Handle_return_false_if_order_is_not_persisted()
  30. {
  31. var buyerId = "1234";
  32. var fakeOrderCmd = FakeOrderRequestWithBuyer(new Dictionary<string, object>
  33. { ["cardExpiration"] = DateTime.Now.AddYears(1) });
  34. _orderRepositoryMock.Setup(orderRepo => orderRepo.GetAsync(It.IsAny<int>()))
  35. .Returns(Task.FromResult<Order>(FakeOrder()));
  36. _orderRepositoryMock.Setup(buyerRepo => buyerRepo.UnitOfWork.SaveChangesAsync(default(CancellationToken)))
  37. .Returns(Task.FromResult(1));
  38. _identityServiceMock.Setup(svc => svc.GetUserIdentity()).Returns(buyerId);
  39. //Act
  40. var handler = new CreateOrderCommandHandler(_mediator.Object, _orderRepositoryMock.Object, _identityServiceMock.Object);
  41. var result = await handler.Handle(fakeOrderCmd);
  42. //Assert
  43. Assert.False(result);
  44. }
  45. [Fact]
  46. public void Handle_throws_exception_when_no_buyerId()
  47. {
  48. //Assert
  49. Assert.Throws<ArgumentNullException>(() => new Buyer(string.Empty));
  50. }
  51. private Buyer FakeBuyer()
  52. {
  53. return new Buyer(Guid.NewGuid().ToString());
  54. }
  55. private Order FakeOrder()
  56. {
  57. return new Order("1", new Address("street", "city", "state", "country", "zipcode"), 1, "12", "111", "fakeName", DateTime.Now.AddYears(1));
  58. }
  59. private CreateOrderCommand FakeOrderRequestWithBuyer(Dictionary<string, object> args = null)
  60. {
  61. return new CreateOrderCommand(
  62. new List<BasketItem>(),
  63. userId: args != null && args.ContainsKey("userId") ? (string)args["userId"] : null,
  64. city: args != null && args.ContainsKey("city") ? (string)args["city"] : null,
  65. street: args != null && args.ContainsKey("street") ? (string)args["street"] : null,
  66. state: args != null && args.ContainsKey("state") ? (string)args["state"] : null,
  67. country: args != null && args.ContainsKey("country") ? (string)args["country"] : null,
  68. zipcode: args != null && args.ContainsKey("zipcode") ? (string)args["zipcode"] : null,
  69. cardNumber: args != null && args.ContainsKey("cardNumber") ? (string)args["cardNumber"] : "1234",
  70. cardExpiration: args != null && args.ContainsKey("cardExpiration") ? (DateTime)args["cardExpiration"] : DateTime.MinValue,
  71. cardSecurityNumber: args != null && args.ContainsKey("cardSecurityNumber") ? (string)args["cardSecurityNumber"] : "123",
  72. cardHolderName: args != null && args.ContainsKey("cardHolderName") ? (string)args["cardHolderName"] : "XXX",
  73. cardTypeId: args != null && args.ContainsKey("cardTypeId") ? (int)args["cardTypeId"] : 0);
  74. }
  75. }
  76. }