Search in sources :

Example 16 with ListenerSubscribeMessage

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

the class ListenerOkHttpTest method aggregateBondedAddedRaisingError.

@Test
public void aggregateBondedAddedRaisingError() 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.AGGREGATE_BONDED_ADDED.toString();
    Map<String, Object> transactionStatusError = new HashMap<>();
    transactionStatusError.put("address", address.encoded());
    transactionStatusError.put("code", "Fail 666");
    transactionStatusError.put("hash", getHash(transactionInfo));
    transactionStatusError.put("deadline", 123);
    List<Transaction> transactions = new ArrayList<>();
    List<Throwable> exceptions = new ArrayList<>();
    listener.aggregateBondedAddedOrError(address, getHash(transactionInfo)).doOnError(exceptions::add).forEach(transactions::add);
    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())));
    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 " + getHash(transactionInfo), exceptions.get(0).getMessage());
}
Also used : Address(io.nem.symbol.sdk.model.account.Address) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) 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.okhttp_gson.model.TransactionInfoDTO) JsonObject(com.google.gson.JsonObject) Test(org.junit.jupiter.api.Test)

Example 17 with ListenerSubscribeMessage

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

the class ListenerOkHttpTest method cosignatureAdded.

@Test
public void cosignatureAdded() throws InterruptedException, ExecutionException, TimeoutException {
    simulateWebSocketStartup();
    Cosignature cosignature = new Cosignature().parentHash("aParentHash").signature("aSignature").version(BigInteger.ONE).signerPublicKey(Account.generateNewAccount(networkType).getPublicKey());
    JsonObject transactionInfoDtoJsonObject = jsonHelper.convert(cosignature, JsonObject.class);
    Address address = Address.createFromPublicKey(cosignature.getSignerPublicKey(), networkType);
    String channelName = ListenerChannel.COSIGNATURE.toString();
    List<CosignatureSignedTransaction> transactions = new ArrayList<>();
    listener.cosignatureAdded(address).forEach(transactions::add);
    handle(transactionInfoDtoJsonObject, channelName + "/" + address.plain());
    Assertions.assertEquals(1, transactions.size());
    Assertions.assertEquals(cosignature.getSignerPublicKey(), transactions.get(0).getSigner().getPublicKey().toHex());
    Assertions.assertEquals(cosignature.getParentHash(), transactions.get(0).getParentHash());
    Assertions.assertEquals(cosignature.getSignature(), transactions.get(0).getSignature());
    Mockito.verify(webSocketMock).send(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + address.plain())));
}
Also used : CosignatureSignedTransaction(io.nem.symbol.sdk.model.transaction.CosignatureSignedTransaction) Address(io.nem.symbol.sdk.model.account.Address) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Cosignature(io.nem.symbol.sdk.openapi.okhttp_gson.model.Cosignature) ListenerSubscribeMessage(io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage) Test(org.junit.jupiter.api.Test)

Example 18 with ListenerSubscribeMessage

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

the class ListenerOkHttpTest method shouldHandleStatus.

@Test
public void shouldHandleStatus() throws InterruptedException, ExecutionException, TimeoutException {
    Account account1 = Account.generateNewAccount(networkType);
    AtomicReference<TransactionStatusError> reference = new AtomicReference<>();
    simulateWebSocketStartup();
    Assertions.assertNotNull(listener.status(account1.getAddress()).subscribe(reference::set));
    Map<String, Object> message = new HashMap<>();
    message.put("hash", "1234hash");
    message.put("address", account1.getAddress().encoded());
    message.put("code", "some error");
    message.put("deadline", 5555);
    handle(message, "status/" + account1.getAddress().plain());
    Assertions.assertNotNull(reference.get());
    Assertions.assertEquals(message.get("hash"), reference.get().getHash());
    Assertions.assertEquals(message.get("code"), reference.get().getStatus());
    Assertions.assertEquals(account1.getAddress(), reference.get().getAddress());
    Mockito.verify(webSocketMock).send(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, "status" + "/" + account1.getAddress().plain())));
}
Also used : Account(io.nem.symbol.sdk.model.account.Account) TransactionStatusError(io.nem.symbol.sdk.model.transaction.TransactionStatusError) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) JsonObject(com.google.gson.JsonObject) ListenerSubscribeMessage(io.nem.symbol.sdk.infrastructure.ListenerSubscribeMessage) Test(org.junit.jupiter.api.Test)

Example 19 with ListenerSubscribeMessage

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

the class ListenerVertxTest method subscribeAlias.

@ParameterizedTest
@ValueSource(strings = { "CONFIRMED_ADDED", "AGGREGATE_BONDED_ADDED", "UNCONFIRMED_ADDED" })
public void subscribeAlias(ListenerChannel channel) throws InterruptedException, ExecutionException, TimeoutException {
    String channelName = channel.toString();
    simulateWebSocketStartup();
    TransactionInfoDTO transactionInfo = TestHelperVertx.loadTransactionInfoDTO("transferEmptyMessage.json");
    NamespaceId namespaceId = NamespaceId.createFromName("alias");
    ObjectNode transactionInfoDtoJsonObject = jsonHelper.convert(transactionInfo, ObjectNode.class);
    List<Transaction> transactions = new ArrayList<>();
    BiFunction<UnresolvedAddress, String, Observable<? extends Transaction>> subscriber = channel == ListenerChannel.CONFIRMED_ADDED ? listener::confirmed : channel == ListenerChannel.UNCONFIRMED_ADDED ? listener::unconfirmedAdded : listener::aggregateBondedAdded;
    TransactionMetaDTO meta = jsonHelper.convert(transactionInfo.getMeta(), TransactionMetaDTO.class);
    subscriber.apply(namespaceId, meta.getHash()).forEach(transactions::add);
    handle(transactionInfoDtoJsonObject, channelName + "/" + namespaceId.plain());
    Assertions.assertEquals(1, transactions.size());
    Mockito.verify(webSocketMock).handler(Mockito.any());
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + namespaceId.plain())));
    Mockito.verify(webSocketMock).writeTextMessage(jsonHelper.print(new ListenerSubscribeMessage(this.wsId, channelName + "/" + namespaceId.plain())));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) 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) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) TransactionMetaDTO(io.nem.symbol.sdk.openapi.vertx.model.TransactionMetaDTO) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with ListenerSubscribeMessage

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

the class ListenerVertxTest method subscribeToHashWhenInvalidHash.

@ParameterizedTest
@ValueSource(strings = { "AGGREGATE_BONDED_REMOVED", "UNCONFIRMED_REMOVED" })
public void subscribeToHashWhenInvalidHash(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", "invalidHash");
    message.put("meta", meta);
    handle(message, channelName + "/" + address.plain());
    Assertions.assertEquals(0, hashes.size());
    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)

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