public async Task SecondaryObjectService_UpdateAsync_Valid() { // This test verifies that UpdateAsync works with valid inputs var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); var source = new ApiModels.SecondaryObject() { Description = "New Description", Name = "New Name", }; _secondaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>())) .ReturnsAsync((Guid id) => new DomainModels.SecondaryObject(id) { Description = "Description 1", Name = "Name 1", PrimaryObject = new DomainModels.PrimaryObject(Guid.NewGuid()) { Description = "Description", Name = "Name", }, }); var destination = await secondaryObjectService.UpdateAsync(Guid.NewGuid(), source); Assert.IsNotNull(destination); Assert.AreEqual(source.Description, destination.Description); Assert.AreNotEqual(Guid.Empty, destination.Id); Assert.AreEqual(source.Name, destination.Name); Assert.IsNotNull(destination.PrimaryObject); _unitOfWork.Verify(_ => _.SaveChangesAsync(), Times.Once); }
public async Task SecondaryObjectService_CreateAsync_InputModel_Null() { // This test verifies that the CreateAsync will not accept null dependencies var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); ApiModels.SecondaryObject source = null; _primaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync((Guid id) => new DomainModels.PrimaryObject(id)); var destination = await secondaryObjectService.CreateAsync(Guid.NewGuid(), source); }
public async Task SecondaryObjectService_UpdateAsync_InputModel_Null() { // This test verifies that the UpdateAsync will not accept null dependencies var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); ApiModels.SecondaryObject source = null; await secondaryObjectService.UpdateAsync(Guid.NewGuid(), source); }
public async Task <ApiModels.SecondaryObject> UpdateAsync(Guid id, ApiModels.SecondaryObject inputModel) { var domainSecondaryObject = await _secondaryObjectService.UpdateAsync(id, inputModel); Ensure.That(domainSecondaryObject, nameof(domainSecondaryObject)) .WithException(_ => new HttpResponseException(HttpStatusCode.NotFound)) .IsNotNull(); return(this.Map(domainSecondaryObject)); }
private static void Map(ApiModels.SecondaryObject source, DomainModels.SecondaryObject target) { Ensure.That(source, nameof(source)).IsNotNull(); Ensure.That(target, nameof(target)).IsNotNull(); Ensure.That(source.Name, $"{nameof(target)}.{nameof(ApiModels.SecondaryObject.Name)}").WithException(_ => new DemoInputValidationException()).IsNotNullOrWhiteSpace(); Ensure.That(source.Description, $"{nameof(target)}.{nameof(ApiModels.SecondaryObject.Description)}").WithException(_ => new DemoInputValidationException()).IsNotNullOrWhiteSpace(); target.Description = source.Description; target.Name = source.Name; }
public async Task SecondaryObjectService_CreateAsync_PrimaryObjectId_Empty() { // This test verifies that the CreateAsync will not accept null dependencies var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); var source = new ApiModels.SecondaryObject() { Description = "New Description", Name = "New Name", }; var destination = await secondaryObjectService.CreateAsync(Guid.Empty, source); }
public async Task SecondaryObjectService_CreateAsync_PrimaryObject_NotFound() { // This test verifies that CreateAsync throws an error when the primary object is not found var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); var source = new ApiModels.SecondaryObject() { Description = "New Description", Name = "New Name", }; _primaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(null as DomainModels.PrimaryObject); var destination = await secondaryObjectService.CreateAsync(Guid.NewGuid(), source); }
public async Task SecondaryObjectService_CreateAsync_InputModel_Name_WhiteSpace() { // This test verifies that the CreateAsync will not accept invalid inputs var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); var source = new ApiModels.SecondaryObject() { Description = "New Description", Name = " ", }; _primaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync((Guid id) => new DomainModels.PrimaryObject(id)); var destination = await secondaryObjectService.CreateAsync(Guid.NewGuid(), source); }
public async Task SecondaryObjectService_UpdateAsync_NotFound() { // This test verifies that UpdateAsync throws an error when the object is not found var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); var source = new ApiModels.SecondaryObject() { Description = "New Description", Name = "New Name", }; _secondaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(null as DomainModels.SecondaryObject); await secondaryObjectService.UpdateAsync(Guid.NewGuid(), source); Assert.Fail("Expected exception was not thrown"); }
public async Task SecondaryObjectsController_CreateAsync_Valid() { // This test verifies that the controller returns the correct result when ISecondaryObjectService.CreateAsync returns an object var secondaryObjectsController = new SecondaryObjectsController(_secondaryObjectService.Object); var sourceSecondaryObject = new ApiModels.SecondaryObject() { Description = "Description 1", Name = "Name 1", }; _secondaryObjectService.Setup(_ => _.CreateAsync(It.IsAny <Guid>(), It.IsAny <ApiModels.SecondaryObject>())).ReturnsAsync(new DomainModels.SecondaryObject(Guid.NewGuid())); var destinationSecondaryObject = await secondaryObjectsController.CreateAsync(Guid.NewGuid(), sourceSecondaryObject); Assert.IsNotNull(destinationSecondaryObject); }
public async Task <DomainModels.SecondaryObject> UpdateAsync(Guid id, ApiModels.SecondaryObject inputModel) { Ensure.That(id, nameof(id)).IsNotEmpty(); Ensure.That(inputModel, nameof(inputModel)).IsNotNull(); var domainSecondaryObject = await _secondaryObjectRepository.GetByIdAsync(id); Ensure.That(domainSecondaryObject, nameof(domainSecondaryObject)) .WithException(_ => GetDemoEntityNotFoundException(id)) .IsNotNull(); Map(inputModel, domainSecondaryObject); await _unitOfWork.SaveChangesAsync(); return(domainSecondaryObject); }
public async Task SecondaryObjectsController_CreateAsync_NotCreated() { // This test verifies that the controller throws the correct HttpResponseException when no object is returned from ISecondaryObjectService.CreateAsync var secondaryObjectsController = new SecondaryObjectsController(_secondaryObjectService.Object); ApiModels.SecondaryObject sourceSecondaryObject = null; _secondaryObjectService.Setup(_ => _.CreateAsync(It.IsAny <Guid>(), It.IsAny <ApiModels.SecondaryObject>())).ReturnsAsync(null as DomainModels.SecondaryObject); try { await secondaryObjectsController.CreateAsync(Guid.NewGuid(), sourceSecondaryObject); Assert.Fail(); } catch (HttpResponseException ex) { Assert.IsNotNull(ex.Response); Assert.AreEqual(HttpStatusCode.BadRequest, ex.Response.StatusCode); } }
public async Task SecondaryObjectService_UpdateAsync_InputModel_Description_Empty() { // This test verifies that the UpdateAsync will not accept invalid inputs var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); var source = new ApiModels.SecondaryObject() { Description = string.Empty, Name = "New Name", }; _secondaryObjectRepository.Setup(_ => _.GetByIdAsync(It.IsAny <Guid>())) .ReturnsAsync((Guid id) => new DomainModels.SecondaryObject(id) { Description = "Description 1", Name = "Name 1", PrimaryObject = new DomainModels.PrimaryObject(Guid.NewGuid()) { Description = "Description", Name = "Name", }, }); var destination = await secondaryObjectService.UpdateAsync(Guid.NewGuid(), source); }
public async Task <ApiModels.SecondaryObject> CreateAsync(Guid primaryObjectd, ApiModels.SecondaryObject inputModel) { var domainSecondaryObject = await _secondaryObjectService.CreateAsync(primaryObjectd, inputModel); Ensure.That(domainSecondaryObject, nameof(domainSecondaryObject)) .WithException(_ => new HttpResponseException(HttpStatusCode.BadRequest)) .IsNotNull(); return(this.Map(domainSecondaryObject)); }
public async Task <DomainModels.SecondaryObject> CreateAsync(Guid primaryObjectId, ApiModels.SecondaryObject inputModel) { Ensure.That(primaryObjectId, nameof(primaryObjectId)).IsNotEmpty(); Ensure.That(inputModel, nameof(inputModel)).IsNotNull(); var domainPrimaryObject = await _primaryObjectRepoistory.GetByIdAsync(primaryObjectId); Ensure.That(domainPrimaryObject, nameof(domainPrimaryObject)) .WithException(_ => { var ex = new DemoEntityNotFoundException(); ex.Data.Add($"{nameof(DomainModels.PrimaryObject)}.{nameof(DomainModels.PrimaryObject.Id)}", primaryObjectId.ToString()); return(ex); }) .IsNotNull(); var domainSecondaryObject = new DomainModels.SecondaryObject(Guid.NewGuid()); Map(inputModel, domainSecondaryObject); domainSecondaryObject.PrimaryObject = domainPrimaryObject; domainSecondaryObject.PrimaryObject_Id = domainPrimaryObject.Id; domainPrimaryObject.SecondaryObjects.Add(domainSecondaryObject); await _unitOfWork.SaveChangesAsync(); return(domainSecondaryObject); }