public async Task <IList <ParticipantsObject> > Get() { var stringTask = await _httpClient.GetAsync(new Uri(ApiUri + _apiParticipants)); ParticipantsResponse participantsResponse = new ParticipantsResponse(); List <ParticipantsObject> participantsList = new List <ParticipantsObject>(); participantsResponse = JsonConvert.DeserializeObject <ParticipantsResponse>(stringTask.Content.ReadAsStringAsync().Result); // Add all the participant objects to the list foreach (var participant in participantsResponse.ParticipantsObject) { participantsList.Add(participant); } // If there are more pages, page through them using the 'next' pointer while (participantsResponse.MetaObject.Next != null) { // Use the 'next' pointer to get the next page stringTask = await _httpClient.GetAsync(new Uri(ApiUri + participantsResponse.MetaObject.Next)); participantsResponse = JsonConvert.DeserializeObject <ParticipantsResponse>(stringTask.Content.ReadAsStringAsync().Result); // Add all the participant objects to the list foreach (var participant in participantsResponse.ParticipantsObject) { participantsList.Add(participant); } } return(participantsList); }
public ViewResult RsvpForm(ParticipantsResponse participantsResponse) { if (ModelState.IsValid) { Repository.AddResponse(participantsResponse); return(View("Thanks", participantsResponse)); } else { //There is a validation error in the form return(View()); } }
public async Task <int> GetTotal() { var stringTask = await _httpClient.GetAsync(new Uri(ApiUri + _apiParticipants)); if ((int)stringTask.StatusCode == 200) { ParticipantsResponse participantsResponse = new ParticipantsResponse(); participantsResponse = JsonConvert.DeserializeObject <ParticipantsResponse>(stringTask.Content.ReadAsStringAsync().Result); return(participantsResponse.MetaObject.TotalCount); } throw new Exception((int)stringTask.StatusCode + " - " + stringTask.ReasonPhrase); }
public void TestGettingTotalCountOfParticipant() { // Arrange // The URI we are using in the test var requestUri = new Uri("https://localhost/api/admin/status/v1/participant/"); ParticipantsResponse ParticipantsResponse = new ParticipantsResponse { MetaObject = new MetaObject { TotalCount = 15 } }; // Serialise the object var expectedResponse = JsonConvert.SerializeObject(ParticipantsResponse); // Set up the mock with the expected response var mockResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(expectedResponse) }; var mockHandler = new Mock <HttpClientHandler>(); mockHandler .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.Is <HttpRequestMessage>(message => message.RequestUri == requestUri), ItExpr.IsAny <CancellationToken>()) .Returns(Task.FromResult(mockResponse)); // Set up the HttpClient using the mock handler object HttpClient client = new HttpClient(mockHandler.Object); // Initialise an instance of the Participants class for testing using the HttpClient Participants participants = new Participants(client, "https://localhost"); // Act var participantsTotalCount = participants.GetTotal().Result; // Assert Assert.True(participantsTotalCount == 15); }
public void TestGettingPagedParticipantsList() { // Arrange // The URI we are using in the test var requestUri = new Uri("https://localhost/api/admin/status/v1/participant/"); var requestUriTwo = new Uri("https://localhost/api/admin/status/v1/participant/?limit=20&offset=20"); // Page 1 List <ParticipantsObject> testParticipantsList = new List <ParticipantsObject>(); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); testParticipantsList.Add(new ParticipantsObject { CallQuality = "1_good" }); MetaObject MetaObject = new MetaObject { Next = "/api/admin/status/v1/participant/?limit=20&offset=20" }; ParticipantsResponse ParticipantsResponse = new ParticipantsResponse { MetaObject = MetaObject, ParticipantsObject = testParticipantsList }; // Page 2 List <ParticipantsObject> testParticipantsListTwo = new List <ParticipantsObject>(); testParticipantsListTwo.Add(new ParticipantsObject { CallQuality = "4_terrible" }); MetaObject MetaObjectTwo = new MetaObject { Next = null }; ParticipantsResponse ParticipantsResponseTwo = new ParticipantsResponse { MetaObject = MetaObjectTwo, ParticipantsObject = testParticipantsListTwo }; // Serialise the object var expectedResponse = JsonConvert.SerializeObject(ParticipantsResponse); var expectedResponseTwo = JsonConvert.SerializeObject(ParticipantsResponseTwo); // Set up the mock with the expected response var mockResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(expectedResponse) }; var mockResponseTwo = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(expectedResponseTwo) }; var mockHandler = new Mock <HttpClientHandler>(); mockHandler .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.Is <HttpRequestMessage>(message => message.RequestUri == requestUri), ItExpr.IsAny <CancellationToken>()) .Returns(Task.FromResult(mockResponse)); mockHandler .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.Is <HttpRequestMessage>(message => message.RequestUri == requestUriTwo), ItExpr.IsAny <CancellationToken>()) .Returns(Task.FromResult(mockResponseTwo)); // Set up the HttpClient using the mock handler object HttpClient client = new HttpClient(mockHandler.Object); // Initialise an instance of the Participants class for testing using the HttpClient Participants participants = new Participants(client, "https://localhost"); // Act var participantsResult = participants.Get().Result; // Assert // Assert that a List of participants was returned with 21 items Assert.True(participantsResult.Count == 21); }