public void DeleteContactCallsCollaborators() { var contact = ContactEntityObjectMother.Random(); repo.Setup(x => x.GetById(contact.Id)).Returns(contact); repo.Setup(x => x.Delete(It.Is <IdValueObject>(p => p == contact.Id))); uow.Setup(x => x.StartChanges()); uow.Setup(x => x.CommitChanges()); eventBus.Setup( x => x.Record(It.Is <ContactDeletedDomainEvent>( p => p.FirstName == contact.Name.FirstName && p.LastName == contact.Name.LastName && p.AggregateRootId == contact.Id.Value))); eventBus.Setup(x => x.PublishAsync()).Returns(Task.Delay(500)); var cmd = new DeleteContactCommand(contact.Id.Value); var handler = new DeleteContactCommandHandler(uow.Object, eventBus.Object, repo.Object); var x = handler.Handle(cmd, new System.Threading.CancellationToken()).Result; repo.VerifyAll(); uow.VerifyAll(); eventBus.VerifyAll(); }
public void InvalidContactThrowsException() { var id = ContactEntityObjectMother.Random().Id.Value; mediator.Setup(x => x.Send(It.Is <DeleteContactCommand>(x => x.Id == id), It.IsAny <CancellationToken>())).Throws(new EntityNotFound("Invalid entity")); var returned = controller.Delete(id).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is NotFoundObjectResult); var br = returned.Result as NotFoundObjectResult; 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 GetAnExistingContactReturnEntity() { var entity = ContactEntityObjectMother.Random(); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddPhoneNumber(PhoneValueObjectObjectMother.Random()); entity.AddPhoneNumber(PhoneValueObjectObjectMother.Random()); _repo.Add(entity); _context.SaveChanges(); var fromRepo = _repo.GetById(entity.Id); Assert.IsNotNull(fromRepo); Assert.AreEqual(entity.Id, fromRepo.Id); Assert.AreEqual(entity.Name, fromRepo.Name); Assert.AreEqual(entity.EmailAddresses.Count, fromRepo.EmailAddresses.Count); Assert.AreEqual(entity.PhoneNumbers.Count, fromRepo.PhoneNumbers.Count); foreach (var email in entity.EmailAddresses) { Assert.IsTrue(fromRepo.EmailAddresses.Contains(email)); } foreach (var phone in entity.PhoneNumbers) { Assert.IsTrue(fromRepo.PhoneNumbers.Contains(phone)); } }
public void CheckReturnFalseForNonExistingContact() { var entity = ContactEntityObjectMother.Random(); Assert.IsFalse(_repo.ExistsContactWithName(entity.Name)); Assert.IsFalse(_repo.ExistsContactWithName(entity.Name, entity.Id)); }
public void ExistingContactReturnsEntity() { var contact = ContactEntityObjectMother.Random(); repo.Setup(x => x.GetById(contact.Id)).Returns(contact); var cmd = new GetContactQuery(contact.Id.Value); var handler = new GetContactQueryHandler(uow.Object, eventBus.Object, repo.Object); var model = handler.Handle(cmd, new System.Threading.CancellationToken()).Result; Assert.IsNotNull(model); Assert.AreEqual(contact.Id.Value, model.Id); Assert.AreEqual(contact.Name.FirstName, model.FirstName); Assert.AreEqual(contact.Name.LastName, model.LastName); Assert.AreEqual(contact.EmailAddresses.Count, model.EmailAddresses.Count()); Assert.AreEqual(contact.PhoneNumbers.Count, model.PhoneNumbers.Count()); foreach (var email in contact.EmailAddresses) { Assert.IsTrue(model.EmailAddresses.Contains(email.Value)); } foreach (var phone in contact.PhoneNumbers) { Assert.IsTrue(model.PhoneNumbers.Any(x => x.PhoneType == phone.PhoneType && x.PhoneNumber == phone.PhoneNumber)); } repo.VerifyAll(); }
public void CreateNewContactContainsNoDependencies() { var contact = ContactEntityObjectMother.Random(); Assert.NotNull(contact); Assert.AreEqual(0, contact.EmailAddresses.Count); Assert.AreEqual(0, contact.PhoneNumbers.Count); }
public void Init() { var contact = ContactEntityObjectMother.Random(); _event = new MyEvent(contact.Id.Value); _bus = new ContactsBook.Infrastructure.EventsBus.EventBus(_dict); MySubscriber.DomainEvent = null; }
public void ValidContactReturnsOk() { var entity = ContactEntityObjectMother.Random(); uow.StartChanges(); repository.Add(entity); uow.CommitChanges(); VerifyCall("/" + entity.Id.Value, System.Net.HttpStatusCode.OK); }
public void CheckReturnTrueForExistingContact() { var entity = ContactEntityObjectMother.Random(); _repo.Add(entity); _context.SaveChanges(); Assert.IsTrue(_repo.ExistsContactWithName(entity.Name)); Assert.IsFalse(_repo.ExistsContactWithName(entity.Name, entity.Id)); }
public void ContactAddEmailAddressesAddNonExistingOrNull() { var contact = ContactEntityObjectMother.Random(); var email1 = EmailValueObjectObjectMother.Random(); contact.AddEmailAddress(email1); contact.AddEmailAddress(null); contact.AddEmailAddress(email1); Assert.AreEqual(1, contact.EmailAddresses.Count); Assert.IsTrue(contact.EmailAddresses.Contains(email1)); }
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 ContactAddPhoneNumbersAddNonExistingOrNull() { var contact = ContactEntityObjectMother.Random(); var phone1 = PhoneValueObjectObjectMother.Random(); contact.AddPhoneNumber(phone1); contact.AddEmailAddress(null); contact.AddPhoneNumber(phone1); Assert.AreEqual(1, contact.PhoneNumbers.Count); Assert.IsTrue(contact.PhoneNumbers.Contains(phone1)); }
public void ContactRemoveAllPhoneNumbersRemoveAllPhoneNumbers() { var contact = ContactEntityObjectMother.Random(); var phone1 = PhoneValueObjectObjectMother.Random(); var phone2 = PhoneValueObjectObjectMother.Random(); contact.AddPhoneNumber(phone1); contact.AddPhoneNumber(phone2); contact.RemoveAllPhoneNumbers(); Assert.AreEqual(0, contact.PhoneNumbers.Count); }
public void ContactRemoveAllAddressRemoveAllAddresses() { var contact = ContactEntityObjectMother.Random(); var email1 = EmailValueObjectObjectMother.Random(); var email2 = EmailValueObjectObjectMother.Random(); contact.AddEmailAddress(email1); contact.AddEmailAddress(email2); contact.RemoveAllEmailAddress(); Assert.AreEqual(0, contact.EmailAddresses.Count); }
public void ContactAddEmailAddressesAddsMailAddresses() { var contact = ContactEntityObjectMother.Random(); var email1 = EmailValueObjectObjectMother.Random(); var email2 = EmailValueObjectObjectMother.Random(); contact.AddEmailAddress(email1); contact.AddEmailAddress(email2); Assert.AreEqual(2, contact.EmailAddresses.Count); Assert.IsTrue(contact.EmailAddresses.Contains(email1)); Assert.IsTrue(contact.EmailAddresses.Contains(email2)); }
public void ContactRemovePhoneNumberDoesNotThrowError() { var contact = ContactEntityObjectMother.Random(); var phone1 = PhoneValueObjectObjectMother.Random(); var phone2 = PhoneValueObjectObjectMother.Random(); contact.AddPhoneNumber(phone1); contact.RemovePhoneNumber(phone2); Assert.AreEqual(1, contact.PhoneNumbers.Count); Assert.IsTrue(contact.PhoneNumbers.Contains(phone1)); }
public void ContactAddPhoneNumbersAddsPhoneNumbers() { var contact = ContactEntityObjectMother.Random(); var phone1 = PhoneValueObjectObjectMother.Random(); var phone2 = PhoneValueObjectObjectMother.Random(); contact.AddPhoneNumber(phone1); contact.AddPhoneNumber(phone2); Assert.AreEqual(2, contact.PhoneNumbers.Count); Assert.IsTrue(contact.PhoneNumbers.Contains(phone1)); Assert.IsTrue(contact.PhoneNumbers.Contains(phone2)); }
public void ContactRemoveEmailAddressesDoesNotThrowError() { var contact = ContactEntityObjectMother.Random(); var email1 = EmailValueObjectObjectMother.Random(); var email2 = EmailValueObjectObjectMother.Random(); contact.AddEmailAddress(email1); contact.RemoveEmailAddress(email2); Assert.AreEqual(1, contact.EmailAddresses.Count); Assert.IsTrue(contact.EmailAddresses.Contains(email1)); }
public void SearchWithMatchesReturnCorrectResult() { _repo.Add(ContactEntityObjectMother.Random()); _repo.Add(ContactEntityObjectMother.Random()); _repo.Add(ContactEntityObjectMother.Random()); _repo.Add(ContactEntityObjectMother.Random()); _repo.Add(ContactEntityObjectMother.Random()); var first = ContactEntityObjectMother.Random(); first.Name = new ContactNameValueObject("Peter", "Parker"); var second = ContactEntityObjectMother.Random(); second.Name = new ContactNameValueObject("Peter", "Cloud"); var third = ContactEntityObjectMother.Random(); third.Name = new ContactNameValueObject("Foo", "Pet"); _repo.Add(first); _repo.Add(second); _repo.Add(third); _context.SaveChanges(); var results1 = _repo.SearchByCriteria(new ContactSearchCriteria(1, 1, "Pet")); var results2 = _repo.SearchByCriteria(new ContactSearchCriteria(2, 1, "Pet")); var results3 = _repo.SearchByCriteria(new ContactSearchCriteria(3, 1, "Pet")); var results4 = _repo.SearchByCriteria(new ContactSearchCriteria(1, 3, "Pet")); Assert.IsNotNull(results1); Assert.IsNotNull(results2); Assert.IsNotNull(results3); Assert.IsNotNull(results4); Assert.AreEqual(3, results1.Total); Assert.AreEqual(3, results2.Total); Assert.AreEqual(3, results3.Total); Assert.AreEqual(3, results4.Total); Assert.IsNotNull(results1.Results); Assert.IsNotNull(results2.Results); Assert.IsNotNull(results3.Results); Assert.IsNotNull(results4.Results); Assert.AreEqual(1, results1.Results.Count()); Assert.AreEqual(1, results2.Results.Count()); Assert.AreEqual(1, results3.Results.Count()); Assert.AreEqual(3, results4.Results.Count()); Assert.AreEqual(first.Id.Value, results1.Results.First().ContactId); Assert.AreEqual(second.Id.Value, results2.Results.First().ContactId); Assert.AreEqual(third.Id.Value, results3.Results.First().ContactId); var r4 = results4.Results.ToList(); Assert.AreEqual(first.Id.Value, r4[0].ContactId); Assert.AreEqual(second.Id.Value, r4[1].ContactId); Assert.AreEqual(third.Id.Value, r4[2].ContactId); }
public void CheckReturnTrueForExistingContactWithOtherId() { var firstEntity = ContactEntityObjectMother.Random(); _repo.Add(firstEntity); _context.SaveChanges(); var secondEntity = ContactEntityObjectMother.Random(); secondEntity.Name = firstEntity.Name; Assert.IsTrue(_repo.ExistsContactWithName(firstEntity.Name)); Assert.IsFalse(_repo.ExistsContactWithName(firstEntity.Name, firstEntity.Id)); Assert.IsTrue(_repo.ExistsContactWithName(secondEntity.Name, secondEntity.Id)); }
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 InvalidContactThrowsException() { var id = ContactEntityObjectMother.Random().Id.Value; mediator.Setup(x => x.Send(It.Is <GetContactQuery>(x => x.Id == id), It.IsAny <CancellationToken>())).Throws(new EntityNotFound("Invalid entity")); var returned = controller.Get(id).Result; Assert.IsNotNull(returned); Assert.IsNotNull(returned.Result); Assert.IsTrue(returned.Result is NotFoundResult); mediator.VerifyAll(); }
public void ContactAddEmailsListAddAllValidAndNoRepeated() { var contact = ContactEntityObjectMother.Random(); var email1 = EmailValueObjectObjectMother.Random(); var email2 = EmailValueObjectObjectMother.Random(); contact.AddEmailAddresses(new List <string> { email1.Value, email2.Value, null, email1.Value, email2.Value }); Assert.AreEqual(2, contact.EmailAddresses.Count); Assert.IsTrue(contact.EmailAddresses.Contains(email1)); Assert.IsTrue(contact.EmailAddresses.Contains(email2)); }
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 AddContactAddTheEntity() { var entity = ContactEntityObjectMother.Random(); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddPhoneNumber(PhoneValueObjectObjectMother.Random()); entity.AddPhoneNumber(PhoneValueObjectObjectMother.Random()); _repo.Add(entity); _context.SaveChanges(); VerifySaved(entity); }
public void SearchWithoutMatchesReturnEmptyresult() { _repo.Add(ContactEntityObjectMother.Random()); _repo.Add(ContactEntityObjectMother.Random()); _repo.Add(ContactEntityObjectMother.Random()); _repo.Add(ContactEntityObjectMother.Random()); _repo.Add(ContactEntityObjectMother.Random()); _context.SaveChanges(); var results = _repo.SearchByCriteria(new ContactSearchCriteria(1, 1, DateTime.Now.ToLongDateString() + DateTime.Now.Ticks.ToString())); Assert.IsNotNull(results); Assert.AreEqual(0, results.Total); Assert.IsNotNull(results.Results); Assert.AreEqual(0, results.Results.Count()); }
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 DeleteRemoveTheContact() { var entity = ContactEntityObjectMother.Random(); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddEmailAddress(EmailValueObjectObjectMother.Random()); entity.AddPhoneNumber(PhoneValueObjectObjectMother.Random()); entity.AddPhoneNumber(PhoneValueObjectObjectMother.Random()); _repo.Add(entity); _context.SaveChanges(); _repo.Delete(entity.Id); _context.SaveChanges(); Assert.IsFalse(_context.Contacts.Any(x => x.Id == entity.Id.Value)); Assert.IsFalse(_context.ContactEmails.Any(x => x.ContactId == entity.Id.Value)); Assert.IsFalse(_context.ContactPhones.Any(x => x.ContactId == entity.Id.Value)); }
public void ContactAddPhonesListAddAllValidAndNoRepeated() { var contact = ContactEntityObjectMother.Random(); var phone1 = PhoneValueObjectObjectMother.Random(); var phone2 = PhoneValueObjectObjectMother.Random(); contact.AddPhoneNumbers(new List <Tuple <PhoneType, string> > { new Tuple <PhoneType, string>(phone1.PhoneType, phone1.PhoneNumber), new Tuple <PhoneType, string>(phone2.PhoneType, phone2.PhoneNumber), null, new Tuple <PhoneType, string>(phone1.PhoneType, phone1.PhoneNumber), new Tuple <PhoneType, string>(phone2.PhoneType, phone2.PhoneNumber) }); Assert.AreEqual(2, contact.PhoneNumbers.Count); Assert.IsTrue(contact.PhoneNumbers.Contains(phone1)); Assert.IsTrue(contact.PhoneNumbers.Contains(phone2)); }
public void SearchCallsCollaborators() { var rnd = new Random(); var page = rnd.Next(1, 100); var size = rnd.Next(1, 100); var text = Faker.Lorem.GetFirstWord(); var contact = ContactEntityObjectMother.Random(); var toReturn = new SearchResults <ContactDto>( rnd.Next(1, 100), new List <ContactDto>() { new ContactDto() { FirstName = contact.Name.FirstName, LastName = contact.Name.LastName, ContactId = contact.Id.Value, EmailsCount = contact.EmailAddresses.Count, PhonesCount = contact.PhoneNumbers.Count } } ); repo.Setup( x => x.SearchByCriteria(It.Is <ContactSearchCriteria>(p => p.PageNumber == page && p.PageSize == size && p.Text == text))) .Returns(toReturn); var cmd = new GetContactsQuery(page, size, text); var handler = new GetContactsQueryHandler(uow.Object, eventBus.Object, repo.Object); var result = handler.Handle(cmd, new System.Threading.CancellationToken()).Result; Assert.IsNotNull(result); Assert.AreEqual(toReturn.Total, result.Total); Assert.AreEqual(1, result.Results.Count()); Assert.AreEqual(contact.Id.Value, result.Results.First().ContactId); Assert.AreEqual(contact.Name.FirstName, result.Results.First().FirstName); Assert.AreEqual(contact.Name.LastName, result.Results.First().LastName); Assert.AreEqual(contact.EmailAddresses.Count, result.Results.First().EmailsCount); Assert.AreEqual(contact.PhoneNumbers.Count, result.Results.First().PhonesCount); }