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