public void UpdateContactCallsCollaborators() { var model = ContactsModelObjectMother.Random(); mediator.Setup(x => x.Send(It.IsAny <UpdateContactCommand>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(Unit.Value)); var returned = controller.Update(model).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is OkObjectResult); var ok = returned.Result as OkObjectResult; Assert.IsNotNull(ok.Value); var apiResult = ok.Value as ApiContactResultModel; Assert.IsNotNull(apiResult); Assert.AreEqual(model.Id, apiResult.ContactId); Assert.IsNull(apiResult.Errors); Assert.IsTrue(apiResult.Success); mediator.VerifyAll(); }
public void InvalidContactThrowsException() { var model = ContactsModelObjectMother.Random(); var errors = new List <ValidationResult>(); var maxErrors = new Random().Next(1, 10); for (var i = 0; i < maxErrors; i++) { errors.Add(new ValidationResult(Faker.Lorem.Sentence())); } mediator.Setup(x => x.Send(It.IsAny <UpdateContactCommand>(), It.IsAny <CancellationToken>())).Throws(new EntityNotFound("Invalid contact")); var returned = controller.Update(model).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is NotFoundObjectResult); var notfound = returned.Result as NotFoundObjectResult; Assert.IsNotNull(notfound.Value); var apiResult = notfound.Value as ApiContactResultModel; Assert.IsNotNull(apiResult); Assert.IsNull(apiResult.ContactId); Assert.IsNotNull(apiResult.Errors); Assert.AreEqual(1, apiResult.Errors.Count()); Assert.IsFalse(apiResult.Success); mediator.VerifyAll(); }
public void AddContactCallsCollaboratorsAsync() { var model = ContactsModelObjectMother.Random(); model.Id = null; mediator.Setup(x => x.Send(It.IsAny <AddContactCommand>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(Unit.Value)); var returned = controller.Add(model).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is OkObjectResult); var ok = returned.Result as OkObjectResult; Assert.IsNotNull(ok.Value); var apiResult = ok.Value as ApiContactResultModel; Assert.IsNotNull(apiResult); Assert.DoesNotThrow(() => new IdValueObject(apiResult.ContactId)); Assert.IsNull(apiResult.Errors); Assert.IsTrue(apiResult.Success); mediator.VerifyAll(); }
public void InvalidModelThrowsException() { var model = ContactsModelObjectMother.Random(); mediator.Setup(x => x.Send(It.IsAny <UpdateContactCommand>(), It.IsAny <CancellationToken>())).Throws(new InvalidEntityException("Invalid entity")); var returned = controller.Update(model).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is BadRequestObjectResult); var br = returned.Result as BadRequestObjectResult; Assert.IsNotNull(br.Value); var apiResult = br.Value as ApiContactResultModel; Assert.IsNotNull(apiResult); Assert.IsNull(apiResult.ContactId); Assert.IsNotNull(apiResult.Errors); Assert.AreEqual(1, apiResult.Errors.Count()); Assert.IsFalse(apiResult.Success); mediator.VerifyAll(); }
public void ValidContactReturnsOk() { var model = ContactsModelObjectMother.Random(); model.Id = null; VerifyCall(model, System.Net.HttpStatusCode.OK); }
public void InvalidContactReturnsBadRequest() { var model = ContactsModelObjectMother.Random(); model.FirstName = null; VerifyCall(model, System.Net.HttpStatusCode.BadRequest); }
public void AddContactWithInvalidValuesThrowsException() { var model = ContactsModelObjectMother.Random(); model.FirstName = null; var cmd = new AddContactCommand(model); var handler = new AddContactCommandHandler(uow.Object, eventBus.Object, repo.Object); Assert.Throws <EntityValidationException>(() => handler.Handle(cmd, new System.Threading.CancellationToken())); }
public void ValidContactReturnsOk() { var entity = ContactEntityObjectMother.Random(); uow.StartChanges(); repository.Add(entity); uow.CommitChanges(); var model = ContactsModelObjectMother.FromEntity(entity); VerifyCall(model, System.Net.HttpStatusCode.OK); }
public void ContactAlreadyExistsReturnBadRequest() { var entity = ContactEntityObjectMother.Random(); uow.StartChanges(); repository.Add(entity); uow.CommitChanges(); var model = ContactsModelObjectMother.FromEntity(entity); model.Id = null; VerifyCall(model, System.Net.HttpStatusCode.BadRequest); }
public void ContactAlreadyExistsReturnBadRequest() { var firstContact = ContactEntityObjectMother.Random(); var secondContact = ContactEntityObjectMother.Random(); secondContact.Name = firstContact.Name; uow.StartChanges(); repository.Add(firstContact); uow.CommitChanges(); var model = ContactsModelObjectMother.FromEntity(secondContact); VerifyCall(model, System.Net.HttpStatusCode.BadRequest); }
public void AddRepeatedContactThrowsException() { var original = ContactEntityObjectMother.Random(); var duplicated = ContactEntityObjectMother.Random(); duplicated.Name = original.Name; repo.Setup(x => x.ExistsContactWithName(duplicated.Name, null)).Returns(true); var model = ContactsModelObjectMother.FromEntity(duplicated); var cmd = new AddContactCommand(model); var handler = new AddContactCommandHandler(uow.Object, eventBus.Object, repo.Object); Assert.Throws <DomainException>(() => handler.Handle(cmd, new System.Threading.CancellationToken())); }
public void SearchContactCallsCollaborators() { var model = ContactsModelObjectMother.Random(); var dto = new ContactDto() { ContactId = model.Id, FirstName = model.FirstName, LastName = model.LastName, EmailsCount = model.EmailAddresses.Count(), PhonesCount = model.PhoneNumbers.Count() }; var result = new SearchResults <ContactDto>(1, new List <ContactDto>() { dto }); mediator.Setup(x => x.Send(It.IsAny <GetContactsQuery>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult(result)); var returned = controller.Search(new ContactsSearchCriteriaModel()).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is OkObjectResult); var ok = returned.Result as OkObjectResult; Assert.IsNotNull(ok.Value); var apiResult = ok.Value as SearchResults <ContactDto>; Assert.IsNotNull(apiResult); Assert.AreEqual(result.Total, apiResult.Total); Assert.IsNotNull(apiResult.Results); Assert.AreEqual(result.Results.Count(), apiResult.Results.Count()); foreach (var contactDto in apiResult.Results) { Assert.IsTrue(result.Results.Any(x => x.ContactId == contactDto.ContactId)); } mediator.VerifyAll(); }
public void AddContactCallsCollaborators() { var model = ContactsModelObjectMother.Random(); repo.Setup(x => x.Add(It.IsAny <ContactEntity>())); uow.Setup(x => x.StartChanges()); uow.Setup(x => x.CommitChanges()); eventBus.Setup(x => x.Record(It.Is <ContactAddedDomainEvent>(p => p.FirstName == model.FirstName && p.LastName == model.LastName && p.AggregateRootId == model.Id))); eventBus.Setup(x => x.PublishAsync()).Returns(Task.Delay(500)); var cmd = new AddContactCommand(model); var handler = new AddContactCommandHandler(uow.Object, eventBus.Object, repo.Object); var x = handler.Handle(cmd, new System.Threading.CancellationToken()).Result; repo.VerifyAll(); uow.VerifyAll(); eventBus.VerifyAll(); }
public void GetContactCallsCollaborators() { var model = ContactsModelObjectMother.Random(); mediator.Setup(x => x.Send(It.Is <GetContactQuery>(x => x.Id == model.Id), It.IsAny <CancellationToken>())).Returns(Task.FromResult(model)); var returned = controller.Get(model.Id).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is OkObjectResult); var ok = returned.Result as OkObjectResult; Assert.IsNotNull(ok.Value); var apiResult = ok.Value as ContactsModel; Assert.IsNotNull(apiResult); Assert.AreEqual(model.Id, apiResult.Id); Assert.AreEqual(model.FirstName, apiResult.FirstName); Assert.AreEqual(model.LastName, apiResult.LastName); Assert.IsNotNull(apiResult.PhoneNumbers); Assert.IsNotNull(apiResult.EmailAddresses); Assert.AreEqual(model.PhoneNumbers.Count(), apiResult.PhoneNumbers.Count()); Assert.AreEqual(model.EmailAddresses.Count(), apiResult.EmailAddresses.Count()); foreach (var email in apiResult.EmailAddresses) { Assert.IsTrue(model.EmailAddresses.Any(x => x == email)); } foreach (var phone in apiResult.PhoneNumbers) { Assert.IsTrue(model.PhoneNumbers.Any(x => x.PhoneType == phone.PhoneType && x.PhoneNumber == phone.PhoneNumber)); } mediator.VerifyAll(); }
public void InvalidModelValidationThrowsException() { var model = ContactsModelObjectMother.Random(); var errors = new List <ValidationResult>(); var maxErrors = new Random().Next(1, 10); for (var i = 0; i < maxErrors; i++) { errors.Add(new ValidationResult(Faker.Lorem.Sentence())); } mediator.Setup(x => x.Send(It.IsAny <UpdateContactCommand>(), It.IsAny <CancellationToken>())).Throws(new EntityValidationException(errors)); var returned = controller.Update(model).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is BadRequestObjectResult); var br = returned.Result as BadRequestObjectResult; Assert.IsNotNull(br.Value); var apiResult = br.Value as ApiContactResultModel; Assert.IsNotNull(apiResult); Assert.IsNull(apiResult.ContactId); Assert.IsNotNull(apiResult.Errors); Assert.AreEqual(errors.Count, apiResult.Errors.Count()); Assert.IsFalse(apiResult.Success); foreach (var error in apiResult.Errors) { Assert.IsTrue(errors.Any(x => x.ErrorMessage == error)); } mediator.VerifyAll(); }
public void UpdateSameContactDoesNotThrowsException() { var contact = ContactEntityObjectMother.Random(); var model = ContactsModelObjectMother.FromEntity(contact); repo.Setup(x => x.GetById(contact.Id)).Returns(contact); repo.Setup(x => x.ExistsContactWithName(contact.Name, contact.Id)).Returns(false); repo.Setup(x => x.Update(It.IsAny <ContactEntity>())); uow.Setup(x => x.StartChanges()); uow.Setup(x => x.CommitChanges()); eventBus.Setup(x => x.Record(It.Is <ContactUpdatedDomainEvent>(p => p.FirstName == model.FirstName && p.LastName == model.LastName && p.AggregateRootId == model.Id))); eventBus.Setup(x => x.PublishAsync()).Returns(Task.Delay(500)); var cmd = new UpdateContactCommand(model); var handler = new UpdateContactCommandHandler(uow.Object, eventBus.Object, repo.Object); var result = handler.Handle(cmd, new System.Threading.CancellationToken()).Result; repo.VerifyAll(); uow.VerifyAll(); eventBus.VerifyAll(); }
public void UnknownContactReturnsNotFound() { var model = ContactsModelObjectMother.Random(); VerifyCall(model, System.Net.HttpStatusCode.NotFound); }