Search in sources :

Example 1 with ElectionSetup

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();
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Channel(com.github.dedis.popstellar.model.objects.Channel) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup)

Example 2 with ElectionSetup

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));
    }
}
Also used : Calendar(java.util.Calendar) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) LargeTest(androidx.test.filters.LargeTest) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest) Test(org.junit.Test)

Example 3 with ElectionSetup

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());
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Election(com.github.dedis.popstellar.model.objects.Election) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) ElectionHandler.electionSetupWitnessMessage(com.github.dedis.popstellar.utility.handler.data.ElectionHandler.electionSetupWitnessMessage) ElectionSetup(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup) Test(org.junit.Test)

Aggregations

ElectionSetup (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionSetup)3 Test (org.junit.Test)2 LargeTest (androidx.test.filters.LargeTest)1 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)1 ElectionQuestion (com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion)1 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)1 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)1 Channel (com.github.dedis.popstellar.model.objects.Channel)1 Election (com.github.dedis.popstellar.model.objects.Election)1 Lao (com.github.dedis.popstellar.model.objects.Lao)1 WitnessMessage (com.github.dedis.popstellar.model.objects.WitnessMessage)1 ElectionHandler.electionSetupWitnessMessage (com.github.dedis.popstellar.utility.handler.data.ElectionHandler.electionSetupWitnessMessage)1 HiltAndroidTest (dagger.hilt.android.testing.HiltAndroidTest)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 Disposable (io.reactivex.disposables.Disposable)1 Calendar (java.util.Calendar)1