Example #1
0
        public void CastVote_NoCitizen_Throws()
        {
            // Arrange
            A.CallTo(() => _repo.GetCitizen(CitizenId)).Returns(null);

            // Act
            var exception = Record.Exception(() => _service.CastVote(CitizenId, BallotItemId, ValidOption, null));

            // Assert
            Assert.NotNull(exception);
            Assert.IsType <RecordNotFoundException>(exception);
            Assert.Equal("Could not find that citizen", exception.Message);
            A.CallTo(() => _repo.GetCitizen(A <int> ._)).MustHaveHappenedOnceExactly();
            A.CallTo(() => _repo.GetBallotItem(A <int> ._)).MustNotHaveHappened();
            A.CallTo(() => _repo.IsCitizenEligibleToVoteOnBallotItem(A <int> ._, A <int> ._)).MustNotHaveHappened();
            A.CallTo(() => _repo.GetVote(A <int> ._, A <int> ._)).MustNotHaveHappened();
            A.CallTo(() => _repo.AddVote(A <int> ._, A <int> ._, A <int> ._, A <string> ._)).MustNotHaveHappened();
        }
Example #2
0
        public VoteConfirmation CastVote(int citizenId, int ballotItemId, int ballotItemOption, string writeIn = null)
        {
            // Verify that the citizen exists
            var citizen = _repo.GetCitizen(citizenId);

            if (citizen == null)
            {
                throw new RecordNotFoundException("Could not find that citizen");
            }

            // Verify that the ballot item exists
            var ballotItem = _repo.GetBallotItem(ballotItemId);

            if (ballotItem == null)
            {
                throw new RecordNotFoundException("Could not find that ballot item");
            }

            // Verify that the citizen is eligible to cast a vote on the ballot item
            var isEligible = _repo.IsCitizenEligibleToVoteOnBallotItem(citizenId, ballotItemId);

            if (!isEligible)
            {
                throw new IneligibleVoteException("That citizen is not eligible to vote on that ballot item");
            }

            // Verify that the citizen hasn't already cast a vote on the ballot item
            var vote = _repo.GetVote(citizenId, ballotItemId);

            if (vote != null)
            {
                throw new AlreadyVotedException("That citizen has already voted on that ballot item");
            }

            // Verify that the chosen option is valid
            if (ballotItemOption == 0)
            {
                if (!ballotItem.IsWriteInOptionAvailable)
                {
                    throw new InvalidVoteException("The write-in option is not available for that ballot item");
                }
                if (string.IsNullOrWhiteSpace(writeIn))
                {
                    throw new InvalidVoteException("The write-in value cannot be null or empty");
                }
            }
            else if (ballotItem.Options.All(option => option.BallotItemOptionId != ballotItemOption))
            {
                throw new InvalidVoteException("That option is not valid");
            }

            // Cast the vote
            var voteConfirmation = _repo.AddVote(citizenId, ballotItemId, ballotItemOption, writeIn);

            return(voteConfirmation);
        }
Example #3
0
 public CastVoteTests()
 {
     _repo = A.Fake <IVotingRepository>(options => options.Strict());
     A.CallTo(() => _repo.GetCitizen(CitizenId)).Returns(Citizen);
     A.CallTo(() => _repo.GetBallotItem(BallotItemId)).Returns(BallotItemWithoutWriteIn);
     A.CallTo(() => _repo.IsCitizenEligibleToVoteOnBallotItem(CitizenId, BallotItemId)).Returns(true);
     A.CallTo(() => _repo.GetVote(CitizenId, BallotItemId)).Returns(null);
     A.CallTo(() => _repo.AddVote(CitizenId, BallotItemId, A <int> ._, A <string> ._)).Returns(VoteConfirmation);
     _service = new VotingService(_repo);
 }