### Starting Microcks Container with Testcontainers C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet demonstrates how to initialize and start a Microcks container using Testcontainers for .NET. It specifies the required `uber` distribution image for Microcks, which does not have a MongoDB dependency. The `StartAsync()` method asynchronously launches the container. ```csharp MicrocksContainer container = new MicrocksBuilder() .WithImage("quay.io/microcks/microcks-uber:1.11.0") .Build(); await container.StartAsync(); ``` -------------------------------- ### Configuring WebApplication for External Access in C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet shows the structure of an `ApplicationBuilder` class, which is used to create and configure a `WebApplication` instance. It includes examples of adding controllers and other services, demonstrating how to set up an application that can be accessed externally by Microcks. ```C# public static WebApplication Create(params string[] args) { var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); // Example: Configure OpenTelemetry, HTTP clients, controllers, Swagger, etc. // ...existing code... var app = builder.Build(); app.MapControllers(); // Example: Enable Swagger in development, HTTPS redirection, authorization, etc. // ...existing code... return app; } ``` -------------------------------- ### Initializing MicrocksContainersEnsemble in C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet shows how to create and start a `MicrocksContainersEnsemble` instance. This ensemble is designed to manage additional Microcks services and supports advanced features beyond what the basic `MicrocksContainer` offers, such as asynchronous contract testing. ```C# MicrocksContainersEnsemble ensemble = new MicrocksContainerEnsemble(network, MicrocksImage) .WithMainArtifacts("pastry-orders-asyncapi.yml"); await ensemble.StartAsync(); ``` -------------------------------- ### Importing Artifacts Before Container Start in Microcks C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This code shows how to pre-configure a Microcks container to import main and secondary artifacts (e.g., OpenAPI, Postman Collection) before it starts. The `WithMainArtifacts()` and `WithSecondaryArtifacts()` methods are used during the builder phase to specify artifact paths, which are then imported upon container startup. ```csharp MicrocksContainer container = new MicrocksBuilder() .WithMainArtifacts("apipastries-openapi.yaml") .WithSecondaryArtifacts("apipastries-postman-collection.json") .Build(); await container.StartAsync(); ``` -------------------------------- ### Importing Artifacts After Container Start in Microcks C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet illustrates how to import artifacts into a Microcks container *after* it has already started. The `ImportAsMainArtifact()` and `ImportAsSecondaryArtifact()` methods allow dynamic loading of OpenAPI or Postman Collection files into the running Microcks instance. ```csharp container.ImportAsMainArtifact("apipastries-openapi.yaml"); container.ImportAsSecondaryArtifact("apipastries-postman-collection.json"); ``` -------------------------------- ### Importing Repository Snapshots into Microcks Container C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This example demonstrates importing a complete Microcks repository snapshot during container initialization. The `WithSnapshots()` method allows specifying a JSON file containing a full repository configuration, which is then loaded into Microcks upon startup, enabling bulk content import. ```csharp MicrocksContainer container = new MicrocksBuilder() .WithSnapshots("microcks-repository.json") .Build(); await container.StartAsync(); ``` -------------------------------- ### Exposing Application Port to Microcks Container in C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This code demonstrates how to start a .NET application, dynamically retrieve the port assigned by Kestrel, and then expose this port to the Microcks Testcontainers instance. This ensures that the Microcks container can access the running application for contract testing. ```C# using var app = ApplicationBuilder.Create(); app.Urls.Add("http://127.0.0.1:0"); await app.StartAsync(); // Get the port assigned by Kestrel var port = app.Services.GetRequiredService() .Features .Get() .Addresses .First() .Split(':') .Last(); // Expose the port to the Microcks container IEnumerable ports = [(ushort)port]; await TestcontainersSettings.ExposeHostPortsAsync(ports) .ConfigureAwait(false); ``` -------------------------------- ### Initializing Kafka and Microcks Ensemble for Async API Testing (C#) Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet demonstrates how to initialize and start a KafkaContainer and a MicrocksContainersEnsemble for asynchronous API testing. It configures Kafka with a specific listener and connects Microcks to it using WithKafkaConnection, enabling the ensemble to interact with the Kafka broker for mocking and testing. ```C# KafkaContainer kafkaContainer new KafkaBuilder() .WithImage(KafkaImage) .WithNetwork(network) .WithNetworkAliases("kafka") .WithListener("kafka:19092") .Build(); await kafkaContainer.StartAsync(); MicrocksContainersEnsemble ensemble = new MicrocksContainerEnsemble(network, MicrocksImage) .WithMainArtifacts("pastry-orders-asyncapi.yml") .WithKafkaConnection(new KafkaConnection($"kafka:19092")); await ensemble.StartAsync(); ``` -------------------------------- ### Performing Asynchronous Contract Testing with Microcks (C#) Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet illustrates how to initiate and finalize an asynchronous contract test using the TestEndpointAsync method of the MicrocksContainer. It starts the test, allows the System Under Test to produce events, and then awaits the TestResult to assert the success of the test. ```C# // Start the test, making Microcks listen the endpoint provided in testRequest Task testResultFuture = ensemble.MicrocksContainer.TestEndpointAsync(testRequest); // Here below: activate your app to make it produce events on this endpoint. // myapp.InvokeBusinessMethodThatTriggerEvents(); // Now retrieve the final test result and assert. TestResult testResult = await testResultFuture; testResult.IsSuccess.Should().BeTrue(); ``` -------------------------------- ### Getting Mock Endpoint Invocation Count in Microcks C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet shows how to retrieve the exact number of times a specific mock endpoint has been invoked. The `GetServiceInvocationsCount()` method returns an integer representing the total number of invocations for the specified service and version, providing detailed usage statistics. ```csharp var serviceInvocationsCount = container.GetServiceInvocationsCount("API Pastries", "0.0.1"); ``` -------------------------------- ### Launching OpenAPI Contract Tests with Microcks Testcontainers in C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet demonstrates how to initialize a Microcks container and execute an OpenAPI schema contract test against a locally running application. It configures a TestRequest with the service ID, runner type, test endpoint, and timeout, then asserts the success of the test result. ```C# private int port; public async Task InitializeAsync() { container = new MicrocksBuilder() .WithExposedPort(port) .Build(); await container.StartAsync(); } [Fact] public async Task testOpenAPIContract() { var testRequest = new TestRequest { ServiceId = "API Pastries:0.0.1", RunnerType = TestRunnerType.OPEN_API_SCHEMA, TestEndpoint = $"http://host.testcontainers.internal:{port}", Timeout = TimeSpan.FromMilliseconds(2000) }; TestResult testResult = await container.TestEndpointAsync(testRequest); testResult.Success.Should().BeTrue(); } ``` -------------------------------- ### Refactoring Program.cs for External Port Exposure in C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This code snippet illustrates how to refactor the `Program.cs` file in a .NET application to include a `Main` method. This change is necessary to allow the application to be hosted on a specific, accessible port, which is crucial for external services like Microcks to interact with it. ```C# public class Program { public static void Main(string[] args) { var app = ApplicationBuilder.Create(args); app.Run(); } } ``` -------------------------------- ### Enabling Async Features with MicrocksContainersEnsemble in C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet illustrates how to activate asynchronous features, such as WebSocket support, on a `MicrocksContainersEnsemble`. By using the `WithAsyncFeature()` method during initialization, the ensemble is configured to handle advanced asynchronous contract testing scenarios. ```C# MicrocksContainersEnsemble ensemble = new MicrocksContainerEnsemble(network, MicrocksImage) .WithMainArtifacts("pastry-orders-asyncapi.yml") .WithAsyncFeature(); await ensemble.StartAsync(); ``` -------------------------------- ### Retrieving Kafka Mock Topic from Microcks Async Minion (C#) Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet shows how to retrieve the mock Kafka topic name from the AsyncMinionContainer of the Microcks ensemble. It uses the GetKafkaMockTopic method, providing the API name, version, and operation to obtain the dynamically generated topic for mocking purposes. ```C# string kafkaTopic = ensemble.AsyncMinionContainer .GetKafkaMockTopic("Pastry orders API", "0.1.0", "SUBSCRIBE pastry/orders"); ``` -------------------------------- ### Adding Microcks Testcontainers Package to .NET Project Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This command adds the Microcks.Testcontainers NuGet package to a .NET project. It specifies version 0.2.0, ensuring that the project uses a specific, stable release of the library for embedding Microcks instances in tests. ```Shell dotnet add package Microcks.Testcontainers --version 0.2.0 ``` -------------------------------- ### Accessing MicrocksContainer from Ensemble in C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This code demonstrates how to access the underlying `MicrocksContainer` instance when working with a `MicrocksContainersEnsemble`. The `MicrocksContainer` is wrapped by the ensemble and can still be used for operations like importing artifacts. ```C# MicrocksContainer microcks = ensemble.MicrocksContainer; microcks.ImportAsMainArtifact(...); ``` -------------------------------- ### Verifying Mock Endpoint Invocation in Microcks C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This code demonstrates how to verify if a specific mock endpoint has been invoked within the Microcks container. The `Verify()` method checks the invocation count for the given service and version, returning a boolean indicating whether at least one invocation occurred. ```csharp var invoked = container.Verify("API Pastries", "0.0.1"); ``` -------------------------------- ### Retrieving REST Mock Endpoints from Microcks Container C# Source: https://github.com/microcks/microcks-testcontainers-dotnet/blob/main/README.md This snippet shows how to retrieve a mock endpoint URL for a specific REST API from the running Microcks container. The `GetRestMockEndpoint()` method takes the service name and version as parameters, returning the base URL for the mocked API, which can then be used to configure client calls. ```csharp var baseApiUrl = container.GetRestMockEndpoint("API Pastries", "0.0.1"); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.