Search in sources :

Example 1 with ElectionResultQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion in project popstellar by dedis.

the class ElectionHandler method handleElectionResult.

/**
 * Process an ElectionResult message.
 *
 * @param context the HandlerContext of the message
 * @param electionResult the message that was received
 */
public static void handleElectionResult(HandlerContext context, ElectionResult electionResult) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Log.d(TAG, "handling election result");
    Lao lao = laoRepository.getLaoByChannel(channel);
    Election election = laoRepository.getElectionByChannel(channel);
    List<ElectionResultQuestion> resultsQuestions = electionResult.getElectionQuestionResults();
    Log.d(TAG, "size of resultsQuestions is " + resultsQuestions.size());
    if (resultsQuestions.isEmpty())
        throw new DataHandlingException(electionResult, "the questions results is empty");
    election.setResults(resultsQuestions);
    election.setEventState(RESULTS_READY);
    lao.updateElection(election.getId(), election);
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Lao(com.github.dedis.popstellar.model.objects.Lao) ElectionResultQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion) DataHandlingException(com.github.dedis.popstellar.utility.error.DataHandlingException) Election(com.github.dedis.popstellar.model.objects.Election)

Example 2 with ElectionResultQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion in project popstellar by dedis.

the class ElectionQuestionResultTest method resultsCantBeEmpty.

@Test
public void resultsCantBeEmpty() {
    List<QuestionResult> emptyList = new ArrayList<>();
    assertThrows(IllegalArgumentException.class, () -> new ElectionResultQuestion(questionId, emptyList));
}
Also used : ArrayList(java.util.ArrayList) ElectionResultQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion) QuestionResult(com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult) Test(org.junit.Test)

Example 3 with ElectionResultQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion in project popstellar by dedis.

the class Election method setResults.

public void setResults(List<ElectionResultQuestion> electionResultsQuestions) {
    if (electionResultsQuestions == null) {
        throw new IllegalArgumentException("the list of winners should not be null");
    }
    for (ElectionResultQuestion resultQuestion : electionResultsQuestions) {
        List<QuestionResult> questionResults = resultQuestion.getResult();
        String questionId = resultQuestion.getId();
        if (questionResults == null) {
            results.put(questionId, new ArrayList<>());
        } else {
            questionResults.sort((r1, r2) -> r2.getCount().compareTo(r1.getCount()));
            this.results.put(questionId, questionResults);
        }
    }
}
Also used : ElectionResultQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion) QuestionResult(com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult)

Example 4 with ElectionResultQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion in project popstellar by dedis.

the class ElectionQuestionResultTest method fieldsCantBeNull.

@Test
public void fieldsCantBeNull() {
    assertThrows(IllegalArgumentException.class, () -> new ElectionResultQuestion(null, results));
    assertThrows(IllegalArgumentException.class, () -> new ElectionResultQuestion(questionId, null));
}
Also used : ElectionResultQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion) Test(org.junit.Test)

Example 5 with ElectionResultQuestion

use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion in project popstellar by dedis.

the class ElectionTest method resultsAreCorrectlySorted.

@Test
public void resultsAreCorrectlySorted() {
    List<QuestionResult> unsortedResults = new ArrayList<>();
    unsortedResults.add(new QuestionResult("Candidate1", 30));
    unsortedResults.add(new QuestionResult("Candidate2", 23));
    unsortedResults.add(new QuestionResult("Candidate3", 16));
    unsortedResults.add(new QuestionResult("Candidate4", 43));
    List<ElectionResultQuestion> resultQuestion = Collections.singletonList(new ElectionResultQuestion("question_id", unsortedResults));
    election.setResults(resultQuestion);
    List<QuestionResult> sortedResults = election.getResultsForQuestionId("question_id");
    QuestionResult firstResult = sortedResults.get(0);
    assertThat(firstResult.getBallot(), is("Candidate4"));
    assertThat(firstResult.getCount(), is(43));
    QuestionResult secondResult = sortedResults.get(1);
    assertThat(secondResult.getBallot(), is("Candidate1"));
    assertThat(secondResult.getCount(), is(30));
    QuestionResult thirdResult = sortedResults.get(2);
    assertThat(thirdResult.getBallot(), is("Candidate2"));
    assertThat(thirdResult.getCount(), is(23));
    QuestionResult fourthResult = sortedResults.get(3);
    assertThat(fourthResult.getBallot(), is("Candidate3"));
    assertThat(fourthResult.getCount(), is(16));
}
Also used : ArrayList(java.util.ArrayList) ElectionResultQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion) QuestionResult(com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult) Test(org.junit.Test)

Aggregations

ElectionResultQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion)6 QuestionResult (com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult)4 Test (org.junit.Test)4 Election (com.github.dedis.popstellar.model.objects.Election)2 ArrayList (java.util.ArrayList)2 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)1 ElectionResult (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResult)1 Channel (com.github.dedis.popstellar.model.objects.Channel)1 Lao (com.github.dedis.popstellar.model.objects.Lao)1 LAORepository (com.github.dedis.popstellar.repository.LAORepository)1 DataHandlingException (com.github.dedis.popstellar.utility.error.DataHandlingException)1