Search in sources :

Example 11 with MetadataSearchCriteria

use of io.nem.symbol.sdk.api.MetadataSearchCriteria in project nem2-sdk-java by nemtech.

the class MetadataTransactionServiceTest method shouldNotCreateAccountMetadataTransactionFactoryWhenBug.

@Test
public void shouldNotCreateAccountMetadataTransactionFactoryWhenBug() {
    BigInteger metadataKey = BigInteger.valueOf(10);
    String newValue = "the new Message";
    IllegalArgumentException expectedException = new IllegalArgumentException("Some unexpected error");
    MetadataSearchCriteria criteria = new MetadataSearchCriteria().sourceAddress(sourceAddress).scopedMetadataKey(metadataKey).targetAddress(targetAccount.getAddress()).metadataType(MetadataType.ACCOUNT);
    Mockito.when(metadataRepositoryMock.search(Mockito.eq(criteria))).thenReturn(Observable.error(expectedException));
    IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> ExceptionUtils.propagate(() -> service.createAccountMetadataTransactionFactory(targetAccount.getAddress(), metadataKey, newValue, sourceAddress).toFuture().get()));
    Assertions.assertEquals(expectedException, exception);
    Mockito.verify(metadataRepositoryMock).search(Mockito.eq(criteria));
}
Also used : MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) BigInteger(java.math.BigInteger) Test(org.junit.jupiter.api.Test)

Example 12 with MetadataSearchCriteria

use of io.nem.symbol.sdk.api.MetadataSearchCriteria in project nem2-sdk-java by nemtech.

the class MetadataTransactionServiceTest method shouldCreateMosaicMetadataTransactionFactoryWhenNotFound.

@Test
void shouldCreateMosaicMetadataTransactionFactoryWhenNotFound() throws Exception {
    BigInteger metadataKey = BigInteger.valueOf(10);
    String newValue = "the new Message";
    MetadataSearchCriteria criteria = new MetadataSearchCriteria().sourceAddress(sourceAddress).scopedMetadataKey(metadataKey).targetId(mosaicId).metadataType(MetadataType.MOSAIC);
    Mockito.when(metadataRepositoryMock.search(Mockito.eq(criteria))).thenReturn(Observable.just(new Page<>(Collections.emptyList())));
    MosaicMetadataTransactionFactory result = service.createMosaicMetadataTransactionFactory(targetAccount.getAddress(), metadataKey, newValue, sourceAddress, mosaicId).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(mosaicId.getId(), result.getTargetMosaicId().getId());
    Mockito.verify(metadataRepositoryMock).search(Mockito.eq(criteria));
}
Also used : MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) BigInteger(java.math.BigInteger) Page(io.nem.symbol.sdk.api.Page) MosaicMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.MosaicMetadataTransactionFactory) Test(org.junit.jupiter.api.Test)

Example 13 with MetadataSearchCriteria

use of io.nem.symbol.sdk.api.MetadataSearchCriteria in project nem2-sdk-java by nemtech.

the class MetadataTransactionServiceTest method shouldNotCreateMosaicMetadataTransactionFactoryWhenAnyOtherRemoteException.

@Test
void shouldNotCreateMosaicMetadataTransactionFactoryWhenAnyOtherRemoteException() {
    BigInteger metadataKey = BigInteger.valueOf(10);
    String newValue = "the new Message";
    RepositoryCallException expectedException = new RepositoryCallException("Some other problem.", 500, null);
    MetadataSearchCriteria criteria = new MetadataSearchCriteria().sourceAddress(sourceAddress).scopedMetadataKey(metadataKey).targetId(mosaicId).metadataType(MetadataType.MOSAIC);
    Mockito.when(metadataRepositoryMock.search(Mockito.eq(criteria))).thenReturn(Observable.error(expectedException));
    RepositoryCallException exception = Assertions.assertThrows(RepositoryCallException.class, () -> ExceptionUtils.propagate(() -> service.createMosaicMetadataTransactionFactory(targetAccount.getAddress(), metadataKey, newValue, sourceAddress, mosaicId).toFuture().get()));
    Assertions.assertEquals(expectedException, exception);
    Mockito.verify(metadataRepositoryMock).search(criteria);
}
Also used : MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) RepositoryCallException(io.nem.symbol.sdk.api.RepositoryCallException) BigInteger(java.math.BigInteger) Test(org.junit.jupiter.api.Test)

Example 14 with MetadataSearchCriteria

use of io.nem.symbol.sdk.api.MetadataSearchCriteria in project nem2-sdk-java by nemtech.

the class MetadataTransactionServiceTest method shouldCreateAccountMetadataTransactionFactoryWhenNotFound.

@Test
void shouldCreateAccountMetadataTransactionFactoryWhenNotFound() throws Exception {
    BigInteger metadataKey = BigInteger.valueOf(10);
    String newValue = "the new Message";
    MetadataSearchCriteria criteria = new MetadataSearchCriteria().sourceAddress(sourceAddress).scopedMetadataKey(metadataKey).targetAddress(targetAccount.getAddress()).metadataType(MetadataType.ACCOUNT);
    Mockito.when(metadataRepositoryMock.search(Mockito.eq(criteria))).thenReturn(Observable.just(new Page<>(Collections.emptyList())));
    AccountMetadataTransactionFactory result = service.createAccountMetadataTransactionFactory(targetAccount.getAddress(), metadataKey, newValue, sourceAddress).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());
    Mockito.verify(metadataRepositoryMock).search(Mockito.eq(criteria));
}
Also used : MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) BigInteger(java.math.BigInteger) AccountMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.AccountMetadataTransactionFactory) Page(io.nem.symbol.sdk.api.Page) Test(org.junit.jupiter.api.Test)

Example 15 with MetadataSearchCriteria

use of io.nem.symbol.sdk.api.MetadataSearchCriteria in project nem2-sdk-java by nemtech.

the class MetadataTransactionServiceImpl method processMetadata.

/**
 * Generic way of processing a metadata entity and creating a new metadata transaction factory
 * depending on the existing metadata value. This works for Account, Mosaic and Namespace
 * metadata.
 *
 * @param criteria the criteria
 * @param transactionFactory the function that creates a transaction factory
 * @param newValue the new value you want to set.
 * @param <T> the type of the transaction factory.
 * @return an Observable of a transaction factory.
 */
private <T extends MetadataTransactionFactory> Observable<T> processMetadata(MetadataSearchCriteria criteria, TriFunction<byte[], NetworkType, Deadline, T> transactionFactory, String newValue) {
    return Observable.combineLatest(networkTypeObservable, epochAdjustmentObservable, (networkType, epochAdjustment) -> metadataRepository.search(criteria).map(page -> {
        Deadline deadline = Deadline.create(epochAdjustment);
        if (page.getData().isEmpty()) {
            return transactionFactory.apply(StringEncoder.getBytes(newValue), networkType, deadline);
        } else {
            byte[] originalValue = page.getData().get(0).getValue();
            byte[] newValueBytes = StringEncoder.getBytes(newValue);
            byte[] xoredBytes = ConvertUtils.xor(originalValue, newValueBytes);
            T factory = transactionFactory.apply(xoredBytes, networkType, deadline);
            factory.valueSizeDelta(newValueBytes.length - originalValue.length);
            return factory;
        }
    })).flatMap(f -> f);
}
Also used : UnresolvedMosaicId(io.nem.symbol.sdk.model.mosaic.UnresolvedMosaicId) NamespaceId(io.nem.symbol.sdk.model.namespace.NamespaceId) MetadataTransactionService(io.nem.symbol.sdk.api.MetadataTransactionService) MosaicMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.MosaicMetadataTransactionFactory) NamespaceMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory) ConvertUtils(io.nem.symbol.core.utils.ConvertUtils) AccountMetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.AccountMetadataTransactionFactory) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory) MetadataType(io.nem.symbol.sdk.model.metadata.MetadataType) Function(java.util.function.Function) StringEncoder(io.nem.symbol.core.utils.StringEncoder) MetadataRepository(io.nem.symbol.sdk.api.MetadataRepository) Objects(java.util.Objects) Deadline(io.nem.symbol.sdk.model.transaction.Deadline) MetadataTransactionFactory(io.nem.symbol.sdk.model.transaction.MetadataTransactionFactory) Duration(java.time.Duration) AliasService(io.nem.symbol.sdk.api.AliasService) MetadataSearchCriteria(io.nem.symbol.sdk.api.MetadataSearchCriteria) Observable(io.reactivex.Observable) BigInteger(java.math.BigInteger) Address(io.nem.symbol.sdk.model.account.Address) NetworkType(io.nem.symbol.sdk.model.network.NetworkType) Deadline(io.nem.symbol.sdk.model.transaction.Deadline)

Aggregations

MetadataSearchCriteria (io.nem.symbol.sdk.api.MetadataSearchCriteria)25 BigInteger (java.math.BigInteger)19 Test (org.junit.jupiter.api.Test)15 Metadata (io.nem.symbol.sdk.model.metadata.Metadata)14 Page (io.nem.symbol.sdk.api.Page)8 Address (io.nem.symbol.sdk.model.account.Address)5 MosaicMetadataTransactionFactory (io.nem.symbol.sdk.model.transaction.MosaicMetadataTransactionFactory)4 NamespaceMetadataTransactionFactory (io.nem.symbol.sdk.model.transaction.NamespaceMetadataTransactionFactory)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 MetadataRepository (io.nem.symbol.sdk.api.MetadataRepository)3 RepositoryCallException (io.nem.symbol.sdk.api.RepositoryCallException)3 MetadataType (io.nem.symbol.sdk.model.metadata.MetadataType)3 NamespaceId (io.nem.symbol.sdk.model.namespace.NamespaceId)3 AccountMetadataTransactionFactory (io.nem.symbol.sdk.model.transaction.AccountMetadataTransactionFactory)3 ConvertUtils (io.nem.symbol.core.utils.ConvertUtils)2 MetadataTransactionService (io.nem.symbol.sdk.api.MetadataTransactionService)2 RepositoryFactory (io.nem.symbol.sdk.api.RepositoryFactory)2 MosaicId (io.nem.symbol.sdk.model.mosaic.MosaicId)2 AggregateTransaction (io.nem.symbol.sdk.model.transaction.AggregateTransaction)2