public async Task SecondaryObjectsController_UpdateAsync_Found() { // This test verifies that the controller does not throw an error when updating a valid object var secondaryObjectsController = new SecondaryObjectsController(_secondaryObjectService.Object); var sourceSecondaryObject = new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 1.1", Name = "Name 1.1", PrimaryObject = new DomainModels.PrimaryObject(Guid.NewGuid()) { Description = "Description 1", Name = "Name 1", }, }; sourceSecondaryObject.PrimaryObject_Id = sourceSecondaryObject.PrimaryObject.Id; sourceSecondaryObject.PrimaryObject.SecondaryObjects = new List <DomainModels.SecondaryObject>() { sourceSecondaryObject }; _secondaryObjectService.Setup(_ => _.UpdateAsync(sourceSecondaryObject.Id, It.IsAny <ApiModels.SecondaryObject>())).ReturnsAsync(sourceSecondaryObject); var destinationSecondaryObject = await secondaryObjectsController.UpdateAsync(sourceSecondaryObject.Id, new ApiModels.SecondaryObject()); Assert.IsNotNull(destinationSecondaryObject); Assert.AreEqual(sourceSecondaryObject.Description, destinationSecondaryObject.Description); Assert.AreEqual(sourceSecondaryObject.Id, destinationSecondaryObject.Id); Assert.AreEqual(sourceSecondaryObject.Name, destinationSecondaryObject.Name); Assert.IsNotNull(destinationSecondaryObject.PrimaryObject); Assert.IsNull(destinationSecondaryObject.PrimaryObject.Description); Assert.AreEqual(sourceSecondaryObject.PrimaryObject.Id, destinationSecondaryObject.PrimaryObject.Id); Assert.IsNull(destinationSecondaryObject.PrimaryObject.Name); Assert.IsNull(destinationSecondaryObject.PrimaryObject.SecondaryObjects); }
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); }
public async Task SecondaryObjectService_GetAsync_Valid() { // This test verifies that GetAsync works with valid inputs var secondaryObjectService = new SecondaryObjectService(_primaryObjectRepository.Object, _secondaryObjectRepository.Object, _unitOfWork.Object); var source = new DomainModels.SecondaryObject(Guid.NewGuid()) { Description = "Description 1", Name = "Name 1", PrimaryObject = new DomainModels.PrimaryObject(Guid.NewGuid()) { Description = "Description", Name = "Name", }, }; source.PrimaryObject_Id = source.PrimaryObject.Id; source.PrimaryObject.SecondaryObjects = new List <DomainModels.SecondaryObject>() { source }; _secondaryObjectRepository.Setup(_ => _.GetByIdAsync(source.Id)).ReturnsAsync(source); var destination = await secondaryObjectService.GetAsync(source.Id); Assert.AreEqual(source, destination); _unitOfWork.Verify(_ => _.SaveChangesAsync(), Times.Never); }
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; }
private ApiModels.SecondaryObject Map(DomainModels.SecondaryObject domainSecondaryObject) { return(new ApiModels.SecondaryObject() { Description = domainSecondaryObject.Description, Id = domainSecondaryObject.Id, Name = domainSecondaryObject.Name, PrimaryObject = new ApiModels.PrimaryObject() { Id = domainSecondaryObject.PrimaryObject_Id, }, }); }
public async Task SecondaryObjectsController_UpdateAsync_NotFound() { // This test verifies that the controller throws the correct HttpResponseException when a non-existant object is updated var secondaryObjectsController = new SecondaryObjectsController(_secondaryObjectService.Object); DomainModels.SecondaryObject sourceSecondaryObject = null; _secondaryObjectService.Setup(_ => _.UpdateAsync(It.IsAny <Guid>(), It.IsAny <ApiModels.SecondaryObject>())).ReturnsAsync(sourceSecondaryObject); try { await secondaryObjectsController.UpdateAsync(Guid.NewGuid(), new ApiModels.SecondaryObject()); Assert.Fail(); } catch (HttpResponseException ex) { Assert.IsNotNull(ex.Response); Assert.AreEqual(HttpStatusCode.NotFound, ex.Response.StatusCode); } }