Ejemplo n.º 1
0
        public async Task ShouldCreateMessagePactAfterVerifyingConsumer()
        {
            var handler = new RecipeAddedHandler();

            var fakePactBrokerMessageHandler = new FakePactBrokerMessageHandler();
            fakePactBrokerMessageHandler.Configure(HttpMethod.Put, "http://localhost:9292").RespondsWith(HttpStatusCode.Created);
            var publisher = new PactPublisher(new HttpClient(fakePactBrokerMessageHandler) { BaseAddress = new Uri("http://localhost:9292") }, "1.0", "local");

            var builder = new MessagePactBuilder("messageConsumer", "messageProvider", publisher);

            await builder.SetUp(_messageBuilder
                .VerifyConsumer<RecipeAdded>(m => handler.Handle(m)))
                .BuildAsync();

            // Check if handler has been called
            Assert.IsNotNull(handler.ReceivedRecipes.FirstOrDefault());
            Assert.AreEqual("A Recipe", handler.ReceivedRecipes.First().Name);

            // Check if pact has been published and tagged
            Assert.AreEqual("messageConsumer",
                JsonConvert.DeserializeObject<Contract>(fakePactBrokerMessageHandler
                        .GetStatus(HttpMethod.Put, "http://localhost:9292").SentRequestContents.First().Value).Consumer
                    .Name);
            Assert.IsTrue(fakePactBrokerMessageHandler.GetStatus(HttpMethod.Put, "http://localhost:9292").SentRequestContents.Last().Key.Contains("local"));

            // Check if pact has been written to project directory.
            var buildDirectory = AppContext.BaseDirectory;
            var pactDir =
                Path.GetFullPath(
                    $"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            var pactFile = File.ReadAllText(pactDir + "messageConsumer-messageProvider.json");
            Assert.AreEqual("messageConsumer", JsonConvert.DeserializeObject<Contract>(pactFile).Consumer.Name);
            File.Delete(pactDir + "messageConsumer-messageProvider.json");
        }
Ejemplo n.º 2
0
        public async Task ShouldPublishVerificationResults()
        {
            var buildDirectory               = AppContext.BaseDirectory;
            var pactDir                      = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            var pactFileToReturn             = File.ReadAllText(pactDir + "messageConsumer-messageProvider.json");
            var fakePactBrokerMessageHandler = new FakePactBrokerMessageHandler();

            fakePactBrokerMessageHandler
            .Configure(HttpMethod.Get,
                       "http://localhost:9292/pacts/provider/messageProvider/consumer/messageConsumer/latest")
            .RespondsWith(HttpStatusCode.Created).Returns(JsonConvert.DeserializeObject(pactFileToReturn));
            fakePactBrokerMessageHandler
            .Configure(HttpMethod.Post, "http://localhost:9292/publish/verification/results/path")
            .RespondsWith(HttpStatusCode.Created);

            var recipeRepository     = new FakeRecipeRepository();
            var providerStateHandler = new ProviderStateHandler(recipeRepository);
            var messageSender        = new MessageSender(recipeRepository);

            var config = new PactVerifierConfig
            {
                ProviderStateHandler       = providerStateHandler.Handle,
                MessageProducer            = messageSender.Send,
                ProviderVersion            = "1.0",
                PublishVerificationResults = true,
                PactBrokerClient           = new HttpClient(fakePactBrokerMessageHandler)
                {
                    BaseAddress = new Uri("http://localhost:9292")
                }
            };

            var pactVerifier = new PactVerifier(config);

            await pactVerifier.VerifyPactAsync("pacts/provider/messageProvider/consumer/messageConsumer/latest");

            var sentVerificationResults = JsonConvert.DeserializeObject <VerificationResults>(
                fakePactBrokerMessageHandler.GetStatus(HttpMethod.Post,
                                                       "http://localhost:9292/publish/verification/results/path")
                .SentRequestContents.First().Value);

            Assert.IsTrue(sentVerificationResults.Success);
            Assert.AreEqual(1, sentVerificationResults.TestResults.Summary.TestCount);
            Assert.AreEqual(0, sentVerificationResults.TestResults.Summary.FailureCount);
        }
Ejemplo n.º 3
0
        public async Task TaggingIsOptional()
        {
            var fakeHttpMessageHandler = new FakePactBrokerMessageHandler();

            fakeHttpMessageHandler.Configure(HttpMethod.Put, "http://local-pact-broker")
            .RespondsWith(HttpStatusCode.Created);

            var pactPublisher = new PactPublisher(new HttpClient(fakeHttpMessageHandler)
            {
                BaseAddress = new Uri("http://local-pact-broker")
            }, "1.0");

            await pactPublisher.PublishAsync(_pact);

            Assert.AreEqual("http://local-pact-broker/pacts/provider/provider/consumer/consumer/version/1.0", fakeHttpMessageHandler.GetStatus(HttpMethod.Put, "http://local-pact-broker").SentRequestContents.First().Key);
            var sentContract = JsonConvert.DeserializeObject <Contract>(fakeHttpMessageHandler.GetStatus(HttpMethod.Put, "http://local-pact-broker").SentRequestContents.First().Value);

            Assert.IsNotNull(sentContract);
            Assert.AreEqual(_pact.Consumer.Name, sentContract.Consumer.Name);
        }
Ejemplo n.º 4
0
        public async Task PublishTags()
        {
            const string baseAddress     = "http://local-pact-broker";
            const string providerName    = "test-provider";
            const string providerVersion = "1.0.0";

            var tags = new List <string> {
                "test", "tag"
            };
            var path = $"{baseAddress}/pacticipants/{providerName}/versions/{providerVersion}/tags/";

            var fakeHttpMessageHandler = new FakePactBrokerMessageHandler();

            fakeHttpMessageHandler.Configure(HttpMethod.Put, path).RespondsWith(HttpStatusCode.Created);

            await PactVerifier.PublishTags(new HttpClient(fakeHttpMessageHandler) { BaseAddress = new Uri(baseAddress) }, providerName, providerVersion, tags);

            foreach (var tag in tags)
            {
                fakeHttpMessageHandler.GetStatus(HttpMethod.Put, path).CalledUrls.ShouldContain($"{path}{tag}");
            }
        }
Ejemplo n.º 5
0
        public async Task PublishingFailedTests()
        {
            var fakeHttpMessageHandler = new FakePactBrokerMessageHandler();

            fakeHttpMessageHandler.Configure(HttpMethod.Post, "http://publish/").RespondsWith(HttpStatusCode.Created);

            await PactVerifier.PublishVerificationResultsAsync(
                _pact,
                new List <Test> {
                new Test {
                    Description = "test1"
                },
                new Test {
                    Description = "test2", Issues = new List <string> {
                        "Something failed"
                    }
                }
            },
                "1.0",
                new HttpClient(fakeHttpMessageHandler) { BaseAddress = new Uri("http://local-pact-broker") },
                "http://publish");

            var sentVerificationResults = JsonConvert.DeserializeObject <VerificationResults>(fakeHttpMessageHandler.GetStatus(HttpMethod.Post, "http://publish/").SentRequestContents.First().Value);

            Assert.IsFalse(sentVerificationResults.Success);
            Assert.AreEqual("1.0", sentVerificationResults.ProviderApplicationVersion);
            Assert.AreEqual(_pact.Provider.Name, sentVerificationResults.ProviderName);
            Assert.AreEqual(2, sentVerificationResults.TestResults.Summary.TestCount);
            Assert.AreEqual(1, sentVerificationResults.TestResults.Summary.FailureCount);
            Assert.AreEqual("passed", sentVerificationResults.TestResults.Tests.First(t => t.Description == "test1").Status);
            Assert.AreEqual("failed", sentVerificationResults.TestResults.Tests.First(t => t.Description == "test2").Status);
            Assert.AreEqual("Something failed", sentVerificationResults.TestResults.Tests.First(t => t.Description == "test2").Issues.First());
        }
Ejemplo n.º 6
0
        public async Task ShouldMatchRequest()
        {
            var url = "http://localhost:9396";

            var fakePactBrokerMessageHandler = new FakePactBrokerMessageHandler();

            fakePactBrokerMessageHandler.Configure(HttpMethod.Put, "http://localhost:9292").RespondsWith(HttpStatusCode.Created);
            var publisher = new PactPublisher(new HttpClient(fakePactBrokerMessageHandler)
            {
                BaseAddress = new Uri("http://localhost:9292")
            }, "1.0", "local");

            var builder = new PactBuilder("V3-consumer", "V3-provider", url, publisher);

            var recipeId = Guid.Parse("2860dedb-a193-425f-b73e-ef02db0aa8cf");

            var ingredient = Some.Object.With(
                Some.Element.Named("name").Like("Salt"),
                Some.Element.Named("amount").Like(5.5),
                Some.Element.Named("unit").Like("gram"));

            builder.SetUp(Pact.Interaction
                          .Given(new ProviderState {
                Name = $"There is a recipe with id `{recipeId}`"
            })
                          .UponReceiving("a request")
                          .With(Pact.Request
                                .WithHeader("Accept", "application/json")
                                .WithMethod(Method.GET)
                                .WithPath($"/api/recipes/{recipeId}"))
                          .WillRespondWith(Pact.Response
                                           .WithStatus(200)
                                           .WithHeader("Content-Type", "application/json")
                                           .WithBody(Pact.JsonContent.With(
                                                         Some.Element.Named("name").Like("A Recipe"),
                                                         Some.Element.Named("instructions").Like("Mix it up"),
                                                         Some.Array.Named("ingredients").Of(ingredient)
                                                         ))));

            using (var client = new HttpClient {
                BaseAddress = new Uri(url)
            })
            {
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                var response = await client.GetAsync($"api/recipes/{recipeId}");

                Assert.IsTrue(response.IsSuccessStatusCode);
            }

            await builder.BuildAsync();

            // Check if pact has been published and tagged
            Assert.AreEqual("V3-consumer",
                            JsonConvert.DeserializeObject <Contract>(fakePactBrokerMessageHandler
                                                                     .GetStatus(HttpMethod.Put, "http://localhost:9292").SentRequestContents.First().Value).Consumer
                            .Name);
            Assert.IsTrue(fakePactBrokerMessageHandler.GetStatus(HttpMethod.Put, "http://localhost:9292")
                          .SentRequestContents.Last().Key.Contains("local"));

            // Check if pact has been written to project directory.
            var buildDirectory = AppContext.BaseDirectory;
            var pactDir        = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            var pactFile       = File.ReadAllText(pactDir + "V3-consumer-V3-provider.json");

            Assert.AreEqual("V3-consumer", JsonConvert.DeserializeObject <Contract>(pactFile).Consumer.Name);
            File.Delete(pactDir + "V3-consumer-V3-provider.json");
        }