public BugModel(Bug bug) { this.Id = bug.Id; this.Text = bug.Text; this.Status = bug.Status.ToString(); this.LogDate = bug.LogDate; }
public HttpResponseMessage Create(CreateBugModel bugModel) { if (!this.ModelState.IsValid) { return this.Request.CreateResponse(HttpStatusCode.BadRequest); } if (string.IsNullOrEmpty(bugModel.Description)) { return this.Request.CreateResponse(HttpStatusCode.BadRequest, "Description cannot be null or empty"); } if (bugModel.LogDate == DateTime.MinValue || bugModel.LogDate == DateTime.MaxValue) { return this.Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid LogDate: " + bugModel.LogDate); } var bug = new Bug() { Description = bugModel.Description, LogDate = bugModel.LogDate, Status = BugStatus.Pending }; this.BugLoggerData.Bugs.Add(bug); this.BugLoggerData.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created, bug); return response; }
public IHttpActionResult Create(BugModel bug) { if (!this.ModelState.IsValid) { return BadRequest(this.ModelState); } if (string.IsNullOrEmpty(bug.Text)) { return BadRequest("Bug should have text"); } if (bug.LogDate == null) { bug.LogDate = DateTime.Now; } bug.Status = BugStatus.Pending; var newBug = new Bug() { Text = bug.Text, Status = bug.Status, LogDate = bug.LogDate }; this.bugsRepository.Add(newBug); this.bugsRepository.SaveChanges(); bug.BugId = newBug.BugId; return Ok(bug); }
public void PostNewBug_WhenTextEmpty_ShouldReturnBadRequestResult() { var bug = new Bug() { Text = string.Empty }; var response = server.CreatePostRequest("api/bugs/create", bug); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void UpdatingUnexistingBug_ShouldThrowArgumentException() { var bug = new Bug() { BugId = 11, Text = "updated" }; this.mockedRepo.BugsRepository.Update(bug); }
private void Update(Bug bug) { var bugToUpdate = this.FakeBugs.Where(b => b.BugId == bug.BugId).FirstOrDefault(); if (bugToUpdate == null) { throw new ArgumentException("Bug with this id does not exists."); } bugToUpdate.Text = bug.Text; }
private void DeleteBug(Bug bug) { var bugToDelete = this.FakeBugs.Where(b => b.BugId == bug.BugId).FirstOrDefault(); if (bugToDelete == null) { throw new ArgumentException("Bug with this id does not exists."); } this.FakeBugs.Remove(bugToDelete); }
public void PostNewBug_WhenTextIsNull_ShouldReturn400() { IBugLoggerData data = Mock.Create<IBugLoggerData>(); Bug bug = new Bug() { Text = null }; var server = new InMemoryHttpServer(this.inMemoryServerUrl, data); var response = server.Post("/api/bugs", bug); Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); }
public void UpdatingExistingBug_ShouldReturnBug() { var bug = new Bug() { BugId = 1, Text = "updated" }; this.mockedRepo.BugsRepository.Update(bug); Assert.AreEqual(bug.Text, this.mockedRepo.BugsRepository.All().First().Text); }
public void DeleteExistingBug_ShouldDecreaseBugsCount() { var bug = new Bug() { BugId = 1 }; var expectedCount = this.mockedRepo.BugsRepository.All().Count() - 1; this.mockedRepo.BugsRepository.Delete(bug); Assert.AreEqual(expectedCount, this.mockedRepo.BugsRepository.All().Count()); }
public void AddBugWithEmptyDescriptionShouldThrowsValidationException() { // Arrange -> Prepare the objects var bug = new Bug() { Description = string.Empty, LogDate = DateTime.Now }; // Act -> Test the objects var bugRepository = new BugRepository(new BugLoggerDbContext()); bugRepository.Add(bug); bugRepository.SaveChanges(); // Assert -> Validate the result var bugFromDb = bugRepository.Find(bug.BugId); Assert.IsNotNull(bugFromDb); Assert.AreEqual(bug.Description, bugFromDb.Description); }
public void AddBugShouldBeAddedToDatabaseAndShouldBeReturnedFromRepository() { // Arrange -> Prepare the objects var bug = new Bug() { Description = "bug-1", LogDate = DateTime.Now }; // Act -> Test the objects var bugRepository = new BugRepository(new BugLoggerDbContext()); bugRepository.Add(bug); bugRepository.SaveChanges(); // Assert -> Validate the result var bugFromDb = bugRepository.Find(bug.BugId); Assert.IsNotNull(bugFromDb); Assert.AreEqual(bug.Description, bugFromDb.Description); }
// POST api/bugs public IHttpActionResult Post(Bug bug) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (string.IsNullOrWhiteSpace(bug.Text)) { return this.BadRequest("Bug text cannot be null or empty!"); } var newBug = new Bug { LogDate = DateTime.Now, Status = BugStatus.Pending, Text = bug.Text }; this.data.Bugs.Add(newBug); this.data.SaveChanges(); return this.CreatedAtRoute("DefaultApi", new { id = newBug.Id }, new BugModel(newBug)); }
public void AddBug_WhenBugTextIsValid_ShouldBeAddedToRepoWithLogDateAndStatusPending() { var bugs = new List<Bug>(); var bug = new Bug() { Text = "NEW TEST BUG" }; var data = Mock.Create<IBugLoggerData>(); Mock.Arrange(() => data.Bugs.All()).Returns(() => bugs.AsQueryable()); Mock.Arrange(() => data.Bugs.Add(Arg.IsAny<Bug>())).DoInstead((Bug b) => bugs.Add(b)); bool saveChangesIsCalled = false; Mock.Arrange(() => data.SaveChanges()).DoInstead(() => { saveChangesIsCalled = true; }); var controller = new BugsController(data); this.ConfigureController(controller); //act controller.Post(bug); //assert Assert.AreEqual(data.Bugs.All().Count(), 1); var bugInDb = data.Bugs.All().FirstOrDefault(); Assert.AreEqual(bug.Text, bugInDb.Text); Assert.IsNotNull(bugInDb.LogDate); Assert.AreEqual(BugStatus.Pending, bugInDb.Status); Assert.IsTrue(saveChangesIsCalled); }
public IHttpActionResult PostBug(BugModel bug) { if (string.IsNullOrEmpty(bug.Text)) { var ex = new ArgumentException(); return this.BadRequest(ex.Message); } var newBug = new Bug { LogDate = DateTime.Now, Status = Status.Pending, Text = bug.Text }; this.data.Bugs.Add(newBug); this.data.SaveChanges(); var location = new Uri(this.Url.Link("DefaultApi", new { id = newBug.Id })); var response = this.Created(location, newBug); return response; }
private void AddToFakeBugs(Bug bug) { this.FakeBugs.Add(bug); }
private Bug GetValidTestBug() { var bug = new Bug() { Text = "Test New bug", LogDate = DateTime.Now, Status = BugStatus.Pending }; return bug; }