Search in sources :

Example 1 with TransactionBuilder

use of io.nem.symbol.catapult.builders.TransactionBuilder in project nem2-sdk-java by nemtech.

the class BinarySerializationImpl method deserializeToFactory.

/**
 * Deserialization of transactions. All the code related to the deserialization is handled in the
 * class and its helpers. Transaction Model Objects are not polluted with deserialization
 * functionality.
 *
 * @param payload the byte array payload
 * @return the {@link TransactionFactory}.
 */
@Override
public TransactionFactory<?> deserializeToFactory(byte[] payload) {
    Validate.notNull(payload, "Payload must not be null");
    DataInputStream stream = SerializationUtils.toDataInput(payload);
    TransactionBuilder builder = TransactionBuilderHelper.loadFromBinary(stream);
    return toTransactionFactory(builder);
}
Also used : EmbeddedTransactionBuilder(io.nem.symbol.catapult.builders.EmbeddedTransactionBuilder) TransactionBuilder(io.nem.symbol.catapult.builders.TransactionBuilder) DataInputStream(java.io.DataInputStream)

Example 2 with TransactionBuilder

use of io.nem.symbol.catapult.builders.TransactionBuilder 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 3 with TransactionBuilder

use of io.nem.symbol.catapult.builders.TransactionBuilder in project nem2-sdk-java by nemtech.

the class BinarySerializationImpl method serialize.

/**
 * Serialization basic implementation, it just delegates the work to the transactions.
 *
 * @param transaction the transaction
 * @return the serialized transaction.
 */
@Override
public <T extends Transaction> byte[] serialize(T transaction) {
    Validate.notNull(transaction, "Transaction must not be null");
    TransactionBuilder transactionBuilder = getTransactionBuilder(transaction);
    return serializeTransaction(transactionBuilder.serialize(), transaction);
}
Also used : EmbeddedTransactionBuilder(io.nem.symbol.catapult.builders.EmbeddedTransactionBuilder) TransactionBuilder(io.nem.symbol.catapult.builders.TransactionBuilder)

Aggregations

TransactionBuilder (io.nem.symbol.catapult.builders.TransactionBuilder)3 EmbeddedTransactionBuilder (io.nem.symbol.catapult.builders.EmbeddedTransactionBuilder)2 BinarySerialization (io.nem.symbol.sdk.api.BinarySerialization)1 BinarySerializationImpl (io.nem.symbol.sdk.infrastructure.BinarySerializationImpl)1 Transaction (io.nem.symbol.sdk.model.transaction.Transaction)1 TransactionInfoDTO (io.nem.symbol.sdk.openapi.okhttp_gson.model.TransactionInfoDTO)1 DataInputStream (java.io.DataInputStream)1 List (java.util.List)1 Map (java.util.Map)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1