public async Task PrimaryObjectService_DeleteAsync_Valid() { // This test verifies that DeleteAsync works with valid inputs var primaryObjectService = new PrimaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); var source = new DomainModels.PrimaryObject(Guid.NewGuid()) { Description = "Description", Name = "Name", SecondaryObjects = new List <DomainModels.SecondaryObject>() { new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 1", Name = "Name 1", }, new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 2", Name = "Name 2", }, }, }; foreach (var sourceSecondaryObject in source.SecondaryObjects) { sourceSecondaryObject.PrimaryObject = source; sourceSecondaryObject.PrimaryObject_Id = source.Id; } _primaryObjectRepository.Setup(_ => _.GetByIdAsync(source.Id)).ReturnsAsync(source); await primaryObjectService.DeleteAsync(source.Id); _unitOfWork.Verify(_ => _.SaveChangesAsync(), Times.Once); }
private static void Map(ApiModels.PrimaryObject source, DomainModels.PrimaryObject target) { Ensure.That(source, nameof(source)).IsNotNull(); Ensure.That(target, nameof(target)).IsNotNull(); Ensure.That(source.Name, $"{nameof(target)}.{nameof(ApiModels.PrimaryObject.Name)}").WithException(_ => new DemoInputValidationException()).IsNotNullOrWhiteSpace(); Ensure.That(source.Description, $"{nameof(target)}.{nameof(ApiModels.PrimaryObject.Description)}").WithException(_ => new DemoInputValidationException()).IsNotNullOrWhiteSpace(); target.Description = source.Description; target.Name = source.Name; }
public async Task <DomainModels.PrimaryObject> CreateAsync(ApiModels.PrimaryObject inputModel) { Ensure.That(inputModel, nameof(inputModel)).IsNotNull(); var domainPrimaryObject = new DomainModels.PrimaryObject(Guid.NewGuid()); Map(inputModel, domainPrimaryObject); _primaryObjectRepoistory.Add(domainPrimaryObject); await _unitOfWork.SaveChangesAsync(); return(domainPrimaryObject); }
public async Task PrimaryObjectsController_UpdateAsync_Found() { // This test verifies that the controller does not throw an error when updating a valid object var primaryObjectsController = new PrimaryObjectsController(_primaryObjectService.Object); var sourcePrimaryObject = new DomainModels.PrimaryObject(Guid.NewGuid()) { Description = "Description 1", Name = "Name 1", SecondaryObjects = new List <DomainModels.SecondaryObject>() { new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 1.1", Name = "Name 1.1", }, new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 1.2", Name = "Name 1.2", }, }, }; foreach (var sourceSecondaryObject in sourcePrimaryObject.SecondaryObjects) { sourceSecondaryObject.PrimaryObject = sourcePrimaryObject; sourceSecondaryObject.PrimaryObject_Id = sourcePrimaryObject.Id; } _primaryObjectService.Setup(_ => _.UpdateAsync(sourcePrimaryObject.Id, It.IsAny <ApiModels.PrimaryObject>())).ReturnsAsync(sourcePrimaryObject); var destinationPrimaryObject = await primaryObjectsController.UpdateAsync(sourcePrimaryObject.Id, new ApiModels.PrimaryObject()); Assert.IsNotNull(destinationPrimaryObject); Assert.AreEqual(sourcePrimaryObject.Description, destinationPrimaryObject.Description); Assert.AreEqual(sourcePrimaryObject.Id, destinationPrimaryObject.Id); Assert.AreEqual(sourcePrimaryObject.Name, destinationPrimaryObject.Name); Assert.IsNotNull(destinationPrimaryObject.SecondaryObjects); Assert.AreEqual(sourcePrimaryObject.SecondaryObjects.Count, destinationPrimaryObject.SecondaryObjects.Count()); foreach (var destinationSecondaryObject in destinationPrimaryObject.SecondaryObjects) { var sourceSecondaryObject = sourcePrimaryObject.SecondaryObjects.SingleOrDefault(_ => _.Id == destinationSecondaryObject.Id); Assert.IsNotNull(sourceSecondaryObject); Assert.IsNull(destinationSecondaryObject.Description); Assert.AreEqual(sourceSecondaryObject.Id, destinationSecondaryObject.Id); Assert.IsNull(destinationSecondaryObject.Name); Assert.IsNull(destinationSecondaryObject.PrimaryObject); } }
private ApiModels.PrimaryObject Map(DomainModels.PrimaryObject domainPrimaryObject) { return(new ApiModels.PrimaryObject() { Description = domainPrimaryObject.Description, Id = domainPrimaryObject.Id, Name = domainPrimaryObject.Name, SecondaryObjects = domainPrimaryObject.SecondaryObjects .Select(domainSecondaryObject => new ApiModels.SecondaryObject() { Id = domainSecondaryObject.Id, }) .ToArray(), }); }
public async Task PrimaryObjectsController_UpdateAsync_NotFound() { // This test verifies that the controller throws the correct HttpResponseException when a non-existant object is updated var primaryObjectsController = new PrimaryObjectsController(_primaryObjectService.Object); DomainModels.PrimaryObject sourcePrimaryObject = null; _primaryObjectService.Setup(_ => _.UpdateAsync(It.IsAny <Guid>(), It.IsAny <ApiModels.PrimaryObject>())).ReturnsAsync(sourcePrimaryObject); try { await primaryObjectsController.UpdateAsync(Guid.NewGuid(), new ApiModels.PrimaryObject()); Assert.Fail(); } catch (HttpResponseException ex) { Assert.IsNotNull(ex.Response); Assert.AreEqual(HttpStatusCode.NotFound, ex.Response.StatusCode); } }