public async Task <CollectionResult <PersonViewModel> > GetAll(params string[] sortBy) { var result = new CollectionResult <PersonViewModel>(); using (var client = new HttpClient()) { try { client.BaseAddress = new Uri("http://demo6957850.mockable.io/"); string sorting = sortBy != null ? $"?sort={sortBy[0]}" : string.Empty; var response = await client.GetAsync("people" + sorting); response.EnsureSuccessStatusCode(); var stringResult = await response.Content.ReadAsStringAsync(); IEnumerable <PersonViewModel> results = JsonConvert.DeserializeObject <IEnumerable <PersonViewModel> >(stringResult); result.SetResults(results); result.IsSuccessful = true; } catch (HttpRequestException httpRequestException) { result.IsSuccessful = false; result.Message = httpRequestException.Message; } } return(result); }
public void Handle_NoSorting_SingleEntiyResult() { // Arrange PeopleQuery request = new PeopleQuery(); PersonViewModel person = new PersonViewModel { FirstName = "FName", Surname = "SName", Score = 0 }; var result = new CollectionResult <PersonViewModel>(); result.IsSuccessful = true; result.SetResults(new List <PersonViewModel> { person }); service.Setup(x => x.GetAll()).ReturnsAsync(result); // Act handler.Handle(request); // Assert Assert.IsTrue(request.Result.IsSuccessful); Assert.IsTrue(request.Result.GetResults().Contains(person)); service.VerifyAll(); }
public void Handle_ValidInput_OrderedResultsByFirstName() { // Arrange PeopleQuery request = new PeopleQuery { OrderBy = "FirstName" }; PersonViewModel person = new PersonViewModel { FirstName = "f1", Surname = "s1", Score = 0 }; PersonViewModel person2 = new PersonViewModel { FirstName = "f2", Surname = "s2", Score = 76 }; PersonViewModel person3 = new PersonViewModel { FirstName = "f3", Surname = "s3", Score = 23 }; PersonViewModel person4 = new PersonViewModel { FirstName = "f4", Surname = "s4", Score = 1 }; var result = new CollectionResult <PersonViewModel>(); result.IsSuccessful = true; result.SetResults((new List <PersonViewModel> { person, person2, person3, person4 }).OrderBy(x => x.FirstName)); service.Setup(x => x.GetAll(new string[] { request.OrderBy })).ReturnsAsync(result); // Act handler.Handle(request); // Assert Assert.IsTrue(request.Result.IsSuccessful); IEnumerable <PersonViewModel> results = request.Result.GetResults(); Assert.AreEqual(4, results.ToList().Count()); Assert.AreEqual(new string[] { "f1", "f2", "f3", "f4" }, results.Select(x => x.FirstName)); service.VerifyAll(); }