Search in sources :

Example 26 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class ConsensusHandlerTest method handleConsensusWithInvalidMessageIdTest.

@Test
public void handleConsensusWithInvalidMessageIdTest() {
    // When an invalid instance id is used in handler for elect_accept and learn,
    // it should throw an InvalidMessageIdException
    ConsensusElectAccept electAcceptInvalid = new ConsensusElectAccept(INSTANCE_ID, INVALID_MSG_ID, true);
    ConsensusLearn learnInvalid = new ConsensusLearn(INSTANCE_ID, INVALID_MSG_ID, CREATION_TIME, true, Collections.emptyList());
    ConsensusFailure failureInvalid = new ConsensusFailure(INSTANCE_ID, INVALID_MSG_ID, CREATION_TIME);
    MessageGeneral electAcceptInvalidMsg = getMsg(ORGANIZER_KEY, electAcceptInvalid);
    MessageGeneral learnInvalidMsg = getMsg(ORGANIZER_KEY, learnInvalid);
    MessageGeneral failureMsg = getMsg(ORGANIZER_KEY, failureInvalid);
    assertThrows(InvalidMessageIdException.class, () -> messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, electAcceptInvalidMsg));
    assertThrows(InvalidMessageIdException.class, () -> messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, learnInvalidMsg));
    assertThrows(InvalidMessageIdException.class, () -> messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, failureMsg));
}
Also used : ConsensusElectAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ConsensusLearn(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusLearn) ConsensusFailure(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusFailure) Test(org.junit.Test)

Example 27 with MessageGeneral

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

Example 28 with MessageGeneral

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

Example 29 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class LaoHandlerTest method testHandleUpdateLao.

@Test
public void testHandleUpdateLao() throws DataHandlingException {
    // Create the update LAO message
    UpdateLao updateLao = new UpdateLao(SENDER, CREATE_LAO.getCreation(), "new name", Instant.now().getEpochSecond(), new HashSet<>());
    MessageGeneral message = new MessageGeneral(SENDER_KEY, updateLao, GSON);
    // Create the expected WitnessMessage
    WitnessMessage expectedMessage = updateLaoNameWitnessMessage(message.getMessageId(), updateLao, lao);
    // Call the message handler
    messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
    // Check the WitnessMessage has been created
    Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
    assertTrue(witnessMessage.isPresent());
    assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
    assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
Also used : UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) LaoHandler.updateLaoNameWitnessMessage(com.github.dedis.popstellar.utility.handler.data.LaoHandler.updateLaoNameWitnessMessage) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) Test(org.junit.Test)

Example 30 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class LaoHandlerTest method setup.

@Before
public void setup() throws GeneralSecurityException, IOException {
    lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
    lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
    when(messageSender.subscribe(any())).then(args -> Completable.complete());
    laoRepository = new LAORepository();
    messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
    // Create one LAO and add it to the LAORepository
    lao = new Lao(CREATE_LAO.getName(), CREATE_LAO.getOrganizer(), CREATE_LAO.getCreation());
    lao.setLastModified(lao.getCreation());
    laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
    laoRepository.setAllLaoSubject();
    // Add the CreateLao message to the LAORepository
    createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
    laoRepository.getMessageById().put(createLaoMessage.getMessageId(), createLaoMessage);
}
Also used : LAOState(com.github.dedis.popstellar.repository.LAOState) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) LAORepository(com.github.dedis.popstellar.repository.LAORepository) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) Before(org.junit.Before)

Aggregations

MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)33 Test (org.junit.Test)18 Lao (com.github.dedis.popstellar.model.objects.Lao)10 Channel (com.github.dedis.popstellar.model.objects.Channel)8 LAORepository (com.github.dedis.popstellar.repository.LAORepository)8 Disposable (io.reactivex.disposables.Disposable)7 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)6 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)6 RollCall (com.github.dedis.popstellar.model.objects.RollCall)6 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)6 Before (org.junit.Before)6 ConsensusElectAccept (com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept)5 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)5 Election (com.github.dedis.popstellar.model.objects.Election)5 WitnessMessage (com.github.dedis.popstellar.model.objects.WitnessMessage)5 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)5 LAOState (com.github.dedis.popstellar.repository.LAOState)5 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)4 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)4 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)4