public TransactionConfirmationWatcherTests()
        {
            this.callbackRepository = Substitute.For <ICallbackRepository>();
            this.ruleRepository     = Substitute.ForPartsOf <FakeRuleRepository>();

            this.blockStorage = Substitute.For <IBlocksStorage>();
            this.blockStorage
            .GetAsync(Arg.Any <uint256>(), Arg.Any <CancellationToken>())
            .Returns(info => mockedBlocks[info.ArgAt <uint256>(0)].ToValueTuple());

            this.callbackExecuter = Substitute.For <ICallbackExecuter>();
            this.logger           = Substitute.For <ILogger <TransactionConfirmationWatcher> >();
            this.watchRepository  = new FakeWatchRepository();

            this.handler = this.subject = new TransactionConfirmationWatcher
                                          (
                this.callbackRepository,
                this.ruleRepository,
                this.blockStorage,
                this.callbackExecuter,
                this.watchRepository,
                this.logger
                                          );
            this.blockListener = this.subject;
            this.defaultUrl    = new Uri("http://zcoin.io");

            MockCallbackRepository();
        }
 public TransactionConfirmationWatcherTests()
 {
     this.block   = ZcoinNetworks.Instance.Regtest.GetGenesis();
     this.handler = new Mock <ITransactionConfirmationWatcherHandler <object> >();
     this.blocks  = new Mock <IBlocksStorage>();
     this.subject = new TransactionConfirmationWatcher <object>(this.handler.Object, this.blocks.Object);
 }
        public async Task Initialize_WithNonEmptyRepository_ShouldInitializeWatches()
        {
            // Arrange.
            var tx = Transaction.Parse(TestTransaction.Raw1, ZcoinNetworks.Instance.Mainnet);

            var builder = new WatchArgsBuilder(this.callbackRepository);

            builder.Timeout     = TimeSpan.FromSeconds(1);
            builder.Transaction = tx.GetHash();

            var callback = await this.callbackRepository.AddAsync
                           (
                builder.Ip, builder.CallbackUrl, CancellationToken.None
                           );

            _ = await this.ruleRepository.AddAsync
                (
                builder.Transaction, builder.Confirmations, builder.Timeout, builder.SuccessData,
                builder.TimeoutData, callback, CancellationToken.None
                );

            // Completed watch
            var completedCallback = new Callback
                                    (
                Guid.NewGuid(),
                IPAddress.Loopback,
                DateTime.UtcNow,
                true,
                this.defaultUrl
                                    );

            var watch = await this.ruleRepository.AddAsync
                        (
                builder.Transaction, builder.Confirmations, builder.Timeout, builder.SuccessData,
                builder.TimeoutData, completedCallback, CancellationToken.None
                        );

            await this.ruleRepository.UpdateStatusAsync(watch.Id, RuleStatus.Success, CancellationToken.None);

            // Act.
            ITransactionConfirmationWatcherHandler <Rule> localHandler;
            TransactionConfirmationWatcher localWatcher;

            localHandler = localWatcher = new TransactionConfirmationWatcher
                                          (
                this.callbackRepository,
                this.ruleRepository,
                this.blockStorage,
                this.callbackExecuter,
                this.watchRepository,
                this.logger
                                          );

            await localWatcher.StartAsync(CancellationToken.None);

            var retrievedCount = (await localHandler.CreateContextsAsync(tx, CancellationToken.None)).Count();

            Thread.Sleep(TimeSpan.FromSeconds(2));

            // Assert.
            Assert.Equal(1, retrievedCount);

            _ = this.callbackExecuter.Received(1).ExecuteAsync
                (
                Arg.Any <Guid>(),
                Arg.Any <Uri>(),
                Arg.Is <CallbackResult>
                (
                    result => result.Status == CallbackResult.StatusError
                ),
                Arg.Any <CancellationToken>()
                );
        }