Search in sources :

Example 6 with BinarySerialization

use of io.nem.symbol.sdk.api.BinarySerialization in project nem2-sdk-java by nemtech.

the class TransactionMapperSerializationVertxTest method testDtoToModelMapping.

@ParameterizedTest
@MethodSource("transactionJsonFiles")
void testDtoToModelMapping(String jsonFilename) {
    String json = TestHelperVertx.loadResource(jsonFilename);
    TransactionInfoDTO originalTransactionInfo = jsonHelper.parse(json, TransactionInfoDTO.class);
    Transaction transactionModel = transactionMapper.mapFromDto(originalTransactionInfo);
    Assertions.assertNotNull(transactionModel);
    TransactionInfoDTO mappedTransactionInfo = (TransactionInfoDTO) transactionMapper.mapToDto(transactionModel);
    Map<String, Object> transactionMap = jsonHelper.convert(mappedTransactionInfo.getTransaction(), Map.class);
    Map<String, Object> originalTransactionMap = jsonHelper.convert(originalTransactionInfo.getTransaction(), Map.class);
    originalTransactionMap.put("size", transactionModel.getSize());
    originalTransactionInfo.setTransaction(originalTransactionMap);
    // Patching the sort
    mappedTransactionInfo.setTransaction(transactionMap);
    mappedTransactionInfo.setMeta(jsonHelper.convert(mappedTransactionInfo.getMeta(), Map.class));
    Assertions.assertEquals(jsonHelper.prettyPrint(originalTransactionInfo), jsonHelper.prettyPrint(mappedTransactionInfo));
    BinarySerialization serialization = new BinarySerializationImpl();
    Transaction transaction = transactionMapper.mapFromDto(mappedTransactionInfo);
    byte[] serialize1 = serialization.serialize(transactionModel);
    byte[] serialize2 = serialization.serialize(transaction);
    Assertions.assertEquals(ConvertUtils.toHex(serialize1), ConvertUtils.toHex(serialize2));
    removeMeta(originalTransactionInfo);
    byte[] serialize3 = serialization.serialize(transactionModel);
    Assertions.assertEquals(ConvertUtils.toHex(serialize1), ConvertUtils.toHex(serialize3));
    Transaction deserialized3 = serialization.deserialize(serialize3);
    TransactionInfoDTO deserializedTransaction = (TransactionInfoDTO) transactionMapper.mapToDto(deserialized3, false);
    deserializedTransaction.setTransaction(jsonHelper.convert(deserializedTransaction.getTransaction(), Map.class));
    removeMeta(deserializedTransaction);
    Assertions.assertEquals(jsonHelper.prettyPrint(originalTransactionInfo), jsonHelper.prettyPrint(deserializedTransaction));
}
Also used : BinarySerialization(io.nem.symbol.sdk.api.BinarySerialization) Transaction(io.nem.symbol.sdk.model.transaction.Transaction) TransactionInfoDTO(io.nem.symbol.sdk.openapi.vertx.model.TransactionInfoDTO) Map(java.util.Map) BinarySerializationImpl(io.nem.symbol.sdk.infrastructure.BinarySerializationImpl) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with BinarySerialization

use of io.nem.symbol.sdk.api.BinarySerialization in project nem2-sdk-java by nemtech.

the class OkHttpAggregateTransactionTest method serialization.

@Test
@DisplayName("Serialization")
void serialization() {
    Address address = Address.generateRandom(networkType);
    TransferTransaction transferTx = TransferTransactionFactory.create(networkType, deadline, address, Collections.singletonList(createAbsolute(BigInteger.valueOf(10000000)))).message(new PlainMessage("")).build();
    PublicAccount signer = Account.generateNewAccount(networkType).getPublicAccount();
    AggregateTransaction aggregateTx = AggregateTransactionFactory.createComplete(networkType, deadline, Collections.singletonList(transferTx.toAggregate(signer))).build();
    byte[] actual = aggregateTx.serialize();
    BinarySerialization serialization = BinarySerializationImpl.INSTANCE;
    AggregateTransaction deserialized = (AggregateTransaction) serialization.deserialize(actual);
    assertEquals(signer, deserialized.getInnerTransactions().get(0).getSigner().get());
}
Also used : BinarySerialization(io.nem.symbol.sdk.api.BinarySerialization) Address(io.nem.symbol.sdk.model.account.Address) PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) AggregateTransaction(io.nem.symbol.sdk.model.transaction.AggregateTransaction) PublicAccount(io.nem.symbol.sdk.model.account.PublicAccount) TransferTransaction(io.nem.symbol.sdk.model.transaction.TransferTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 8 with BinarySerialization

use of io.nem.symbol.sdk.api.BinarySerialization in project nem2-sdk-java by nemtech.

the class TransactionMapperSerializationOkHttpTest method testDtoToModelMapping.

@ParameterizedTest
@MethodSource("transactionJsonFiles")
void testDtoToModelMapping(String jsonFilename) {
    String json = TestHelperOkHttp.loadResource(jsonFilename);
    TransactionInfoDTO originalTransactionInfo = jsonHelper.parse(json, TransactionInfoDTO.class);
    Transaction transactionModel = transactionMapper.mapFromDto(originalTransactionInfo);
    Integer version = jsonHelper.getInteger(originalTransactionInfo, "transaction", "version");
    Integer type = jsonHelper.getInteger(originalTransactionInfo, "transaction", "type");
    Integer network = jsonHelper.getInteger(originalTransactionInfo, "transaction", "network");
    Assertions.assertEquals(transactionModel.getVersion(), version);
    Assertions.assertEquals(transactionModel.getType().getValue(), type);
    Assertions.assertEquals(transactionModel.getNetworkType().getValue(), network);
    Assertions.assertNotNull(transactionModel);
    TransactionInfoDTO mappedTransactionInfo = (TransactionInfoDTO) transactionMapper.mapToDto(transactionModel);
    Map<String, Object> transactionMap = jsonHelper.convert(mappedTransactionInfo.getTransaction(), Map.class);
    Map<String, Object> originalTransactionMap = jsonHelper.convert(originalTransactionInfo.getTransaction(), Map.class);
    originalTransactionMap.put("size", transactionModel.getSize());
    originalTransactionInfo.setTransaction(originalTransactionMap);
    // Patching the sort
    mappedTransactionInfo.setTransaction(transactionMap);
    mappedTransactionInfo.setMeta(jsonHelper.convert(mappedTransactionInfo.getMeta(), Map.class));
    BinarySerialization serialization = new BinarySerializationImpl();
    String serialized = ConvertUtils.toHex(serialization.serialize(transactionModel));
    TransactionBuilder builder = TransactionBuilderHelper.loadFromBinary(SerializationUtils.toDataInput(serialization.serialize(transactionModel)));
    System.out.println(ConvertUtils.toHex(builder.serialize()));
    serialization.deserialize(serialization.serialize(transactionModel));
    Assertions.assertEquals(jsonHelper.prettyPrint(originalTransactionInfo), jsonHelper.prettyPrint(mappedTransactionInfo));
    Assertions.assertEquals(serialized, ConvertUtils.toHex(serialization.serialize(transactionMapper.mapFromDto(mappedTransactionInfo))));
    originalTransactionInfo.setMeta(null);
    Map<String, Object> transactionJson = (Map<String, Object>) originalTransactionInfo.getTransaction();
    if (transactionJson.containsKey("transactions")) {
        List<Map<String, Object>> transactionsJson = (List<Map<String, Object>>) transactionJson.get("transactions");
        transactionsJson.forEach(t -> t.remove("meta"));
    }
}
Also used : BinarySerialization(io.nem.symbol.sdk.api.BinarySerialization) Transaction(io.nem.symbol.sdk.model.transaction.Transaction) TransactionInfoDTO(io.nem.symbol.sdk.openapi.okhttp_gson.model.TransactionInfoDTO) TransactionBuilder(io.nem.symbol.catapult.builders.TransactionBuilder) List(java.util.List) Map(java.util.Map) BinarySerializationImpl(io.nem.symbol.sdk.infrastructure.BinarySerializationImpl) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

BinarySerialization (io.nem.symbol.sdk.api.BinarySerialization)8 Test (org.junit.jupiter.api.Test)6 BinarySerializationImpl (io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)4 Address (io.nem.symbol.sdk.model.account.Address)4 PublicAccount (io.nem.symbol.sdk.model.account.PublicAccount)4 PlainMessage (io.nem.symbol.sdk.model.message.PlainMessage)4 AggregateTransaction (io.nem.symbol.sdk.model.transaction.AggregateTransaction)4 TransferTransaction (io.nem.symbol.sdk.model.transaction.TransferTransaction)4 Account (io.nem.symbol.sdk.model.account.Account)2 Deadline (io.nem.symbol.sdk.model.transaction.Deadline)2 SignedTransaction (io.nem.symbol.sdk.model.transaction.SignedTransaction)2 Transaction (io.nem.symbol.sdk.model.transaction.Transaction)2 Map (java.util.Map)2 DisplayName (org.junit.jupiter.api.DisplayName)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 AggregateBondedTransactionBuilder (io.nem.symbol.catapult.builders.AggregateBondedTransactionBuilder)1 TransactionBuilder (io.nem.symbol.catapult.builders.TransactionBuilder)1 KeyPair (io.nem.symbol.core.crypto.KeyPair)1 PersistentHarvestingDelegationMessage (io.nem.symbol.sdk.model.message.PersistentHarvestingDelegationMessage)1