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