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