public void ShouldRemoveHighValueItem() { int validId = 1; HighValueItemDto removedHighValueItem = new HighValueItemDto() { HighValueItemId = 1, Name = "Smartphone", ItemCategoryId = 1, Value = 700, UserId = userId, }; _highValueItemServiceMock .Setup(x => x.HighValueItemExists(It.IsAny <int>())) .Returns <int>(x => true); _highValueItemServiceMock .Setup(x => x.DeleteHighValueItem(It.IsAny <int>())) .Returns <int>(x => removedHighValueItem); var actionResult = _controller.DeleteHighValueItem(validId); var getActionResult = _controller.GetHighValueItem(validId); var resultGet = getActionResult.Result as NotFoundResult; Assert.IsAssignableFrom <NotFoundResult>(resultGet); }
public void ShouldReturnBadRequestForInvalidModelStatePostRequests() { HighValueItemDto emptyHighValueItem = new HighValueItemDto(); var actionResult = _controller.PostHighValueItem(emptyHighValueItem); var result = actionResult.Result as BadRequestResult; Assert.IsAssignableFrom <BadRequestResult>(result); }
private HighValueItemDto GetHighValueItemDto(HighValueItem highValueItem) { try { HighValueItemDto highValueItemDto = _mapper.Map(highValueItem, new HighValueItemDto()); highValueItemDto.Category = GetItemCategoryDto(highValueItemDto.ItemCategoryId); return(highValueItemDto); } catch (Exception) { throw; } }
public HighValueItemDto GetHighValueItemDto(int id) { try { HighValueItem highValueItem = GetHighValueItem(id); HighValueItemDto highValueItemDto = _mapper.Map(highValueItem, new HighValueItemDto()); highValueItemDto.Category = GetItemCategoryDto(highValueItemDto.ItemCategoryId); return(highValueItemDto); } catch (Exception) { throw; } }
public HighValueItemDto AddHighValueItem(HighValueItemDto dto) { try { HighValueItem newHighValueItem = _mapper.Map <HighValueItemDto, HighValueItem>(dto); _context.HighValueItem.Add(newHighValueItem); _context.SaveChanges(); return(GetHighValueItemDto(newHighValueItem)); } catch (Exception ex) { throw; } }
public ActionResult <HighValueItemDto> PostHighValueItem([FromBody] HighValueItemDto highValueItemDto) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } HighValueItemDto createdHighValueItemDto = _highValueItemService.AddHighValueItem(highValueItemDto); return(CreatedAtAction(nameof(PostHighValueItem), new { id = createdHighValueItemDto.HighValueItemId }, createdHighValueItemDto)); } catch (Exception) { return(BadRequest()); } }
public ActionResult <HighValueItemDto> GetHighValueItem([FromRoute] int id) { try { HighValueItemDto highValueItemDto = _highValueItemService.GetHighValueItemDto(id); if (highValueItemDto == null) { return(NotFound()); } return(Ok(highValueItemDto)); } catch (Exception) { return(BadRequest()); } }
public void ShouldSaveHighValueItem() { HighValueItemDto savedHighValueItem = null; _highValueItemServiceMock .Setup(x => x.AddHighValueItem(It.IsAny <HighValueItemDto>())) .Callback <HighValueItemDto>(highValueItem => { savedHighValueItem = highValueItem; }); _controller.PostHighValueItem(_newHighValueItem); _highValueItemServiceMock .Verify(x => x.AddHighValueItem(It.IsAny <HighValueItemDto>()), Times.Once ); Assert.NotNull(savedHighValueItem); Assert.AreEqual(_newHighValueItem.Name, savedHighValueItem.Name); Assert.AreEqual(_newHighValueItem.Value, savedHighValueItem.Value); Assert.AreEqual(_newHighValueItem.ItemCategoryId, savedHighValueItem.ItemCategoryId); Assert.AreEqual(_newHighValueItem.UserId, savedHighValueItem.UserId); Assert.AreEqual(_categoriesWithItems.First().Category.ItemCategoryId, savedHighValueItem.ItemCategoryId); }
public void Setup() { _newHighValueItem = new HighValueItemDto { Name = "SmartTV", ItemCategoryId = 1, Value = 1200, UserId = userId, }; _categoriesWithItems = new List <CategoryWithItemsDto> { new CategoryWithItemsDto { Category = new ItemCategoryDto { ItemCategoryId = 1, Name = "Eletronics", }, Items = new List <HighValueItemDto> { new HighValueItemDto { HighValueItemId = 1, Name = "Smartphone", ItemCategoryId = 1, Value = 700, UserId = userId, }, new HighValueItemDto { HighValueItemId = 2, Name = "Laptop", ItemCategoryId = 1, Value = 1700, UserId = userId, }, } }, new CategoryWithItemsDto { Category = new ItemCategoryDto { ItemCategoryId = 1, Name = "Clothing", }, Items = new List <HighValueItemDto> { new HighValueItemDto { HighValueItemId = 3, Name = "Dress", ItemCategoryId = 2, Value = 1700, UserId = userId, } } }, }; _userItems = new List <HighValueItemDto>() { new HighValueItemDto { HighValueItemId = 1, Name = "Smartphone", ItemCategoryId = 1, Value = 700, UserId = userId, }, new HighValueItemDto { HighValueItemId = 2, Name = "Laptop", ItemCategoryId = 1, Value = 1700, UserId = userId, }, new HighValueItemDto { HighValueItemId = 3, Name = "Dress", ItemCategoryId = 2, Value = 1700, UserId = userId, } }; _highValueItemServiceMock = new Mock <IHighValueItemsService>(); _highValueItemServiceMock.Setup(x => x.GetHighValueItemsPerCategoriesByUser(userId)) .Returns(_categoriesWithItems); _highValueItemServiceMock.Setup(x => x.GetHighValueItemsByUser(userId)) .Returns(_userItems); _controller = new HighValueItemController( _highValueItemServiceMock.Object); }