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