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.

90 lines
4.0 KiB

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