public void TestSendInvitationsAsync_BatchDataIsNull_Throws()
        {
            var target = new NfieldSurveyInviteRespondentsService();

            Assert.Throws <ArgumentNullException>(() =>
                                                  UnwrapAggregateException(target.SendInvitationsAsync(SurveyId, null)));
        }
        public void TestSendInvitationsAsync_SurveyIdIsEmpty_Throws()
        {
            var target = new NfieldSurveyInviteRespondentsService();

            Assert.Throws <ArgumentException>(() =>
                                              UnwrapAggregateException(target.SendInvitationsAsync("  ", null)));
        }
        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);
        }