public void Post_ShouldStoreDataInRepo_WhenPostingValidLearningData() { // Arrange var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict); var expectedPrimaryKey = 5; var expectedResultObject = new LearningDataDto() { Name = NewLearningDataTemplate.Name, Description = NewLearningDataTemplate.Description, Id = expectedPrimaryKey, Number = NewLearningDataTemplate.Number, ImageData = NewLearningDataTemplate.ImageData, ImageTitle = NewLearningDataTemplate.ImageTitle }; dataRepoMock.Setup(repo => repo.Add(NewLearningDataTemplate)).Returns(expectedPrimaryKey); dataRepoMock.Setup(repo => repo.Retrieve(expectedPrimaryKey)).Returns(expectedResultObject); var sut = new Controllers.LearningDataController(dataRepoMock.Object); // Act var result = sut.Post(NewLearningDataTemplate); var specificResult = result as CreatedAtActionResult; // Assert Assert.NotNull(specificResult); Assert.Equal("Get", specificResult.ActionName); Assert.Equal(StatusCodes.Status201Created, specificResult.StatusCode); Assert.Equal(expectedResultObject, specificResult.Value); VerifyMock(dataRepoMock); }
public void Put_ShouldUpdateDataInRepo_WhenValidUpdateRequestIsSent() { // Arrange var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict); var learningData = new LearningDataDto() { Id = DefaultLearningData.First().Id, Name = "This is an updated name", Description = DefaultLearningData.First().Description, ImageData = DefaultLearningData.First().ImageData, Number = DefaultLearningData.First().Number }; // Expect the controller to test if the data set exists before trying to update it dataRepoMock.Setup(repo => repo.Retrieve(learningData.Id)).Returns(DefaultLearningData.First()); dataRepoMock.Setup(repo => repo.Update(learningData)); var sut = new Controllers.LearningDataController(dataRepoMock.Object); // Act var result = sut.Put(learningData.Id, learningData); var okResult = result as OkResult; // Assert Assert.NotNull(okResult); VerifyMock(dataRepoMock); }
public void Update(LearningDataDto learningData) { if (learningData == null || learningData.ImageData == null) { throw new ArgumentNullException("learningData"); } var imageDataForDb = new ImageData() { Title = learningData.ImageTitle, Data = Convert.FromBase64String(learningData.ImageData), LearningDataId = learningData.Id }; _dataAccessFactory.CreateImageDataAccess(_configuration).UpdateImageData(imageDataForDb); var learningDataForDb = new LearningData() { LearningDataId = learningData.Id, Name = learningData.Name, Description = learningData.Description, Number = learningData.Number, CategoryId = learningData.CategoryId }; _dataAccessFactory.CreateLearningDataAccess(_configuration).UpdateLearningData(learningDataForDb); }
private int AddLearningData(LearningDataDto learningData) { var learningDataForDb = new LearningData() { Name = learningData.Name, Description = learningData.Description, Number = learningData.Number, CategoryId = learningData.CategoryId }; return(_dataAccessFactory.CreateLearningDataAccess(_configuration).AddLearningData(learningDataForDb)); }
public IActionResult Post([FromBody] LearningDataDto value) { if (!ModelState.IsValid) { return(BadRequest()); } var learningDataId = _learningDataRepo.Add(value); var newObject = _learningDataRepo.Retrieve(learningDataId); return(CreatedAtAction(nameof(Get), new { id = newObject.Id }, newObject)); }
public int Add(LearningDataDto learningData) { if (learningData == null || learningData.ImageData == null) { throw new ArgumentNullException("learningData"); } var learningDataId = AddLearningData(learningData); AddImageData(learningData, learningDataId); return(learningDataId); }
public IActionResult Put(int id, [FromBody] LearningDataDto value) { if (!ModelState.IsValid) { return(BadRequest()); } if (_learningDataRepo.Retrieve(id) == null) { return(NotFound()); } value.Id = id; _learningDataRepo.Update(value); return(Ok()); }
public void Put_ShouldReturnBadRequest_WhenUpdateRequestIsSentForIncompleteData() { // Arrange var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict); var incompleteData = new LearningDataDto(); var sut = new Controllers.LearningDataController(dataRepoMock.Object); sut.ModelState.AddModelError("test", "test"); // Act var result = sut.Put(DefaultLearningData.First().Id, incompleteData); var badRequestResult = result as BadRequestResult; // Assert Assert.NotNull(badRequestResult); VerifyMock(dataRepoMock); }
public void Update_ShouldCallUpdateOnDataLayer_WhenProvidingValidUpdateRequest() { // Arrange var updatedDataDto = new LearningDataDto() { Id = 0, Name = "New Name", Description = "New description", Number = 911, ImageData = "QUJD", ImageTitle = "New Title", CategoryId = 42 }; var expectedLearningData = new LearningData() { LearningDataId = updatedDataDto.Id, Name = updatedDataDto.Name, Description = updatedDataDto.Description, Number = updatedDataDto.Number, CategoryId = updatedDataDto.CategoryId }; var expectedImageData = new ImageData() { LearningDataId = updatedDataDto.Id, Title = updatedDataDto.ImageTitle, Data = Convert.FromBase64String(updatedDataDto.ImageData) }; ImageDataAccessMock.Setup(x => x.UpdateImageData(It.IsAny <ImageData>())).Returns(true); LearningDataAccessMock.Setup(x => x.UpdateLearningData(It.IsAny <LearningData>())).Returns(true); // Act Sut.Update(updatedDataDto); // Assert // We need to verify mocks manually since object arguments are being provided LearningDataAccessMock.Verify(mock => mock.UpdateLearningData( It.Is <LearningData>(data => CompareLearningData(data, expectedLearningData)) )); LearningDataAccessMock.VerifyNoOtherCalls(); ImageDataAccessMock.Verify(mock => mock.UpdateImageData( It.Is <ImageData>(data => CompareImageData(data, expectedImageData)) )); ImageDataAccessMock.VerifyNoOtherCalls(); }
private void AddImageData(LearningDataDto learningData, int learningDataId) { var imageString = learningData.ImageData; if (imageString.Contains(',')) { imageString = imageString.Split(',').ElementAt(1); } var imageDataForDb = new ImageData() { Title = learningData.ImageTitle, Data = Convert.FromBase64String(imageString), LearningDataId = learningDataId }; _dataAccessFactory.CreateImageDataAccess(_configuration).AddImageData(imageDataForDb); }
public void Put_ShouldReturnNotFoundResult_WhenUpdateRequestIsSentForUnknownId() { // Arrange var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict); var learningData = new LearningDataDto() { Id = 5000000, Name = "Invalid", Description = "Invalid", Number = 0, ImageData = null }; dataRepoMock.Setup(repo => repo.Retrieve(learningData.Id)).Returns <LearningDataDto>(null); var sut = new Controllers.LearningDataController(dataRepoMock.Object); // Act var result = sut.Put(learningData.Id, learningData); var notFoundResult = result as NotFoundResult; // Assert Assert.NotNull(notFoundResult); VerifyMock(dataRepoMock); }