public void Inquiry_WhenCalled__WithValidIdAndEmail_MustReturnsCustomer() { //Arrange var mockCustomerService = new Mock <ICustomerService>(); var validId = 1; var validEmail = "*****@*****.**"; var customer = new Customer { CustomerId = validId, Name = "Customer 1", Email = validEmail, MobileNo = "0891234567" }; mockCustomerService.Setup(s => s.GetCustomerByIdAndEmail(validId, validEmail)).Returns(customer); CustomersController controller = new CustomersController(mockCustomerService.Object); var criteria = new CustomerInquiryRequestMessage { CustomerID = validId.ToString(), Email = validEmail }; //Act var action = controller.Inquiry(criteria); var result = action.Result as OkObjectResult; var actual = result?.Value as Customer; var expected = customer; //Assert Assert.Equal(expected, actual); }
public void Inquiry_WhenCalled__WithInvalidEmail_MustReturnsErrorResponseMessage_InvalidEmail() { //Arrange var mockCustomerService = new Mock <ICustomerService>(); var validId = 1; var validEmail = "*****@*****.**"; var customer = new Customer { CustomerId = validId, Name = "Customer 1", Email = validEmail, MobileNo = "0891234567" }; mockCustomerService.Setup(s => s.GetCustomerByIdAndEmail(validId, validEmail)).Returns(customer); CustomersController controller = new CustomersController(mockCustomerService.Object); var invalidEmail = "invalid@email"; var criteria = new CustomerInquiryRequestMessage { Email = invalidEmail }; //Act var action = controller.Inquiry(criteria); var badRequestResult = action.Result as BadRequestObjectResult; var actual = badRequestResult?.Value as ErrorResponseMessage; var expected = new ErrorResponseMessage { Message = "Invalid Email" }; //Assert Assert.Equal(expected.Message, actual?.Message); }
public void Inquiry_WhenCalled__WithInvalidEmail_MustReturnsBadRequestObjectResult() { //Arrange var mockCustomerService = new Mock <ICustomerService>(); var validId = 1; var validEmail = "*****@*****.**"; var customer = new Customer { CustomerId = validId, Name = "Customer 1", Email = validEmail, MobileNo = "0891234567" }; mockCustomerService.Setup(s => s.GetCustomerByIdAndEmail(validId, validEmail)).Returns(customer); CustomersController controller = new CustomersController(mockCustomerService.Object); var invalidEmail = "invalid@email"; var criteria = new CustomerInquiryRequestMessage { Email = invalidEmail }; //Act var actual = controller.Inquiry(criteria); //Assert Assert.IsType <BadRequestObjectResult>(actual.Result); }
public void Inquiry_WhenCalled__WithEmptyRequestParameter_MustReturnsErrorResponseMessage_NoInquiryCriteria() { //Arrange var mockCustomerService = new Mock <ICustomerService>(); CustomersController controller = new CustomersController(mockCustomerService.Object); var criteria = new CustomerInquiryRequestMessage(); //Act var action = controller.Inquiry(criteria); var badRequestResult = action.Result as BadRequestObjectResult; var actual = badRequestResult?.Value as ErrorResponseMessage; var expected = new ErrorResponseMessage { Message = "No inquiry criteria" }; //Assert Assert.Equal(expected.Message, actual?.Message); }
public void Inquiry_WhenCalled__WithNotFoundEmailOnly_MustReturnsNotFound() { //Arrange var mockCustomerService = new Mock <ICustomerService>(); var notFoundEmail = "*****@*****.**"; mockCustomerService.Setup(s => s.GetCustomerByEmail(notFoundEmail)).Returns(() => null); CustomersController controller = new CustomersController(mockCustomerService.Object); var criteria = new CustomerInquiryRequestMessage { Email = notFoundEmail }; //Act var actual = controller.Inquiry(criteria); //Assert Assert.IsType <NotFoundObjectResult>(actual.Result); }
public void Inquiry_WhenCalled__WithNotFoundIdOnly_MustReturnsNotFound() { //Arrange var mockCustomerService = new Mock <ICustomerService>(); var notFoundId = 10; mockCustomerService.Setup(s => s.GetCustomerById(notFoundId)).Returns(() => null); CustomersController controller = new CustomersController(mockCustomerService.Object); var criteria = new CustomerInquiryRequestMessage { CustomerID = notFoundId.ToString() }; //Act var actual = controller.Inquiry(criteria); //Assert Assert.IsType <NotFoundObjectResult>(actual.Result); }
public void Inquiry_WhenCalled_AndErrorOccurred_MustReturnsStatusCode500() { //Arrange var mockCustomerService = new Mock <ICustomerService>(); string email = "*****@*****.**"; mockCustomerService.Setup(s => s.GetCustomerByEmail(email)).Throws(new Exception("Any exception")); CustomersController controller = new CustomersController(mockCustomerService.Object); var criteria = new CustomerInquiryRequestMessage { Email = email }; //Act var result = controller.Inquiry(criteria); var actual = result.Result as ObjectResult; var expected = 500; //Assert Assert.Equal(expected, actual?.StatusCode); }
public ActionResult <Customer> Inquiry(CustomerInquiryRequestMessage criteria) { try { if (criteria == null || string.IsNullOrEmpty(criteria.CustomerID) && string.IsNullOrEmpty(criteria.Email)) { return(BadRequest(new ErrorResponseMessage { Message = "No inquiry criteria" })); } if (!string.IsNullOrEmpty(criteria.CustomerID) && !string.IsNullOrEmpty(criteria.Email)) { if (!InquiryCriteriaValidation.ValidateCustomerID(criteria.CustomerID)) { return(BadRequest(new ErrorResponseMessage { Message = "Invalid Customer ID" })); } if (!InquiryCriteriaValidation.ValidateEmail(criteria.Email)) { return(BadRequest(new ErrorResponseMessage { Message = "Invalid Email" })); } var customer = _customerService.GetCustomerByIdAndEmail(Convert.ToInt32(criteria.CustomerID), criteria.Email); if (customer == null) { return(NotFound(null)); } return(Ok(customer)); } if (!string.IsNullOrEmpty(criteria.CustomerID)) { if (!InquiryCriteriaValidation.ValidateCustomerID(criteria.CustomerID)) { return(BadRequest(new ErrorResponseMessage { Message = "Invalid Customer ID" })); } var customer = _customerService.GetCustomerById(Convert.ToInt32(criteria.CustomerID)); if (customer == null) { return(NotFound(null)); } return(Ok(customer)); } if (!string.IsNullOrEmpty(criteria.Email)) { if (!InquiryCriteriaValidation.ValidateEmail(criteria.Email)) { return(BadRequest(new ErrorResponseMessage { Message = "Invalid Email" })); } var customer = _customerService.GetCustomerByEmail(criteria.Email); if (customer == null) { return(NotFound(null)); } return(Ok(customer)); } return(BadRequest(null)); } catch (Exception ex) { return(StatusCode(500, new ErrorResponseMessage { Message = "An error occured" })); } }