public IHttpActionResult GetCats() { var json_data = string.Empty; //fetch data using (var webClient = new WebClient()) { try { json_data = webClient.DownloadString(System.Configuration.ConfigurationManager.AppSettings["JsonSourceAddress"]); } catch { return(BadRequest("Error retrieving Json feed")); } } GenderViewModel genderModel; try { genderModel = CatsFetcher.FetchCats(json_data); } catch (Exception e) { return(BadRequest(e.Message)); } return(Ok(genderModel)); }
public void FemaleCats() { var json = "[{\"name\":\"maleA\",\"gender\":\"Male\",\"age\":20,\"pets\":[{\"name\":\"catA\",\"type\":\"Cat\"}]},{\"name\":\"femaleA\",\"gender\":\"Female\",\"age\":20,\"pets\":[{\"name\":\"catB\",\"type\":\"Cat\"}]}, {\"name\":\"femaleB\",\"gender\":\"Female\",\"age\":20,\"pets\":[{\"name\":\"catC\",\"type\":\"Cat\"}]}, {\"name\":\"femaleC\",\"gender\":\"Female\",\"age\":20,\"pets\":[{\"name\":\"dog1\",\"type\":\"Dog\"}]}]"; var genders = CatsFetcher.FetchCats(json); var expectedFemaleCats = 2; var actualFemaleCats = genders.Females.Count; Assert.AreEqual(expectedFemaleCats, actualFemaleCats, 0, "Wrong number of male cats"); }
public void CatSorting() { var json = "[{\"name\":\"maleA\",\"gender\":\"Male\",\"age\":20,\"pets\":[{\"name\":\"A\",\"type\":\"Cat\"}]},{\"name\":\"maleB\",\"gender\":\"Male\",\"age\":20,\"pets\":[{\"name\":\"C\",\"type\":\"Cat\"}]}, {\"name\":\"maleC\",\"gender\":\"Male\",\"age\":20,\"pets\":[{\"name\":\"D\",\"type\":\"Cat\"}]}, {\"name\":\"maleD\",\"gender\":\"Male\",\"age\":20,\"pets\":[{\"name\":\"B\",\"type\":\"Cat\"}]}]"; var genders = CatsFetcher.FetchCats(json); var expectedOrder = new List <string>(); expectedOrder.Add("A"); expectedOrder.Add("B"); expectedOrder.Add("C"); expectedOrder.Add("D"); var actualOrder = genders.Males; CollectionAssert.AreEqual(expectedOrder, actualOrder, "Wrong ordering of cats"); }