Nfield.Services.Implementation.NfieldMediaFilesService.GetCountAsync C# (CSharp) Method

GetCountAsync() public method

public GetCountAsync ( string surveyId ) : Task
surveyId string
return Task
        public Task<int> GetCountAsync(string surveyId)
        {
            if (string.IsNullOrEmpty(surveyId))
            {
                throw new ArgumentNullException("surveyId");
            }
            var uri = $"{MediaFilesApi(surveyId, null).AbsoluteUri}Count";
            return Client.GetAsync(uri)
                .ContinueWith(rmt => int.Parse(rmt.Result.Content.ReadAsStringAsync().Result))
                .FlattenExceptions();
        }

Usage Example

        public void TestGetCountAsync_Always_ReturnsCorrectCount()
        {
            const string surveyId = "SurveyId";
            const int expectedCount = 4;
            const string uri = ServiceAddress + "Surveys/" + surveyId + "/MediaFiles/Count";

            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.GetAsync(uri))
                .Returns(CreateTask(HttpStatusCode.OK, new StringContent(expectedCount.ToString())));

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

            var actual = target.GetCountAsync(surveyId).Result;

            Assert.Equal(expectedCount, actual);
        }