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