Exemplo n.º 1
0
        /// <summary>
        /// Handle a request to unregister a person.
        ///
        /// The method takes a PersonState object sent from
        /// a client application, which contains information
        /// about the person who should be registered.
        ///
        /// If the person does not exists or if the person
        /// already voted, the method will return false.
        /// Otherwise the method will return true meaning
        /// that the unregistration successed and that a log
        /// entity in the database was created.
        /// </summary>
        /// <param name="person">The person to be unregistrated.</param>
        /// <param name="clientName">The id of the client.</param>
        /// <returns>A boolean value determining if the request failed or successed.</returns>
        public bool UnregisterVoteRequestHandler(string clientName, Person person)
        {
            Contract.Requires(person != null);
            Contract.Ensures(!Contract.OldValue(VoterIdToPersonRequestHandler("", person.VoterId).Exists) || !Contract.OldValue(VoterIdToPersonRequestHandler("", person.VoterId).Voted) ? Contract.Result <bool>() == false : Contract.Result <bool>() == true);

            var personEntity = new PersonEntity();

            personEntity.Load(new Hashtable {
                { "id", person.DbId }
            });

            if (personEntity.Exists() && personEntity.Voted)
            {
                var log = new LogEntity {
                    PersonDbId   = personEntity.DbId,
                    Action       = "unregister",
                    Client       = clientName,
                    PollingTable = clientName,
                    Timestamp    = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds
                };

                log.Save();

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        public void TestPersonObject()
        {
            var person = new PersonEntity();

            person.Load(new Hashtable {
                { "id", 2 }
            });

            var a = person.ToObject();
            var b = person.ToObject();

            var c = person.ToObject();

            c.FirstName = "George";

            var d = c;

            Assert.That(a.Equals(b));
            Assert.That(a.Equals((object)b));

            Assert.False(c.Equals(b));
            Assert.False(c.Equals((object)b));

            Assert.That(c.Equals(d));
            Assert.That(c.Equals((object)d));

            Assert.False(c.Equals((Person)null));
            Assert.False(c.Equals((object)null));
            Assert.False(c.Equals(new PersonEntity()));


            Assert.That(a.GetHashCode() == -1070491939);

            Assert.That(a.ToString() == "2,0123456789,Christian,Olsson, Table of Fish, 08-12-2011 10:08:41, True");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Handle a request for a PersonState object by barcode.
        ///
        /// Takes a barcode number as parameter and constructs the
        /// proper Person entity. If the entity does not exists
        /// a PersonState object will still be passed back,
        /// but the Exist property of the object will return false.
        /// </summary>
        /// <param name="voterId">The barcode number to search for a person by.</param>
        /// <param name="clientName">The id of the client.</param>
        /// <returns>A PersonState object filled with information from the Person entity.</returns>
        public Person VoterIdToPersonRequestHandler(string clientName, int voterId)
        {
            var personEntity = new PersonEntity();

            personEntity.Load(new Hashtable {
                { "voter_id", voterId }
            });

            return(personEntity.ToObject());
        }
Exemplo n.º 4
0
        public void TestPersonAndLogInteractivity()
        {
            var person = new PersonEntity();

            person.Load(new Hashtable {
                { "id", 1 }
            });

            Assert.That(true);

            var logsBefore          = person.GetLogs();
            var mostRecentLogBefore = person.GetMostRecentLog();

            Assert.That(logsBefore.Count == 0);
            Assert.That(mostRecentLogBefore == null);

            var logA = new LogEntity {
                PersonDbId   = person.DbId,
                Action       = "register",
                Client       = "someotherclient 8",
                PollingTable = "8",
                Timestamp    = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds
            };

            logA.Save();

            Assert.That(logA.Exists());

            var logB = new LogEntity {
                PersonDbId   = person.DbId,
                Action       = "unregister",
                Client       = "someotherclient 8",
                PollingTable = "8",
                Timestamp    = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds + 1
            };

            logB.Save();

            Assert.That(logB.Exists());

            var logsAfter          = person.GetLogs();
            var mostRecentLogAfter = person.GetMostRecentLog();

            Assert.That(logsAfter.Count == 2);

            Assert.That(mostRecentLogAfter != null);
            Assert.That(mostRecentLogAfter.Exists());
            Assert.That(mostRecentLogAfter.Action == "unregister");

            logA.Delete();
            logB.Delete();

            Assert.That(!logA.Exists());
            Assert.That(!logB.Exists());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Handle a request for a PersonState object by CPR.
        ///
        /// Takes a CPR number as parameter and constructs the
        /// proper Person entity. If the entity does not exists
        /// a PersonState object will still be passed back,
        /// but the Exist property of the object will return false.
        /// </summary>
        /// <param name="cpr">The CPR number to search for a person by.</param>
        /// <param name="clientName">The id of the client.</param>
        /// <returns>A PersonState object filled with information from the Person entity.</returns>
        public Person CprToPersonRequestHandler(string clientName, string cpr)
        {
            Contract.Requires(cpr != null);

            var personEntity = new PersonEntity();

            personEntity.Load(new Hashtable {
                { "cpr", cpr }
            });

            return(personEntity.ToObject());
        }
Exemplo n.º 6
0
        public void TestUnregisterVoteRequestHandlerWitUnexistingPerson()
        {
            var personEntity = new PersonEntity();

            personEntity.Load(new Hashtable {
                { "id", 669 }
            });

            var person = personEntity.ToObject();

            Assert.That(!this.server.UnregisterVoteRequestHandler("test client", person));
        }
Exemplo n.º 7
0
        public void TestPersonCreationUpdatingAndDeletion()
        {
            var person = new PersonEntity();

            person.Firstname    = "Jan";
            person.Lastname     = "Aagaard Meier";
            person.Cpr          = "0123456789";
            person.VoterId      = 1337;
            person.PollingVenue = "ITU";
            person.PollingTable = "42";

            person.Save();

            person = new PersonEntity();
            person.Load(new Hashtable {
                { "polling_table", "42" }
            });

            Assert.That(person.Exists());
            Assert.That(person.Firstname == "Jan");
            Assert.That(person.Lastname == "Aagaard Meier");
            Assert.That(person.Cpr == "0123456789");
            Assert.That(person.VoterId == 1337);
            Assert.That(person.PollingVenue == "ITU");
            Assert.That(person.PollingTable == "42");

            person.Firstname = "Niels";
            person.VoterId   = 314;

            person.Save();

            person = new PersonEntity();
            person.Load(new Hashtable {
                { "polling_table", "42" }
            });

            Assert.That(person.Exists());
            Assert.That(person.Firstname == "Niels");
            Assert.That(person.Lastname == "Aagaard Meier");
            Assert.That(person.Cpr == "0123456789");
            Assert.That(person.VoterId == 314);
            Assert.That(person.PollingVenue == "ITU");
            Assert.That(person.PollingTable == "42");

            person.Delete();

            person = new PersonEntity();
            person.Load(new Hashtable {
                { "polling_table", "42" }
            });

            Assert.That(!person.Exists());
        }
Exemplo n.º 8
0
        public void TestUnregisterVoteRequestHandlerWithExistingVotePerson()
        {
            var personEntity = new PersonEntity();

            personEntity.Load(new Hashtable {
                { "id", 2 }
            });

            var person = personEntity.ToObject();

            Assert.That(this.server.UnregisterVoteRequestHandler("test client", person));

            var logEntity = personEntity.GetMostRecentLog();

            logEntity.Delete();
        }
Exemplo n.º 9
0
        public void TestLogResource()
        {
            var person = new PersonEntity();

            person.Load(new Hashtable {
                { "id", 2 }
            });

            var resource = new LogResource();

            resource.SetPerson(person);

            var result = resource.Build();

            Assert.That(resource.GetCount() == 1);
            Assert.That(resource.GetCountTotal() == 1);

            Assert.That(result[0].Action == "register");
        }