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));
}
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);
}
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);
}
}
}
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));
}
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"));
}
Aggregations