public void CommentOnMatchTest() { //Arrange. User commentarist = GetFakeUser(); CommentaryDto made = new CommentaryDto() { makerUsername = commentarist.UserName, text = "this is a comment" }; CommentModelIn input = new CommentModelIn() { Text = "this is a comment" }; matchService.Setup(ms => ms.CommentOnEncounter(3, "username", input.Text)).Returns(made); //Act. IActionResult result = controller.CommentOnMatch(3, input); CreatedAtRouteResult createdResult = result as CreatedAtRouteResult; CommentModelOut comment = createdResult.Value as CommentModelOut; //Assert. matchService.Verify(ms => ms.CommentOnEncounter(3, "username", input.Text), Times.Once); Assert.IsNotNull(result); Assert.IsNotNull(createdResult); Assert.AreEqual(201, createdResult.StatusCode); Assert.AreEqual("GetCommentById", createdResult.RouteName); Assert.IsNotNull(comment); Assert.AreEqual(comment.Text, input.Text); Assert.AreEqual("username", comment.MakerUsername); }
public void CreateCommentByNoExistingUser() { //Arrange. ControllerContext fakeContext = GetFakeControllerContext(); controller.ControllerContext = fakeContext; CommentModelIn input = new CommentModelIn() { Text = "this is a comment" }; Exception internalEx = new UserNotFoundException(); Exception toThrow = new ServiceException(internalEx.Message, ErrorType.ENTITY_NOT_FOUND); matchService.Setup(ms => ms.CommentOnEncounter(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <string>())).Throws(toThrow); //Act. IActionResult result = controller.CommentOnMatch(3, input); NotFoundObjectResult badRequest = result as NotFoundObjectResult; ErrorModelOut error = badRequest.Value as ErrorModelOut; //Assert. matchService.Verify(ms => ms.CommentOnEncounter(3, "username", input.Text), Times.Once); Assert.IsNotNull(result); Assert.IsNotNull(badRequest); Assert.AreEqual(404, badRequest.StatusCode); Assert.IsNotNull(error); Assert.AreEqual(error.ErrorMessage, toThrow.Message); }
private IActionResult TryAddComment(int matchId, CommentModelIn input) { string username = HttpContext.User.Claims.First(c => c.Type.Equals(AuthenticationConstants.USERNAME_CLAIM)).Value; CommentaryDto created = encounterService.CommentOnEncounter(matchId, username, input.Text); CommentModelOut output = new CommentModelOut { Id = created.commentId, MakerUsername = username, Text = input.Text }; return(CreatedAtRoute("GetCommentById", new { id = output.Id }, output)); }
public IActionResult AddEncounterComment(string encounterId, [FromBody] CommentModelIn menssage) { try { CreateSession(); encounterSimpleServices.AddComment(encounterId, menssage.Message); return(Ok()); } catch (ServicesException e) { return(BadRequest(e.Message)); } }
private IActionResult AddValidFormatComment(int matchId, CommentModelIn input) { IActionResult result; try { SetSession(); result = TryAddComment(matchId, input); } catch (ServiceException e) { result = errors.GenerateError(e); } return(result); }
public IActionResult CommentOnMatch(int matchId, CommentModelIn input) { IActionResult result; if (ModelState.IsValid) { result = AddValidFormatComment(matchId, input); } else { result = BadRequest(ModelState); } return(result); }
public void AddCommentToEncounterDoesNotExistsEncountersController() { var encounterServices = new Mock <IEncounterSimpleServices>(); var encounterQueryServices = new Mock <IEncounterQueryServices>(); var fixtureGeneratorServices = new Mock <IFixtureServices>(); ILoginServices loginServices = new LoginServicesMock(santiago); var httpContext = new DefaultHttpContext(); httpContext.Request.Headers["Authorization"] = ""; var controllerContext = new ControllerContext() { HttpContext = httpContext, }; EncounterDTO encounter = new EncounterDTO() { Id = IntToGuid(4), SportName = "Futbol", TeamIds = new List <string>() { "Peñarol", "Nacional" } }; IEnumerable <EncounterDTO> encounters = new List <EncounterDTO>() { encounter }; encounterServices.Setup(e => e.AddComment(4 + "", "This is a test comment in a mock!")).Throws(new ServicesException()); var controller = new EncountersController(loginServices, encounterServices.Object, new LoggerStub(), encounterQueryServices.Object, fixtureGeneratorServices.Object, teamServices.Object) { ControllerContext = controllerContext, }; var comment = new CommentModelIn() { Message = "This is a test comment in a mock!" }; var obtainedResult = controller.AddEncounterComment(4 + "", comment) as BadRequestObjectResult; encounterServices.Verify(m => m.AddComment(4 + "", "This is a test comment in a mock!"), Times.AtMostOnce()); Assert.IsNotNull(obtainedResult); Assert.AreEqual(400, obtainedResult.StatusCode); }
public void CommentWithBadFormatTest() { //Arrange. CommentModelIn input = new CommentModelIn() { Text = "this is a comment", }; controller.ModelState.AddModelError("", "Error"); //Act. IActionResult result = controller.CommentOnMatch(3, input); BadRequestObjectResult badRequest = result as BadRequestObjectResult; //Assert. matchService.Verify(ms => ms.CommentOnEncounter(It.IsAny <int>(), It.IsAny <string>(), input.Text), Times.Never); Assert.IsNotNull(result); Assert.IsNotNull(badRequest); Assert.AreEqual(400, badRequest.StatusCode); }
public IActionResult Post([FromBody] CommentModelIn commentModel) { try { if (ModelState.IsValid) { Comment comment = commentModel.TransformToEntity(); comment = commentService.CreateComment(comment); CommentModelOut modelOut = new CommentModelOut(comment); return(CreatedAtRoute("GetComment", new { id = comment.Id }, modelOut)); } else { return(BadRequest(ModelState)); } } catch (NotExistsException ex) { return(BadRequest(ex.Message)); } }