public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { //Parse query parameter string id = req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "id", true) == 0) .Value; Guid idGuid; if (!Guid.TryParse(id, out idGuid)) { return(req.CreateResponse(HttpStatusCode.BadRequest, "Id is not a valid")); } string resultJson = null; try { //TODO: Implement IOC var customerDataFactory = new ShelteredDepthsDataFactory(new ShelteredDepthsApi()); var customerService = new CustomerService(customerDataFactory); resultJson = customerService.GetCustomer(idGuid); } catch (Exception e) { log.Error("GetCustomer failed", e); return(req.CreateResponse(HttpStatusCode.BadRequest, $"An error has occured: {e}")); } var jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter(); return(resultJson == null ? req.CreateResponse(HttpStatusCode.BadRequest, "Empty Data") : req.CreateResponse(HttpStatusCode.OK, resultJson, jsonFormatter, new MediaTypeWithQualityHeaderValue("application/json"))); }
public void GetCustomerWithInvalidApiResultReturnsNullObject() { //Setup var id = new Guid(); var customersJson = string.Empty; var customerApi = new Mock <ICustomerServiceApi>(); customerApi.Setup(arg => arg.GetCustomerAsync(It.IsAny <Guid>())) .Returns(Task.FromResult(customersJson)); //Run var customerDataFactory = new ShelteredDepthsDataFactory(customerApi.Object); var customerService = new CustomerService(customerDataFactory); var resultJson = customerService.GetCustomer(id); //Has Completed Assert.IsNotNull(resultJson, "Json incorrectly returns null on Empty API response"); //Is an empty Customers object var nullCustomerString = JsonConvert.SerializeObject(new Customer() { id = id.ToString() }); Assert.IsTrue(resultJson == nullCustomerString, "Json is not an empty Customers response"); }
public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { string resultJson = null; try { //Parse page parameter and default to 0 string pageString = req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "page", true) == 0) .Value; int page; int.TryParse(pageString, out page); //TODO: Implement IOC var customerDataFactory = new ShelteredDepthsDataFactory(new ShelteredDepthsApi()); var customerService = new CustomerService(customerDataFactory); resultJson = customerService.GetCustomers(page); } catch (Exception e) { log.Error("GetAll failed", e); return(req.CreateResponse(HttpStatusCode.BadRequest, $"An error has occured {e.Message}")); } var jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter(); return(resultJson == null ? req.CreateResponse(HttpStatusCode.BadRequest, "No data found") : req.CreateResponse(HttpStatusCode.OK, resultJson, jsonFormatter, new MediaTypeWithQualityHeaderValue("application/json"))); }
//[TestMethod] public void LiveIntergrationTest_GetCustomersReturnsObject() { var customerDataFactory = new ShelteredDepthsDataFactory(new ShelteredDepthsApi()); var customerService = new CustomerService(customerDataFactory); var resultJson = customerService.GetCustomers(); //Has Completed Assert.IsNotNull(resultJson, "Json incorrectly returns null for valid API response"); //Has Content Assert.IsTrue(resultJson.Length > 0, "Json incorrectly returns empty for valid API response"); }
//[TestMethod] public void LiveIntergrationTest_GetCustomerAllReturnsObject() { //Run var id = new Guid("be1d35af-6020-4445-9451-f13facfcfc9a"); var customerDataFactory = new ShelteredDepthsDataFactory(new ShelteredDepthsApi()); var customerService = new CustomerService(customerDataFactory); var resultJson = customerService.GetCustomer(id); //Has Completed Assert.IsNotNull(resultJson, "Json incorrectly returns null for valid API response"); //Has Content Assert.IsTrue(resultJson.Length > 0, "Json incorrectly returns empty for valid API response"); }
public void GetCustomersReturnsObject() { //Setup var customersJson = File.ReadAllText("Json/customers.json"); var customerApi = new Mock <ICustomerServiceApi>(); customerApi.Setup(arg => arg.GetCustomersAsync()) .Returns(Task.FromResult(customersJson)); //Run var customerDataFactory = new ShelteredDepthsDataFactory(customerApi.Object); var customerService = new CustomerService(customerDataFactory); var resultJson = customerService.GetCustomers(); //Has Completed Assert.IsNotNull(resultJson, "Json incorrectly returns null for valid API response"); //Has Content Assert.IsTrue(resultJson.Length > 0, "Json incorrectly returns empty for valid API response"); }
public void GetCustomerAllReturnsObject() { //Setup var id = new Guid("be1d35af-6020-4445-9451-f13facfcfc9a"); var customerJson = File.ReadAllText("Json/customer.json"); var customerApi = new Mock <ICustomerServiceApi>(); customerApi.Setup(arg => arg.GetCustomerAsync(It.IsAny <Guid>())) .Returns(Task.FromResult(customerJson)); //Run var customerDataFactory = new ShelteredDepthsDataFactory(customerApi.Object); var customerService = new CustomerService(customerDataFactory); var resultJson = customerService.GetCustomer(id); //Has Completed Assert.IsNotNull(resultJson, "Json incorrectly returns null for valid API response"); //Has Content Assert.IsTrue(resultJson.Length > 0, "Json incorrectly returns empty for valid API response"); }
public void GetCustomersWithInvalidApiResultReturnsNullObject() { //Setup var customersJson = "<Invalid>Text</Invalid>"; var customerApi = new Mock <ICustomerServiceApi>(); customerApi.Setup(arg => arg.GetCustomersAsync()) .Returns(Task.FromResult(customersJson)); //Run var customerDataFactory = new ShelteredDepthsDataFactory(customerApi.Object); var customerService = new CustomerService(customerDataFactory); var resultJson = customerService.GetCustomers(); //Has Completed Assert.IsNotNull(resultJson, "Json incorrectly returns null on Empty API response"); //Is an empty Customers object var nullCustomersString = JsonConvert.SerializeObject(new Customers() { ListOfCustomers = new List <Customer>() }); Assert.IsTrue(resultJson == nullCustomersString, "Json is not an empty Customers response"); }