Exemplo n.º 1
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 EditBugInputModel()
            {
                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);
        }
Exemplo n.º 2
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");
            EditBugInputModel editBugData = null;
            var httpResponse = bugsController.EditBug(1234, editBugData)
                               .ExecuteAsync(new CancellationToken()).Result;

            // Assert -> HTTP status code 400 (BadRequest)
            Assert.AreEqual(HttpStatusCode.BadRequest, httpResponse.StatusCode);
        }
        public void EditBugStatusOnly()
        {
            var controller = new BugsController(dataMock.Object);

            SetupController(controller, "Bugs");

            var bugToEdit = new Bug
            {
                Id          = 22,
                Title       = "Bug4e Tuka",
                Description = "ne6to ne e nared",
                Status      = BugStatus.InProgress,
                DateCreated = DateTime.Now
            };

            dataMock.Setup(data => data.Bugs.Find(It.IsAny <object>())).Returns(bugToEdit);

            var updatedBug = new EditBugInputModel {
                Status = "Closed"
            };
            var result = controller.EditBug(2, updatedBug).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            var resultContent = result.Content.ReadAsStringAsync().Result;

            Assert.IsTrue(resultContent.Contains("Bug #" + bugToEdit.Id + " patched."));
        }
        public void EditUnexistingBug_ShouldReturnNotFound()
        {
            var controller = new BugsController(dataMock.Object);

            SetupController(controller, "Bugs");

            dataMock.Setup(data => data.Bugs.Find(It.IsAny <object>())).Returns(null as Bug);

            var updatedBug = new EditBugInputModel {
                Title = "New Title"
            };
            var result = controller.EditBug(2, updatedBug).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.NotFound, result.StatusCode);
        }
Exemplo n.º 5
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 EditBugInputModel()
            {
                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);
        }
Exemplo n.º 6
0
        public IHttpActionResult EditBug(int id, EditBugInputModel model)
        {
            var bug = this.Data.Bugs.Find(id);

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

            if (model == null)
            {
                return(this.BadRequest("You must provide new bug data in order to change a bug."));
            }

            if (model.Title != null)
            {
                if (model.Title == string.Empty)
                {
                    return(this.BadRequest("The bug title cannot be empty."));
                }

                bug.Title = model.Title;
            }

            if (model.Description != null)
            {
                bug.Description = model.Description;
            }

            if (model.Status != null)
            {
                BugStatus status;
                if (!Enum.TryParse(model.Status, true, out status))
                {
                    return(this.BadRequest("The provided status is invalid."));
                }

                bug.Status = status;
            }

            this.Data.Bugs.Update(bug);
            this.Data.SaveChanges();

            return(this.Ok(new { Message = "Bug #" + bug.Id + " patched." }));
        }
        public IHttpActionResult EditBug(int id, EditBugInputModel bugInput)
        {
            if (bugInput == null)
            {
                return this.BadRequest();
            }

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

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

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

            if (bugInput.Title != null)
            {
                bug.Title = bugInput.Title;
            }

            if (bugInput.Description != null)
            {
                bug.Description = bugInput.Description;
            }

            if (bugInput.Status != null)
            {
                bug.Status = bugInput.Status;
            }

            this.Data.Bugs.AddOrUpdate();
            this.Data.SaveChanges();

            return this.Ok(
                new
                {
                    Message = "Bug #" + id + " patched."
                }
            );
        }
        public IHttpActionResult EditBug(int id, EditBugInputModel bugData)
        {
            if (bugData == null)
            {
                return(BadRequest("Missing bug data to patch."));
            }

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

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

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

            if (bugData.Title != null)
            {
                bug.Title = bugData.Title;
            }
            if (bugData.Description != null)
            {
                bug.Description = bugData.Description;
            }
            if (bugData.Status != null)
            {
                bug.Status = bugData.Status.Value;
            }

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

            return(this.Ok(
                       new
            {
                Message = "Bug #" + id + " patched."
            }
                       ));
        }
        public void EditBugInvalidStatus_ShouldReturnBadRequest()
        {
            var controller = new BugsController(dataMock.Object);

            SetupController(controller, "Bugs");

            var bugToEdit = new Bug
            {
                Title       = "Bug4e Tuka",
                Description = "ne6to ne e nared",
                Status      = BugStatus.InProgress,
                DateCreated = DateTime.Now
            };

            dataMock.Setup(data => data.Bugs.Find(It.IsAny <object>())).Returns(bugToEdit);

            var updatedBug = new EditBugInputModel {
                Title = "Changed", Status = "Pesho"
            };
            var result = controller.EditBug(2, updatedBug).ExecuteAsync(new CancellationToken()).Result;

            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }