public void SmsController_ValidPostMessage_ShouldReturnCreated() { // Arrange var repository = new MockSmsRepository(); var controller = new SmsController(repository); var model = new SendSmsBindingModel { To = Guid.NewGuid().ToString(), Message = Guid.NewGuid().ToString() }; // Act var actionResult = controller.Post(model); // Assert Assert.IsTrue(actionResult is CreatedNegotiatedContentResult <SmsMessage>); }
public void SendSmsBindingModel_WhenValid_ShouldBeValid() { // Arrange var model = new SendSmsBindingModel { To = Guid.NewGuid().ToString(), Message = Guid.NewGuid().ToString() }; var context = new ValidationContext(model, serviceProvider: null, items: null); var results = new List <ValidationResult>(); // Act var isValid = Validator.TryValidateObject(model, context, results, true); // Assert Assert.IsTrue(isValid); }
public void SmsController_ValidPostMessage_ShouldSaveToTableAndQueue() { // Arrange var repository = new MockSmsRepository(); var controller = new SmsController(repository); var model = new SendSmsBindingModel { To = Guid.NewGuid().ToString(), Message = Guid.NewGuid().ToString() }; // Act var actionResult = controller.Post(model); // Assert Assert.AreEqual(1, repository.QueueLength, "failed to save to queue"); Assert.AreEqual(1, repository.TableSize, "failed to save to table"); }
public void SmsController_ValidPostMessage_ShouldReturnCompleteResponse() { // Arrange var repository = new MockSmsRepository(); var controller = new SmsController(repository); var model = new SendSmsBindingModel { To = Guid.NewGuid().ToString(), Message = Guid.NewGuid().ToString() }; // Act var actionResult = controller.Post(model); var result = (actionResult as CreatedNegotiatedContentResult <SmsMessage>).Content; // Assert Assert.IsTrue(result.Created > DateTime.MinValue, "created too late"); Assert.IsTrue(Guid.Empty != result.Guid, "no guid generated"); }
// POST api/Sms public IHttpActionResult Post(SendSmsBindingModel model) { if (model == null) { return(BadRequest("no parameters")); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var created = DateTime.UtcNow; var result = new SmsMessage { Created = created, From = model.From, Message = model.Message, To = model.To }; _repository.InsertThenQueue(result); return(Created(result.Guid.ToString(), result)); }
public void SendSmsBindingModel_WhenMissingOrEmptyMessage_ShouldBeInvalid() { // Arrange var modelNull = new SendSmsBindingModel { To = Guid.NewGuid().ToString() }; var contextNull = new ValidationContext(modelNull, serviceProvider: null, items: null); var resultsNull = new List <ValidationResult>(); var modelEmpty = new SendSmsBindingModel { To = Guid.NewGuid().ToString(), Message = "" }; var contextEmpty = new ValidationContext(modelEmpty, serviceProvider: null, items: null); var resultsEmpty = new List <ValidationResult>(); // Act var isValidNull = Validator.TryValidateObject(modelNull, contextNull, resultsNull, true); var isValidEmpty = Validator.TryValidateObject(modelEmpty, contextEmpty, resultsEmpty, true); // Assert Assert.IsFalse(isValidNull); Assert.IsFalse(isValidEmpty); }