Included file-scope statement for Ordering.UnitTest project
This commit is contained in:
		
							parent
							
								
									3d3322b5e6
								
							
						
					
					
						commit
						0ba2b33825
					
				| @ -1,91 +1,79 @@ | |||||||
| using System; | namespace UnitTest.Ordering.Application; | ||||||
| 
 | 
 | ||||||
| namespace UnitTest.Ordering.Application | public class IdentifiedCommandHandlerTest | ||||||
| { | { | ||||||
|     using global::Ordering.API.Application.Models; |     private readonly Mock<IRequestManager> _requestManager; | ||||||
|     using MediatR; |     private readonly Mock<IMediator> _mediator; | ||||||
|     using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; |     private readonly Mock<ILogger<IdentifiedCommandHandler<CreateOrderCommand, bool>>> _loggerMock; | ||||||
|     using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; | 
 | ||||||
|     using Microsoft.Extensions.Logging; |     public IdentifiedCommandHandlerTest() | ||||||
|     using Moq; |  | ||||||
|     using System.Collections.Generic; |  | ||||||
|     using System.Threading.Tasks; |  | ||||||
|     using Xunit; |  | ||||||
|     public class IdentifiedCommandHandlerTest |  | ||||||
|     { |     { | ||||||
|         private readonly Mock<IRequestManager> _requestManager; |         _requestManager = new Mock<IRequestManager>(); | ||||||
|         private readonly Mock<IMediator> _mediator; |         _mediator = new Mock<IMediator>(); | ||||||
|         private readonly Mock<ILogger<IdentifiedCommandHandler<CreateOrderCommand, bool>>> _loggerMock; |         _loggerMock = new Mock<ILogger<IdentifiedCommandHandler<CreateOrderCommand, bool>>>(); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|         public IdentifiedCommandHandlerTest() |     [Fact] | ||||||
|         { |     public async Task Handler_sends_command_when_order_no_exists() | ||||||
|             _requestManager = new Mock<IRequestManager>(); |     { | ||||||
|             _mediator = new Mock<IMediator>(); |         // Arrange | ||||||
|             _loggerMock = new Mock<ILogger<IdentifiedCommandHandler<CreateOrderCommand, bool>>>(); |         var fakeGuid = Guid.NewGuid(); | ||||||
|         } |         var fakeOrderCmd = new IdentifiedCommand<CreateOrderCommand, bool>(FakeOrderRequest(), fakeGuid); | ||||||
| 
 | 
 | ||||||
|         [Fact] |         _requestManager.Setup(x => x.ExistAsync(It.IsAny<Guid>())) | ||||||
|         public async Task Handler_sends_command_when_order_no_exists() |             .Returns(Task.FromResult(false)); | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var fakeGuid = Guid.NewGuid(); |  | ||||||
|             var fakeOrderCmd = new IdentifiedCommand<CreateOrderCommand, bool>(FakeOrderRequest(), fakeGuid); |  | ||||||
| 
 | 
 | ||||||
|             _requestManager.Setup(x => x.ExistAsync(It.IsAny<Guid>())) |         _mediator.Setup(x => x.Send(It.IsAny<IRequest<bool>>(), default(System.Threading.CancellationToken))) | ||||||
|                .Returns(Task.FromResult(false)); |             .Returns(Task.FromResult(true)); | ||||||
| 
 | 
 | ||||||
|             _mediator.Setup(x => x.Send(It.IsAny<IRequest<bool>>(), default(System.Threading.CancellationToken))) |         //Act | ||||||
|                .Returns(Task.FromResult(true)); |         var handler = new IdentifiedCommandHandler<CreateOrderCommand, bool>(_mediator.Object, _requestManager.Object, _loggerMock.Object); | ||||||
|  |         var cltToken = new System.Threading.CancellationToken(); | ||||||
|  |         var result = await handler.Handle(fakeOrderCmd, cltToken); | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var handler = new IdentifiedCommandHandler<CreateOrderCommand, bool>(_mediator.Object, _requestManager.Object, _loggerMock.Object); |         Assert.True(result); | ||||||
|             var cltToken = new System.Threading.CancellationToken(); |         _mediator.Verify(x => x.Send(It.IsAny<IRequest<bool>>(), default(System.Threading.CancellationToken)), Times.Once()); | ||||||
|             var result = await handler.Handle(fakeOrderCmd, cltToken); |     } | ||||||
| 
 | 
 | ||||||
|             //Assert |     [Fact] | ||||||
|             Assert.True(result); |     public async Task Handler_sends_no_command_when_order_already_exists() | ||||||
|             _mediator.Verify(x => x.Send(It.IsAny<IRequest<bool>>(), default(System.Threading.CancellationToken)), Times.Once()); |     { | ||||||
|         } |         // Arrange | ||||||
|  |         var fakeGuid = Guid.NewGuid(); | ||||||
|  |         var fakeOrderCmd = new IdentifiedCommand<CreateOrderCommand, bool>(FakeOrderRequest(), fakeGuid); | ||||||
| 
 | 
 | ||||||
|         [Fact] |         _requestManager.Setup(x => x.ExistAsync(It.IsAny<Guid>())) | ||||||
|         public async Task Handler_sends_no_command_when_order_already_exists() |             .Returns(Task.FromResult(true)); | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var fakeGuid = Guid.NewGuid(); |  | ||||||
|             var fakeOrderCmd = new IdentifiedCommand<CreateOrderCommand, bool>(FakeOrderRequest(), fakeGuid); |  | ||||||
| 
 | 
 | ||||||
|             _requestManager.Setup(x => x.ExistAsync(It.IsAny<Guid>())) |         _mediator.Setup(x => x.Send(It.IsAny<IRequest<bool>>(), default(System.Threading.CancellationToken))) | ||||||
|                .Returns(Task.FromResult(true)); |             .Returns(Task.FromResult(true)); | ||||||
| 
 | 
 | ||||||
|             _mediator.Setup(x => x.Send(It.IsAny<IRequest<bool>>(), default(System.Threading.CancellationToken))) |         //Act | ||||||
|                .Returns(Task.FromResult(true)); |         var handler = new IdentifiedCommandHandler<CreateOrderCommand, bool>(_mediator.Object, _requestManager.Object, _loggerMock.Object); | ||||||
|  |         var cltToken = new System.Threading.CancellationToken(); | ||||||
|  |         var result = await handler.Handle(fakeOrderCmd, cltToken); | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var handler = new IdentifiedCommandHandler<CreateOrderCommand, bool>(_mediator.Object, _requestManager.Object, _loggerMock.Object); |         Assert.False(result); | ||||||
|             var cltToken = new System.Threading.CancellationToken(); |         _mediator.Verify(x => x.Send(It.IsAny<IRequest<bool>>(), default(System.Threading.CancellationToken)), Times.Never()); | ||||||
|             var result = await handler.Handle(fakeOrderCmd, cltToken); |     } | ||||||
| 
 | 
 | ||||||
|             //Assert |     private CreateOrderCommand FakeOrderRequest(Dictionary<string, object> args = null) | ||||||
|             Assert.False(result); |     { | ||||||
|             _mediator.Verify(x => x.Send(It.IsAny<IRequest<bool>>(), default(System.Threading.CancellationToken)), Times.Never()); |         return new CreateOrderCommand( | ||||||
|         } |             new List<BasketItem>(), | ||||||
| 
 |             userId: args != null && args.ContainsKey("userId") ? (string)args["userId"] : null, | ||||||
|         private CreateOrderCommand FakeOrderRequest(Dictionary<string, object> args = null) |             userName: args != null && args.ContainsKey("userName") ? (string)args["userName"] : null, | ||||||
|         { |             city: args != null && args.ContainsKey("city") ? (string)args["city"] : null, | ||||||
|             return new CreateOrderCommand( |             street: args != null && args.ContainsKey("street") ? (string)args["street"] : null, | ||||||
|                 new List<BasketItem>(), |             state: args != null && args.ContainsKey("state") ? (string)args["state"] : null, | ||||||
|                 userId: args != null && args.ContainsKey("userId") ? (string)args["userId"] : null, |             country: args != null && args.ContainsKey("country") ? (string)args["country"] : null, | ||||||
|                 userName: args != null && args.ContainsKey("userName") ? (string)args["userName"] : null, |             zipcode: args != null && args.ContainsKey("zipcode") ? (string)args["zipcode"] : null, | ||||||
|                 city: args != null && args.ContainsKey("city") ? (string)args["city"] : null, |             cardNumber: args != null && args.ContainsKey("cardNumber") ? (string)args["cardNumber"] : "1234", | ||||||
|                 street: args != null && args.ContainsKey("street") ? (string)args["street"] : null, |             cardExpiration: args != null && args.ContainsKey("cardExpiration") ? (DateTime)args["cardExpiration"] : DateTime.MinValue, | ||||||
|                 state: args != null && args.ContainsKey("state") ? (string)args["state"] : null, |             cardSecurityNumber: args != null && args.ContainsKey("cardSecurityNumber") ? (string)args["cardSecurityNumber"] : "123", | ||||||
|                 country: args != null && args.ContainsKey("country") ? (string)args["country"] : null, |             cardHolderName: args != null && args.ContainsKey("cardHolderName") ? (string)args["cardHolderName"] : "XXX", | ||||||
|                 zipcode: args != null && args.ContainsKey("zipcode") ? (string)args["zipcode"] : null, |             cardTypeId: args != null && args.ContainsKey("cardTypeId") ? (int)args["cardTypeId"] : 0); | ||||||
|                 cardNumber: args != null && args.ContainsKey("cardNumber") ? (string)args["cardNumber"] : "1234", |  | ||||||
|                 cardExpiration: args != null && args.ContainsKey("cardExpiration") ? (DateTime)args["cardExpiration"] : DateTime.MinValue, |  | ||||||
|                 cardSecurityNumber: args != null && args.ContainsKey("cardSecurityNumber") ? (string)args["cardSecurityNumber"] : "123", |  | ||||||
|                 cardHolderName: args != null && args.ContainsKey("cardHolderName") ? (string)args["cardHolderName"] : "XXX", |  | ||||||
|                 cardTypeId: args != null && args.ContainsKey("cardTypeId") ? (int)args["cardTypeId"] : 0); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,97 +1,83 @@ | |||||||
| using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; | using Microsoft.eShopOnContainers.Services.Ordering.API.Application.IntegrationEvents; | ||||||
| using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services; | 
 | ||||||
| using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; | namespace UnitTest.Ordering.Application; | ||||||
|  | 
 | ||||||
| using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | ||||||
| using Moq; |  | ||||||
| using System; |  | ||||||
| using System.Threading; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| 
 | 
 | ||||||
| 
 | public class NewOrderRequestHandlerTest | ||||||
| namespace UnitTest.Ordering.Application |  | ||||||
| { | { | ||||||
|     using global::Ordering.API.Application.IntegrationEvents; |     private readonly Mock<IOrderRepository> _orderRepositoryMock; | ||||||
|     using global::Ordering.API.Application.Models; |     private readonly Mock<IIdentityService> _identityServiceMock; | ||||||
|     using MediatR; |     private readonly Mock<IMediator> _mediator; | ||||||
|     using Microsoft.Extensions.Logging; |     private readonly Mock<IOrderingIntegrationEventService> _orderingIntegrationEventService; | ||||||
|     using System.Collections.Generic; |  | ||||||
|     using Xunit; |  | ||||||
| 
 | 
 | ||||||
|     public class NewOrderRequestHandlerTest |     public NewOrderRequestHandlerTest() | ||||||
|     { |     { | ||||||
|         private readonly Mock<IOrderRepository> _orderRepositoryMock; |  | ||||||
|         private readonly Mock<IIdentityService> _identityServiceMock; |  | ||||||
|         private readonly Mock<IMediator> _mediator; |  | ||||||
|         private readonly Mock<IOrderingIntegrationEventService> _orderingIntegrationEventService; |  | ||||||
| 
 | 
 | ||||||
|         public NewOrderRequestHandlerTest() |         _orderRepositoryMock = new Mock<IOrderRepository>(); | ||||||
|         { |         _identityServiceMock = new Mock<IIdentityService>(); | ||||||
|  |         _orderingIntegrationEventService = new Mock<IOrderingIntegrationEventService>(); | ||||||
|  |         _mediator = new Mock<IMediator>(); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|             _orderRepositoryMock = new Mock<IOrderRepository>(); |     [Fact] | ||||||
|             _identityServiceMock = new Mock<IIdentityService>(); |     public async Task Handle_return_false_if_order_is_not_persisted() | ||||||
|             _orderingIntegrationEventService = new Mock<IOrderingIntegrationEventService>(); |     { | ||||||
|             _mediator = new Mock<IMediator>(); |         var buyerId = "1234"; | ||||||
|         } |  | ||||||
| 
 | 
 | ||||||
|         [Fact] |         var fakeOrderCmd = FakeOrderRequestWithBuyer(new Dictionary<string, object> | ||||||
|         public async Task Handle_return_false_if_order_is_not_persisted() |         { ["cardExpiration"] = DateTime.Now.AddYears(1) }); | ||||||
|         { |  | ||||||
|             var buyerId = "1234"; |  | ||||||
| 
 | 
 | ||||||
|             var fakeOrderCmd = FakeOrderRequestWithBuyer(new Dictionary<string, object> |         _orderRepositoryMock.Setup(orderRepo => orderRepo.GetAsync(It.IsAny<int>())) | ||||||
|             { ["cardExpiration"] = DateTime.Now.AddYears(1) }); |             .Returns(Task.FromResult<Order>(FakeOrder())); | ||||||
| 
 | 
 | ||||||
|             _orderRepositoryMock.Setup(orderRepo => orderRepo.GetAsync(It.IsAny<int>())) |         _orderRepositoryMock.Setup(buyerRepo => buyerRepo.UnitOfWork.SaveChangesAsync(default(CancellationToken))) | ||||||
|                .Returns(Task.FromResult<Order>(FakeOrder())); |             .Returns(Task.FromResult(1)); | ||||||
| 
 | 
 | ||||||
|             _orderRepositoryMock.Setup(buyerRepo => buyerRepo.UnitOfWork.SaveChangesAsync(default(CancellationToken))) |         _identityServiceMock.Setup(svc => svc.GetUserIdentity()).Returns(buyerId); | ||||||
|                 .Returns(Task.FromResult(1)); |  | ||||||
| 
 | 
 | ||||||
|             _identityServiceMock.Setup(svc => svc.GetUserIdentity()).Returns(buyerId); |         var LoggerMock = new Mock<ILogger<CreateOrderCommandHandler>>(); | ||||||
|  |         //Act | ||||||
|  |         var handler = new CreateOrderCommandHandler(_mediator.Object, _orderingIntegrationEventService.Object, _orderRepositoryMock.Object, _identityServiceMock.Object, LoggerMock.Object); | ||||||
|  |         var cltToken = new System.Threading.CancellationToken(); | ||||||
|  |         var result = await handler.Handle(fakeOrderCmd, cltToken); | ||||||
| 
 | 
 | ||||||
|             var LoggerMock = new Mock<ILogger<CreateOrderCommandHandler>>(); |         //Assert | ||||||
|             //Act |         Assert.False(result); | ||||||
|             var handler = new CreateOrderCommandHandler(_mediator.Object, _orderingIntegrationEventService.Object, _orderRepositoryMock.Object, _identityServiceMock.Object, LoggerMock.Object); |     } | ||||||
|             var cltToken = new System.Threading.CancellationToken(); |  | ||||||
|             var result = await handler.Handle(fakeOrderCmd, cltToken); |  | ||||||
| 
 | 
 | ||||||
|             //Assert |     [Fact] | ||||||
|             Assert.False(result); |     public void Handle_throws_exception_when_no_buyerId() | ||||||
|         } |     { | ||||||
|  |         //Assert | ||||||
|  |         Assert.Throws<ArgumentNullException>(() => new Buyer(string.Empty, string.Empty)); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|         [Fact] |     private Buyer FakeBuyer() | ||||||
|         public void Handle_throws_exception_when_no_buyerId() |     { | ||||||
|         { |         return new Buyer(Guid.NewGuid().ToString(), "1"); | ||||||
|             //Assert |     } | ||||||
|             Assert.Throws<ArgumentNullException>(() => new Buyer(string.Empty, string.Empty)); |  | ||||||
|         } |  | ||||||
| 
 | 
 | ||||||
|         private Buyer FakeBuyer() |     private Order FakeOrder() | ||||||
|         { |     { | ||||||
|             return new Buyer(Guid.NewGuid().ToString(), "1"); |         return new Order("1", "fakeName", new Address("street", "city", "state", "country", "zipcode"), 1, "12", "111", "fakeName", DateTime.Now.AddYears(1)); | ||||||
|         } |     } | ||||||
| 
 | 
 | ||||||
|         private Order FakeOrder() |     private CreateOrderCommand FakeOrderRequestWithBuyer(Dictionary<string, object> args = null) | ||||||
|         { |     { | ||||||
|             return new Order("1", "fakeName", new Address("street", "city", "state", "country", "zipcode"), 1, "12", "111", "fakeName", DateTime.Now.AddYears(1)); |         return new CreateOrderCommand( | ||||||
|         } |             new List<BasketItem>(), | ||||||
| 
 |             userId: args != null && args.ContainsKey("userId") ? (string)args["userId"] : null, | ||||||
|         private CreateOrderCommand FakeOrderRequestWithBuyer(Dictionary<string, object> args = null) |             userName: args != null && args.ContainsKey("userName") ? (string)args["userName"] : null, | ||||||
|         { |             city: args != null && args.ContainsKey("city") ? (string)args["city"] : null, | ||||||
|             return new CreateOrderCommand( |             street: args != null && args.ContainsKey("street") ? (string)args["street"] : null, | ||||||
|                 new List<BasketItem>(), |             state: args != null && args.ContainsKey("state") ? (string)args["state"] : null, | ||||||
|                 userId: args != null && args.ContainsKey("userId") ? (string)args["userId"] : null, |             country: args != null && args.ContainsKey("country") ? (string)args["country"] : null, | ||||||
|                 userName: args != null && args.ContainsKey("userName") ? (string)args["userName"] : null, |             zipcode: args != null && args.ContainsKey("zipcode") ? (string)args["zipcode"] : null, | ||||||
|                 city: args != null && args.ContainsKey("city") ? (string)args["city"] : null, |             cardNumber: args != null && args.ContainsKey("cardNumber") ? (string)args["cardNumber"] : "1234", | ||||||
|                 street: args != null && args.ContainsKey("street") ? (string)args["street"] : null, |             cardExpiration: args != null && args.ContainsKey("cardExpiration") ? (DateTime)args["cardExpiration"] : DateTime.MinValue, | ||||||
|                 state: args != null && args.ContainsKey("state") ? (string)args["state"] : null, |             cardSecurityNumber: args != null && args.ContainsKey("cardSecurityNumber") ? (string)args["cardSecurityNumber"] : "123", | ||||||
|                 country: args != null && args.ContainsKey("country") ? (string)args["country"] : null, |             cardHolderName: args != null && args.ContainsKey("cardHolderName") ? (string)args["cardHolderName"] : "XXX", | ||||||
|                 zipcode: args != null && args.ContainsKey("zipcode") ? (string)args["zipcode"] : null, |             cardTypeId: args != null && args.ContainsKey("cardTypeId") ? (int)args["cardTypeId"] : 0); | ||||||
|                 cardNumber: args != null && args.ContainsKey("cardNumber") ? (string)args["cardNumber"] : "1234", |  | ||||||
|                 cardExpiration: args != null && args.ContainsKey("cardExpiration") ? (DateTime)args["cardExpiration"] : DateTime.MinValue, |  | ||||||
|                 cardSecurityNumber: args != null && args.ContainsKey("cardSecurityNumber") ? (string)args["cardSecurityNumber"] : "123", |  | ||||||
|                 cardHolderName: args != null && args.ContainsKey("cardHolderName") ? (string)args["cardHolderName"] : "XXX", |  | ||||||
|                 cardTypeId: args != null && args.ContainsKey("cardTypeId") ? (int)args["cardTypeId"] : 0); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,148 +1,134 @@ | |||||||
| using MediatR; | namespace UnitTest.Ordering.Application; | ||||||
| using Microsoft.AspNetCore.Mvc; | 
 | ||||||
| using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands; |  | ||||||
| using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries; | using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries; | ||||||
| using Microsoft.eShopOnContainers.Services.Ordering.API.Controllers; |  | ||||||
| using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services; |  | ||||||
| using Microsoft.Extensions.Logging; |  | ||||||
| using Moq; |  | ||||||
| using Ordering.API.Application.Commands; |  | ||||||
| using System; |  | ||||||
| using System.Linq; |  | ||||||
| using System.Threading; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| using Xunit; |  | ||||||
| 
 | 
 | ||||||
| namespace UnitTest.Ordering.Application | public class OrdersWebApiTest | ||||||
| { | { | ||||||
|     public class OrdersWebApiTest |     private readonly Mock<IMediator> _mediatorMock; | ||||||
|  |     private readonly Mock<IOrderQueries> _orderQueriesMock; | ||||||
|  |     private readonly Mock<IIdentityService> _identityServiceMock; | ||||||
|  |     private readonly Mock<ILogger<OrdersController>> _loggerMock; | ||||||
|  | 
 | ||||||
|  |     public OrdersWebApiTest() | ||||||
|     { |     { | ||||||
|         private readonly Mock<IMediator> _mediatorMock; |         _mediatorMock = new Mock<IMediator>(); | ||||||
|         private readonly Mock<IOrderQueries> _orderQueriesMock; |         _orderQueriesMock = new Mock<IOrderQueries>(); | ||||||
|         private readonly Mock<IIdentityService> _identityServiceMock; |         _identityServiceMock = new Mock<IIdentityService>(); | ||||||
|         private readonly Mock<ILogger<OrdersController>> _loggerMock; |         _loggerMock = new Mock<ILogger<OrdersController>>(); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|         public OrdersWebApiTest() |     [Fact] | ||||||
|         { |     public async Task Cancel_order_with_requestId_success() | ||||||
|             _mediatorMock = new Mock<IMediator>(); |     { | ||||||
|             _orderQueriesMock = new Mock<IOrderQueries>(); |         //Arrange | ||||||
|             _identityServiceMock = new Mock<IIdentityService>(); |         _mediatorMock.Setup(x => x.Send(It.IsAny<IdentifiedCommand<CancelOrderCommand, bool>>(), default(CancellationToken))) | ||||||
|             _loggerMock = new Mock<ILogger<OrdersController>>(); |             .Returns(Task.FromResult(true)); | ||||||
|         } |  | ||||||
| 
 | 
 | ||||||
|         [Fact] |         //Act | ||||||
|         public async Task Cancel_order_with_requestId_success() |         var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); | ||||||
|         { |         var actionResult = await orderController.CancelOrderAsync(new CancelOrderCommand(1), Guid.NewGuid().ToString()) as OkResult; | ||||||
|             //Arrange |  | ||||||
|             _mediatorMock.Setup(x => x.Send(It.IsAny<IdentifiedCommand<CancelOrderCommand, bool>>(), default(CancellationToken))) |  | ||||||
|                 .Returns(Task.FromResult(true)); |  | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); |         Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK); | ||||||
|             var actionResult = await orderController.CancelOrderAsync(new CancelOrderCommand(1), Guid.NewGuid().ToString()) as OkResult; |  | ||||||
| 
 | 
 | ||||||
|             //Assert |     } | ||||||
|             Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK); |  | ||||||
| 
 | 
 | ||||||
|         } |     [Fact] | ||||||
|  |     public async Task Cancel_order_bad_request() | ||||||
|  |     { | ||||||
|  |         //Arrange | ||||||
|  |         _mediatorMock.Setup(x => x.Send(It.IsAny<IdentifiedCommand<CancelOrderCommand, bool>>(), default(CancellationToken))) | ||||||
|  |             .Returns(Task.FromResult(true)); | ||||||
| 
 | 
 | ||||||
|         [Fact] |         //Act | ||||||
|         public async Task Cancel_order_bad_request() |         var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); | ||||||
|         { |         var actionResult = await orderController.CancelOrderAsync(new CancelOrderCommand(1), String.Empty) as BadRequestResult; | ||||||
|             //Arrange |  | ||||||
|             _mediatorMock.Setup(x => x.Send(It.IsAny<IdentifiedCommand<CancelOrderCommand, bool>>(), default(CancellationToken))) |  | ||||||
|                 .Returns(Task.FromResult(true)); |  | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); |         Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.BadRequest); | ||||||
|             var actionResult = await orderController.CancelOrderAsync(new CancelOrderCommand(1), String.Empty) as BadRequestResult; |     } | ||||||
| 
 | 
 | ||||||
|             //Assert |     [Fact] | ||||||
|             Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.BadRequest); |     public async Task Ship_order_with_requestId_success() | ||||||
|         } |     { | ||||||
|  |         //Arrange | ||||||
|  |         _mediatorMock.Setup(x => x.Send(It.IsAny<IdentifiedCommand<ShipOrderCommand, bool>>(), default(System.Threading.CancellationToken))) | ||||||
|  |             .Returns(Task.FromResult(true)); | ||||||
| 
 | 
 | ||||||
|         [Fact] |         //Act | ||||||
|         public async Task Ship_order_with_requestId_success() |         var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); | ||||||
|         { |         var actionResult = await orderController.ShipOrderAsync(new ShipOrderCommand(1), Guid.NewGuid().ToString()) as OkResult; | ||||||
|             //Arrange |  | ||||||
|             _mediatorMock.Setup(x => x.Send(It.IsAny<IdentifiedCommand<ShipOrderCommand, bool>>(), default(System.Threading.CancellationToken))) |  | ||||||
|                 .Returns(Task.FromResult(true)); |  | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); |         Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK); | ||||||
|             var actionResult = await orderController.ShipOrderAsync(new ShipOrderCommand(1), Guid.NewGuid().ToString()) as OkResult; |  | ||||||
| 
 | 
 | ||||||
|             //Assert |     } | ||||||
|             Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK); |  | ||||||
| 
 | 
 | ||||||
|         } |     [Fact] | ||||||
|  |     public async Task Ship_order_bad_request() | ||||||
|  |     { | ||||||
|  |         //Arrange | ||||||
|  |         _mediatorMock.Setup(x => x.Send(It.IsAny<IdentifiedCommand<CreateOrderCommand, bool>>(), default(System.Threading.CancellationToken))) | ||||||
|  |             .Returns(Task.FromResult(true)); | ||||||
| 
 | 
 | ||||||
|         [Fact] |         //Act | ||||||
|         public async Task Ship_order_bad_request() |         var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); | ||||||
|         { |         var actionResult = await orderController.ShipOrderAsync(new ShipOrderCommand(1), String.Empty) as BadRequestResult; | ||||||
|             //Arrange |  | ||||||
|             _mediatorMock.Setup(x => x.Send(It.IsAny<IdentifiedCommand<CreateOrderCommand, bool>>(), default(System.Threading.CancellationToken))) |  | ||||||
|                 .Returns(Task.FromResult(true)); |  | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); |         Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.BadRequest); | ||||||
|             var actionResult = await orderController.ShipOrderAsync(new ShipOrderCommand(1), String.Empty) as BadRequestResult; |     } | ||||||
| 
 | 
 | ||||||
|             //Assert |     [Fact] | ||||||
|             Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.BadRequest); |     public async Task Get_orders_success() | ||||||
|         } |     { | ||||||
|  |         //Arrange | ||||||
|  |         var fakeDynamicResult = Enumerable.Empty<OrderSummary>(); | ||||||
| 
 | 
 | ||||||
|         [Fact] |         _identityServiceMock.Setup(x => x.GetUserIdentity()) | ||||||
|         public async Task Get_orders_success() |             .Returns(Guid.NewGuid().ToString()); | ||||||
|         { |  | ||||||
|             //Arrange |  | ||||||
|             var fakeDynamicResult = Enumerable.Empty<OrderSummary>(); |  | ||||||
| 
 | 
 | ||||||
|             _identityServiceMock.Setup(x => x.GetUserIdentity()) |         _orderQueriesMock.Setup(x => x.GetOrdersFromUserAsync(Guid.NewGuid())) | ||||||
|                 .Returns(Guid.NewGuid().ToString()); |             .Returns(Task.FromResult(fakeDynamicResult)); | ||||||
| 
 | 
 | ||||||
|             _orderQueriesMock.Setup(x => x.GetOrdersFromUserAsync(Guid.NewGuid())) |         //Act | ||||||
|                 .Returns(Task.FromResult(fakeDynamicResult)); |         var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); | ||||||
|  |         var actionResult = await orderController.GetOrdersAsync(); | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); |         Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); | ||||||
|             var actionResult = await orderController.GetOrdersAsync(); |     } | ||||||
| 
 | 
 | ||||||
|             //Assert |     [Fact] | ||||||
|             Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); |     public async Task Get_order_success() | ||||||
|         } |     { | ||||||
|  |         //Arrange | ||||||
|  |         var fakeOrderId = 123; | ||||||
|  |         var fakeDynamicResult = new Order(); | ||||||
|  |         _orderQueriesMock.Setup(x => x.GetOrderAsync(It.IsAny<int>())) | ||||||
|  |             .Returns(Task.FromResult(fakeDynamicResult)); | ||||||
| 
 | 
 | ||||||
|         [Fact] |         //Act | ||||||
|         public async Task Get_order_success() |         var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); | ||||||
|         { |         var actionResult = await orderController.GetOrderAsync(fakeOrderId) as OkObjectResult; | ||||||
|             //Arrange |  | ||||||
|             var fakeOrderId = 123; |  | ||||||
|             var fakeDynamicResult = new Order(); |  | ||||||
|             _orderQueriesMock.Setup(x => x.GetOrderAsync(It.IsAny<int>())) |  | ||||||
|                 .Returns(Task.FromResult(fakeDynamicResult)); |  | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); |         Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK); | ||||||
|             var actionResult = await orderController.GetOrderAsync(fakeOrderId) as OkObjectResult; |     } | ||||||
| 
 | 
 | ||||||
|             //Assert |     [Fact] | ||||||
|             Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK); |     public async Task Get_cardTypes_success() | ||||||
|         } |     { | ||||||
|  |         //Arrange | ||||||
|  |         var fakeDynamicResult = Enumerable.Empty<CardType>(); | ||||||
|  |         _orderQueriesMock.Setup(x => x.GetCardTypesAsync()) | ||||||
|  |             .Returns(Task.FromResult(fakeDynamicResult)); | ||||||
| 
 | 
 | ||||||
|         [Fact] |         //Act | ||||||
|         public async Task Get_cardTypes_success() |         var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); | ||||||
|         { |         var actionResult = await orderController.GetCardTypesAsync(); | ||||||
|             //Arrange |  | ||||||
|             var fakeDynamicResult = Enumerable.Empty<CardType>(); |  | ||||||
|             _orderQueriesMock.Setup(x => x.GetCardTypesAsync()) |  | ||||||
|                 .Returns(Task.FromResult(fakeDynamicResult)); |  | ||||||
| 
 | 
 | ||||||
|             //Act |         //Assert | ||||||
|             var orderController = new OrdersController(_mediatorMock.Object, _orderQueriesMock.Object, _identityServiceMock.Object, _loggerMock.Object); |         Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); | ||||||
|             var actionResult = await orderController.GetCardTypesAsync(); |  | ||||||
| 
 |  | ||||||
|             //Assert |  | ||||||
|             Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,48 +1,46 @@ | |||||||
| using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | namespace UnitTest.Ordering; | ||||||
| using System; |  | ||||||
| 
 | 
 | ||||||
| namespace UnitTest.Ordering | using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | ||||||
|  | 
 | ||||||
|  | public class AddressBuilder | ||||||
| { | { | ||||||
|     public class AddressBuilder |     public Address Build() | ||||||
|     { |     { | ||||||
|         public Address Build() |         return new Address("street", "city", "state", "country", "zipcode"); | ||||||
|         { |     } | ||||||
|             return new Address("street", "city", "state", "country", "zipcode"); | } | ||||||
|         } | 
 | ||||||
|     } | public class OrderBuilder | ||||||
| 
 | { | ||||||
|     public class OrderBuilder |     private readonly Order order; | ||||||
|     { | 
 | ||||||
|         private readonly Order order; |     public OrderBuilder(Address address) | ||||||
| 
 |     { | ||||||
|         public OrderBuilder(Address address) |         order = new Order( | ||||||
|         { |             "userId", | ||||||
|             order = new Order( |             "fakeName", | ||||||
|                 "userId", |             address, | ||||||
|                 "fakeName", |             cardTypeId: 5, | ||||||
|                 address, |             cardNumber: "12", | ||||||
|                 cardTypeId: 5, |             cardSecurityNumber: "123", | ||||||
|                 cardNumber: "12", |             cardHolderName: "name", | ||||||
|                 cardSecurityNumber: "123", |             cardExpiration: DateTime.UtcNow); | ||||||
|                 cardHolderName: "name", |     } | ||||||
|                 cardExpiration: DateTime.UtcNow); | 
 | ||||||
|         } |     public OrderBuilder AddOne( | ||||||
| 
 |         int productId, | ||||||
|         public OrderBuilder AddOne( |         string productName, | ||||||
|             int productId, |         decimal unitPrice, | ||||||
|             string productName, |         decimal discount, | ||||||
|             decimal unitPrice, |         string pictureUrl, | ||||||
|             decimal discount, |         int units = 1) | ||||||
|             string pictureUrl, |     { | ||||||
|             int units = 1) |         order.AddOrderItem(productId, productName, unitPrice, discount, pictureUrl, units); | ||||||
|         { |         return this; | ||||||
|             order.AddOrderItem(productId, productName, unitPrice, discount, pictureUrl, units); |     } | ||||||
|             return this; | 
 | ||||||
|         } |     public Order Build() | ||||||
| 
 |     { | ||||||
|         public Order Build() |         return order; | ||||||
|         { |  | ||||||
|             return order; |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,9 +1,4 @@ | |||||||
| using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate; | public class BuyerAggregateTest | ||||||
| using Ordering.Domain.Exceptions; |  | ||||||
| using System; |  | ||||||
| using Xunit; |  | ||||||
| 
 |  | ||||||
| public class BuyerAggregateTest |  | ||||||
| { | { | ||||||
|     public BuyerAggregateTest() |     public BuyerAggregateTest() | ||||||
|     { } |     { } | ||||||
|  | |||||||
| @ -1,10 +1,6 @@ | |||||||
| using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | namespace Ordering.UnitTests.Domain; | ||||||
| using Ordering.Domain.Events; |  | ||||||
| using Ordering.Domain.Exceptions; |  | ||||||
| using System; |  | ||||||
| using UnitTest.Ordering; |  | ||||||
| using Xunit; |  | ||||||
| 
 | 
 | ||||||
|  | using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; | ||||||
| public class OrderAggregateTest | public class OrderAggregateTest | ||||||
| { | { | ||||||
|     public OrderAggregateTest() |     public OrderAggregateTest() | ||||||
|  | |||||||
| @ -1,189 +1,182 @@ | |||||||
| using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork; | namespace Ordering.UnitTests.Domain.SeedWork; | ||||||
| using System; |  | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Linq; |  | ||||||
| using Xunit; |  | ||||||
| 
 | 
 | ||||||
| namespace Ordering.UnitTests.Domain.SeedWork | public class ValueObjectTests | ||||||
| { | { | ||||||
|     public class ValueObjectTests |     public ValueObjectTests() | ||||||
|  |     { } | ||||||
|  | 
 | ||||||
|  |     [Theory] | ||||||
|  |     [MemberData(nameof(EqualValueObjects))] | ||||||
|  |     public void Equals_EqualValueObjects_ReturnsTrue(ValueObject instanceA, ValueObject instanceB, string reason) | ||||||
|     { |     { | ||||||
|         public ValueObjectTests() |         // Act | ||||||
|         { } |         var result = EqualityComparer<ValueObject>.Default.Equals(instanceA, instanceB); | ||||||
| 
 | 
 | ||||||
|         [Theory] |         // Assert | ||||||
|         [MemberData(nameof(EqualValueObjects))] |         Assert.True(result, reason); | ||||||
|         public void Equals_EqualValueObjects_ReturnsTrue(ValueObject instanceA, ValueObject instanceB, string reason) |     } | ||||||
|  | 
 | ||||||
|  |     [Theory] | ||||||
|  |     [MemberData(nameof(NonEqualValueObjects))] | ||||||
|  |     public void Equals_NonEqualValueObjects_ReturnsFalse(ValueObject instanceA, ValueObject instanceB, string reason) | ||||||
|  |     { | ||||||
|  |         // Act | ||||||
|  |         var result = EqualityComparer<ValueObject>.Default.Equals(instanceA, instanceB); | ||||||
|  | 
 | ||||||
|  |         // Assert | ||||||
|  |         Assert.False(result, reason); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private static readonly ValueObject APrettyValueObject = new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3")); | ||||||
|  | 
 | ||||||
|  |     public static readonly TheoryData<ValueObject, ValueObject, string> EqualValueObjects = new TheoryData<ValueObject, ValueObject, string> | ||||||
|  |     { | ||||||
|         { |         { | ||||||
|             // Act |             null, | ||||||
|             var result = EqualityComparer<ValueObject>.Default.Equals(instanceA, instanceB); |             null, | ||||||
|  |             "they should be equal because they are both null" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             APrettyValueObject, | ||||||
|  |             APrettyValueObject, | ||||||
|  |             "they should be equal because they are the same object" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3")), | ||||||
|  |             new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3")), | ||||||
|  |             "they should be equal because they have equal members" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3"), notAnEqualityComponent: "xpto"), | ||||||
|  |             new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3"), notAnEqualityComponent: "xpto2"), | ||||||
|  |             "they should be equal because all equality components are equal, even though an additional member was set" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectB(1, "2",  1, 2, 3 ), | ||||||
|  |             new ValueObjectB(1, "2",  1, 2, 3 ), | ||||||
|  |             "they should be equal because all equality components are equal, including the 'C' list" | ||||||
|  |         } | ||||||
|  |     }; | ||||||
| 
 | 
 | ||||||
|             // Assert |     public static readonly TheoryData<ValueObject, ValueObject, string> NonEqualValueObjects = new TheoryData<ValueObject, ValueObject, string> | ||||||
|             Assert.True(result, reason); |     { | ||||||
|  |         { | ||||||
|  |             new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(2, "3")), | ||||||
|  |             new ValueObjectA(a: 2, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(2, "3")), | ||||||
|  |             "they should not be equal because the 'A' member on ValueObjectA is different among them" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(2, "3")), | ||||||
|  |             new ValueObjectA(a: 1, b: null, c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(2, "3")), | ||||||
|  |             "they should not be equal because the 'B' member on ValueObjectA is different among them" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(a: 2, b: "3")), | ||||||
|  |             new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(a: 3, b: "3")), | ||||||
|  |             "they should not be equal because the 'A' member on ValueObjectA's 'D' member is different among them" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(a: 2, b: "3")), | ||||||
|  |             new ValueObjectB(a: 1, b: "2"), | ||||||
|  |             "they should not be equal because they are not of the same type" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectB(1, "2",  1, 2, 3 ), | ||||||
|  |             new ValueObjectB(1, "2",  1, 2, 3, 4 ), | ||||||
|  |             "they should be not be equal because the 'C' list contains one additional value" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectB(1, "2",  1, 2, 3, 5 ), | ||||||
|  |             new ValueObjectB(1, "2",  1, 2, 3 ), | ||||||
|  |             "they should be not be equal because the 'C' list contains one additional value" | ||||||
|  |         }, | ||||||
|  |         { | ||||||
|  |             new ValueObjectB(1, "2",  1, 2, 3, 5 ), | ||||||
|  |             new ValueObjectB(1, "2",  1, 2, 3, 4 ), | ||||||
|  |             "they should be not be equal because the 'C' lists are not equal" | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         [Theory] |     }; | ||||||
|         [MemberData(nameof(NonEqualValueObjects))] |  | ||||||
|         public void Equals_NonEqualValueObjects_ReturnsFalse(ValueObject instanceA, ValueObject instanceB, string reason) |  | ||||||
|         { |  | ||||||
|             // Act |  | ||||||
|             var result = EqualityComparer<ValueObject>.Default.Equals(instanceA, instanceB); |  | ||||||
| 
 | 
 | ||||||
|             // Assert |     private class ValueObjectA : ValueObject | ||||||
|             Assert.False(result, reason); |     { | ||||||
|  |         public ValueObjectA(int a, string b, Guid c, ComplexObject d, string notAnEqualityComponent = null) | ||||||
|  |         { | ||||||
|  |             A = a; | ||||||
|  |             B = b; | ||||||
|  |             C = c; | ||||||
|  |             D = d; | ||||||
|  |             NotAnEqualityComponent = notAnEqualityComponent; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private static readonly ValueObject APrettyValueObject = new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3")); |         public int A { get; } | ||||||
|  |         public string B { get; } | ||||||
|  |         public Guid C { get; } | ||||||
|  |         public ComplexObject D { get; } | ||||||
|  |         public string NotAnEqualityComponent { get; } | ||||||
| 
 | 
 | ||||||
|         public static readonly TheoryData<ValueObject, ValueObject, string> EqualValueObjects = new TheoryData<ValueObject, ValueObject, string> |         protected override IEnumerable<object> GetEqualityComponents() | ||||||
|         { |         { | ||||||
|             { |             yield return A; | ||||||
|                 null, |             yield return B; | ||||||
|                 null, |             yield return C; | ||||||
|                 "they should be equal because they are both null" |             yield return D; | ||||||
|             }, |         } | ||||||
|             { |     } | ||||||
|                 APrettyValueObject, |  | ||||||
|                 APrettyValueObject, |  | ||||||
|                 "they should be equal because they are the same object" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3")), |  | ||||||
|                 new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3")), |  | ||||||
|                 "they should be equal because they have equal members" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3"), notAnEqualityComponent: "xpto"), |  | ||||||
|                 new ValueObjectA(1, "2", Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), new ComplexObject(2, "3"), notAnEqualityComponent: "xpto2"), |  | ||||||
|                 "they should be equal because all equality components are equal, even though an additional member was set" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectB(1, "2",  1, 2, 3 ), |  | ||||||
|                 new ValueObjectB(1, "2",  1, 2, 3 ), |  | ||||||
|                 "they should be equal because all equality components are equal, including the 'C' list" |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
| 
 | 
 | ||||||
|         public static readonly TheoryData<ValueObject, ValueObject, string> NonEqualValueObjects = new TheoryData<ValueObject, ValueObject, string> |     private class ValueObjectB : ValueObject | ||||||
|  |     { | ||||||
|  |         public ValueObjectB(int a, string b, params int[] c) | ||||||
|         { |         { | ||||||
|             { |             A = a; | ||||||
|                 new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(2, "3")), |             B = b; | ||||||
|                 new ValueObjectA(a: 2, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(2, "3")), |             C = c.ToList(); | ||||||
|                 "they should not be equal because the 'A' member on ValueObjectA is different among them" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(2, "3")), |  | ||||||
|                 new ValueObjectA(a: 1, b: null, c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(2, "3")), |  | ||||||
|                 "they should not be equal because the 'B' member on ValueObjectA is different among them" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(a: 2, b: "3")), |  | ||||||
|                 new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(a: 3, b: "3")), |  | ||||||
|                 "they should not be equal because the 'A' member on ValueObjectA's 'D' member is different among them" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectA(a: 1, b: "2", c: Guid.Parse("97ea43f0-6fef-4fb7-8c67-9114a7ff6ec0"), d: new ComplexObject(a: 2, b: "3")), |  | ||||||
|                 new ValueObjectB(a: 1, b: "2"), |  | ||||||
|                 "they should not be equal because they are not of the same type" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectB(1, "2",  1, 2, 3 ), |  | ||||||
|                 new ValueObjectB(1, "2",  1, 2, 3, 4 ), |  | ||||||
|                 "they should be not be equal because the 'C' list contains one additional value" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectB(1, "2",  1, 2, 3, 5 ), |  | ||||||
|                 new ValueObjectB(1, "2",  1, 2, 3 ), |  | ||||||
|                 "they should be not be equal because the 'C' list contains one additional value" |  | ||||||
|             }, |  | ||||||
|             { |  | ||||||
|                 new ValueObjectB(1, "2",  1, 2, 3, 5 ), |  | ||||||
|                 new ValueObjectB(1, "2",  1, 2, 3, 4 ), |  | ||||||
|                 "they should be not be equal because the 'C' lists are not equal" |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|         }; |  | ||||||
| 
 |  | ||||||
|         private class ValueObjectA : ValueObject |  | ||||||
|         { |  | ||||||
|             public ValueObjectA(int a, string b, Guid c, ComplexObject d, string notAnEqualityComponent = null) |  | ||||||
|             { |  | ||||||
|                 A = a; |  | ||||||
|                 B = b; |  | ||||||
|                 C = c; |  | ||||||
|                 D = d; |  | ||||||
|                 NotAnEqualityComponent = notAnEqualityComponent; |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             public int A { get; } |  | ||||||
|             public string B { get; } |  | ||||||
|             public Guid C { get; } |  | ||||||
|             public ComplexObject D { get; } |  | ||||||
|             public string NotAnEqualityComponent { get; } |  | ||||||
| 
 |  | ||||||
|             protected override IEnumerable<object> GetEqualityComponents() |  | ||||||
|             { |  | ||||||
|                 yield return A; |  | ||||||
|                 yield return B; |  | ||||||
|                 yield return C; |  | ||||||
|                 yield return D; |  | ||||||
|             } |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private class ValueObjectB : ValueObject |         public int A { get; } | ||||||
|  |         public string B { get; } | ||||||
|  | 
 | ||||||
|  |         public List<int> C { get; } | ||||||
|  | 
 | ||||||
|  |         protected override IEnumerable<object> GetEqualityComponents() | ||||||
|         { |         { | ||||||
|             public ValueObjectB(int a, string b, params int[] c) |             yield return A; | ||||||
|  |             yield return B; | ||||||
|  | 
 | ||||||
|  |             foreach (var c in C) | ||||||
|             { |             { | ||||||
|                 A = a; |                 yield return c; | ||||||
|                 B = b; |  | ||||||
|                 C = c.ToList(); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             public int A { get; } |  | ||||||
|             public string B { get; } |  | ||||||
| 
 |  | ||||||
|             public List<int> C { get; } |  | ||||||
| 
 |  | ||||||
|             protected override IEnumerable<object> GetEqualityComponents() |  | ||||||
|             { |  | ||||||
|                 yield return A; |  | ||||||
|                 yield return B; |  | ||||||
| 
 |  | ||||||
|                 foreach (var c in C) |  | ||||||
|                 { |  | ||||||
|                     yield return c; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         private class ComplexObject : IEquatable<ComplexObject> |  | ||||||
|         { |  | ||||||
|             public ComplexObject(int a, string b) |  | ||||||
|             { |  | ||||||
|                 A = a; |  | ||||||
|                 B = b; |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             public int A { get; set; } |  | ||||||
| 
 |  | ||||||
|             public string B { get; set; } |  | ||||||
| 
 |  | ||||||
|             public override bool Equals(object obj) |  | ||||||
|             { |  | ||||||
|                 return Equals(obj as ComplexObject); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             public bool Equals(ComplexObject other) |  | ||||||
|             { |  | ||||||
|                 return other != null && |  | ||||||
|                        A == other.A && |  | ||||||
|                        B == other.B; |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             public override int GetHashCode() |  | ||||||
|             { |  | ||||||
|                 return HashCode.Combine(A, B); |  | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     private class ComplexObject : IEquatable<ComplexObject> | ||||||
|  |     { | ||||||
|  |         public ComplexObject(int a, string b) | ||||||
|  |         { | ||||||
|  |             A = a; | ||||||
|  |             B = b; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public int A { get; set; } | ||||||
|  | 
 | ||||||
|  |         public string B { get; set; } | ||||||
|  | 
 | ||||||
|  |         public override bool Equals(object obj) | ||||||
|  |         { | ||||||
|  |             return Equals(obj as ComplexObject); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public bool Equals(ComplexObject other) | ||||||
|  |         { | ||||||
|  |             return other != null && | ||||||
|  |                     A == other.A && | ||||||
|  |                     B == other.B; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public override int GetHashCode() | ||||||
|  |         { | ||||||
|  |             return HashCode.Combine(A, B); | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user