Search in sources :

Example 1 with RestrictionMosaicRepository

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

the class MosaicAddressRestrictionIntegrationTest method createMosaicAddressRestrictionAndValidateEndpoints.

@ParameterizedTest
@EnumSource(RepositoryType.class)
void createMosaicAddressRestrictionAndValidateEndpoints(RepositoryType type) {
    Account testAccount = helper().createTestAccount(type);
    Account testAccount2 = config().getTestAccount2();
    // 1) Create a mosaic
    MosaicId mosaicId = createMosaic(type, testAccount);
    BigInteger restrictionKey = BigInteger.valueOf(22222);
    // 2) Create a global restriction on the mosaic
    MosaicGlobalRestrictionTransaction mosaicGlobalRestrictionTransaction = MosaicGlobalRestrictionTransactionFactory.create(getNetworkType(), getDeadline(), mosaicId, restrictionKey, BigInteger.valueOf(20), MosaicRestrictionType.GE).maxFee(maxFee).build();
    announceAndValidate(type, testAccount, mosaicGlobalRestrictionTransaction);
    sleep(1000);
    // 3)Create a new MosaicAddressRestrictionTransaction
    BigInteger originalRestrictionValue = BigInteger.valueOf(30);
    Address targetAddress = testAccount2.getAddress();
    MosaicAddressRestrictionTransaction createTransaction = MosaicAddressRestrictionTransactionFactory.create(getNetworkType(), getDeadline(), mosaicId, restrictionKey, targetAddress, originalRestrictionValue).maxFee(maxFee).build();
    // 4)Announce and validate
    MosaicAddressRestrictionTransaction announce1 = announceAggregateAndValidate(type, createTransaction, testAccount).getLeft();
    sleep(1000);
    assertTransaction(restrictionKey, createTransaction, announce1);
    // 5) Validate that endpoints have the data.
    sleep(1000);
    RestrictionMosaicRepository restrictionRepository = getRepositoryFactory(type).createRestrictionMosaicRepository();
    assertMosaicAddressRestriction(restrictionRepository, targetAddress, createTransaction, targetAddress, mosaicId);
    // 6) Update the restriction
    MosaicAddressRestrictionTransaction updateTransaction = MosaicAddressRestrictionTransactionFactory.create(getNetworkType(), getDeadline(), mosaicId, restrictionKey, targetAddress, BigInteger.valueOf(40)).previousRestrictionValue(originalRestrictionValue).maxFee(maxFee).build();
    // 7) Announce and validate.
    MosaicAddressRestrictionTransaction announced = announceAggregateAndValidate(type, updateTransaction, testAccount).getLeft();
    sleep(1000);
    assertTransaction(restrictionKey, updateTransaction, announced);
    assertMosaicAddressRestriction(restrictionRepository, targetAddress, updateTransaction, targetAddress, mosaicId);
}
Also used : Account(io.nem.symbol.sdk.model.account.Account) MosaicGlobalRestrictionTransaction(io.nem.symbol.sdk.model.transaction.MosaicGlobalRestrictionTransaction) Address(io.nem.symbol.sdk.model.account.Address) MosaicAddressRestrictionTransaction(io.nem.symbol.sdk.model.transaction.MosaicAddressRestrictionTransaction) MosaicId(io.nem.symbol.sdk.model.mosaic.MosaicId) BigInteger(java.math.BigInteger) RestrictionMosaicRepository(io.nem.symbol.sdk.api.RestrictionMosaicRepository) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with RestrictionMosaicRepository

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

the class MosaicAddressRestrictionIntegrationTest method createMultiAddressRestrictions.

@ParameterizedTest
@EnumSource(RepositoryType.class)
void createMultiAddressRestrictions(RepositoryType type) {
    Account testAccount = helper().createTestAccount(type);
    Account testAccount2 = config().getTestAccount2();
    Account testAccount3 = config().getTestAccount3();
    // 1) Create a mosaic
    MosaicId mosaicId = createMosaic(type, testAccount);
    BigInteger restrictionKey1 = BigInteger.valueOf(11);
    BigInteger restrictionKey2 = BigInteger.valueOf(22);
    // 2) Create a global restriction on the mosaic
    MosaicGlobalRestrictionTransaction mosaicGlobalRestrictionTransaction1 = MosaicGlobalRestrictionTransactionFactory.create(getNetworkType(), getDeadline(), mosaicId, restrictionKey1, BigInteger.valueOf(20), MosaicRestrictionType.GE).maxFee(maxFee).build();
    // 2) Create a global restriction on the mosaic
    MosaicGlobalRestrictionTransaction mosaicGlobalRestrictionTransaction2 = MosaicGlobalRestrictionTransactionFactory.create(getNetworkType(), getDeadline(), mosaicId, restrictionKey2, BigInteger.valueOf(10), MosaicRestrictionType.GT).maxFee(maxFee).build();
    announceAndValidate(type, testAccount, mosaicGlobalRestrictionTransaction1);
    announceAndValidate(type, testAccount, mosaicGlobalRestrictionTransaction2);
    sleep(1000);
    // 3)Create a new MosaicAddressRestrictionTransaction
    MosaicAddressRestrictionTransaction createTransaction1 = MosaicAddressRestrictionTransactionFactory.create(getNetworkType(), getDeadline(), mosaicId, restrictionKey1, testAccount2.getAddress(), BigInteger.valueOf(30)).maxFee(maxFee).build();
    MosaicAddressRestrictionTransaction createTransaction2 = MosaicAddressRestrictionTransactionFactory.create(getNetworkType(), getDeadline(), mosaicId, restrictionKey1, testAccount3.getAddress(), BigInteger.valueOf(20)).maxFee(maxFee).build();
    MosaicAddressRestrictionTransaction createTransaction3 = MosaicAddressRestrictionTransactionFactory.create(getNetworkType(), getDeadline(), mosaicId, restrictionKey2, testAccount3.getAddress(), BigInteger.valueOf(70)).maxFee(maxFee).build();
    announceAndValidate(type, testAccount, createTransaction1);
    announceAndValidate(type, testAccount, createTransaction2);
    announceAndValidate(type, testAccount, createTransaction3);
    sleep(1000);
    RestrictionMosaicRepository restrictionRepository = getRepositoryFactory(type).createRestrictionMosaicRepository();
    PaginationStreamer<MosaicRestriction<?>, MosaicRestrictionSearchCriteria> streamer = restrictionRepository.streamer();
    List<MosaicRestriction<?>> restrictions = get(streamer.search(new MosaicRestrictionSearchCriteria().targetAddress(testAccount.getAddress())).toList().toObservable());
    Assertions.assertEquals(1, restrictions.size());
}
Also used : Account(io.nem.symbol.sdk.model.account.Account) MosaicGlobalRestrictionTransaction(io.nem.symbol.sdk.model.transaction.MosaicGlobalRestrictionTransaction) MosaicAddressRestrictionTransaction(io.nem.symbol.sdk.model.transaction.MosaicAddressRestrictionTransaction) MosaicId(io.nem.symbol.sdk.model.mosaic.MosaicId) BigInteger(java.math.BigInteger) RestrictionMosaicRepository(io.nem.symbol.sdk.api.RestrictionMosaicRepository) MosaicRestriction(io.nem.symbol.sdk.model.restriction.MosaicRestriction) MosaicRestrictionSearchCriteria(io.nem.symbol.sdk.api.MosaicRestrictionSearchCriteria) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with RestrictionMosaicRepository

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

the class RestrictionMosaicRepositoryIntegrationTest method searchAddressRestriction.

@ParameterizedTest
@EnumSource(RepositoryType.class)
void searchAddressRestriction(RepositoryType type) {
    RepositoryFactory repositoryFactory = getRepositoryFactory(type);
    RestrictionMosaicRepository repository = repositoryFactory.createRestrictionMosaicRepository();
    MosaicRestrictionEntryType entryType = MosaicRestrictionEntryType.ADDRESS;
    Page<MosaicRestriction<?>> page = get(repository.search(new MosaicRestrictionSearchCriteria().entryType(entryType)));
    System.out.println(page.getData().size());
    page.getData().forEach(restriction -> {
        Assertions.assertTrue(restriction instanceof MosaicAddressRestriction);
        Assertions.assertEquals(entryType, restriction.getEntryType());
        Assertions.assertNotNull(restriction.getMosaicId());
        Assertions.assertNotNull(restriction.getCompositeHash());
        Assertions.assertFalse(restriction.getRestrictions().isEmpty());
        Assertions.assertNotNull(((MosaicAddressRestriction) restriction).getTargetAddress());
    });
}
Also used : MosaicRestrictionEntryType(io.nem.symbol.sdk.model.restriction.MosaicRestrictionEntryType) MosaicAddressRestriction(io.nem.symbol.sdk.model.restriction.MosaicAddressRestriction) RestrictionMosaicRepository(io.nem.symbol.sdk.api.RestrictionMosaicRepository) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory) MosaicRestriction(io.nem.symbol.sdk.model.restriction.MosaicRestriction) MosaicRestrictionSearchCriteria(io.nem.symbol.sdk.api.MosaicRestrictionSearchCriteria) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with RestrictionMosaicRepository

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

the class MosaicRestrictionTransactionServiceTest method setup.

@BeforeEach
void setup() {
    RepositoryFactory factory = mock(RepositoryFactory.class);
    restrictionMosaicRepository = mock(RestrictionMosaicRepository.class);
    when(factory.createRestrictionMosaicRepository()).thenReturn(restrictionMosaicRepository);
    namespaceRepository = mock(NamespaceRepository.class);
    when(factory.createNamespaceRepository()).thenReturn(namespaceRepository);
    when(factory.getNetworkType()).thenReturn(Observable.just(networkType));
    when(factory.getEpochAdjustment()).thenReturn(Observable.just(Duration.ofSeconds(10)));
    service = new MosaicRestrictionTransactionServiceImpl(factory);
    when(restrictionMosaicRepository.search(eq(new MosaicRestrictionSearchCriteria().pageNumber(1).mosaicId(mosaicId1).entryType(MosaicRestrictionEntryType.GLOBAL)))).thenReturn(Observable.just(toPage(mockGlobalRestriction())));
    when(restrictionMosaicRepository.search(eq(new MosaicRestrictionSearchCriteria().pageNumber(1).mosaicId(mosaicId2).entryType(MosaicRestrictionEntryType.GLOBAL)))).thenReturn(Observable.just(toPage(mockGlobalRestriction())));
    when(restrictionMosaicRepository.search(eq(new MosaicRestrictionSearchCriteria().pageNumber(1).mosaicId(mosaicIdWrongKey).entryType(MosaicRestrictionEntryType.GLOBAL)))).thenReturn(Observable.error(() -> new IllegalStateException("Not a nice mosaic id")));
    when(restrictionMosaicRepository.search(eq(new MosaicRestrictionSearchCriteria().pageNumber(1).mosaicId(mosaicIdNotFound).entryType(MosaicRestrictionEntryType.GLOBAL)))).thenReturn(Observable.just(toPage()));
    when(restrictionMosaicRepository.search(eq(new MosaicRestrictionSearchCriteria().pageNumber(1).mosaicId(mosaicId1).targetAddress(account1.getAddress()).entryType(MosaicRestrictionEntryType.ADDRESS)))).thenReturn(Observable.just(toPage(mockAddressRestriction())));
    when(restrictionMosaicRepository.search(eq(new MosaicRestrictionSearchCriteria().pageNumber(1).mosaicId(mosaicId2).targetAddress(account1.getAddress()).entryType(MosaicRestrictionEntryType.ADDRESS)))).thenReturn(Observable.just(toPage()));
    when(restrictionMosaicRepository.search(eq(new MosaicRestrictionSearchCriteria().pageNumber(1).mosaicId(mosaicIdWrongKey).targetAddress(account1.getAddress()).entryType(MosaicRestrictionEntryType.ADDRESS)))).thenReturn(Observable.error(() -> new IllegalStateException("Not a nice mosaic id")));
    when(restrictionMosaicRepository.search(eq(new MosaicRestrictionSearchCriteria().pageNumber(1).mosaicId(mosaicIdNotFound).targetAddress(account1.getAddress()).entryType(MosaicRestrictionEntryType.ADDRESS)))).thenReturn(Observable.just(toPage()));
    when(namespaceRepository.getNamespace(Mockito.any())).thenReturn(Observable.error(new IllegalStateException("Alias does not exist")));
    when(namespaceRepository.getNamespace(accountAlias1)).thenReturn(Observable.just(createAlias(account1.getAddress())));
    when(namespaceRepository.getNamespace(accountAlias2)).thenReturn(Observable.just(createAlias(account2.getAddress())));
    when(namespaceRepository.getNamespace(mosaicAlias1)).thenReturn(Observable.just(createAlias(mosaicId1)));
    when(namespaceRepository.getNamespace(mosaicAlias2)).thenReturn(Observable.just(createAlias(mosaicId2)));
}
Also used : NamespaceRepository(io.nem.symbol.sdk.api.NamespaceRepository) RestrictionMosaicRepository(io.nem.symbol.sdk.api.RestrictionMosaicRepository) RepositoryFactory(io.nem.symbol.sdk.api.RepositoryFactory) MosaicRestrictionSearchCriteria(io.nem.symbol.sdk.api.MosaicRestrictionSearchCriteria) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with RestrictionMosaicRepository

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

the class StateProofServiceTest method mosaicRestriction.

@Test
void mosaicRestriction() throws Exception {
    RestrictionMosaicRepository repository = mock(RestrictionMosaicRepository.class);
    when(factory.createRestrictionMosaicRepository()).thenReturn(repository);
    String id = "hash";
    MosaicRestriction<?> state = Mockito.mock(MosaicRestriction.class);
    when(state.getCompositeHash()).thenReturn(id);
    when(state.serialize()).thenReturn(ConvertUtils.fromHexToBytes(serialized));
    when(repository.getMosaicRestrictions(eq(id))).thenReturn(Observable.just(state));
    when(repository.getMosaicRestrictionsMerkle(eq(id))).thenReturn(Observable.just(tree));
    StateMerkleProof<MosaicRestriction<?>> proof = service.mosaicRestriction(id).toFuture().get();
    Assertions.assertTrue(proof.isValid());
    Assertions.assertEquals(state, proof.getState());
}
Also used : RestrictionMosaicRepository(io.nem.symbol.sdk.api.RestrictionMosaicRepository) MosaicRestriction(io.nem.symbol.sdk.model.restriction.MosaicRestriction) Test(org.junit.jupiter.api.Test)

Aggregations

RestrictionMosaicRepository (io.nem.symbol.sdk.api.RestrictionMosaicRepository)13 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 EnumSource (org.junit.jupiter.params.provider.EnumSource)10 MosaicId (io.nem.symbol.sdk.model.mosaic.MosaicId)8 MosaicRestrictionSearchCriteria (io.nem.symbol.sdk.api.MosaicRestrictionSearchCriteria)7 MosaicRestriction (io.nem.symbol.sdk.model.restriction.MosaicRestriction)6 MosaicGlobalRestrictionTransaction (io.nem.symbol.sdk.model.transaction.MosaicGlobalRestrictionTransaction)6 BigInteger (java.math.BigInteger)6 RepositoryFactory (io.nem.symbol.sdk.api.RepositoryFactory)4 MosaicAddressRestrictionTransaction (io.nem.symbol.sdk.model.transaction.MosaicAddressRestrictionTransaction)4 MosaicRestrictionType (io.nem.symbol.sdk.model.transaction.MosaicRestrictionType)4 MosaicRestrictionTransactionService (io.nem.symbol.sdk.api.MosaicRestrictionTransactionService)3 Address (io.nem.symbol.sdk.model.account.Address)3 Account (io.nem.symbol.sdk.model.account.Account)2 NamespaceId (io.nem.symbol.sdk.model.namespace.NamespaceId)2 MosaicRestrictionEntryType (io.nem.symbol.sdk.model.restriction.MosaicRestrictionEntryType)2 NamespaceRepository (io.nem.symbol.sdk.api.NamespaceRepository)1 MosaicAddressRestriction (io.nem.symbol.sdk.model.restriction.MosaicAddressRestriction)1 MosaicGlobalRestriction (io.nem.symbol.sdk.model.restriction.MosaicGlobalRestriction)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1