public void TestAddAsync_ServerAccepts_ReturnsSurvey()
        {
            var survey = new Survey(SurveyType.Basic) { SurveyName = "New Survey" };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            var content = new StringContent(JsonConvert.SerializeObject(survey));
            mockedHttpClient
                .Setup(client => client.PostAsJsonAsync(ServiceAddress + "surveys/", survey))
                .Returns(CreateTask(HttpStatusCode.OK, content));

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

            var actual = target.AddAsync(survey).Result;

            Assert.Equal(survey.SurveyName, actual.SurveyName);
            Assert.Equal(survey.SurveyType, actual.SurveyType);
        }
        /// <summary>
        /// See <see cref="INfieldSurveysService.AddAsync"/>
        /// </summary>
        public Task<Survey> AddAsync(Survey survey)
        {
            if (survey == null)
            {
                throw new ArgumentNullException("survey");
            }

            return Client.PostAsJsonAsync(SurveysApi.AbsoluteUri, survey)
                         .ContinueWith(task => task.Result.Content.ReadAsStringAsync().Result)
                         .ContinueWith(task => JsonConvert.DeserializeObject<Survey>(task.Result))
                         .FlattenExceptions();
        }
        /// <summary>
        /// See <see cref="INfieldSurveysService.UpdateAsync"/>
        /// </summary>
        public Task<Survey> UpdateAsync(Survey survey)
        {
            if (survey == null)
            {
                throw new ArgumentNullException("survey");
            }

            var updatedSurvey = new UpdateSurvey
            {
                ClientName = survey.ClientName,
                Description = survey.Description,
                SurveyName = survey.SurveyName,
                InterviewerInstruction = survey.InterviewerInstruction
            };

            return Client.PatchAsJsonAsync(SurveysApi + survey.SurveyId, updatedSurvey)
             .ContinueWith(
                 responseMessageTask => responseMessageTask.Result.Content.ReadAsStringAsync().Result)
             .ContinueWith(
                 stringTask => JsonConvert.DeserializeObject<Survey>(stringTask.Result))
             .FlattenExceptions();
        }
        /// <summary>
        /// See <see cref="INfieldSurveysService.RemoveAsync"/>
        /// </summary>
        public Task RemoveAsync(Survey survey)
        {
            if (survey == null)
            {
                throw new ArgumentNullException("survey");
            }

            return
                Client.DeleteAsync(SurveysApi.AbsoluteUri + survey.SurveyId)
                      .FlattenExceptions();
        }
        public void TestUpdateAsync_InterviewerExists_ReturnsInterviewer()
        {
            const string surveyId = "aSurveyId";
            var survey = new Survey(SurveyType.Basic)
            {
                SurveyId = surveyId,
                Description = "updated description"
            };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.PatchAsJsonAsync(ServiceAddress + "surveys/" + surveyId, It.IsAny<UpdateSurvey>()))
                .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(survey))));

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

            var actual = target.UpdateAsync(survey).Result;

            Assert.Equal(survey.Description, actual.Description);
        }
        public void TestSamplingPointAddAsync_ServerAcceptsSamplingPoint_ReturnsSamplingPoint()
        {
            const string samplingPointGroupId = "MyGroupId";
            var office = new FieldworkOffice { OfficeId = "OfficeId" };
            var survey = new Survey(SurveyType.Basic) { SurveyId = "SurveyId" };
            var samplingPoint = new SamplingPoint
            {
                SamplingPointId = "SamplingPointId",
                FieldworkOfficeId = office.OfficeId,
                GroupId = samplingPointGroupId
            };

            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            var content = new StringContent(JsonConvert.SerializeObject(samplingPoint));
            mockedHttpClient
                .Setup(
                    client =>
                        client.PostAsJsonAsync(
                            string.Format("{0}surveys/{1}/samplingpoints", ServiceAddress, survey.SurveyId),
                            samplingPoint))
                .Returns(CreateTask(HttpStatusCode.OK, content));

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

            var actual = target.SamplingPointAddAsync(survey.SurveyId, samplingPoint).Result;

            Assert.Equal(samplingPoint.SamplingPointId, actual.SamplingPointId);
            Assert.Equal(samplingPoint.FieldworkOfficeId, actual.FieldworkOfficeId);
            Assert.Equal(samplingPoint.GroupId, actual.GroupId);
        }
        public void TestRemoveAsync_ServerRemovedSurvey_DoesNotThrow()
        {
            const string surveyId = "Survey X";
            var survey = new Survey(SurveyType.Basic) { SurveyId = surveyId };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.DeleteAsync(ServiceAddress + "surveys/" + surveyId))
                .Returns(CreateTask(HttpStatusCode.OK));

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

            Assert.DoesNotThrow(() => target.RemoveAsync(survey).Wait());
        }