BlockchainEvents { ListenerRegistration addPaymentListener(@NonNull final EventListener<PaymentInfo> listener) { checkNotNull(listener, "listener"); final kin.base.requests.EventListener<TransactionResponse> responseListener = new kin.base.requests.EventListener<TransactionResponse>() { @Override public void onEvent(TransactionResponse transactionResponse) { extractPaymentsFromTransaction(transactionResponse, listener); } }; transactionsStream.addListener(responseListener); return new ListenerRegistration(new Runnable() { @Override public void run() { transactionsStream.removeListener(responseListener); } }); } BlockchainEvents(Server server, String accountId);   }
@Test public void addPaymentListener() throws Exception { enqueueTransactionsResponses(); final CountDownLatch latch = new CountDownLatch(1); final List<PaymentInfo> actualResults = new ArrayList<>(); blockchainEvents.addPaymentListener(new EventListener<PaymentInfo>() { @Override public void onEvent(PaymentInfo data) { actualResults.add(data); if (actualResults.size() == 2) { latch.countDown(); } } }); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertThat(actualResults.size(), equalTo(2)); PaymentInfo payment1 = actualResults.get(0); PaymentInfo payment2 = actualResults.get(1); assertThat(payment1.hash().id(), equalTo("3eb3024a9c03451e7c8b8d3ba525a3a241e286cb694a262444d46d92e7605f22")); assertThat(payment1.sourcePublicKey(), equalTo("GBLUDU6Y6KVM5MCJWOLPVVSJEVICGEGXHOOHEAPWRSXJ7XVMBFKISOLR")); assertThat(payment1.destinationPublicKey(), equalTo("GANPYEGVH3ZVQFMQVVYFRP7U3HKE5LIJ3345ORI26G2OAX7HV66VIE7F")); assertThat(payment1.amount(), equalTo(new BigDecimal("250"))); assertThat(payment1.createdAt(), equalTo("2018-11-19T15:59:07Z")); assertThat(payment1.fee(), equalTo(100L)); assertThat(payment1.memo(), equalTo("1-test-test1")); assertThat(payment2.hash().id(), equalTo("c4ad29472150a741c0924086a76fea1aac326261afacee05c0b36be7e8fb5727")); assertThat(payment2.sourcePublicKey(), equalTo("GBLUDU6Y6KVM5MCJWOLPVVSJEVICGEGXHOOHEAPWRSXJ7XVMBFKISOLR")); assertThat(payment2.destinationPublicKey(), equalTo("GANPYEGVH3ZVQFMQVVYFRP7U3HKE5LIJ3345ORI26G2OAX7HV66VIE7F")); assertThat(payment2.amount(), equalTo(new BigDecimal("250"))); assertThat(payment2.createdAt(), equalTo("2018-11-19T16:36:42Z")); assertThat(payment2.fee(), equalTo(100L)); assertThat(payment2.memo(), equalTo("1-test-test1")); }
@Test public void addPaymentListener_StopListener_NoEvents() throws Exception { enqueueTransactionsResponses(); final CountDownLatch latch = new CountDownLatch(1); final int[] eventsCount = {0}; ListenerRegistration listenerRegistration = blockchainEvents .addPaymentListener(new EventListener<PaymentInfo>() { @Override public void onEvent(PaymentInfo data) { eventsCount[0]++; if (eventsCount[0] == 2) { latch.countDown(); } } }); assertTrue(latch.await(1, TimeUnit.SECONDS)); listenerRegistration.remove(); enqueueTransactionsResponses(); enqueueTransactionsResponses(); Thread.sleep(500); assertThat(eventsCount[0], equalTo(2)); }
@SuppressWarnings("ConstantConditions") @Test public void addPaymentListener_NullListener_IllegalArgumentException() throws Exception { expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage("listener"); blockchainEvents.addPaymentListener(null); }