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_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);
        }
Пример #3
0
        public IHttpActionResult PatchExistingBug(int id, EditBugBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest());
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var bug = this.Data.Bugs.GetById(id);

            if (bug == null)
            {
                return(this.NotFound());
            }

            var hasChanged = false;

            if (!string.IsNullOrEmpty(model.Title))
            {
                bug.Title  = model.Title;
                hasChanged = true;
            }

            if (!string.IsNullOrEmpty(model.Description))
            {
                bug.Description = model.Description;
                hasChanged      = true;
            }

            if (!string.IsNullOrEmpty(model.Status))
            {
                try
                {
                    bug.Status = (Status)Enum.Parse(typeof(Status), model.Status, true);
                }
                catch (ArgumentException ex)
                {
                    return(this.BadRequest("Invalid status"));
                }

                hasChanged = true;
            }

            if (!hasChanged)
            {
                return(this.BadRequest());
            }

            this.Data.SaveChanges();
            var viewModel = new MessageViewModel()
            {
                Message = "Bug #" + bug.Id + " patched."
            };

            return(this.Ok(viewModel));
        }
        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." }));
        }
Пример #6
0
        public void EditBug_NonExistingBug_ShouldReturn404NotFound()
        {
            // Arrange -> create a few bugs
            var dataLayerMock = new BugTrackerDataMock();
            var bugsMock      = dataLayerMock.Bugs;

            bugsMock.Add(new Bug()
            {
                Id = 1, Title = "Bug #1"
            });
            bugsMock.Add(new Bug()
            {
                Id = 2, Title = "Bug #2"
            });

            // Act -> edit bug data
            var bugsController = new BugsController(dataLayerMock);

            this.SetupControllerForTesting(bugsController, "bugs");
            var editBugData = new EditBugBindingModel()
            {
                Title = "new title"
            };
            var httpResponse = bugsController.EditBug(1234, editBugData)
                               .ExecuteAsync(new CancellationToken()).Result;

            // Assert -> HTTP status code 404 (Not Found)
            Assert.AreEqual(HttpStatusCode.NotFound, httpResponse.StatusCode);
        }
Пример #7
0
        public void EditBug_InvalidBugData_ShouldReturn400BadRequest()
        {
            // Arrange -> create a few bugs
            var dataLayerMock = new BugTrackerDataMock();
            var bugsMock      = dataLayerMock.Bugs;

            bugsMock.Add(new Bug()
            {
                Id = 1, Title = "Bug #1"
            });
            bugsMock.Add(new Bug()
            {
                Id = 2, Title = "Bug #2"
            });

            // Act -> edit bug data
            var bugsController = new BugsController(dataLayerMock);

            this.SetupControllerForTesting(bugsController, "bugs");
            EditBugBindingModel editBugData = null;
            var httpResponse = bugsController.EditBug(1, editBugData)
                               .ExecuteAsync(new CancellationToken()).Result;

            // Assert -> HTTP status code 400 (BadRequest)
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);
        }
Пример #8
0
        public void EditBug_ExistingBug_ShouldReturn200OK_CorrectBugData()
        {
            // Arrange -> create a few bugs
            var dataLayerMock = new BugTrackerDataMock();
            var bugsMock      = dataLayerMock.Bugs;

            bugsMock.Add(new Bug()
            {
                Id = 1, Title = "Bug #1"
            });
            bugsMock.Add(new Bug()
            {
                Id          = 2,
                Title       = "Bug #2 for edit ...",
                Description = "Description for edit ...",
                Status      = BugStatus.Fixed
            });
            bugsMock.Add(new Bug()
            {
                Id = 3, Title = "Bug #3"
            });
            var newTitle       = "new title";
            var newDescription = "new description";
            var newStatus      = BugStatus.Closed;

            // Act -> edit bug data
            var bugsController = new BugsController(dataLayerMock);

            this.SetupControllerForTesting(bugsController, "bugs");
            var editBugData = new EditBugBindingModel()
            {
                Title       = newTitle,
                Description = newDescription,
                Status      = newStatus
            };
            var httpResponse = bugsController.EditBug(2, editBugData)
                               .ExecuteAsync(new CancellationToken()).Result;

            // Assert -> HTTP status code 200 (OK) + bug data changed
            Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);

            Assert.AreEqual(2, bugsMock.Find(2).Id);
            Assert.AreEqual(newTitle, bugsMock.Find(2).Title);
            Assert.AreEqual(newDescription, bugsMock.Find(2).Description);
            Assert.AreEqual(newStatus, bugsMock.Find(2).Status);
        }
Пример #9
0
        public IHttpActionResult EditBug(int id, [FromBody] EditBugBindingModel bugsData)
        {
            if (bugsData == null)
            {
                return(BadRequest());
            }

            var existingBug = db.Bugs.Find(id);

            if (existingBug == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (bugsData.Title != null)
            {
                existingBug.Title = bugsData.Title;
            }

            if (bugsData.Description != null)
            {
                existingBug.Description = bugsData.Description;
            }
            if (bugsData.Status != null)
            {
                existingBug.Status = (BugStatus)bugsData.Status;
            }

            db.Bugs.Update(existingBug);
            db.SaveChanges();

            return(this.Ok(
                       new
            {
                Message = "Bug #" + id + " patched."
            }
                       ));
        }