Add unit tests for Router configuration and transport layers
Some checks failed
Docs CI / lint-and-preview (push) Has been cancelled
Policy Lint & Smoke / policy-lint (push) Has been cancelled

- Implemented tests for RouterConfig, RoutingOptions, StaticInstanceConfig, and RouterConfigOptions to ensure default values are set correctly.
- Added tests for RouterConfigProvider to validate configurations and ensure defaults are returned when no file is specified.
- Created tests for ConfigValidationResult to check success and error scenarios.
- Developed tests for ServiceCollectionExtensions to verify service registration for RouterConfig.
- Introduced UdpTransportTests to validate serialization, connection, request-response, and error handling in UDP transport.
- Added scripts for signing authority gaps and hashing DevPortal SDK snippets.
This commit is contained in:
StellaOps Bot
2025-12-05 08:01:47 +02:00
parent 635c70e828
commit 6a299d231f
294 changed files with 28434 additions and 1329 deletions

View File

@@ -0,0 +1,70 @@
using Microsoft.Extensions.Logging;
using StellaOps.Microservice;
namespace Examples.Billing.Microservice.Endpoints;
/// <summary>
/// Request model for creating an invoice.
/// </summary>
public sealed record CreateInvoiceRequest
{
public required string CustomerId { get; init; }
public required decimal Amount { get; init; }
public string? Description { get; init; }
public List<LineItem> LineItems { get; init; } = [];
}
/// <summary>
/// Line item for an invoice.
/// </summary>
public sealed record LineItem
{
public required string Description { get; init; }
public required decimal Amount { get; init; }
public int Quantity { get; init; } = 1;
}
/// <summary>
/// Response model after creating an invoice.
/// </summary>
public sealed record CreateInvoiceResponse
{
public required string InvoiceId { get; init; }
public required DateTime CreatedAt { get; init; }
public required string Status { get; init; }
}
/// <summary>
/// Endpoint for creating a new invoice.
/// Demonstrates a typed endpoint with JSON request/response.
/// </summary>
[StellaEndpoint("POST", "/invoices", TimeoutSeconds = 30)]
public sealed class CreateInvoiceEndpoint : IStellaEndpoint<CreateInvoiceRequest, CreateInvoiceResponse>
{
private readonly ILogger<CreateInvoiceEndpoint> _logger;
public CreateInvoiceEndpoint(ILogger<CreateInvoiceEndpoint> logger)
{
_logger = logger;
}
public Task<CreateInvoiceResponse> HandleAsync(
CreateInvoiceRequest request,
CancellationToken cancellationToken)
{
_logger.LogInformation(
"Creating invoice for customer {CustomerId} with amount {Amount}",
request.CustomerId,
request.Amount);
// Simulate invoice creation
var invoiceId = $"INV-{Guid.NewGuid():N}".ToUpperInvariant()[..16];
return Task.FromResult(new CreateInvoiceResponse
{
InvoiceId = invoiceId,
CreatedAt = DateTime.UtcNow,
Status = "draft"
});
}
}

View File

@@ -0,0 +1,58 @@
using Microsoft.Extensions.Logging;
using StellaOps.Microservice;
namespace Examples.Billing.Microservice.Endpoints;
/// <summary>
/// Request model for getting an invoice.
/// </summary>
public sealed record GetInvoiceRequest
{
public required string Id { get; init; }
}
/// <summary>
/// Response model for an invoice.
/// </summary>
public sealed record GetInvoiceResponse
{
public required string InvoiceId { get; init; }
public required string CustomerId { get; init; }
public required decimal Amount { get; init; }
public required string Status { get; init; }
public required DateTime CreatedAt { get; init; }
public DateTime? PaidAt { get; init; }
}
/// <summary>
/// Endpoint for retrieving an invoice by ID.
/// Demonstrates a GET endpoint with path parameters.
/// </summary>
[StellaEndpoint("GET", "/invoices/{id}", TimeoutSeconds = 10, RequiredClaims = ["invoices:read"])]
public sealed class GetInvoiceEndpoint : IStellaEndpoint<GetInvoiceRequest, GetInvoiceResponse>
{
private readonly ILogger<GetInvoiceEndpoint> _logger;
public GetInvoiceEndpoint(ILogger<GetInvoiceEndpoint> logger)
{
_logger = logger;
}
public Task<GetInvoiceResponse> HandleAsync(
GetInvoiceRequest request,
CancellationToken cancellationToken)
{
_logger.LogInformation("Fetching invoice {InvoiceId}", request.Id);
// Simulate invoice lookup
return Task.FromResult(new GetInvoiceResponse
{
InvoiceId = request.Id,
CustomerId = "CUST-001",
Amount = 199.99m,
Status = "paid",
CreatedAt = DateTime.UtcNow.AddDays(-7),
PaidAt = DateTime.UtcNow.AddDays(-1)
});
}
}

View File

@@ -0,0 +1,60 @@
using System.Text.Json;
using Microsoft.Extensions.Logging;
using StellaOps.Microservice;
namespace Examples.Billing.Microservice.Endpoints;
/// <summary>
/// Endpoint for uploading attachments to an invoice.
/// Demonstrates streaming upload using IRawStellaEndpoint.
/// </summary>
[StellaEndpoint("POST", "/invoices/{id}/attachments", SupportsStreaming = true, TimeoutSeconds = 300)]
public sealed class UploadAttachmentEndpoint : IRawStellaEndpoint
{
private readonly ILogger<UploadAttachmentEndpoint> _logger;
public UploadAttachmentEndpoint(ILogger<UploadAttachmentEndpoint> logger)
{
_logger = logger;
}
public async Task<RawResponse> HandleAsync(
RawRequestContext context,
CancellationToken cancellationToken)
{
var invoiceId = context.PathParameters.GetValueOrDefault("id") ?? "unknown";
var contentType = context.Headers["Content-Type"] ?? "application/octet-stream";
_logger.LogInformation(
"Uploading attachment for invoice {InvoiceId}, Content-Type: {ContentType}",
invoiceId,
contentType);
// Read the streamed body
long totalBytes = 0;
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await context.Body.ReadAsync(buffer, cancellationToken)) > 0)
{
totalBytes += bytesRead;
// In a real implementation, you would write to storage here
}
_logger.LogInformation(
"Received {TotalBytes} bytes for invoice {InvoiceId}",
totalBytes,
invoiceId);
// Return success response
var response = new
{
invoiceId,
attachmentId = $"ATT-{Guid.NewGuid():N}"[..16].ToUpperInvariant(),
size = totalBytes,
uploadedAt = DateTime.UtcNow
};
return RawResponse.Ok(JsonSerializer.Serialize(response));
}
}

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>preview</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0-rc.2.25502.107" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\src\__Libraries\StellaOps.Microservice\StellaOps.Microservice.csproj" />
<ProjectReference Include="..\..\..\..\src\__Libraries\StellaOps.Router.Common\StellaOps.Router.Common.csproj" />
<ProjectReference Include="..\..\..\..\src\__Libraries\StellaOps.Router.Transport.InMemory\StellaOps.Router.Transport.InMemory.csproj" />
<!-- Reference the source generator -->
<ProjectReference Include="..\..\..\..\src\__Libraries\StellaOps.Microservice.SourceGen\StellaOps.Microservice.SourceGen.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<Content Include="microservice.yaml" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,40 @@
using Examples.Billing.Microservice.Endpoints;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StellaOps.Microservice;
using StellaOps.Router.Common.Enums;
using StellaOps.Router.Transport.InMemory;
var builder = Host.CreateApplicationBuilder(args);
// Configure the Stella microservice
builder.Services.AddStellaMicroservice(options =>
{
options.ServiceName = "billing";
options.Version = "1.0.0";
options.Region = "demo";
options.InstanceId = $"billing-{Environment.MachineName}";
options.ConfigFilePath = "microservice.yaml";
options.Routers =
[
new RouterEndpointConfig
{
Host = "localhost",
Port = 5100,
TransportType = TransportType.InMemory
}
];
});
// Register endpoint handlers
builder.Services.AddScoped<CreateInvoiceEndpoint>();
builder.Services.AddScoped<GetInvoiceEndpoint>();
builder.Services.AddScoped<UploadAttachmentEndpoint>();
// Add in-memory transport
builder.Services.AddInMemoryTransport();
var host = builder.Build();
Console.WriteLine("Billing microservice starting...");
await host.RunAsync();

View File

@@ -0,0 +1,21 @@
# Microservice YAML Configuration for Billing Service
# Overrides code-defined endpoint settings
endpoints:
# Override timeout for invoice creation
- method: POST
path: /invoices
timeout: 45s # Allow more time for complex invoice creation
# Override streaming settings for file upload
- method: POST
path: /invoices/{id}/attachments
timeout: 5m # Allow large file uploads
streaming: true
# Add claim requirements for getting invoices
- method: GET
path: /invoices/{id}
requiringClaims:
- type: "scope"
value: "invoices:read"