Search in sources :

Example 1 with QuestionResult

use of com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult 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 2 with QuestionResult

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

the class ElectionResultPagerAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull Pager2ViewHolder holder, int position) {
    Election election = mLaoDetailViewModel.getCurrentElection();
    // setting the question
    ElectionQuestion electionQuestion = election.getElectionQuestions().get(position);
    String question = electionQuestion.getQuestion();
    holder.questionView.setText(question);
    List<QuestionResult> questionResults = election.getResultsForQuestionId(electionQuestion.getId());
    List<ElectionResultListAdapter.ElectionResult> electionResults = new ArrayList<>();
    for (int i = 0; i < questionResults.size(); i++) {
        electionResults.add(new ElectionResultListAdapter.ElectionResult(questionResults.get(i).getBallot(), questionResults.get(i).getCount()));
    }
    adapter.clear();
    adapter.addAll(electionResults);
    holder.resultListView.setAdapter(adapter);
}
Also used : ArrayList(java.util.ArrayList) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) Election(com.github.dedis.popstellar.model.objects.Election) QuestionResult(com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult)

Example 3 with QuestionResult

use of com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult 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 QuestionResult

use of com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult 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)

Example 5 with QuestionResult

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

the class ElectionHandlerTest method testHandleElectionResult.

@Test
public void testHandleElectionResult() throws DataHandlingException {
    // Create the result Election message
    QuestionResult questionResult = new QuestionResult(electionQuestion.getBallotOptions().get(0), 2);
    ElectionResultQuestion electionResultQuestion = new ElectionResultQuestion("id", Collections.singletonList(questionResult));
    ElectionResult electionResult = new ElectionResult(Collections.singletonList(electionResultQuestion));
    MessageGeneral message = new MessageGeneral(SENDER_KEY, electionResult, GSON);
    // Call the message handler
    messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL.subChannel(election.getId()), message);
    // Check the Election is present with state RESULTS_READY and the results
    Optional<Election> electionOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getElection(election.getId());
    assertTrue(electionOpt.isPresent());
    assertEquals(EventState.RESULTS_READY, electionOpt.get().getState());
    assertEquals(Collections.singletonList(questionResult), electionOpt.get().getResultsForQuestionId("id"));
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ElectionResult(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResult) ElectionResultQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion) Election(com.github.dedis.popstellar.model.objects.Election) QuestionResult(com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult) Test(org.junit.Test)

Aggregations

QuestionResult (com.github.dedis.popstellar.model.network.method.message.data.election.QuestionResult)5 ElectionResultQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Election (com.github.dedis.popstellar.model.objects.Election)2 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)1 ElectionQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion)1 ElectionResult (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResult)1