private void PersistPactFile() { if (String.IsNullOrEmpty(ConsumerName)) { throw new InvalidOperationException( "ConsumerName has not been set, please supply a consumer name using the ServiceConsumer method."); } if (String.IsNullOrEmpty(ProviderName)) { throw new InvalidOperationException( "ProviderName has not been set, please supply a provider name using the HasPactWith method."); } var pactDetails = new PactDetails { Provider = new Pacticipant { Name = ProviderName }, Consumer = new Pacticipant { Name = ConsumerName } }; var pactFilePath = Path.Combine(pactConfig.PactDir, pactDetails.GeneratePactFileName()); var pactFile = new MessagePactFile { Provider = pactDetails.Provider, Consumer = pactDetails.Consumer, Messages = mockMq.Interactions() }; var pactFileJson = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings); try { File.WriteAllText(pactFilePath, pactFileJson); } catch (DirectoryNotFoundException) { Directory.CreateDirectory(pactConfig.PactDir); File.WriteAllText(pactFilePath, pactFileJson); } }
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")); } } }