Search in sources :

Example 11 with MessageGeneral

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

the class LAONetworkManagerTest method publishSendsRightMessage.

@Test
public void publishSendsRightMessage() {
    TestSchedulerProvider schedulerProvider = new TestSchedulerProvider();
    TestScheduler testScheduler = schedulerProvider.getTestScheduler();
    LAONetworkManager networkManager = new LAONetworkManager(laoRepository, handler, connection, JsonModule.provideGson(DataRegistryModule.provideDataRegistry()), schedulerProvider);
    Answer<?> answer = args -> {
        // Retrieve subscribe object
        Publish publish = args.getArgument(0);
        // Make sure the channel is correct
        assertEquals(CHANNEL, publish.getChannel());
        MessageGeneral messageGeneral = publish.getMessage();
        assertEquals(DATA, messageGeneral.getData());
        // Return a positive result
        messages.onNext(new Result(publish.getRequestId()));
        return null;
    };
    doAnswer(answer).when(connection).sendMessage(any(Publish.class));
    // Actual test
    Disposable disposable = networkManager.publish(KEY_PAIR, CHANNEL, DATA).subscribe();
    testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
    disposable.dispose();
    networkManager.dispose();
    verify(connection).sendMessage(any(Publish.class));
    verify(connection, atLeastOnce()).observeMessage();
    verify(connection).observeConnectionEvents();
    verify(connection).close();
    verifyNoMoreInteractions(connection);
}
Also used : TestSchedulerProvider(com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Publish(com.github.dedis.popstellar.model.network.method.Publish) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) BehaviorSubject(io.reactivex.subjects.BehaviorSubject) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSchedulerProvider(com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Unsubscribe(com.github.dedis.popstellar.model.network.method.Unsubscribe) DataRegistryModule(com.github.dedis.popstellar.di.DataRegistryModule) HashSet(java.util.HashSet) WebSocket(com.tinder.scarlet.WebSocket) Answer(org.mockito.stubbing.Answer) GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Error(com.github.dedis.popstellar.model.network.answer.Error) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ErrorCode(com.github.dedis.popstellar.model.network.answer.ErrorCode) JsonModule(com.github.dedis.popstellar.di.JsonModule) Before(org.junit.Before) Channel(com.github.dedis.popstellar.model.objects.Channel) Result(com.github.dedis.popstellar.model.network.answer.Result) Catchup(com.github.dedis.popstellar.model.network.method.Catchup) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) Base64DataUtils(com.github.dedis.popstellar.testutils.Base64DataUtils) Query(com.github.dedis.popstellar.model.network.method.Query) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) MessageHandler(com.github.dedis.popstellar.utility.handler.MessageHandler) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Disposable(io.reactivex.disposables.Disposable) TestScheduler(io.reactivex.schedulers.TestScheduler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Disposable(io.reactivex.disposables.Disposable) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) TestScheduler(io.reactivex.schedulers.TestScheduler) Publish(com.github.dedis.popstellar.model.network.method.Publish) Result(com.github.dedis.popstellar.model.network.answer.Result) Test(org.junit.Test)

Example 12 with MessageGeneral

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

the class ElectionStartFragmentTest method failureTest.

@Test
public void failureTest() throws DataHandlingException {
    setupViewModel(PAST_TIME);
    // Nodes 3 try to start and failed
    MessageGeneral elect3Msg = createMsg(node3KeyPair, elect);
    messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, elect3Msg);
    ConsensusFailure failure3 = new ConsensusFailure(INSTANCE_ID, elect3Msg.getMessageId(), PAST_TIME);
    MessageGeneral failure3Msg = createMsg(node3KeyPair, failure3);
    messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, failure3Msg);
    nodeAssertions(nodesGrid(), node3Pos, "Start Failed\n" + node3, false);
    // We try to start and failed
    MessageGeneral elect1Msg = createMsg(mainKeyPair, elect);
    messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, elect1Msg);
    ConsensusFailure failure1 = new ConsensusFailure(INSTANCE_ID, elect1Msg.getMessageId(), PAST_TIME);
    MessageGeneral failure1Msg = createMsg(mainKeyPair, failure1);
    messageHandler.handleMessage(laoRepository, messageSender, consensusChannel, failure1Msg);
    displayAssertions(STATUS_READY, START_START, true);
    nodeAssertions(nodesGrid(), ownPos, "Start Failed\n" + publicKey, false);
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ConsensusFailure(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusFailure) HiltAndroidTest(dagger.hilt.android.testing.HiltAndroidTest) Test(org.junit.Test)

Example 13 with MessageGeneral

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

the class RollCallHandlerTest method testHandleOpenRollCall.

@Test
public void testHandleOpenRollCall() throws DataHandlingException {
    // Create the open Roll Call message
    OpenRollCall openRollCall = new OpenRollCall(CREATE_LAO.getId(), rollCall.getId(), rollCall.getStart(), EventState.CREATED);
    MessageGeneral message = new MessageGeneral(SENDER_KEY, openRollCall, GSON);
    // Call the message handler
    messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
    // Check the Roll Call is present with state OPENED and the correct ID
    Optional<RollCall> rollCallOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getRollCall(openRollCall.getUpdateId());
    assertTrue(rollCallOpt.isPresent());
    assertEquals(EventState.OPENED, rollCallOpt.get().getState());
    assertEquals(openRollCall.getUpdateId(), rollCallOpt.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 = openRollCallWitnessMessage(message.getMessageId(), rollCallOpt.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) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) RollCallHandler.closeRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.closeRollCallWitnessMessage) RollCallHandler.createRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.createRollCallWitnessMessage) RollCallHandler.openRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.openRollCallWitnessMessage) Test(org.junit.Test)

Example 14 with MessageGeneral

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

the class LaoDetailViewModel method updateLaoName.

/**
 * Method to update the name of a Lao by sending an updateLao msg and a stateLao msg to the
 * backend
 */
public void updateLaoName() {
    Log.d(TAG, "Updating lao name to " + mLaoName.getValue());
    Lao lao = getCurrentLaoValue();
    Channel channel = lao.getChannel();
    KeyPair mainKey = keyManager.getMainKeyPair();
    long now = Instant.now().getEpochSecond();
    UpdateLao updateLao = new UpdateLao(mainKey.getPublicKey(), lao.getCreation(), mLaoName.getValue(), now, lao.getWitnesses());
    MessageGeneral msg = new MessageGeneral(mainKey, updateLao, gson);
    Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> {
        Log.d(TAG, "updated lao name");
        dispatchLaoUpdate("lao name", updateLao, lao, channel, msg);
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_update_lao));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) 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)

Example 15 with MessageGeneral

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

the class LaoDetailViewModel method updateLaoWitnesses.

/**
 * Method to update the list of witnesses of a Lao by sending an updateLao msg and a stateLao msg
 * to the backend
 */
public void updateLaoWitnesses() {
    Log.d(TAG, "Updating lao witnesses ");
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = lao.getChannel();
    KeyPair mainKey = keyManager.getMainKeyPair();
    long now = Instant.now().getEpochSecond();
    UpdateLao updateLao = new UpdateLao(mainKey.getPublicKey(), lao.getCreation(), lao.getName(), now, witnesses);
    MessageGeneral msg = new MessageGeneral(mainKey, updateLao, gson);
    Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> {
        Log.d(TAG, "updated lao witnesses");
        dispatchLaoUpdate("lao state with new witnesses", updateLao, lao, channel, msg);
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_update_lao));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) 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)

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