public void Modify_Existing_Bug_With_InCorrect_Data_Should_Return_400BadRequest_And_Do_Not_Modify_Bug()
        {
            // Arrange
            var fakeBugToModify = this.mock.BugRepositoryMock.Object.All().FirstOrDefault();
            var newBugData = new EditBugBindingModel { };

            var mockContext = new Mock<IBugTrackerData>();
            mockContext.Setup(c => c.Bugs)
                .Returns(this.mock.BugRepositoryMock.Object);

            var bugsController = new BugsController(mockContext.Object);
            this.SetupController(bugsController);
            bugsController.ModelState.AddModelError("No data", "Empty binding model.");

            // Act
            var response = bugsController.EditExistingBug(fakeBugToModify.Id, newBugData)
                .ExecuteAsync(CancellationToken.None).Result;

            // Assert
            var fakeBugAfterEditing = this.mock.BugRepositoryMock.Object.All().FirstOrDefault();
            Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
            Assert.AreEqual(fakeBugToModify.Title, fakeBugAfterEditing.Title);
            Assert.AreEqual(fakeBugToModify.Description, fakeBugAfterEditing.Description);
            Assert.AreEqual(fakeBugToModify.Status, fakeBugAfterEditing.Status);
        }
        public void Modify_Existing_Bug_With_Correct_Data_Should_Return_200OK_And_Modify_Bug_In_Repository()
        {
            // Arrange
            var fakeBugToModify = this.mock.BugRepositoryMock.Object.All().FirstOrDefault();
            var newBugData = new EditBugBindingModel
            {
                Title = "Modified title",
                Description = "Modified description",
                Status = "Closed"
            };

            var mockContext = new Mock<IBugTrackerData>();
            mockContext.Setup(c => c.Bugs)
                .Returns(this.mock.BugRepositoryMock.Object);

            var bugsController = new BugsController(mockContext.Object);
            this.SetupController(bugsController);

            // Act
            var response = bugsController.EditExistingBug(fakeBugToModify.Id, newBugData)
                .ExecuteAsync(CancellationToken.None).Result;

            // Assert
            var fakeBugAfterEditing = this.mock.BugRepositoryMock.Object.All().LastOrDefault();
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
            Assert.AreEqual(newBugData.Title, fakeBugAfterEditing.Title);
            Assert.AreEqual(newBugData.Description, fakeBugAfterEditing.Description);
            Assert.AreEqual(newBugData.Status, fakeBugAfterEditing.Status.ToString());
        }
        public IHttpActionResult EditExistingBug(int id, EditBugBindingModel model)
        {
            var bugInDb = this.Data.Bugs.All().FirstOrDefault(b => b.Id == id);
            if (bugInDb == null)
            {
                return this.NotFound();
            }

            if (model == null)
            {
                return this.BadRequest("Missing bug properties to change.");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest("Invalid bug properties.");
            }

            var modelStatus = (Status)Enum.Parse(typeof(Status), model.Status);

            bugInDb.Title = model.Title ?? bugInDb.Title;
            bugInDb.Description = model.Description ?? bugInDb.Description;
            bugInDb.Status = modelStatus != bugInDb.Status ? modelStatus : bugInDb.Status;

            this.Data.Bugs.Update(bugInDb);
            this.Data.SaveChanges();
            return this.Ok(new { Message = "Bug #" + id + " patched." });
        }
        public void Modify_NonExisting_Bug_With_Correct_Data_Should_Return_404NotFound()
        {
            // Arrange
            var newBugData = new EditBugBindingModel
            {
                Title = "Modified title",
                Description = "Modified description",
                Status = "Closed"
            };

            var mockContext = new Mock<IBugTrackerData>();
            mockContext.Setup(c => c.Bugs)
                .Returns(this.mock.BugRepositoryMock.Object);

            var bugsController = new BugsController(mockContext.Object);
            this.SetupController(bugsController);

            // Act
            var response = bugsController.EditExistingBug(int.MaxValue, newBugData)
                .ExecuteAsync(CancellationToken.None).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode);
        }