Ejemplo n.º 1
0
        public void Find_WithNoAddedProviderStates_ReturnsNull()
        {
            var providerStates = new ProviderStates();

            var actualProviderState = providerStates.Find("my provider state");

            Assert.Null(actualProviderState);
        }
Ejemplo n.º 2
0
        public void Add_WithProviderState_AddsProviderState()
        {
            const string providerStateDescription = "my provider state";
            var          providerState            = new ProviderState(providerStateDescription);
            var          providerStates           = new ProviderStates();

            providerStates.Add(providerState);

            Assert.Equal(providerState, providerStates.Find(providerStateDescription));
        }
Ejemplo n.º 3
0
        public void Find_WithProviderStateThatDoesNotMatchProviderStates_ReturnsNull()
        {
            const string providerStateDescription = "my provider state 2";
            var          providerState1           = new ProviderState("my provider state");
            var          providerState2           = new ProviderState(providerStateDescription);
            var          providerStates           = new ProviderStates();

            providerStates.Add(providerState1);
            providerStates.Add(providerState2);

            var actualProviderState = providerStates.Find("something else");

            Assert.Null(actualProviderState);
        }
Ejemplo n.º 4
0
        public void Find_WithProviderStateThatHasBeenAdded_ReturnsProviderState()
        {
            const string providerStateDescription = "my provider state 2";
            var          providerState1           = new ProviderState("my provider state");
            var          providerState2           = new ProviderState(providerStateDescription);
            var          providerStates           = new ProviderStates();

            providerStates.Add(providerState1);
            providerStates.Add(providerState2);

            var actualProviderState = providerStates.Find(providerStateDescription);

            Assert.Equal(providerState2, actualProviderState);
        }
        public void Validate(ProviderServicePactFile pactFile, ProviderStates providerStates)
        {
            if (pactFile == null)
            {
                throw new ArgumentException("Please supply a non null pactFile");
            }

            if (pactFile.Consumer == null || String.IsNullOrEmpty(pactFile.Consumer.Name))
            {
                throw new ArgumentException("Please supply a non null or empty Consumer name in the pactFile");
            }

            if (pactFile.Provider == null || String.IsNullOrEmpty(pactFile.Provider.Name))
            {
                throw new ArgumentException("Please supply a non null or empty Provider name in the pactFile");
            }

            if (pactFile.Interactions != null && pactFile.Interactions.Any())
            {
                _reporter.ReportInfo(String.Format("Verifying a Pact between {0} and {1}", pactFile.Consumer.Name, pactFile.Provider.Name));

                var comparisonResult = new ComparisonResult();

                foreach (var interaction in pactFile.Interactions)
                {
                    InvokePactSetUpIfApplicable(providerStates);

                    _reporter.ResetIndentation();

                    ProviderState providerStateItem = null;

                    if (interaction.ProviderState != null)
                    {
                        try
                        {
                            providerStateItem = providerStates.Find(interaction.ProviderState);
                        }
                        catch (Exception)
                        {
                            providerStateItem = null;
                        }

                        if (providerStateItem == null)
                        {
                            throw new InvalidOperationException(String.Format("providerState '{0}' was defined by a consumer, however could not be found. Please supply this provider state.", interaction.ProviderState));
                        }
                    }

                    InvokeProviderStateSetUpIfApplicable(providerStateItem);

                    if (!String.IsNullOrEmpty(interaction.ProviderState))
                    {
                        _reporter.Indent();
                        _reporter.ReportInfo(String.Format("Given {0}", interaction.ProviderState));
                    }

                    _reporter.Indent();
                    _reporter.ReportInfo(String.Format("{0}", interaction.Description));

                    if (interaction.Request != null)
                    {
                        _reporter.Indent();
                        _reporter.ReportInfo(String.Format("with {0} {1}", interaction.Request.Method.ToString().ToUpper(), interaction.Request.Path));
                    }

                    try
                    {
                        var interactionComparisonResult = ValidateInteraction(interaction);
                        comparisonResult.AddChildResult(interactionComparisonResult);
                        _reporter.Indent();
                        _reporter.ReportSummary(interactionComparisonResult);
                    }
                    finally
                    {
                        InvokeProviderStateTearDownIfApplicable(providerStateItem);
                        InvokeTearDownIfApplicable(providerStates);
                    }
                }

                _reporter.ResetIndentation();
                _reporter.ReportFailureReasons(comparisonResult);
                _reporter.Flush();

                if (comparisonResult.HasFailure)
                {
                    throw new PactFailureException(String.Format("See test output or {0} for failure details.",
                                                                 !String.IsNullOrEmpty(_config.LoggerName) ? LogProvider.CurrentLogProvider.ResolveLogPath(_config.LoggerName) : "logs"));
                }
            }
        }
Ejemplo n.º 6
0
        public void Validate(ProviderServicePactFile pactFile, ProviderStates providerStates)
        {
            if (pactFile == null)
            {
                throw new ArgumentException("Please supply a non null pactFile");
            }

            if (pactFile.Consumer == null || String.IsNullOrEmpty(pactFile.Consumer.Name))
            {
                throw new ArgumentException("Please supply a non null or empty Consumer name in the pactFile");
            }

            if (pactFile.Provider == null || String.IsNullOrEmpty(pactFile.Provider.Name))
            {
                throw new ArgumentException("Please supply a non null or empty Provider name in the pactFile");
            }

            if (pactFile.Interactions != null && pactFile.Interactions.Any())
            {
                InvokePactSetUpIfApplicable(providerStates);

                var interationNumber = 1;
                try //TODO: Clean this up once the validators/comparers no longer throw
                {
                    foreach (var interaction in pactFile.Interactions)
                    {
                        ProviderState providerStateItem = null;

                        if (interaction.ProviderState != null)
                        {
                            try
                            {
                                providerStateItem = providerStates.Find(interaction.ProviderState);
                            }
                            catch (Exception)
                            {
                                providerStateItem = null;
                            }

                            if (providerStateItem == null)
                            {
                                throw new InvalidOperationException(String.Format("providerState '{0}' was defined by a consumer, however could not be found. Please supply this provider state.", interaction.ProviderState));
                            }
                        }

                        InvokeInteractionSetUpIfApplicable(providerStateItem);

                        _reporter.ReportInfo(String.Format("{0}) Verifying a Pact between {1} and {2} - {3}.", interationNumber, pactFile.Consumer.Name, pactFile.Provider.Name, interaction.Description));

                        try
                        {
                            ValidateInteraction(interaction);
                        }
                        finally
                        {
                            InvokeInteractionIfApplicable(providerStateItem);
                        }

                        interationNumber++;
                    }

                    _reporter.ThrowIfAnyErrors();
                }
                finally
                {
                    InvokeTearDownIfApplicable(providerStates);
                }
            }
        }
        public void Validate(MessagePactFile pactFile, ProviderStates providerStates)
        {
            if (pactFile == null)
            {
                throw new ArgumentException("Please supply a non null pactFile");
            }

            if (pactFile.Consumer == null || String.IsNullOrEmpty(pactFile.Consumer.Name))
            {
                throw new ArgumentException("Please supply a non null or empty Consumer name in the pactFile");
            }

            if (pactFile.Provider == null || String.IsNullOrEmpty(pactFile.Provider.Name))
            {
                throw new ArgumentException("Please supply a non null or empty Provider name in the pactFile");
            }

            if (pactFile.Messages != null && pactFile.Messages.Any())
            {
                _reporter.ReportInfo(String.Format("Verifying a Pact between {0} and {1}", pactFile.Consumer.Name,
                                                   pactFile.Provider.Name));

                var comparisonResult = new ComparisonResult();

                foreach (var interaction in pactFile.Messages)
                {
                    InvokePactSetUpIfApplicable(providerStates);

                    _reporter.ResetIndentation();

                    ProviderState providerStateItem = null;
                    Message       actualMessage     = null;

                    if (interaction.ProviderState != null)
                    {
                        try
                        {
                            providerStateItem = providerStates.Find(interaction.ProviderState);
                        }
                        catch (Exception)
                        {
                            providerStateItem = null;
                        }

                        //either not found in providerStates or exception was caught
                        if (providerStateItem == null)
                        {
                            throw new InvalidOperationException(String.Format(
                                                                    "providerState '{0}' was defined by a consumer, however could not be found. Please supply this provider state.",
                                                                    interaction.ProviderState));
                        }

                        try
                        {
                            actualMessage = InvokeProviderStateSetUp(providerStateItem);
                        }
                        catch (Exception e)
                        {
                            throw new InvalidOperationException(String.Format(
                                                                    "Actual message could not be generated for providerState '{0}'. error: {1}",
                                                                    interaction.ProviderState, e.Message));
                        }
                    }


                    if (!String.IsNullOrEmpty(interaction.ProviderState))
                    {
                        _reporter.Indent();
                        _reporter.ReportInfo(String.Format("Given {0}", interaction.ProviderState));
                    }

                    _reporter.Indent();
                    _reporter.ReportInfo(String.Format("Upon receiving {0}", interaction.Description));


                    try
                    {
                        var interactionComparisonResult = ValidateInteraction(interaction, actualMessage);
                        comparisonResult.AddChildResult(interactionComparisonResult);
                        _reporter.Indent();
                        _reporter.ReportSummary(interactionComparisonResult);
                    }
                    finally
                    {
                        InvokeProviderStateTearDownIfApplicable(providerStateItem);
                        InvokeTearDownIfApplicable(providerStates);
                    }
                }

                _reporter.ResetIndentation();
                _reporter.ReportFailureReasons(comparisonResult);
                _reporter.Flush();

                if (comparisonResult.HasFailure)
                {
                    throw new PactFailureException(String.Format("See test output or {0} for failure details.",
                                                                 !String.IsNullOrEmpty(_config.LoggerName)
                            ? LogProvider.CurrentLogProvider.ResolveLogPath(_config.LoggerName) : "logs"));
                }
            }
        }
Ejemplo n.º 8
0
        public void Find_WithNullProviderState_ThrowsArgumentNullException()
        {
            var providerStates = new ProviderStates();

            Assert.Throws <ArgumentNullException>(() => providerStates.Find(null));
        }