Search in sources :

Example 1 with NamespaceMetadataTransactionFactory

use of io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory in project nem2-sdk-java by nemtech.

the class NamespaceMetadataIntegrationTest method addMetadataToNamespace.

@ParameterizedTest
@EnumSource(RepositoryType.class)
public void addMetadataToNamespace(RepositoryType type) {
    String namespaceName = "namespace-for-metadata-integration-test-" + Double.valueOf(Math.floor(Math.random() * 10000)).intValue();
    NamespaceId targetNamespaceId = createRootNamespace(type, testAccount, namespaceName);
    System.out.println("Setting metadata " + targetNamespaceId.getIdAsHex());
    String message = "This is the message in the Namespace!";
    BigInteger key = BigInteger.TEN;
    Address targetAddress = testAccount.getAddress();
    MetadataTransactionService metadataTransactionService = new MetadataTransactionServiceImpl(getRepositoryFactory(type));
    Observable<NamespaceMetadataTransactionFactory> namespaceMetadataTransactionFactory = metadataTransactionService.createNamespaceMetadataTransactionFactory(targetAddress, key, message, targetAddress, targetNamespaceId);
    NamespaceMetadataTransaction transaction = get(namespaceMetadataTransactionFactory).build();
    AggregateTransaction aggregateTransaction = AggregateTransactionFactory.createComplete(getNetworkType(), getDeadline(), Collections.singletonList(transaction.toAggregate(testAccount.getPublicAccount()))).maxFee(maxFee).build();
    AggregateTransaction announceCorrectly = announceAndValidate(type, testAccount, aggregateTransaction);
    Assertions.assertEquals(testAccount.getPublicAccount(), announceCorrectly.getSigner().get());
    Assertions.assertEquals(1, announceCorrectly.getInnerTransactions().size());
    Assertions.assertEquals(transaction.getType(), announceCorrectly.getInnerTransactions().get(0).getType());
    NamespaceMetadataTransaction processedTransaction = (NamespaceMetadataTransaction) announceCorrectly.getInnerTransactions().get(0);
    // TODO problem comparing namespaces, sometime they are negative big integers
    Assertions.assertEquals(transaction.getTargetNamespaceId().getIdAsHex(), processedTransaction.getTargetNamespaceId().getIdAsHex());
    Assertions.assertEquals(transaction.getValueSizeDelta(), processedTransaction.getValueSizeDelta());
    Assertions.assertEquals(transaction.getScopedMetadataKey(), processedTransaction.getScopedMetadataKey());
    System.out.println("Metadata '" + message + "' stored!");
    sleep(3000);
    List<Metadata> metadata = get(getRepositoryFactory(type).createMetadataRepository().search(new MetadataSearchCriteria().targetId(targetNamespaceId).metadataType(MetadataType.NAMESPACE))).getData();
    assertMetadata(transaction, metadata);
    assertMetadata(transaction, get(getRepositoryFactory(type).createMetadataRepository().search(new MetadataSearchCriteria().targetId(targetNamespaceId).metadataType(MetadataType.NAMESPACE).scopedMetadataKey(key))).getData());
    assertMetadata(transaction, get(getRepositoryFactory(type).createMetadataRepository().search(new MetadataSearchCriteria().targetId(targetNamespaceId).metadataType(MetadataType.NAMESPACE).targetAddress(targetAddress).scopedMetadataKey(key))).getData());
    Assertions.assertEquals(message, processedTransaction.getValue());
}
Also used : NamespaceMetadataTransaction(io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransaction) MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) Address(io.nem.symbol.sdk.model.account.Address) AggregateTransaction(io.nem.symbol.sdk.model.transaction.AggregateTransaction) Metadata(io.nem.symbol.sdk.model.metadata.Metadata) MetadataTransactionService(io.nem.symbol.sdk.api.MetadataTransactionService) BigInteger(java.math.BigInteger) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) NamespaceMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with NamespaceMetadataTransactionFactory

use of io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory in project nem2-sdk-java by nemtech.

the class NamespaceMetadataTransactionMapper method createFactory.

@Override
protected NamespaceMetadataTransactionFactory createFactory(NetworkType networkType, Deadline deadline, NamespaceMetadataTransactionDTO transaction) {
    UnresolvedAddress targetAddress = MapperUtils.toUnresolvedAddress(transaction.getTargetAddress());
    Integer valueSizeDelta = transaction.getValueSizeDelta();
    BigInteger scopedMetaDataKey = MapperUtils.fromHexToBigInteger(transaction.getScopedMetadataKey());
    byte[] value = ConvertUtils.fromHexToBytes(transaction.getValue());
    NamespaceId targetNamespace = MapperUtils.toNamespaceId(transaction.getTargetNamespaceId());
    NamespaceMetadataTransactionFactory factory = NamespaceMetadataTransactionFactory.create(networkType, deadline, targetAddress, targetNamespace, scopedMetaDataKey, value);
    factory.valueSizeDelta(valueSizeDelta);
    Long valueSize = transaction.getValueSize();
    if (valueSize != null) {
        factory.valueSize(valueSize);
    }
    return factory;
}
Also used : BigInteger(java.math.BigInteger) UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) BigInteger(java.math.BigInteger) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) NamespaceMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory)

Example 3 with NamespaceMetadataTransactionFactory

use of io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory in project nem2-sdk-java by nemtech.

the class NamespaceMetadataTransactionMapper method createFactory.

@Override
protected NamespaceMetadataTransactionFactory createFactory(NetworkType networkType, Deadline deadline, NamespaceMetadataTransactionDTO transaction) {
    UnresolvedAddress targetAddress = MapperUtils.toUnresolvedAddress(transaction.getTargetAddress());
    Integer valueSizeDelta = transaction.getValueSizeDelta();
    BigInteger scopedMetaDataKey = MapperUtils.fromHexToBigInteger(transaction.getScopedMetadataKey());
    byte[] value = ConvertUtils.fromHexToBytes(transaction.getValue());
    NamespaceId targetNamespace = MapperUtils.toNamespaceId(transaction.getTargetNamespaceId());
    NamespaceMetadataTransactionFactory factory = NamespaceMetadataTransactionFactory.create(networkType, deadline, targetAddress, targetNamespace, scopedMetaDataKey, value);
    factory.valueSizeDelta(valueSizeDelta);
    Long valueSize = transaction.getValueSize();
    if (valueSize != null) {
        factory.valueSize(valueSize);
    }
    return factory;
}
Also used : BigInteger(java.math.BigInteger) UnresolvedAddress(io.nem.symbol.sdk.model.account.UnresolvedAddress) BigInteger(java.math.BigInteger) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) NamespaceMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory)

Example 4 with NamespaceMetadataTransactionFactory

use of io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory in project nem2-sdk-java by nemtech.

the class MetadataTransactionServiceTest method shouldCreateNamespaceMetadataTransactionFactory.

@Test
void shouldCreateNamespaceMetadataTransactionFactory() throws Exception {
    BigInteger metadataKey = BigInteger.valueOf(10);
    String oldValue = "The original Message";
    String newValue = "the new Message";
    Metadata metadata = new Metadata("someId", 1, "compositeHash", sourceAddress, targetAccount.getAddress(), metadataKey, MetadataType.NAMESPACE, StringEncoder.getBytes(oldValue), namespaceId.getIdAsHex());
    MetadataSearchCriteria criteria = new MetadataSearchCriteria().sourceAddress(sourceAddress).scopedMetadataKey(metadataKey).targetId(namespaceId).metadataType(MetadataType.NAMESPACE);
    Mockito.when(metadataRepositoryMock.search(Mockito.eq(criteria))).thenReturn(Observable.just(new Page<>(Collections.singletonList(metadata))));
    NamespaceMetadataTransactionFactory result = service.createNamespaceMetadataTransactionFactory(targetAccount.getAddress(), metadataKey, newValue, sourceAddress, namespaceId).toFuture().get();
    Assertions.assertEquals(metadataKey, result.getScopedMetadataKey());
    Assertions.assertNotEquals(oldValue, StringEncoder.getString(result.getValue()));
    Assertions.assertNotEquals(newValue, StringEncoder.getString(result.getValue()));
    Assertions.assertEquals(StringEncoder.getBytes(newValue).length - StringEncoder.getBytes(oldValue).length, result.getValueSizeDelta());
    Assertions.assertEquals(targetAccount.getAddress(), result.getTargetAddress());
    Assertions.assertEquals(namespaceId.getId(), result.getTargetNamespaceId().getId());
    Mockito.verify(metadataRepositoryMock).search(criteria);
}
Also used : MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) Metadata(io.nem.symbol.sdk.model.metadata.Metadata) BigInteger(java.math.BigInteger) Page(io.nem.symbol.sdk.api.Page) NamespaceMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory) Test(org.junit.jupiter.api.Test)

Example 5 with NamespaceMetadataTransactionFactory

use of io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory in project nem2-sdk-java by nemtech.

the class MetadataTransactionServiceTest method shouldCreateNamespaceMetadataTransactionFactoryWhenNotFound.

@Test
void shouldCreateNamespaceMetadataTransactionFactoryWhenNotFound() throws Exception {
    BigInteger metadataKey = BigInteger.valueOf(10);
    String newValue = "the new Message";
    MetadataSearchCriteria criteria = new MetadataSearchCriteria().sourceAddress(sourceAddress).scopedMetadataKey(metadataKey).targetId(namespaceId).metadataType(MetadataType.NAMESPACE);
    Mockito.when(metadataRepositoryMock.search(Mockito.eq(criteria))).thenReturn(Observable.just(new Page<>(Collections.emptyList())));
    NamespaceMetadataTransactionFactory result = service.createNamespaceMetadataTransactionFactory(targetAccount.getAddress(), metadataKey, newValue, sourceAddress, namespaceId).toFuture().get();
    Assertions.assertEquals(metadataKey, result.getScopedMetadataKey());
    Assertions.assertArrayEquals(StringEncoder.getBytes(newValue), result.getValue());
    Assertions.assertEquals(StringEncoder.getBytes(newValue).length, result.getValueSizeDelta());
    Assertions.assertEquals(targetAccount.getAddress(), result.getTargetAddress());
    Assertions.assertEquals(namespaceId.getId(), result.getTargetNamespaceId().getId());
    Mockito.verify(metadataRepositoryMock).search(criteria);
}
Also used : MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) BigInteger(java.math.BigInteger) Page(io.nem.symbol.sdk.api.Page) NamespaceMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory) Test(org.junit.jupiter.api.Test)

Aggregations

NamespaceMetadataTransactionFactory (io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory)5 BigInteger (java.math.BigInteger)5 MetadataSearchCriteria (io.nem.symbol.sdk.api.MetadataSearchCriteria)3 NamespaceId (io.nem.symbol.sdk.model.namespace.NamespaceId)3 Page (io.nem.symbol.sdk.api.Page)2 UnresolvedAddress (io.nem.symbol.sdk.model.account.UnresolvedAddress)2 Metadata (io.nem.symbol.sdk.model.metadata.Metadata)2 Test (org.junit.jupiter.api.Test)2 MetadataTransactionService (io.nem.symbol.sdk.api.MetadataTransactionService)1 Address (io.nem.symbol.sdk.model.account.Address)1 AggregateTransaction (io.nem.symbol.sdk.model.transaction.AggregateTransaction)1 NamespaceMetadataTransaction (io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransaction)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 EnumSource (org.junit.jupiter.params.provider.EnumSource)1