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());
}
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);
}
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));
}
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"));
}
}
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());
}
Aggregations