Nfield.Services.Implementation.NfieldSurveysService.CountsQueryAsync C# (CSharp) Method

CountsQueryAsync() public method

public CountsQueryAsync ( string surveyId ) : Task
surveyId string
return Task
        public Task<SurveyCounts> CountsQueryAsync(string surveyId)
        {
            var uri = string.Format(@"{0}{1}/{2}", SurveysApi.AbsoluteUri, surveyId, CountsControllerName);

            return Client.GetAsync(uri)
                         .ContinueWith(
                             responseMessageTask => responseMessageTask.Result.Content.ReadAsStringAsync().Result)
                         .ContinueWith(
                             stringTask =>
                             JsonConvert.DeserializeObject<SurveyCounts>(stringTask.Result))
                         .FlattenExceptions();
        }

Usage Example

        public void TestCountsQueryAsync_ServerReturnsQuery_ReturnsCounts()
        {
            const int expectedDroppedOutCount = 1;
            const int expectedRejectedCount = 2;
            const int expectedScreenedOutCount = 3;
            const int expectedSuccessfulCount = 4;
            var surveyId = Guid.NewGuid().ToString();
            var expectedCounts =  new SurveyCounts
            {
                DroppedOutCount = expectedDroppedOutCount,
                RejectedCount = expectedRejectedCount,
                ScreenedOutCount = expectedScreenedOutCount,
                SuccessfulCount = expectedSuccessfulCount
            };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            var url = string.Format("{0}surveys/{1}/counts", ServiceAddress, surveyId);
            mockedHttpClient
                .Setup(client => client.GetAsync(url))
                .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(expectedCounts))));

            var target = new NfieldSurveysService();
            target.InitializeNfieldConnection(mockedNfieldConnection.Object);

            var result = target.CountsQueryAsync(surveyId).Result;

            Assert.Equal(expectedDroppedOutCount, result.DroppedOutCount);
            Assert.Equal(expectedRejectedCount, result.RejectedCount);
            Assert.Equal(expectedScreenedOutCount, result.ScreenedOutCount);
            Assert.Equal(expectedSuccessfulCount, result.SuccessfulCount);
        }