<commit_before>﻿using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using NBitcoin;
using Stratis.Bitcoin.Base;
using Stratis.Bitcoin.Base.Deployments;
using Stratis.Bitcoin.BlockPulling;
using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Configuration.Settings;
using Stratis.Bitcoin.Connection;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Consensus.Validators;
using Stratis.Bitcoin.Features.Notifications.Controllers;
using Stratis.Bitcoin.Interfaces;
using Stratis.Bitcoin.P2P;
using Stratis.Bitcoin.P2P.Peer;
using Stratis.Bitcoin.P2P.Protocol.Payloads;
using Stratis.Bitcoin.Tests.Common;
using Stratis.Bitcoin.Tests.Common.Logging;
using Stratis.Bitcoin.Utilities;
using Stratis.Bitcoin.Utilities.JsonErrors;
using Xunit;

namespace Stratis.Bitcoin.Features.Notifications.Tests
{
    public class NotificationsControllerTest : LogsTestBase
    {
        private readonly Network network;

        public NotificationsControllerTest()
        {
            this.network = KnownNetworks.StratisMain;
        }

        private ConsensusManager InstantiateMockedConsensusManager(ConcurrentChain chain)
        {
            NodeSettings nodeSettings = NodeSettings.Default();
            IDateTimeProvider dateTimeProvider = DateTimeProvider.Default;
            var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, nodeSettings.DataFolder, this.LoggerFactory.Object, new SelfEndpointTracker(this.LoggerFactory.Object));
            var networkPeerFactory = new NetworkPeerFactory(this.network, dateTimeProvider, this.LoggerFactory.Object, new PayloadProvider().DiscoverPayloads(), new SelfEndpointTracker(this.LoggerFactory.Object), new Mock<IInitialBlockDownloadState>().Object, new ConnectionManagerSettings());
            var peerDiscovery = new PeerDiscovery(new AsyncLoopFactory(this.LoggerFactory.Object), this.LoggerFactory.Object, this.network, networkPeerFactory, new NodeLifetime(), nodeSettings, peerAddressManager);
            var consensusSettings = new ConsensusSettings(nodeSettings);
            var chainState = new ChainState();
            var connectionSettings = new ConnectionManagerSettings(nodeSettings);
            var selfEndpointTracker = new SelfEndpointTracker(this.LoggerFactory.Object);
            
            ConsensusRuleEngine emptyConsensusRules = new Mock<ConsensusRuleEngine>(this.network, this.LoggerFactory.Object, dateTimeProvider, chain, new NodeDeployments(this.network, chain), new ConsensusSettings(new NodeSettings(this.network)), new Checkpoints(), new Mock<IChainState>().Object, new Mock<IInvalidBlockHashStore>().Object).Object;
            var connectionManager = new ConnectionManager(dateTimeProvider, this.LoggerFactory.Object, this.network, networkPeerFactory, nodeSettings, new NodeLifetime(), new NetworkPeerConnectionParameters(), peerAddressManager, new IPeerConnector[] { }, peerDiscovery, selfEndpointTracker, connectionSettings, new VersionProvider());
            var peerBanning = new PeerBanning(connectionManager, this.LoggerFactory.Object, dateTimeProvider, peerAddressManager);

            ConsensusManager consensusManager = new ConsensusManager(this.network, this.LoggerFactory.Object, chainState, new HeaderValidator(emptyConsensusRules, this.LoggerFactory.Object),
                new IntegrityValidator(emptyConsensusRules, this.LoggerFactory.Object), new PartialValidator(emptyConsensusRules, this.LoggerFactory.Object), new Checkpoints(), consensusSettings, emptyConsensusRules,
                new Mock<IFinalizedBlockInfo>().Object, new Signals.Signals(), peerBanning, new Mock<IInitialBlockDownloadState>().Object, chain, new Mock<IBlockPuller>().Object, new Mock<IBlockStore>().Object, new Mock<IInvalidBlockHashStore>().Object);

            return consensusManager;
        }

        [Theory]
        [InlineData(null)]
        [InlineData("")]
        [Trait("Module", "NotificationsController")]
        public void Given_SyncActionIsCalled_When_QueryParameterIsNullOrEmpty_Then_ReturnBadRequest(string from)
        {
            var chain = new Mock<ConcurrentChain>();
            ConsensusManager consensusManager = this.InstantiateMockedConsensusManager(chain.Object);

            var blockNotification = new Mock<BlockNotification>(this.LoggerFactory.Object, chain.Object, consensusManager, new Signals.Signals(), new AsyncLoopFactory(new LoggerFactory()), new NodeLifetime());

            var notificationController = new NotificationsController(blockNotification.Object, chain.Object);
            IActionResult result = notificationController.SyncFrom(from);

            var errorResult = Assert.IsType<ErrorResult>(result);
            var errorResponse = Assert.IsType<ErrorResponse>(errorResult.Value);
            Assert.Single(errorResponse.Errors);

            ErrorModel error = errorResponse.Errors[0];
            Assert.Equal(400, error.Status);<commit_msg>build errors (#2067)<commit_after>﻿using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using NBitcoin;
using Stratis.Bitcoin.Base;
using Stratis.Bitcoin.Base.Deployments;
using Stratis.Bitcoin.BlockPulling;
using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Configuration.Settings;
using Stratis.Bitcoin.Connection;
using Stratis.Bitcoin.Consensus;
using Stratis.Bitcoin.Consensus.Validators;
using Stratis.Bitcoin.Features.Notifications.Controllers;
using Stratis.Bitcoin.Interfaces;
using Stratis.Bitcoin.P2P;
using Stratis.Bitcoin.P2P.Peer;
using Stratis.Bitcoin.P2P.Protocol.Payloads;
using Stratis.Bitcoin.Tests.Common;
using Stratis.Bitcoin.Tests.Common.Logging;
using Stratis.Bitcoin.Utilities;
using Stratis.Bitcoin.Utilities.JsonErrors;
using Xunit;

namespace Stratis.Bitcoin.Features.Notifications.Tests
{
    public class NotificationsControllerTest : LogsTestBase
    {
        private readonly Network network;

        public NotificationsControllerTest()
        {
            this.network = KnownNetworks.StratisMain;
        }

        private ConsensusManager InstantiateMockedConsensusManager(ConcurrentChain chain)
        {
            NodeSettings nodeSettings = NodeSettings.Default();
            IDateTimeProvider dateTimeProvider = DateTimeProvider.Default;
            var peerAddressManager = new PeerAddressManager(DateTimeProvider.Default, nodeSettings.DataFolder, this.LoggerFactory.Object, new SelfEndpointTracker(this.LoggerFactory.Object));
            var networkPeerFactory = new NetworkPeerFactory(this.network, dateTimeProvider, this.LoggerFactory.Object, new PayloadProvider().DiscoverPayloads(), new SelfEndpointTracker(this.LoggerFactory.Object), new Mock<IInitialBlockDownloadState>().Object, new ConnectionManagerSettings());
            var peerDiscovery = new PeerDiscovery(new AsyncLoopFactory(this.LoggerFactory.Object), this.LoggerFactory.Object, this.network, networkPeerFactory, new NodeLifetime(), nodeSettings, peerAddressManager);
            var consensusSettings = new ConsensusSettings(nodeSettings);
            var chainState = new ChainState();
            var connectionSettings = new ConnectionManagerSettings(nodeSettings);
            var selfEndpointTracker = new SelfEndpointTracker(this.LoggerFactory.Object);

            ConsensusRuleEngine emptyConsensusRules = new Mock<ConsensusRuleEngine>(this.network, this.LoggerFactory.Object, dateTimeProvider, chain, new NodeDeployments(this.network, chain), new ConsensusSettings(new NodeSettings(this.network)), new Checkpoints(), new Mock<IChainState>().Object, new Mock<IInvalidBlockHashStore>().Object).Object;
            var connectionManager = new ConnectionManager(dateTimeProvider, this.LoggerFactory.Object, this.network, networkPeerFactory, nodeSettings, new NodeLifetime(), new NetworkPeerConnectionParameters(), peerAddressManager, new IPeerConnector[] { }, peerDiscovery, selfEndpointTracker, connectionSettings, new VersionProvider());
            var peerBanning = new PeerBanning(connectionManager, this.LoggerFactory.Object, dateTimeProvider, peerAddressManager);

            ConsensusManager consensusManager = new ConsensusManager(this.network, this.LoggerFactory.Object, chainState, new HeaderValidator(emptyConsensusRules, this.LoggerFactory.Object),
                new IntegrityValidator(emptyConsensusRules, this.LoggerFactory.Object), new PartialValidator(emptyConsensusRules, this.LoggerFactory.Object), new Checkpoints(), consensusSettings, emptyConsensusRules,
                new Mock<IFinalizedBlockInfo>().Object, new Signals.Signals(), peerBanning, new Mock<IInitialBlockDownloadState>().Object, chain, new Mock<IBlockPuller>().Object, new Mock<IBlockStore>().Object,
                new Mock<IInvalidBlockHashStore>().Object, new Mock<IConnectionManager>().Object);

            return consensusManager;
        }

        [Theory]
        [InlineData(null)]
        [InlineData("")]
        [Trait("Module", "NotificationsController")]
        public void Given_SyncActionIsCalled_When_QueryParameterIsNullOrEmpty_Then_ReturnBadRequest(string from)
        {
            var chain = new Mock<ConcurrentChain>();
            ConsensusManager consensusManager = this.InstantiateMockedConsensusManager(chain.Object);

            var blockNotification = new Mock<BlockNotification>(this.LoggerFactory.Object, chain.Object, consensusManager, new Signals.Signals(), new AsyncLoopFactory(new LoggerFactory()), new NodeLifetime());

            var notificationController = new NotificationsController(blockNotification.Object, chain.Object);
            IActionResult result = notificationController.SyncFrom(from);

            var errorResult = Assert.IsType<ErrorResult>(result);
            var errorResponse = Assert.IsType<ErrorResponse>(errorResult.Value);
            Assert.Single(errorResponse.Errors);

            ErrorModel error = errorResponse.Errors[0];
            Assert.Equal(400, error.Status);
{'fraction_non_alphanumeric': 0.08805220883534137, 'fraction_numerical': 0.0018072289156626507, 'mean_word_length': 4.685502283105023, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3754142', 'n_tokens_mistral': 2471, 'n_tokens_neox': 2448, 'n_words': 503}