public void Find_WithNoAddedProviderStates_ReturnsNull()
        {
            var providerStates = new ProviderStates();

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

            Assert.Null(actualProviderState);
        }
        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));
        }
        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 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);
        }
        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"));
                }
            }
        }
        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 Find_WithNullProviderState_ThrowsArgumentNullException()
        {
            var providerStates = new ProviderStates();

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