public void TestGetAsync_WhenScriptExits_ReturnsCorrectScript()
        {
            const string surveyId = "SurveyId";
            const string script = "this is the script";
            const string fileName = "fileq.odin";

            var expected = new SurveyScript{Script = script,FileName = fileName};

               _mockedHttpClient
                .Setup(client => client.GetAsync(string.Format("{0}Surveys/{1}/Script/", ServiceAddress, surveyId)))
                .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(expected))));

            var actual = _target.GetAsync(surveyId).Result;

            Assert.Equal(script, actual.Script);
            Assert.Equal(fileName, actual.FileName);
        }
 /// <summary>
 /// See <see cref="INfieldSurveyScriptService.PostAsync(string,Nfield.Models.SurveyScript)"/>
 /// </summary>
 public Task<SurveyScript> PostAsync(string surveyId, SurveyScript surveyScript)
 {
     if (surveyScript == null)
     {
         throw new ArgumentNullException("surveyScript");
     }
     var uri = SurveyScriptUrl(surveyId);
     return Client.PostAsJsonAsync(uri, surveyScript)
         .ContinueWith(
             responseMessageTask => responseMessageTask.Result.Content.ReadAsStringAsync().Result)
         .ContinueWith(
             stringTask =>
                 JsonConvert.DeserializeObject<SurveyScript>(stringTask.Result))
         .FlattenExceptions();
 }
        /// <summary>
        /// See <see cref="INfieldSurveyScriptService.PostAsync(string,string)"/>
        /// </summary>
        public Task<SurveyScript> PostAsync(string surveyId, string filePath)
        {
            var fileName = _fileSystem.Path.GetFileName(filePath);

            if (!_fileSystem.File.Exists(filePath))
                throw new FileNotFoundException(fileName);

            var surveyScript = new SurveyScript
            {
                FileName = fileName,
                Script = _fileSystem.File.ReadAllText(filePath)
            };

            return PostAsync(surveyId, surveyScript);
        }
Exemple #4
0
        static void Main()
        {
            // Example of using the Nfield SDK with a user defined IoC container.
            // In most cases the IoC container is created and managed through the application.
            using(IKernel kernel = new StandardKernel())
            {
            #if DEBUG
                ServicePointManager.ServerCertificateValidationCallback +=
                        (sender, cert, chain, sslPolicyErrors) => true;
            #endif
                InitializeNfield(kernel);

                const string serverUrl = "http://*****:*****@"*QUESTION 1 *CODES 61L1
                               Do you watch TV?
                                1:Yes
                                2:No
                                *END"
                };
                var updatedScript = surveyScriptService.PostAsync("surveyWithOdinScriptId", myScript).Result;
                // Upload survey script with file path
                var scriptFilePath = @"C:\SimpleQ.odin";
                var myUpdatedScript =
                    surveyScriptService.PostAsync("surveyWithOdinScriptId", scriptFilePath).Result;

                // Create survey
                var newSurvey = surveysService.AddAsync(new Survey(SurveyType.Basic)
                {
                    ClientName = "clientName",
                    Description = "description",
                    SurveyName = "abc"
                }).Result;

                surveyScriptService.PostAsync(newSurvey.SurveyId, myScript).Wait();

                var surveyFieldworkService = connection.GetService<INfieldSurveyFieldworkService>();
                //Get survey fieldwork status
                var surveyFieldworkStatus = surveyFieldworkService.GetStatusAsync(newSurvey.SurveyId).Result; //Should be under construction

                // Start the fieldwork for the survey
                surveyFieldworkService.StartFieldworkAsync(newSurvey.SurveyId).Wait();
                surveyFieldworkStatus = surveyFieldworkService.GetStatusAsync(newSurvey.SurveyId).Result; //Should be started

                // Example of a download data request: filtering testdata collected today
                var surveyDataService = connection.GetService<INfieldSurveyDataService>();

                var myRequest = new SurveyDownloadDataRequest
                {
                    DownloadSuccessfulLiveInterviewData = false,
                    DownloadNotSuccessfulLiveInterviewData = false,
                    DownloadOpenAnswerData = true,
                    DownloadClosedAnswerData = true,
                    DownloadSuspendedLiveInterviewData = false,
                    DownloadCapturedMedia = false,
                    DownloadParaData = false,
                    DownloadTestInterviewData = true,
                    DownloadFileName = "MyFileName",
                    StartDate = DateTime.Today.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture), // UTC time start of today
                    EndDate = DateTime.Today.AddDays(1).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture), // UTC time end of today
                    SurveyId = "SomeSurveyId"
                };

                var task = surveyDataService.PostAsync(myRequest).Result;

                // request the background tasks service
                var backgroundTasksService = connection.GetService<INfieldBackgroundTasksService>();

                // Example of performing operations on background tasks.
                var backgroundTaskQuery = backgroundTasksService.QueryAsync().Result.Where(s => s.Id == task.Id);
                var mybackgroundTask = backgroundTaskQuery.FirstOrDefault();

                if (mybackgroundTask != null)
                {
                    var status = mybackgroundTask.Status;
                }

                // Example of creating a new translation
                const string surveyName = "Language";
                const string languageName = "Dutch";
                const string translationName = "ButtonNext";
                const string translationText = "Volgende";
                // First create the survey if it does not exist
                var languageSurvey = surveysService.QueryAsync().Result
                        .SingleOrDefault(s => s.SurveyName == surveyName);
                if (languageSurvey == null)
                    languageSurvey = surveysService.AddAsync(
                        new Survey(SurveyType.Basic)
                        {
                            SurveyName = surveyName
                        }).Result;
                // Then find the language we wish to change a translation for
                var languageService = connection.GetService<INfieldLanguagesService>();
                var language = languageService.QueryAsync(languageSurvey.SurveyId)
                    .Result.SingleOrDefault(l => l.Name == languageName);
                if (language == null)
                {
                    language = languageService.AddAsync(languageSurvey.SurveyId,
                            new Language { Name = languageName }).Result;
                }
                // Now add or update a translation
                var translationsService = connection.GetService<INfieldTranslationsService>();
                var translation = translationsService.QueryAsync(languageSurvey.SurveyId,
                        language.Id).Result.SingleOrDefault(t => t.Name == translationName);
                if (translation == null)
                {
                    translationsService.AddAsync(languageSurvey.SurveyId,
                        language.Id, new Translation
                        {
                            Name = translationName,
                            Text = translationText
                        }).Wait();
                }
                else
                {
                    translation.Text = translationText;
                    translationsService.UpdateAsync(languageSurvey.SurveyId,
                        language.Id, translation).Wait();
                }

            }
        }
        public void TestPostAsync_FileDoesExists_FileUploaded()
        {
            const string surveyId = "SurveyId";
            const string script = "this is the script";
            const string fileName = "fileq.odin";

            _mockedFileSystem.Setup(fs => fs.Path.GetFileName(It.IsAny<string>())).Returns(fileName);
            _mockedFileSystem.Setup(fs => fs.File.Exists(It.IsAny<string>())).Returns(true);
            _mockedFileSystem.Setup(fs => fs.File.ReadAllText(It.IsAny<string>())).Returns(script);

            var surveyScript = new SurveyScript { Script = script, FileName = fileName };

            var stringContent = new StringContent(JsonConvert.SerializeObject(surveyScript));

            _mockedHttpClient
                .Setup(client => client.PostAsJsonAsync(It.IsAny<string>(), It.IsAny<SurveyScript>()))
                .Returns(CreateTask(HttpStatusCode.OK, stringContent));

            _target.PostAsync(surveyId, fileName);

            _mockedHttpClient.Verify(hc => hc.PostAsJsonAsync(It.Is<string>(uri => uri.Contains(surveyId)),
                        It.Is<SurveyScript>(scripts => scripts.FileName == fileName && scripts.Script == script)), Times.Once());
        }
        public void TestPostAsync_ServerAccepts_ReturnsSurveyScript()
        {
            const string surveyId = "SurveyId";
            const string script = "this is the script";
            const string fileName = "fileq.odin";

            var surveyScript = new SurveyScript { Script = script, FileName = fileName };

            var content = new StringContent(JsonConvert.SerializeObject(surveyScript));

            _mockedHttpClient
                .Setup(client => client.PostAsJsonAsync(string.Format("{0}Surveys/{1}/Script/", ServiceAddress, surveyId), surveyScript))
                .Returns(CreateTask(HttpStatusCode.OK, content));

            var actual = _target.PostAsync(surveyId,surveyScript).Result;

            Assert.Equal(surveyScript.FileName, actual.FileName);
            Assert.Equal(surveyScript.Script, actual.Script);
        }