Search in sources :

Example 11 with ListenerSubscribeMessage

use of io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage in project nem2-sdk-java by nemtech.

the class ListenerVertxTest method subscribeToHash.

@ParameterizedTest
@ValueSource(strings = { "AGGREGATE_BONDED_REMOVED", "UNCONFIRMED_REMOVED" })
public void subscribeToHash(ListenerChannel channel) throws InterruptedException, ExecutionException, TimeoutException {
    String channelName = channel.toString();
    simulateWebSocketStartup();
    Address address = Address.generateRandom(NETWORK_TYPE);
    String hash = "someHash";
    List<String> hashes = new ArrayList<>();
    BiFunction<Address, String, Observable<String>> subscriber = channel == ListenerChannel.AGGREGATE_BONDED_REMOVED ? listener::aggregateBondedRemoved : listener::unconfirmedRemoved;
    subscriber.apply(address, hash).forEach(hashes::add);
    Map<String, Map<String, String>> message = new HashMap<>();
    Map<String, String> meta = new HashMap<>();
    meta.put("hash", hash);
    message.put("meta", meta);
    handle(message, channel.toString() + "/" + address.plain());
    Assertions.assertEquals(1, hashes.size());
    Assertions.assertEquals(hash, hashes.get(0));
    Mockito.verify(webSocketMock).handler(Mockito.any());
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
}
Also used : Address(io.nem.symbol.sdk.model.account.Address) UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) ListenerSubscribeMessage(io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage) Map(java.util.Map) HashMap(java.util.HashMap) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with ListenerSubscribeMessage

use of io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage in project nem2-sdk-java by nemtech.

the class ListenerVertxTest method subscribeValid.

@ParameterizedTest
@ValueSource(strings = { "CONFIRMED_ADDED", "AGGREGATE_BONDED_ADDED" })
public void subscribeValid(ListenerChannel channel) throws InterruptedException, ExecutionException, TimeoutException {
    simulateWebSocketStartup();
    TransactionInfoDTO transactionInfo = TestHelperVertx.loadTransactionInfoDTO("aggregateMosaicCreationTransaction.json");
    ObjectNode transactionInfoDtoJsonObject = jsonHelper.convert(transactionInfo, ObjectNode.class);
    Address address = Address.createFromPublicKey(jsonHelper.getString(transactionInfoDtoJsonObject, "transaction", "signerPublicKey"), NETWORK_TYPE);
    String channelName = channel.toString();
    List<Transaction> transactions = new ArrayList<>();
    List<Throwable> exceptions = new ArrayList<>();
    BiFunction<UnresolvedAddress, String, Observable<? extends Transaction>> subscriber = channel == ListenerChannel.CONFIRMED_ADDED ? listener::confirmedOrError : listener::aggregateBondedAddedOrError;
    TransactionMetaDTO meta = jsonHelper.convert(transactionInfo.getMeta(), TransactionMetaDTO.class);
    subscriber.apply(address, meta.getHash()).doOnError(exceptions::add).forEach(transactions::add);
    handle(transactionInfoDtoJsonObject, channelName + "/" + address.plain());
    Assertions.assertEquals(1, transactions.size());
    Assertions.assertEquals(0, exceptions.size());
    Assertions.assertEquals(address, transactions.get(0).getSigner().get().getAddress());
    Mockito.verify(webSocketMock).handler(Mockito.any());
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, "status" + "/" + address.plain())));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Address(io.nem.symbol.sdk.model.account.Address) UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) ListenerSubscribeMessage(io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage) Transaction(io.nem.symbol.sdk.model.transaction.Transaction) CosignatureSignedTransaction(io.nem.symbol.sdk.model.transaction.CosignatureSignedTransaction) TransactionInfoDTO(io.nem.symbol.sdk.openapi.vertx.model.TransactionInfoDTO) TransactionMetaDTO(io.nem.symbol.sdk.openapi.vertx.model.TransactionMetaDTO) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with ListenerSubscribeMessage

use of io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage in project nem2-sdk-java by nemtech.

the class ListenerVertxTest method subscribeValidUsingBase.

@ParameterizedTest
@ValueSource(strings = { "CONFIRMED_ADDED", "AGGREGATE_BONDED_ADDED" })
public void subscribeValidUsingBase(ListenerChannel channel) throws InterruptedException, ExecutionException, TimeoutException {
    simulateWebSocketStartup();
    TransactionInfoDTO transactionInfo = TestHelperVertx.loadTransactionInfoDTO("aggregateMosaicCreationTransaction.json");
    ObjectNode transactionInfoDtoJsonObject = jsonHelper.convert(transactionInfo, ObjectNode.class);
    Address address = Address.createFromPublicKey(jsonHelper.getString(transactionInfoDtoJsonObject, "transaction", "signerPublicKey"), NETWORK_TYPE);
    String channelName = channel.toString();
    List<ListenerMessage<Transaction>> messages = new ArrayList<>();
    List<Throwable> exceptions = new ArrayList<>();
    TransactionMetaDTO meta = jsonHelper.convert(transactionInfo.getMeta(), TransactionMetaDTO.class);
    listener.subscribe(new ListenerRequest<Transaction>(channel, address).transactionHashOrError(meta.getHash())).doOnError(exceptions::add).forEach(messages::add);
    handle(transactionInfoDtoJsonObject, channelName + "/" + address.plain());
    Assertions.assertEquals(1, messages.size());
    Assertions.assertEquals(0, exceptions.size());
    Assertions.assertEquals(meta.getHash(), messages.get(0).getTransactionHash());
    Assertions.assertEquals(channel, messages.get(0).getChannel());
    Assertions.assertEquals(address.plain(), messages.get(0).getChannelParams());
    Assertions.assertEquals(channelName + "/" + address.plain(), messages.get(0).getTopic());
    Assertions.assertEquals(address, messages.get(0).getMessage().getSigner().get().getAddress());
    Mockito.verify(webSocketMock).handler(Mockito.any());
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, "status" + "/" + address.plain())));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Address(io.nem.symbol.sdk.model.account.Address) UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) ArrayList(java.util.ArrayList) ListenerSubscribeMessage(io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage) TransactionInfoDTO(io.nem.symbol.sdk.openapi.vertx.model.TransactionInfoDTO) ListenerRequest(io.nem.symbol.sdk.infrastructure.ListenerRequest) ListenerMessage(io.nem.symbol.sdk.infrastructure.ListenerMessage) TransactionMetaDTO(io.nem.symbol.sdk.openapi.vertx.model.TransactionMetaDTO) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with ListenerSubscribeMessage

use of io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage in project nem2-sdk-java by nemtech.

the class ListenerVertxTest method subscribeOnError.

@ParameterizedTest
@ValueSource(strings = { "CONFIRMED_ADDED", "AGGREGATE_BONDED_ADDED" })
public void subscribeOnError(ListenerChannel channel) throws InterruptedException, ExecutionException, TimeoutException {
    simulateWebSocketStartup();
    TransactionInfoDTO transactionInfo = TestHelperVertx.loadTransactionInfoDTO("aggregateMosaicCreationTransaction.json");
    ObjectNode transactionInfoDtoJsonObject = jsonHelper.convert(transactionInfo, ObjectNode.class);
    Address address = Address.createFromPublicKey(jsonHelper.getString(transactionInfoDtoJsonObject, "transaction", "signerPublicKey"), NETWORK_TYPE);
    String channelName = channel.toString();
    Map<String, Object> transactionStatusError = new HashMap<>();
    transactionStatusError.put("address", address.encoded());
    transactionStatusError.put("code", "Fail 666");
    TransactionMetaDTO meta = jsonHelper.convert(transactionInfo.getMeta(), TransactionMetaDTO.class);
    transactionStatusError.put("hash", meta.getHash());
    transactionStatusError.put("deadline", 123);
    List<Transaction> transactions = new ArrayList<>();
    List<Throwable> exceptions = new ArrayList<>();
    BiFunction<Address, String, Observable<? extends Transaction>> subscriber = channel == ListenerChannel.CONFIRMED_ADDED ? listener::confirmedOrError : listener::aggregateBondedAddedOrError;
    subscriber.apply(address, meta.getHash()).doOnError(exceptions::add).forEach(transactions::add);
    handle(transactionStatusError, "status/" + address.plain());
    Assertions.assertEquals(0, transactions.size());
    Assertions.assertEquals(1, exceptions.size());
    Assertions.assertEquals(TransactionStatusException.class, exceptions.get(0).getClass());
    Assertions.assertEquals("Fail 666 processing transaction " + meta.getHash(), exceptions.get(0).getMessage());
    Mockito.verify(webSocketMock).handler(Mockito.any());
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, "status" + "/" + address.plain())));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Address(io.nem.symbol.sdk.model.account.Address) UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) ListenerSubscribeMessage(io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage) Transaction(io.nem.symbol.sdk.model.transaction.Transaction) CosignatureSignedTransaction(io.nem.symbol.sdk.model.transaction.CosignatureSignedTransaction) TransactionInfoDTO(io.nem.symbol.sdk.openapi.vertx.model.TransactionInfoDTO) TransactionMetaDTO(io.nem.symbol.sdk.openapi.vertx.model.TransactionMetaDTO) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with ListenerSubscribeMessage

use of io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage in project nem2-sdk-java by nemtech.

the class ListenerOkHttpTest method confirmedUsingHash.

@Test
public void confirmedUsingHash() throws InterruptedException, ExecutionException, TimeoutException {
    simulateWebSocketStartup();
    TransactionInfoDTO transactionInfo = TestHelperOkHttp.loadTransactionInfoDTO("aggregateMosaicCreationTransaction.json");
    JsonObject transactionInfoDtoJsonObject = jsonHelper.convert(transactionInfo, JsonObject.class);
    Address address = Address.createFromPublicKey(jsonHelper.getString(transactionInfoDtoJsonObject, "transaction", "signerPublicKey"), networkType);
    String channelName = ListenerChannel.CONFIRMED_ADDED.toString();
    List<Transaction> transactions = new ArrayList<>();
    List<Throwable> exceptions = new ArrayList<>();
    listener.confirmedOrError(address, getHash(transactionInfo)).doOnError(exceptions::add).forEach(transactions::add);
    handle(transactionInfoDtoJsonObject, channelName + "/" + address.plain());
    Assertions.assertEquals(1, transactions.size());
    Assertions.assertEquals(0, exceptions.size());
    final Address address1 = transactions.get(0).getSigner().get().getAddress();
    Assertions.assertEquals(address, address1);
    Mockito.verify(webSocketMock).send(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
    Mockito.verify(webSocketMock).send(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, "status" + "/" + address.plain())));
}
Also used : Address(io.nem.symbol.sdk.model.account.Address) Transaction(io.nem.symbol.sdk.model.transaction.Transaction) CosignatureSignedTransaction(io.nem.symbol.sdk.model.transaction.CosignatureSignedTransaction) TransactionInfoDTO(io.nem.symbol.sdk.openapi.okhttp_gson.model.TransactionInfoDTO) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ListenerSubscribeMessage(io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage) Test(org.junit.jupiter.api.Test)

Aggregations

ListenerSubscribeMessage (io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage)24 ArrayList (java.util.ArrayList)18 Address (io.nem.symbol.sdk.model.account.Address)15 Test (org.junit.jupiter.api.Test)14 CosignatureSignedTransaction (io.nem.symbol.sdk.model.transaction.CosignatureSignedTransaction)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 JsonObject (com.google.gson.JsonObject)10 Transaction (io.nem.symbol.sdk.model.transaction.Transaction)10 HashMap (java.util.HashMap)10 UnresolvedAddress (io.nem.symbol.sdk.model.account.UnresolvedAddress)9 ValueSource (org.junit.jupiter.params.provider.ValueSource)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 Observable (io.reactivex.Observable)7 TransactionInfoDTO (io.nem.symbol.sdk.openapi.okhttp_gson.model.TransactionInfoDTO)6 TransactionInfoDTO (io.nem.symbol.sdk.openapi.vertx.model.TransactionInfoDTO)5 TransactionMetaDTO (io.nem.symbol.sdk.openapi.vertx.model.TransactionMetaDTO)5 Account (io.nem.symbol.sdk.model.account.Account)4 TransactionStatusError (io.nem.symbol.sdk.model.transaction.TransactionStatusError)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Map (java.util.Map)3