public void Upload(string pactBrokerUri, string providerName, string consumerName, string version) { var details = new PactDetails { Provider = new Pacticipant { Name = providerName }, Consumer = new Pacticipant { Name = consumerName } }; var filename = details.GeneratePactFileName(); var dir = Directory.GetCurrentDirectory(); if (!File.Exists("../../pacts/" + filename)) { throw new FileNotFoundException("couldn't find the pact file", Path.Combine(dir, filename)); } _client.Headers.Add("Content-Type", "application/json"); var url = $"{pactBrokerUri}/pacts/provider/{providerName.Replace(" ", "%20")}/consumer/{consumerName.Replace(" ", "%20")}/version/{version}"; Console.WriteLine($"PUT {url}"); _client.UploadFile( url, "PUT", $"../../pacts/{filename}"); }
public void Handle_WithAPostRequestToPactAndPactDirIsDifferentFromDefault_NewPactFileIsSavedWithInteractionsInTheSpecifiedPath() { var pactDetails = new PactDetails { Consumer = new Pacticipant { Name = "Consumer" }, Provider = new Pacticipant { Name = "Provider" } }; var interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { Description = "My description", Request = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/test" }, Response = new ProviderServiceResponse { Status = (int)HttpStatusCode.NoContent } } }; var pactFile = new ProviderServicePactFile { Provider = pactDetails.Provider, Consumer = pactDetails.Consumer, Interactions = interactions }; var config = new PactConfig { PactDir = @"C:\temp" }; var filePath = Path.Combine(config.PactDir, pactDetails.GeneratePactFileName()); var pactFileJson = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings); var pactDetailsJson = JsonConvert.SerializeObject(pactDetails, JsonConfig.ApiSerializerSettings); var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(pactDetailsJson)); var requestStream = new RequestStream(jsonStream, jsonStream.Length, true); var context = new NancyContext { Request = new Request("POST", new Url("http://localhost/pact"), requestStream) }; var handler = GetSubject(config); _mockProviderRepository.Interactions.Returns(interactions); var response = handler.Handle(context); _mockFileSystem.File.Received(1).WriteAllText(filePath, pactFileJson); }
GeneratePactFileName_WhenConsumerAndProviderHaveNotBeenSet_ReturnsFileNameWithNoConsumerAndProviderNameAndDoesNotThrow() { var details = new PactDetails(); var fileName = details.GeneratePactFileName(); Assert.Equal("-.json", fileName); }
public void GeneratePactFileName_WhenConsumerNameAndProviderNameHaveBeenSet_ReturnsFileName() { var details = new PactDetails { Consumer = new Pacticipant { Name = "My Consumer" }, Provider = new Pacticipant { Name = "My Provider" } }; var fileName = details.GeneratePactFileName(); Assert.Equal("my_consumer-my_provider.json", fileName); }
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 Handle_WithAPostRequestToPactAndDirectoryDoesNotExist_DirectoryIsCreatedAndNewPactFileIsSavedWithInteractions() { var pactDetails = new PactDetails { Consumer = new Party { Name = "Consumer" }, Provider = new Party { Name = "Provider" } }; var interactions = new List <ProviderServiceInteraction> { new ProviderServiceInteraction { Description = "My description", Request = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/test" }, Response = new ProviderServiceResponse { Status = (int)HttpStatusCode.NoContent } } }; var pactFile = new ProviderServicePactFile { Provider = pactDetails.Provider, Consumer = pactDetails.Consumer, Interactions = interactions }; var pactFileJson = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings); var pactDetailsJson = JsonConvert.SerializeObject(pactDetails, JsonConfig.ApiSerializerSettings); var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(pactDetailsJson)); var requestStream = new RequestStream(jsonStream, jsonStream.Length, true); var context = new NancyContext { Request = new Request("POST", new Url("http://localhost/pact"), requestStream) }; var filePath = Path.Combine(Constants.DefaultPactFileDirectory, pactDetails.GeneratePactFileName()); var handler = GetSubject(); _mockProviderRepository.Interactions.Returns(interactions); var writeAllTextCount = 0; _mockFileSystem.File .When(x => x.WriteAllText(filePath, pactFileJson)) .Do(x => { writeAllTextCount++; if (writeAllTextCount == 1) { throw new DirectoryNotFoundException("It doesn't exist"); } }); handler.Handle(context); _mockFileSystem.File.Received(2).WriteAllText(filePath, pactFileJson); }
public void Handle_WithAPostRequestToPactAndNoInteractionsHaveBeenRegistered_NewPactFileIsSavedWithNoInteractions() { var pactDetails = new PactDetails { Consumer = new Party { Name = "Consumer" }, Provider = new Party { Name = "Provider" } }; var pactFile = new ProviderServicePactFile { Provider = pactDetails.Provider, Consumer = pactDetails.Consumer, Interactions = new ProviderServiceInteraction[0] }; var pactFileJson = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings); var pactDetailsJson = JsonConvert.SerializeObject(pactDetails, JsonConfig.ApiSerializerSettings); var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(pactDetailsJson)); var requestStream = new RequestStream(jsonStream, jsonStream.Length, true); var context = new NancyContext { Request = new Request("POST", new Url("http://localhost/pact"), requestStream) }; var handler = GetSubject(); handler.Handle(context); _mockFileSystem.File.Received(1).WriteAllText(Path.Combine(Constants.DefaultPactFileDirectory, pactDetails.GeneratePactFileName()), pactFileJson); }
public void VerifyAllHealthCheckPacts() { // Arrange IPactVerifier pactVerifier = new PactVerifier(() => { }, () => { }); var pactDetails = new PactDetails() { Provider = new Pacticipant { Name = PactConstants.PactProviderNames.SqsPocApi }, Consumer = new Pacticipant { Name = PactConstants.PactConsumerNames.Product } }; var pactLocation = $"{ PactConstants.PactRootLocation }\\{ PactConstants.PactProviderNames.SqsPocApi }\\{ PactConstants.PactConsumerNames.Product }\\{ pactDetails.GeneratePactFileName() }"; pactVerifier .ProviderState("The service is up and running and all dependencies are available") .ProviderState("The service is up and running and some dependencies are not available", SetupDependenciesNotAvailable); // Act & Assert using (var testServer = TestServer.Create <Startup>()) //NOTE: This is using the Microsoft.Owin.Testing to test host an owin app :) { pactVerifier = pactVerifier .ServiceProvider(pactDetails.Provider.Name, testServer.HttpClient) .HonoursPactWith(pactDetails.Consumer.Name) .PactUri(pactLocation); pactVerifier.Verify("A request for the status"); } }