Holds all properties of an interviewer
 /// <summary>
 /// See <see cref="INfieldInterviewersService.AddAsync"/>
 /// </summary>
 public Task<Interviewer> AddAsync(Interviewer interviewer)
 {
     return Client.PostAsJsonAsync(InterviewersApi.AbsoluteUri, interviewer)
                  .ContinueWith(task => task.Result.Content.ReadAsStringAsync().Result)
                  .ContinueWith(task => JsonConvert.DeserializeObjectAsync<Interviewer>(task.Result).Result)
                  .FlattenExceptions();
 }
        public void TestAdd_Interviewer_ReturnsInterviewer()
        {
            const string userName = "******";
            const string clientInterviewerId = "ClientInterviewerId";

            var mockedInterviewer = new Interviewer()
                {
                    ClientInterviewerId = clientInterviewerId,
                    UserName = userName
                };

            var mockedConnection = new Mock<INfieldConnection>();
            var mockedService = new Mock<INfieldInterviewersService>();

            mockedConnection.Setup(x => x.GetService<INfieldInterviewersService>())
                    .Returns(mockedService.Object);
            mockedService.Setup(x => x.AddAsync(It.Is<Interviewer>(i => i.ClientInterviewerId == clientInterviewerId)))
                         .Returns(Task.Factory.StartNew(() => new Interviewer
                             {
                                 ClientInterviewerId = clientInterviewerId,
                                 UserName = userName
                             }));

            var target = new InterviewerManager();
            var result = target.Add(mockedConnection.Object, mockedInterviewer);

            Assert.Equal(clientInterviewerId, result.ClientInterviewerId);
        }
 public Interviewer Update(INfieldConnection connection, Interviewer interviewer)
 {
     INfieldInterviewersService interviewersService = connection.GetService<INfieldInterviewersService>();
     var interviewerResult = interviewersService.UpdateAsync(interviewer);
     interviewerResult.Wait();
     return interviewerResult.Result;
 }
        public void Validate(Interviewer interviewer)
        {
            if (interviewer == null)
                throw new ValidationMetadataException(message + "\nValidator Failure: Argument Is Null");

            if (string.IsNullOrEmpty(interviewer.UserName))
                throw new ValidationMetadataException(message + "\nValidator Failure: UserName is Required");

            if (!string.IsNullOrEmpty(interviewer.UserName) && interviewer.UserName.Trim().Length > 50)
                throw new ValidationMetadataException(message + "\nValidator Failure: UserName maximum length is 50");

            if (string.IsNullOrEmpty(interviewer.Password))
                throw new ValidationMetadataException(message + "\nValidator Failure: Password is Required");

            if (!string.IsNullOrEmpty(interviewer.FirstName) && interviewer.FirstName.Trim().Length > 50)
                throw new ValidationMetadataException(message + "\nValidator Failure: FirstName maximum length is 50");

            if (!string.IsNullOrEmpty(interviewer.LastName) && interviewer.LastName.Trim().Length > 50)
                throw new ValidationMetadataException(message + "\nValidator Failure: LastName maximum length is 50");

            if (!string.IsNullOrEmpty(interviewer.EmailAddress) && interviewer.EmailAddress.Trim().Length > 50)
                throw new ValidationMetadataException(message + "\nValidator Failure: EmailAddress maximum length is 50");

            if (!string.IsNullOrEmpty(interviewer.TelephoneNumber) && interviewer.TelephoneNumber.Trim().Length > 30)
                throw new ValidationMetadataException(message + "\nValidator Failure: TelephoneNumber maximum length is 30");

            if (!string.IsNullOrEmpty(interviewer.ClientInterviewerId) && interviewer.ClientInterviewerId.Trim().Length != 8)
                throw new ValidationMetadataException(message + "\nValidator Failure: ClientInterviewerId should be 8 digit");
        }
        public void TestFrom_MapFromInterviewer_SetsCorrectProperty()
        {
            const string expectedUserName = "******";
            Interviewer source = new Interviewer() { UserName = expectedUserName };

            PSObjectMapper target = new PSObjectMapper();
            PSObject result = target.From(source);

            Assert.Equal(expectedUserName, result.Properties["UserName"].Value.ToString());
        }
        /// <summary>
        /// See <see cref="INfieldInterviewersService.ChangePasswordAsync"/>
        /// </summary>
        public Task<Interviewer> ChangePasswordAsync(Interviewer interviewer, string password)
        {
            if (interviewer == null)
            {
                throw new ArgumentNullException("interviewer");
            }

            return Client.PutAsJsonAsync(InterviewersApi + interviewer.InterviewerId, (object)new { Password = password })
                         .ContinueWith(
                             responseMessageTask => responseMessageTask.Result.Content.ReadAsStringAsync().Result)
                         .ContinueWith(
                             stringTask => JsonConvert.DeserializeObjectAsync<Interviewer>(stringTask.Result).Result)
                         .FlattenExceptions();
        }
        /// <summary>
        /// Adds an <see cref="Interviewer"/> to the system with a synchronous operation.
        /// </summary>
        public Interviewer AddInterviewer()
        {
            Interviewer interviewer = new Interviewer
                {
                    ClientInterviewerId = "pomn45dr",
                    FirstName = "Steve",
                    LastName = "Balmer",
                    EmailAddress = "*****@*****.**",
                    TelephoneNumber = "0207821569",
                    UserName = "******",
                    Password = "******"
                };

            return _interviewersService.Add(interviewer);
        }
        public void TestAddAsync_ServerAcceptsInterviewer_ReturnsInterviewer()
        {
            var interviewer = new Interviewer { UserName = "******" };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            var content = new StringContent(JsonConvert.SerializeObject(interviewer));
            mockedHttpClient
                .Setup(client => client.PostAsJsonAsync(ServiceAddress + "interviewers/", interviewer))
                .Returns(CreateTask(HttpStatusCode.OK, content));

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

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

            Assert.Equal(interviewer.UserName, actual.UserName);
        }
        public void TestValidate_InterviewerEmailAddressLengthMoreThanFifty_ThrowsValidationMetadataException()
        {
            var EmailAddressString =
                "Lorem ipsum dolor sit amet, te est tollit definiebas, ullum appareat adversarium an usu. Offendit";
            var stubInterviewer = new Interviewer()
                {
                    UserName = "******",
                    Password = "******",
                    FirstName = "firstname",
                    LastName = "lastname",
                    EmailAddress = EmailAddressString
                };

            var validator = new CustomInterviewerValidatorAttribute();

            var ex = Assert.Throws<ValidationMetadataException>(() => validator.Validate(stubInterviewer));
            Assert.True(ex.Message.Contains("maximum length"));
            Assert.True(ex.Message.Contains("EmailAddress"));
        }
        public void TestValidate_InterviewerClientInterviewerIdLengthMoreThanEight_ThrowsValidationMetadataException()
        {
            var ClientInterviewerIdString =
                "MoreThan8Characters";
            var stubInterviewer = new Interviewer()
            {
                UserName = "******",
                Password = "******",
                FirstName = "firstname",
                LastName = "lastname",
                EmailAddress = "*****@*****.**",
                TelephoneNumber = "TelephoneNumber",
                ClientInterviewerId = ClientInterviewerIdString
            };

            var validator = new CustomInterviewerValidatorAttribute();

            var ex = Assert.Throws<ValidationMetadataException>(() => validator.Validate(stubInterviewer));
            Assert.True(ex.Message.Contains("ClientInterviewerId"));
        }
 /// <summary>
 /// A synchronous version of <see cref="INfieldInterviewersService.UpdateAsync"/>
 /// </summary>
 public static Interviewer Update(this INfieldInterviewersService interviewersService, Interviewer interviewer)
 {
     return interviewersService.UpdateAsync(interviewer).Result;
 }
 /// <summary>
 /// A synchronous version of <see cref="INfieldInterviewersService.RemoveAsync"/>
 /// </summary>
 public static void Remove(this INfieldInterviewersService interviewersService, Interviewer interviewer)
 {
     interviewersService.RemoveAsync(interviewer).Wait();
 }
 /// <summary>
 /// A synchronous version of <see cref="INfieldInterviewersService.ChangePasswordAsync"/>
 /// </summary>
 public static Interviewer ChangePassword(this INfieldInterviewersService interviewersService, Interviewer interviewer, string password)
 {
     return interviewersService.ChangePasswordAsync(interviewer, password).Result;
 }
 /// <summary>
 /// A synchronous version of <see cref="INfieldInterviewersService.AddAsync"/>
 /// </summary>
 public static Interviewer Add(this INfieldInterviewersService interviewersService, Interviewer interviewer)
 {
     return interviewersService.AddAsync(interviewer).Result;
 }
        /// <summary>
        /// Changes the password for an <see cref="Interviewer"/> synchronously.
        /// </summary>
        public Interviewer ChangePassword(Interviewer interviewer, string password)
        {
            if (interviewer == null)
            {
                return null;
            }

            return _interviewersService.ChangePassword(interviewer, password);
        }
        public void TestUpdateAsync_InterviewerExists_ReturnsInterviewer()
        {
            const string InterviewerId = "Interviewer X";
            var interviewer = new Interviewer
            {
                InterviewerId = InterviewerId,
                FirstName = "XXX"
            };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.PatchAsJsonAsync(ServiceAddress + "interviewers/" + InterviewerId, It.IsAny<UpdateInterviewer>()))
                .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(interviewer))));

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

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

            Assert.Equal(interviewer.FirstName, actual.FirstName);
        }
        public void TestQueryAsync_ServerReturnsQuery_ReturnsListWithInterviewers()
        {
            var expectedInterviewers = new Interviewer[]
            { new Interviewer{InterviewerId = "TestInterviewer"},
              new Interviewer{InterviewerId = "AnotherTestInterviewer"}
            };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.GetAsync(ServiceAddress + "interviewers/"))
                .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(expectedInterviewers))));

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

            var actualInterviewers = target.QueryAsync().Result;
            Assert.Equal(expectedInterviewers[0].InterviewerId, actualInterviewers.ToArray()[0].InterviewerId);
            Assert.Equal(expectedInterviewers[1].InterviewerId, actualInterviewers.ToArray()[1].InterviewerId);
            Assert.Equal(2, actualInterviewers.Count());
        }
        /// <summary>
        /// See <see cref="INfieldInterviewersService.UpdateAsync"/>
        /// </summary>
        public Task<Interviewer> UpdateAsync(Interviewer interviewer)
        {
            if (interviewer == null)
            {
                throw new ArgumentNullException("interviewer");
            }

            var updatedInterviewer = new UpdateInterviewer
                {
                    EmailAddress = interviewer.EmailAddress,
                    FirstName = interviewer.FirstName,
                    LastName = interviewer.LastName,
                    TelephoneNumber = interviewer.TelephoneNumber
                };

            return Client.PatchAsJsonAsync(InterviewersApi + interviewer.InterviewerId, updatedInterviewer)
                         .ContinueWith(
                             responseMessageTask => responseMessageTask.Result.Content.ReadAsStringAsync().Result)
                         .ContinueWith(
                             stringTask => JsonConvert.DeserializeObjectAsync<Interviewer>(stringTask.Result).Result)
                         .FlattenExceptions();
        }
        /// <summary>
        /// Adds an <see cref="Interviewer"/> to the system with an asynchronous operation.
        /// </summary>
        public Task<Interviewer> AddInterviewerAsync()
        {
            Interviewer interviewer = new Interviewer
            {
                ClientInterviewerId = "ftropo5i",
                FirstName = "Bill",
                LastName = "Gates",
                EmailAddress = "*****@*****.**",
                TelephoneNumber = "0206598547",
                UserName = "******",
                Password = "******"
            };

            return _interviewersService.AddAsync(interviewer);
        }
        public void TestValidate_InterviewerUserNameLengthMoreThanFifty_ThrowsValidationMetadataException()
        {
            var userNameString =
                "Lorem ipsum dolor sit amet, te est tollit definiebas, ullum appareat adversarium an usu. Offendit";
            var stubInterviewer = new Interviewer() { UserName = userNameString };

            var validator = new CustomInterviewerValidatorAttribute();

            var ex = Assert.Throws<ValidationMetadataException>(() => validator.Validate(stubInterviewer));
            Assert.True(ex.Message.Contains("maximum length"));
            Assert.True(ex.Message.Contains("UserName"));
        }
        public void TestValidate_InterviewerUserNameIsNull_ThrowsValidationMetadataException()
        {
            var stubInterviewer = new Interviewer(){UserName = null};

            var validator = new CustomInterviewerValidatorAttribute();

            var ex = Assert.Throws<ValidationMetadataException>(() => validator.Validate(stubInterviewer));
            Assert.True(ex.Message.Contains("Required"));
        }
        public void TestValidate_InterviewerTelephoneNumberLengthMoreThanThirty_ThrowsValidationMetadataException()
        {
            var TelephoneNumberString =
                "Lorem ipsum dolor sit amet, te est";
            var stubInterviewer = new Interviewer()
            {
                UserName = "******",
                Password = "******",
                FirstName = "firstname",
                LastName = "lastname",
                EmailAddress = "*****@*****.**",
                TelephoneNumber = TelephoneNumberString
            };

            var validator = new CustomInterviewerValidatorAttribute();

            var ex = Assert.Throws<ValidationMetadataException>(() => validator.Validate(stubInterviewer));
            Assert.True(ex.Message.Contains("maximum length"));
            Assert.True(ex.Message.Contains("TelephoneNumber"));
        }
Beispiel #23
0
        static void Main(string[] args)
        {
            using (IKernel kernel = new StandardKernel())
            {
                InitializeNfield(kernel);

                //const string serverUrl = "http://api.nfieldbeta.com/v1";
                const string serverUrl = "http://api.nfieldmr.com/v1";

                // First step is to get an INfieldConnection which provides services used for data access and manipulation.
                INfieldConnection connection = NfieldConnectionFactory.Create(new Uri(serverUrl));

                // User must sign in to the Nfield server with the appropriate credentials prior to using any of the services.

                connection.SignInAsync("hendal", "veljko", "Klagant61").Wait();

                // Request the Interviewers service to manage dodajs.
                INfieldInterviewersService dodajsService = connection.GetService<INfieldInterviewersService>();
                INfieldFieldworkOfficesService foService = connection.GetService<INfieldFieldworkOfficesService>();
                INfieldSurveysService surveyService = connection.GetService<INfieldSurveysService>();

                // Create a new manager to perform the operations on the service.
                NfieldInterviewersManagement dodajsManager = new NfieldInterviewersManagement(dodajsService);
                NfieldSamplingPointManagement samplingPointsManager = new NfieldSamplingPointManagement(surveyService);
                NfieldFieldworkOfficesManagement foManager = new NfieldFieldworkOfficesManagement(foService);

                IEnumerable<Interviewer> allInterviewers = dodajsService.Query().ToList();
                var lista = allInterviewers.ToArray();

                IEnumerable<FieldworkOffice> uredi = foManager.QueryForOfficesAsync();
                var lista_ureda = uredi.ToArray();

                IEnumerable<SamplingPoint> allSamplingPoints = surveyService.SamplingPointsQuery("58e62847-57c3-4e35-ac82-5b9aba568807").ToList();
                //IEnumerable<SamplingPoint> allSamplingPoints = surveyService.SamplingPointsQuery("ce923d5d-79b6-47d2-b377-8f42174dee65").ToList();
                var allPnts = allSamplingPoints.ToArray();

                int i;

                //StreamReader spwfo = new StreamReader(@"..\..\data\sp3.txt");
                for (i=0; i<allPnts.Count(); i++)
                {
                    var tokens = spwfo.ReadLine().Split('|');
                    var mySamplingPoint = new SamplingPoint
                    {
                        SamplingPointId = tokens[0],
                        FieldworkOfficeId = tokens[1],
                        Name = tokens[2],
                        GroupId = tokens[3],
                    };
                    //samplingPointsManager.AddSamplingPoint("678726fe-5900-47a3-b1b6-7c8b2a1c7ae6", mySamplingPoint);
                    //samplingPointsManager.UpdateSamplingPoint("655059d2-0abb-4716-aaea-34549ecd6882", mySamplingPoint);
                    //Console.WriteLine(i);
                }

                SamplingPointKind spk = 0;
                var mySamplingPoint = new SamplingPoint
                {
                    SamplingPointId = "813031",
                    Description = "Survey name: EB81.3; Start address: Ulica Nikole Tesle 7, 23000, Zadar; GPS: 44.1213173,15.235273099999972",
                    Name = "Zadar - Ulica Nikole Tesle 7",
                    FieldworkOfficeId = "88a39030-c2f0-40b4-9c4e-74abe9bce433",
                    Stratum = "HR03U1",
                    GroupId = "12",
                    Kind = spk,
                };
                //samplingPointsManager.AddSamplingPoint("678726fe-5900-47a3-b1b6-7c8b2a1c7ae6", mySamplingPoint);
                //samplingPointsManager.DeleteSamplingPoint("678726fe-5900-47a3-b1b6-7c8b2a1c7ae6", mydSamplingPoint);
                //samplingPointsManager.UpdateSamplingPoint("58e62847-57c3-4e35-ac82-5b9aba568807", mySamplingPoint);
                //samplingPointsManager.UpdateSamplingPoint("655059d2-0abb-4716-aaea-34549ecd6882", mySamplingPoint);

                Console.WriteLine("Ukupno anketara: {0}", allInterviewers.Count());
                Console.WriteLine("Ukupno ureda: {0}", lista_ureda.Count());
                Console.WriteLine("Ukupno startnih točaka: {0}", allPnts.Count());

                StreamWriter datoteka = new StreamWriter(@"..\..\data\sp3.txt");
                //datoteka.WriteLine("REGIONALNI UREDI:");
                for (i = 0; i < lista_ureda.Count(); i++)
                {
                    //datoteka.WriteLine("{0}|{1}", lista_ureda[i].OfficeId, lista_ureda[i].OfficeName);
                    //Console.WriteLine("{0}|{1}", lista_ureda[i].OfficeId, lista_ureda[i].OfficeName);
                }

                //datoteka.WriteLine("STARTNE TOČKE:");
                for (i = 0; i < allSamplingPoints.Count(); i++)
                {
                    //datoteka.WriteLine("{0}|{1}|{2}|{3}|{4}|{5}|{6}", allPnts[i].SamplingPointId, allPnts[i].Description, allPnts[i].Stratum, allPnts[i].FieldworkOfficeId, allPnts[i].Name, allPnts[i].GroupId, allPnts[i].Kind);
                    Console.WriteLine("{0}|{1}|{2}|{3}|{4}", allPnts[i].SamplingPointId, allPnts[i].Name, allPnts[i].Description, allPnts[i].FieldworkOfficeId, allPnts[i].Kind);
                }

                for (i = 503; i < 882; i++)
                {
                //    IEnumerable<Interviewer> tajdodaj = dodajsService.Query().Where(dodaj => string.Equals(dodaj.UserName,"int"+i)).ToList();
                //    var talista = tajdodaj.ToArray();
                //    Interviewer mojint = talista[0];
                //    dodajsService.Remove(mojint);
                //    Console.WriteLine("{0}", "int" + i);
                }
                for (i = 5001; i < 5050; i++)
                {
                    //Console.WriteLine("{0}", "int" + i);
                    Interviewer dodaj = new Interviewer
                    {
                        UserName = "******" + i,
                        Password = "******" + i
                    };
                    //dodajsService.Add(dodaj);
                }
            }
        }
        /// <summary>
        /// Updates an <see cref="Interviewer"/> with an asynchronous operation.
        /// </summary>
        public Task<Interviewer> UpdateInterviewerAsync(Interviewer interviewer)
        {
            if(interviewer == null)
            {
                return null;
            }

            return _interviewersService.UpdateAsync(interviewer);
        }
        public void TestChangePasswordAsync_ServerChangesPassword_ReturnsInterviewer()
        {
            const string Password = "******";
            const string InterviewerId = "Interviewer X";
            var interviewer = new Interviewer {InterviewerId = InterviewerId };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.PutAsJsonAsync(ServiceAddress + "interviewers/" + InterviewerId, It.IsAny<object>()))
                .Returns(CreateTask(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(interviewer))));

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

            var actual = target.ChangePasswordAsync(interviewer, Password).Result;

            Assert.Equal(interviewer.InterviewerId, actual.InterviewerId);
        }
        /// <summary>
        /// Updates an <see cref="Interviewer"/> with a synchronous operation.
        /// </summary>
        public Interviewer UpdateInterviewer(Interviewer interviewer)
        {
            if (interviewer == null)
            {
                return null;
            }

            return _interviewersService.Update(interviewer);
        }
        public void TestRemoveAsync_ServerRemovedInterviewer_DoesNotThrow()
        {
            const string InterviewerId = "Interviewer X";
            var interviewer = new Interviewer { InterviewerId = InterviewerId };
            var mockedNfieldConnection = new Mock<INfieldConnectionClient>();
            var mockedHttpClient = CreateHttpClientMock(mockedNfieldConnection);
            mockedHttpClient
                .Setup(client => client.DeleteAsync(ServiceAddress + "interviewers/" + InterviewerId))
                .Returns(CreateTask(HttpStatusCode.OK));

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

            Assert.DoesNotThrow(() => target.RemoveAsync(interviewer).Wait());
        }
        /// <summary>
        /// Removes an existing <see cref="Interviewer"/> with an asynchronous operation.
        /// </summary>
        public Task RemoveInterviewerAsync(Interviewer interviewer)
        {
            if (interviewer == null)
            {
                return null;
            }

            return _interviewersService.RemoveAsync(interviewer);
        }
        /// <summary>
        /// See <see cref="INfieldInterviewersService.RemoveAsync"/>
        /// </summary>
        public Task RemoveAsync(Interviewer interviewer)
        {
            if (interviewer == null)
            {
                throw new ArgumentNullException("interviewer");
            }

            return
                Client.DeleteAsync(InterviewersApi + interviewer.InterviewerId)
                      .FlattenExceptions();
        }
 public void Remove(INfieldConnection connection, Interviewer interviewer)
 {
     INfieldInterviewersService interviewersService = connection.GetService<INfieldInterviewersService>();
     interviewersService.RemoveAsync(interviewer).Wait();
 }