コード例 #1
0
        public Task <InviteRespondentsStatus> SendInvitationsAsync(string surveyId, InvitationBatch batch)
        {
            Ensure.ArgumentNotNullOrEmptyString(surveyId, nameof(surveyId));
            Ensure.ArgumentNotNull(batch, nameof(batch));

            var uri             = SurveyInviteRespondentsUrl(surveyId);
            var batchWithFilter = new InvitationBatchWithFilter()
            {
                EmailColumnName      = batch.EmailColumnName,
                InvitationTemplateId = batch.InvitationTemplateId,
                Name         = batch.Name,
                ScheduledFor = batch.ScheduledFor,
                Filters      = new[]
                {
                    new SampleFilter {
                        Name = "RespondentKey", Op = "in", Value = string.Join(",", batch.RespondentKeys)
                    }
                }
            };

            return(Client.PostAsJsonAsync(uri, batchWithFilter)
                   .ContinueWith(responseMessageTask => responseMessageTask.Result.Content.ReadAsStringAsync().Result)
                   .ContinueWith(stringResult => JsonConvert.DeserializeObject <InviteRespondentsStatus>(stringResult.Result))
                   .FlattenExceptions());
        }
        public void TestSendInvitationsAsync_ServerAccepts_ReturnsCorrectInviteRespondentsStatus()
        {
            var scheduledFor = DateTime.Now.AddDays(2);
            var batch        = new InvitationBatch
            {
                RespondentKeys = new List <string> {
                    "r1", "r2"
                },
                EmailColumnName      = "email",
                InvitationTemplateId = 2,
                Name         = "FirstBatch",
                ScheduledFor = scheduledFor
            };

            var mockedNfieldConnection = new Mock <INfieldConnectionClient>();
            var mockedHttpClient       = CreateHttpClientMock(mockedNfieldConnection);

            var expectedResult = new InviteRespondentsStatus
            {
                Count        = 2,
                Status       = "Completed",
                ErrorMessage = ""
            };

            var url = new Uri(ServiceAddress, $"Surveys/{SurveyId}/InviteRespondents/");

            mockedHttpClient.Setup(client => client
                                   .PostAsJsonAsync(url, It.Is <InvitationBatchWithFilter>(b =>
                                                                                           b.Name == batch.Name &&
                                                                                           b.ScheduledFor == batch.ScheduledFor &&
                                                                                           b.EmailColumnName == batch.EmailColumnName &&
                                                                                           b.InvitationTemplateId == batch.InvitationTemplateId
                                                                                           )))
            .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(expectedResult))));

            var target = new NfieldSurveyInviteRespondentsService();

            target.InitializeNfieldConnection(mockedNfieldConnection.Object);
            var result = target.SendInvitationsAsync(SurveyId, batch).Result;

            // Verify the filter send
            mockedHttpClient.Verify(s => s.PostAsJsonAsync(
                                        url,
                                        It.Is <InvitationBatchWithFilter>(b =>
                                                                          b.Filters.Count() == 1 &&
                                                                          FilterEquals(b.Filters.First(), "RespondentKey", "in", string.Join(",", batch.RespondentKeys)))),
                                    Times.Once());

            Assert.Equal(expectedResult.Count, result.Count);
            Assert.Equal(expectedResult.Status, result.Status);
            Assert.Equal(expectedResult.ErrorMessage, result.ErrorMessage);
        }