public HttpResponseMessage Post(CustomerInquiryRequest request) { HttpResponseMessage result; try { var errorMessage = default(string); if (IsValidGetRequest(request, out errorMessage)) { var customerTrans = new CustomerInquiryResponse(); var customerID = string.IsNullOrWhiteSpace(request.customerID) ? 0 : int.Parse(request.customerID); customerTrans.customer = this._customerInquiryService.GetCustomerTransaction(customerID, request.email); if (customerTrans.customer != null) { result = Request.CreateResponse(HttpStatusCode.OK, customerTrans.customer); } else { result = Request.CreateResponse(HttpStatusCode.NotFound, "Not found"); } } else { switch (errorMessage) { case "No inquiry criteria": result = result = Request.CreateResponse(HttpStatusCode.BadRequest, ""); break; case "Invalid Customer ID": result = result = Request.CreateResponse(HttpStatusCode.BadRequest, ""); break; default: result = result = Request.CreateResponse(HttpStatusCode.BadRequest, ""); break; } } } catch (ArgumentException arg) { result = Request.CreateResponse(HttpStatusCode.OK, arg.Message); } catch (Exception) { result = Request.CreateResponse(HttpStatusCode.BadRequest, ""); } return(result); }
public void Post_DataNotMatch_ResponseNotFound(string customerID, string email) { CustomerInquiryController controller = new CustomerInquiryController(FakeCustomerInquityServiceWithoutDataForPost()); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); var request = new CustomerInquiryRequest() { customerID = customerID, email = email }; var result = controller.Post(request); Assert.That(result.StatusCode == HttpStatusCode.NotFound); }
public void Post_RequestIsValid_ResponseOK(string customerID, string email) { CustomerInquiryController controller = new CustomerInquiryController(FakeCustomerInquityServiceWithDataForPost(5)); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); var request = new CustomerInquiryRequest() { customerID = customerID, email = email }; var result = controller.Post(request); Assert.That(result.StatusCode == HttpStatusCode.OK); }
private bool IsValidGetRequest(CustomerInquiryRequest request, out string errorMessage) { errorMessage = default(string); if (string.IsNullOrWhiteSpace(request.customerID) && string.IsNullOrWhiteSpace(request.email)) { errorMessage = "No inquiry criteria"; } if (!string.IsNullOrWhiteSpace(request.customerID)) { if (!IsNumeric(request.customerID)) { errorMessage = "Invalid Customer ID"; } } return(string.IsNullOrWhiteSpace(errorMessage)); }