public async Task GetCatsByOwnerGender() { GetPetsModel petDetails = null; // Arrange PetRepository repository = new PetRepository(); PetManager manager = new PetManager(repository); PetController controller = new PetController(manager); // controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); // Act var response = await controller.GetPetsByOwnerGender(PetType.Cat); if (response.IsSuccessStatusCode) { petDetails = await response.Content.ReadAsAsync <GetPetsModel>(); } // Assert Assert.IsNotNull(petDetails); // Assert.AreEqual(4, petDetails.MaleOwnedCats.Count()); Assert.AreEqual(3, petDetails.FemaleOwnedCats.Count()); // Assert.AreEqual("Garfield", petDetails.MaleOwnedCats.ElementAt(0).Name); Assert.AreEqual("Jim", petDetails.MaleOwnedCats.ElementAt(1).Name); Assert.AreEqual("Max", petDetails.MaleOwnedCats.ElementAt(2).Name); Assert.AreEqual("Tom", petDetails.MaleOwnedCats.ElementAt(3).Name); // Assert.AreEqual("Garfield", petDetails.FemaleOwnedCats.ElementAt(0).Name); Assert.AreEqual("Simba", petDetails.FemaleOwnedCats.ElementAt(1).Name); Assert.AreEqual("Tabby", petDetails.FemaleOwnedCats.ElementAt(2).Name); }
public async Task <ActionResult> Index() { GetPetsModel model = null; //Calling Web Api - Pet Service from MVC controller to display the output. //Could have directly called PetManager.cs from here but it requires to instantiate PetManager.cs and PetRepository.cs here which will make those classes tightly coupled to HomeController.cs //Could not implement Dependency Injection for this MVC controller since this is primarily an Web Api project and Web Api DI implementation is already in place. HttpResponseMessage response = await Utility.InitilaizeHttpClient().GetAsync(Constants.SERVICE_GetCatDetails); if (response.IsSuccessStatusCode) { model = await response.Content.ReadAsAsync <GetPetsModel>(); } return(View(model)); }
public async Task <GetPetsModel> GetPetDetails(PetType petType) { GetPetsModel response = new GetPetsModel(); var peopleInfo = await petRepository.GetPetDetails(); if (peopleInfo != null) { //Read all the pets based on owner's gender and based on the pet type in alphabetical order response.MaleOwnedCats = peopleInfo .Where(person => person.Gender == Gender.Male && person.Pets != null && person.Pets.Any(pet => pet.Type == petType)) .SelectMany(owner => owner.Pets.Where(pet => pet.Type == petType)) .OrderBy(pet => pet.Name); response.FemaleOwnedCats = peopleInfo .Where(person => person.Gender == Gender.Female && person.Pets != null && person.Pets.Any(pet => pet.Type == petType)) .SelectMany(owner => owner.Pets.Where(pet => pet.Type == petType)) .OrderBy(pet => pet.Name); } return(response); }