use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup in project popstellar by dedis.
the class LaoDetailViewModel method createNewElection.
/**
* Creates new Election event.
*
* <p>Publish a GeneralMessage containing ElectionSetup data.
*
* @param name the name of the election
* @param creation the creation time of the election
* @param start the start time of the election
* @param end the end time of the election
* @param votingMethod the type of voting method (e.g Plurality)
* @param ballotOptions the list of ballot options
* @param question the question associated to the election
* @return the id of the newly created election event, null if fails to create the event
*/
public String createNewElection(String name, long creation, long start, long end, List<String> votingMethod, List<Boolean> writeIn, List<List<String>> ballotOptions, List<String> question) {
Log.d(TAG, "creating a new election with name " + name);
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return null;
}
Channel channel = lao.getChannel();
ElectionSetup electionSetup = new ElectionSetup(name, creation, start, end, votingMethod, writeIn, ballotOptions, question, lao.getId());
Log.d(TAG, PUBLISH_MESSAGE);
Disposable disposable = networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, electionSetup).subscribe(() -> {
Log.d(TAG, "setup an election");
mElectionCreatedEvent.postValue(new SingleEvent<>(true));
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_create_election));
disposables.add(disposable);
return electionSetup.getId();
}
use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup in project popstellar by dedis.
the class ElectionSetupFragmentTest method multiplePluralityQuestionsTest.
@Test
public void multiplePluralityQuestionsTest() {
setupViewModel();
electionName().perform(click(), typeText(ELECTION_NAME), closeSoftKeyboard());
pickValidDateAndTime();
// Add Question 1, with 3 ballots options, no write in
questionText().perform(click(), typeText("Question 1"), closeSoftKeyboard());
addBallot().perform(click());
for (int i = 0; i < 3; ++i) {
ballotOptionAtPosition(i).perform(click(), typeText("answer 1." + i), closeSoftKeyboard());
}
// Add Question 2, with 2 ballots options, with write in
addQuestion().perform(click());
questionText().perform(click(), typeText("Question 2"), closeSoftKeyboard());
writeIn().perform(click());
for (int i = 0; i < 2; ++i) {
ballotOptionAtPosition(i).perform(click(), typeText("answer 2." + i), closeSoftKeyboard());
}
// Submit and intercept the ElectionSetup message
long minCreation = Instant.now().getEpochSecond();
submit().perform(click());
ElectionSetup electionSetup = getInterceptedElectionSetupMsg();
long maxCreation = Instant.now().getEpochSecond();
// Check that the creation time was when we submit the ElectionSetup
assertTrue(minCreation <= electionSetup.getCreation());
assertTrue(maxCreation >= electionSetup.getCreation());
// Check the start/end time
Calendar calendar = Calendar.getInstance();
calendar.set(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH, HOURS, MINUTES, 0);
long expectedStartTime = calendar.toInstant().getEpochSecond();
calendar.set(YEAR, MONTH_OF_YEAR, DAY_OF_MONTH, HOURS + 1, MINUTES, 0);
long expectedEndTime = calendar.toInstant().getEpochSecond();
assertEquals(expectedStartTime, electionSetup.getStartTime());
assertEquals(expectedEndTime, electionSetup.getEndTime());
assertEquals(ELECTION_NAME, electionSetup.getName());
assertEquals(LAO.getId(), electionSetup.getLao());
List<ElectionQuestion> questionList = electionSetup.getQuestions();
assertEquals(2, questionList.size());
ElectionQuestion question1 = questionList.get(0);
ElectionQuestion question2 = questionList.get(1);
// Check the Questions 1
assertEquals("Question 1", question1.getQuestion());
assertFalse(question1.getWriteIn());
List<String> ballotOptions1 = question1.getBallotOptions();
assertEquals(3, ballotOptions1.size());
for (int i = 0; i < 3; ++i) {
assertEquals("answer 1." + i, ballotOptions1.get(i));
}
// Check the Questions 2
assertEquals("Question 2", question2.getQuestion());
// assertTrue(question2.getWriteIn());
List<String> ballotOptions2 = question2.getBallotOptions();
assertEquals(2, ballotOptions2.size());
for (int i = 0; i < 2; ++i) {
assertEquals("answer 2." + i, ballotOptions2.get(i));
}
}
use of com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup in project popstellar by dedis.
the class ElectionHandlerTest method testHandleElectionSetup.
@Test
public void testHandleElectionSetup() throws DataHandlingException {
// Create the setup Election message
ElectionSetup electionSetup = new ElectionSetup("election 2", election.getCreation(), election.getStartTimestamp(), election.getEndTimestamp(), Collections.singletonList(electionQuestion.getVotingMethod()), Collections.singletonList(electionQuestion.getWriteIn()), Collections.singletonList(electionQuestion.getBallotOptions()), Collections.singletonList(electionQuestion.getQuestion()), lao.getId());
MessageGeneral message = new MessageGeneral(SENDER_KEY, electionSetup, GSON);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
// Check the Election is present with state OPENED and the correct ID
Optional<Election> electionOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getElection(electionSetup.getId());
assertTrue(electionOpt.isPresent());
assertEquals(EventState.OPENED, electionOpt.get().getState());
assertEquals(electionSetup.getId(), electionOpt.get().getId());
// Check the WitnessMessage has been created
Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
assertTrue(witnessMessage.isPresent());
// Check the Witness message contains the expected title and description
WitnessMessage expectedMessage = electionSetupWitnessMessage(message.getMessageId(), electionOpt.get());
assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
Aggregations