public void when_initialize_it_should_initialize_driver()
        {
            var configuration = ConfigurationTests.CreateBrokerIngressConfiguration();
            var driverMock    = new Mock <IBrokerIngressDriver>();

            driverMock
            .Setup(
                driver => driver.Initialize(
                    It.IsAny <IBrokerIngress>(),
                    It.IsAny <IEnumerable <IIngressApi> >(),
                    It.IsAny <IDiContainerAdapter>()))
            .Verifiable();
            configuration.Driver = driverMock.Object;

            var serviceProviderMock = new Mock <IDiContainerAdapter>();

            serviceProviderMock.Setup(adapter => adapter.GetService(typeof(EmptyPipeFitter))).Returns(() => new EmptyPipeFitter());

            var sut = new BrokerIngress(
                Mock.Of <IMessageBroker>(),
                configuration,
                serviceProviderMock.Object);

            sut.Initialize();

            driverMock.Verify();
        }
        public void when_start_consume_messages_on_uninitialized_ingress_it_should_fail()
        {
            var configuration = ConfigurationTests.CreateBrokerIngressConfiguration();
            var ingress       = new BrokerIngress(
                Mock.Of <IMessageBroker>(),
                configuration,
                Mock.Of <IDiContainerAdapter>());

            Action sut = () => ingress.StartConsumeMessages(new[] { "queue name" });

            sut.Should().ThrowExactly <PoezdOperationException>().Which.Message.Should().Contain("not initialized");
        }
        public void when_start_consume_messages_without_queue_names_it_should_fail()
        {
            var configuration = ConfigurationTests.CreateBrokerIngressConfiguration();
            var ingress       = new BrokerIngress(
                Mock.Of <IMessageBroker>(),
                configuration,
                Mock.Of <IDiContainerAdapter>());

            ingress.Initialize();

            Action sut = () => ingress.StartConsumeMessages(queueNamePatterns: null);

            sut.Should().ThrowExactly <ArgumentNullException>().Which.ParamName.Should().Be("queueNamePatterns");
        }
 public void when_some_required_property_not_set_it_should_be_not_validated()
 {
     ConfigurationTests.CreateBrokerIngressConfiguration().With(configuration => configuration.Driver = null)
     .Validate().Should().HaveCount(expected: 1);
     ConfigurationTests.CreateBrokerIngressConfiguration().With(configuration => configuration.DriverConfiguration = null)
     .Validate().Should().HaveCount(expected: 1);
     ConfigurationTests.CreateBrokerIngressConfiguration().With(configuration => configuration.EnterPipeFitterType = null)
     .Validate().Should().HaveCount(expected: 1);
     ConfigurationTests.CreateBrokerIngressConfiguration().With(configuration => configuration.ExitPipeFitterType = null)
     .Validate().Should().HaveCount(expected: 1);
     ConfigurationTests.CreateBrokerIngressConfiguration().With(configuration => configuration.QueueNameMatcherType = null)
     .Validate().Should().HaveCount(expected: 1);
     ConfigurationTests.CreateBrokerIngressConfiguration(shouldAddApis: false)
     .Validate().Should().HaveCount(expected: 1);
 }
        public void when_initialized_twice_it_should_fail()
        {
            var configuration       = ConfigurationTests.CreateBrokerIngressConfiguration();
            var serviceProviderMock = new Mock <IDiContainerAdapter>();

            serviceProviderMock.Setup(adapter => adapter.GetService(typeof(EmptyPipeFitter))).Returns(() => new EmptyPipeFitter());

            var ingress = new BrokerIngress(
                Mock.Of <IMessageBroker>(),
                configuration,
                serviceProviderMock.Object);

            Action sut = () => ingress.Initialize();

            sut.Should().NotThrow();
            sut.Should().ThrowExactly <PoezdOperationException>().Which.Message.Should().Contain(
                "already initialized",
                "it should not be possible to initialize ingress more than once");
        }
        public void when_constructed_it_should_initialize_properties()
        {
            var configuration = ConfigurationTests.CreateBrokerIngressConfiguration();

            configuration.EnterPipeFitterType = typeof(EmptyPipeFitter);
            configuration.ExitPipeFitterType  = typeof(EmptyPipeFitter);

            var serviceProviderMock = new Mock <IDiContainerAdapter>();

            serviceProviderMock.Setup(adapter => adapter.GetService(typeof(EmptyPipeFitter))).Returns(() => new EmptyPipeFitter());

            var sut = new BrokerIngress(
                new Mock <IMessageBroker>().Object,
                configuration,
                serviceProviderMock.Object);

            sut.Configuration.Should().BeSameAs(configuration);
            sut.Configuration.Driver.Should().BeSameAs(configuration.Driver);
            sut.EnterPipeFitter.Should().BeOfType <EmptyPipeFitter>();
            sut.ExitPipeFitter.Should().BeOfType <EmptyPipeFitter>();
            sut.Apis.Should().HaveCount(expected: 1);
        }
        public void when_start_consume_messages_it_should_start_consume_messages_from_underlying_driver()
        {
            var driverMock = new Mock <IBrokerIngressDriver>();
            var driverStartConsumeMessagesCalled = false;

            driverMock.Setup(driver => driver.StartConsumeMessages(It.IsAny <IEnumerable <string> >(), CancellationToken.None))
            .Callback(() => driverStartConsumeMessagesCalled = true)
            .Returns(Task.CompletedTask);

            var configuration = ConfigurationTests.CreateBrokerIngressConfiguration()
                                .With(ingressConfiguration => ingressConfiguration.Driver = driverMock.Object);
            var ingress = new BrokerIngress(
                Mock.Of <IMessageBroker>(),
                configuration,
                Mock.Of <IDiContainerAdapter>());

            ingress.Initialize();

            ingress.StartConsumeMessages(new[] { "queue name" });

            driverStartConsumeMessagesCalled.Should().BeTrue("broker ingress should start message consumption on driver");
        }
        public void when_all_required_properties_set_it_should_be_validated()
        {
            var sut = ConfigurationTests.CreateBrokerIngressConfiguration();

            sut.Validate().Should().NotBeNull().And.Subject.Should().BeEmpty("there is no errors in the configuration");
        }