Search in sources :

Example 6 with BinarySerializationImpl

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

the class TransferTransactionTest method mosaicArrayToBeSorted.

@Test
void mosaicArrayToBeSorted() {
    ArrayList<Mosaic> mosaics = new ArrayList<>();
    mosaics.add(new Mosaic(new MosaicId(new BigInteger("200")), BigInteger.valueOf(1)));
    mosaics.add(new Mosaic(new MosaicId(new BigInteger("100")), BigInteger.valueOf(2)));
    TransferTransaction transaction = TransferTransactionFactory.create(networkType, new Deadline(BigInteger.ONE), new Address("SDZWZJUAYNOWGBTCUDBY3SE5JF4NCC2RDM6SIGQ", networkType), mosaics).message(new PlainMessage("")).build();
    assertEquals(mosaics.get(0).getId().getIdAsLong(), new BigInteger("200").longValue());
    assertEquals(mosaics.get(1).getId().getIdAsLong(), new BigInteger("100").longValue());
    TransferTransaction deserialized = (TransferTransaction) new BinarySerializationImpl().deserialize(transaction.serialize());
    assertEquals(mosaics.get(1).getId(), deserialized.getMosaics().get(0).getId());
    assertEquals(mosaics.get(0).getId(), deserialized.getMosaics().get(1).getId());
}
Also used : Address(io.nem.symbol.sdk.model.account.Address) PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) MosaicId(io.nem.symbol.sdk.model.mosaic.MosaicId) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) BinarySerializationImpl(io.nem.symbol.sdk.infrastructure.BinarySerializationImpl) Test(org.junit.jupiter.api.Test)

Example 7 with BinarySerializationImpl

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

the class TransferTransactionTest method serializeNamespaceTransaction.

@Test
void serializeNamespaceTransaction() {
    NamespaceId namespaceId = NamespaceId.createFromName("testaccount2");
    Assertions.assertEquals("E7CA7E22727DDD88", namespaceId.getIdAsHex());
    TransferTransaction transaction = TransferTransactionFactory.create(networkType, new Deadline(BigInteger.ONE), namespaceId, Collections.singletonList(Currency.CAT_CURRENCY.createAbsolute(BigInteger.ONE))).message(new PlainMessage("test-message")).build();
    byte[] payload = transaction.serialize();
    String payloadHex = ConvertUtils.toHex(payload);
    TransferTransaction newTransaction = (TransferTransaction) new BinarySerializationImpl().deserialize(payload);
    String newPayloadHex = ConvertUtils.toHex(newTransaction.serialize());
    Assertions.assertEquals(transaction.getRecipient().encoded(networkType), newTransaction.getRecipient().encoded(networkType));
    Assertions.assertEquals(payloadHex, newPayloadHex);
}
Also used : PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) BinarySerializationImpl(io.nem.symbol.sdk.infrastructure.BinarySerializationImpl) Test(org.junit.jupiter.api.Test)

Example 8 with BinarySerializationImpl

use of io.nem.symbol.sdk.infrastructure.BinarySerializationImpl 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 9 with BinarySerializationImpl

use of io.nem.symbol.sdk.infrastructure.BinarySerializationImpl 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)

Example 10 with BinarySerializationImpl

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

the class TransferTransactionTest method mosaicArrayToBeSortedHex.

@Test
void mosaicArrayToBeSortedHex() {
    ArrayList<Mosaic> mosaics = new ArrayList<>();
    mosaics.add(new Mosaic(new MosaicId("D525AD41D95FCF29"), BigInteger.valueOf(1)));
    mosaics.add(new Mosaic(new MosaicId("77A1969932D987D7"), BigInteger.valueOf(2)));
    mosaics.add(new Mosaic(new MosaicId("67F2B76F28BD36BA"), BigInteger.valueOf(3)));
    TransferTransaction transaction = TransferTransactionFactory.create(networkType, new Deadline(BigInteger.ONE), new Address("SDZWZJUAYNOWGBTCUDBY3SE5JF4NCC2RDM6SIGQ", networkType), mosaics).message(new PlainMessage("")).build();
    assertEquals("D525AD41D95FCF29", mosaics.get(0).getId().getIdAsHex().toUpperCase());
    assertEquals("77A1969932D987D7", mosaics.get(1).getId().getIdAsHex().toUpperCase());
    assertEquals("67F2B76F28BD36BA", mosaics.get(2).getId().getIdAsHex().toUpperCase());
    TransferTransaction deserialized = (TransferTransaction) new BinarySerializationImpl().deserialize(transaction.serialize());
    assertEquals(mosaics.get(2).getId(), deserialized.getMosaics().get(0).getId());
    assertEquals(mosaics.get(1).getId(), deserialized.getMosaics().get(1).getId());
    assertEquals(mosaics.get(0).getId(), deserialized.getMosaics().get(2).getId());
}
Also used : Address(io.nem.symbol.sdk.model.account.Address) PlainMessage(io.nem.symbol.sdk.model.message.PlainMessage) MosaicId(io.nem.symbol.sdk.model.mosaic.MosaicId) ArrayList(java.util.ArrayList) Mosaic(io.nem.symbol.sdk.model.mosaic.Mosaic) BinarySerializationImpl(io.nem.symbol.sdk.infrastructure.BinarySerializationImpl) Test(org.junit.jupiter.api.Test)

Aggregations

BinarySerializationImpl (io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)11 Test (org.junit.jupiter.api.Test)8 PlainMessage (io.nem.symbol.sdk.model.message.PlainMessage)6 Address (io.nem.symbol.sdk.model.account.Address)5 Mosaic (io.nem.symbol.sdk.model.mosaic.Mosaic)5 MosaicId (io.nem.symbol.sdk.model.mosaic.MosaicId)5 BinarySerialization (io.nem.symbol.sdk.api.BinarySerialization)4 BigInteger (java.math.BigInteger)4 NetworkType (io.nem.symbol.sdk.model.network.NetworkType)3 PublicAccount (io.nem.symbol.sdk.model.account.PublicAccount)2 Transaction (io.nem.symbol.sdk.model.transaction.Transaction)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)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 Hasher (io.nem.symbol.core.crypto.Hasher)1 KeyPair (io.nem.symbol.core.crypto.KeyPair)1 MerkleHashBuilder (io.nem.symbol.core.crypto.MerkleHashBuilder)1