public async Task UpdateCollectionCollectable_UpdatesExistingCollectionCollectable_GivenValidCollectionCollectable() { //Arrange Guid id = new Guid("355e785b-dd47-4fb7-b112-1fb34d189569"); Guid collectionId = new Guid("46df9402-62e1-4ff6-9cb0-0955957ec789"); CollectionCollectableUpdateDto collectable = new CollectionCollectableUpdateDto { CollectableId = new Guid("3a7fd6a5-d654-4647-8374-eba27001b0d3") }; _mockCollectionService .Setup(c => c.FindCollectionById(collectionId)) .Returns(Task.FromResult(new Collection { Type = string.Empty })); _mockCollectableService .Setup(c => c.FindCollectableById(collectable.CollectableId)) .Returns(Task.FromResult(new Collectable())); var retrievedCollectionCollectable = _builder.Build(); _mockCollectableService .Setup(c => c.FindCollectionCollectableById(collectionId, id)) .Returns(Task.FromResult(retrievedCollectionCollectable)); _mockCollectableService.Setup(c => c.UpdateCollectionCollectable(It.IsAny <CollectionCollectable>())); //Act var response = await _controller.UpdateCollectionCollectable(collectionId, id, collectable); //Assert _mockCollectableService.Verify(c => c.UpdateCollectionCollectable(retrievedCollectionCollectable)); }
public async Task <IActionResult> UpdateCollectionCollectable(Guid collectionId, Guid id, [FromBody] CollectionCollectableUpdateDto collectable) { if (collectable == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } if (collectable.CollectionId == Guid.Empty) { collectable.CollectionId = collectionId; } var collection = await _collectionService.FindCollectionById(collectable.CollectionId); if (collection == null) { return(NotFound()); } var collectableItem = await _collectableService .FindCollectableById(collectable.CollectableId); if (collectableItem == null || !collectableItem.GetType().ToString() .ToLowerInvariant().Contains(collection.Type.ToLowerInvariant())) { return(BadRequest()); } var retrievedCollectable = await _collectableService.FindCollectionCollectableById(collectionId, id); if (retrievedCollectable == null) { return(NotFound()); } retrievedCollectable.CollectionId = collectable.CollectionId; retrievedCollectable.CollectableId = collectable.CollectableId; _mapper.Map(collectable, retrievedCollectable); _collectableService.UpdateCollectionCollectable(retrievedCollectable); if (!await _collectableService.Save()) { throw new Exception($"Updating collectable {id} failed on save."); } return(NoContent()); }
public async Task UpdateCollectionCollectable_ReturnsUnprocessableEntityObjectResponse_GivenInvalidCollectionCollectable() { //Arrange CollectionCollectableUpdateDto collectable = new CollectionCollectableUpdateDto(); _controller.ModelState.AddModelError("Condition", "Required"); //Act var response = await _controller.UpdateCollectionCollectable(Guid.Empty, Guid.Empty, collectable); //Assert Assert.IsType <UnprocessableEntityObjectResult>(response); }