use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class ConsensusHandlerTest method handleConsensusDoNothingOnBackendMessageTest.
@Test
public void handleConsensusDoNothingOnBackendMessageTest() throws DataHandlingException {
LAORepository mockLAORepository = mock(LAORepository.class);
Map<MessageID, MessageGeneral> messageById = new HashMap<>();
when(mockLAORepository.getMessageById()).thenReturn(messageById);
ConsensusPrepare prepare = new ConsensusPrepare(INSTANCE_ID, messageId, CREATION_TIME, 3);
ConsensusPromise promise = new ConsensusPromise(INSTANCE_ID, messageId, CREATION_TIME, 3, true, 2);
ConsensusPropose propose = new ConsensusPropose(INSTANCE_ID, messageId, CREATION_TIME, 3, true, Collections.emptyList());
ConsensusAccept accept = new ConsensusAccept(INSTANCE_ID, messageId, CREATION_TIME, 3, true);
messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, prepare));
messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, promise));
messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, propose));
messageHandler.handleMessage(mockLAORepository, messageSender, CONSENSUS_CHANNEL, getMsg(ORGANIZER_KEY, accept));
// The handlers for prepare/promise/propose/accept should do nothing (call or update nothing)
// because theses messages should only be handle in the backend server.
verify(mockLAORepository, never()).getLaoByChannel(any(Channel.class));
verify(mockLAORepository, never()).updateNodes(any(Channel.class));
}
use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class ElectionHandlerTest method setup.
@Before
public void setup() throws GeneralSecurityException, IOException {
lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
laoRepository = new LAORepository();
when(messageSender.subscribe(any())).then(args -> Completable.complete());
laoRepository = new LAORepository();
messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
// Create one LAO
lao = new Lao(CREATE_LAO.getName(), CREATE_LAO.getOrganizer(), CREATE_LAO.getCreation());
lao.setLastModified(lao.getCreation());
// Create one Roll Call and add it to the LAO
rollCall = new RollCall(lao.getId(), Instant.now().getEpochSecond(), "roll call 1");
lao.setRollCalls(new HashMap<String, RollCall>() {
{
put(rollCall.getId(), rollCall);
}
});
// Create one Election and add it to the LAO
election = new Election(lao.getId(), Instant.now().getEpochSecond(), "election 1");
election.setStart(Instant.now().getEpochSecond());
election.setEnd(Instant.now().getEpochSecond() + 20L);
election.setChannel(lao.getChannel().subChannel(election.getId()));
electionQuestion = new ElectionQuestion("question", "Plurality", false, Arrays.asList("a", "b"), election.getId());
election.setElectionQuestions(Collections.singletonList(electionQuestion));
lao.setElections(new HashMap<String, Election>() {
{
put(election.getId(), election);
}
});
// Add the LAO to the LAORepository
laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
laoRepository.setAllLaoSubject();
// Add the CreateLao message to the LAORepository
MessageGeneral createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
laoRepository.getMessageById().put(createLaoMessage.getMessageId(), createLaoMessage);
}
use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class ElectionHandlerTest method testHandleElectionEnd.
@Test
public void testHandleElectionEnd() throws DataHandlingException {
// Create the end Election message
ElectionEnd electionEnd = new ElectionEnd(election.getId(), lao.getId(), "");
MessageGeneral message = new MessageGeneral(SENDER_KEY, electionEnd, GSON);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL.subChannel(election.getId()), message);
// Check the Election is present with state CLOSED and the results
Optional<Election> electionOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getElection(election.getId());
assertTrue(electionOpt.isPresent());
assertEquals(EventState.CLOSED, electionOpt.get().getState());
}
use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class LaoHandlerTest method testHandleStateLao.
@Test
public void testHandleStateLao() throws DataHandlingException {
// Create the state LAO message
StateLao stateLao = new StateLao(CREATE_LAO.getId(), CREATE_LAO.getName(), CREATE_LAO.getCreation(), Instant.now().getEpochSecond(), CREATE_LAO.getOrganizer(), createLaoMessage.getMessageId(), new HashSet<>(), new ArrayList<>());
MessageGeneral message = new MessageGeneral(SENDER_KEY, stateLao, GSON);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
// Check the LAO last modification time and ID was updated
assertEquals((Long) stateLao.getLastModified(), laoRepository.getLaoByChannel(LAO_CHANNEL).getLastModified());
assertEquals(stateLao.getModificationId(), laoRepository.getLaoByChannel(LAO_CHANNEL).getModificationId());
}
use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.
the class ElectionStartFragmentTest method acceptButtonSendElectAcceptMessageTest.
@Test
public void acceptButtonSendElectAcceptMessageTest() throws DataHandlingException {
setupViewModel(PAST_TIME);
// Nodes 3 try to start
MessageGeneral elect3Msg = createMsg(node3KeyPair, elect);
messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, elect3Msg);
// We try to accept node3
nodesGrid().atPosition(node3Pos).perform(ViewActions.click());
ArgumentCaptor<ConsensusElectAccept> captor = ArgumentCaptor.forClass(ConsensusElectAccept.class);
Mockito.verify(messageSender).publish(eq(mainKeyPair), eq(consensusChannel), captor.capture());
ConsensusElectAccept electAccept = captor.getValue();
ConsensusElectAccept expectedElectAccept = new ConsensusElectAccept(INSTANCE_ID, elect3Msg.getMessageId(), true);
assertEquals(expectedElectAccept, electAccept);
}
Aggregations