Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bottomly.net/Bottomly.AppHost/.aspire/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"appHostPath": "../Bottomly.AppHost.csproj"
}
31 changes: 31 additions & 0 deletions bottomly.net/Bottomly.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17281;http://localhost:15221",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21226",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23133",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22202"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15221",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19053",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18014",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20264"
}
}
}
}
31 changes: 31 additions & 0 deletions bottomly.net/Bottomly.AppHost/apphost.run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17219;http://localhost:15148",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21009",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23079",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22198"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15148",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19120",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18106",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20045"
}
}
}
}
8 changes: 8 additions & 0 deletions bottomly.net/Bottomly.AppHost/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions bottomly.net/Bottomly.AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>

<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.1.0"/>
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="10.1.0"/>
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.14.0"/>
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.14.0"/>
</ItemGroup>

</Project>
129 changes: 129 additions & 0 deletions bottomly.net/Bottomly.ServiceDefaults/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
private const string HealthEndpointPath = "/health";
private const string AlivenessEndpointPath = "/alive";

public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.AddServiceDiscovery();
});

// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder)
where TBuilder : IHostApplicationBuilder
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation(tracing =>
// Exclude health check requests from tracing
tracing.Filter = context =>
!context.Request.Path.StartsWithSegments(HealthEndpointPath)
&& !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
)
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder)
where TBuilder : IHostApplicationBuilder
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}

return builder;
}

public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder)
where TBuilder : IHostApplicationBuilder
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);

return builder;
}

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
if (app.Environment.IsDevelopment())
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks(HealthEndpointPath);

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}

return app;
}
}
29 changes: 29 additions & 0 deletions bottomly.net/Bottomly.Tests/Bottomly.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<UserSecretsId>daba8c84-3cbb-425d-9103-50c95448cdb3</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.4"/>
<PackageReference Include="Microsoft.Extensions.Options" Version="10.0.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1"/>
<PackageReference Include="Moq" Version="4.20.72"/>
<PackageReference Include="Shouldly" Version="4.3.0"/>
<PackageReference Include="xunit" Version="2.9.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Bottomly\Bottomly.csproj"/>
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions bottomly.net/Bottomly.Tests/Commands/AddKarmaCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Bottomly.Commands;
using Bottomly.Models;
using Bottomly.Repositories;
using Moq;
using Shouldly;

namespace Bottomly.Tests.Commands;

public class AddKarmaCommandTests
{
private readonly AddKarmaCommand _command;
private readonly Mock<IKarmaRepository> _mockRepo = new();

public AddKarmaCommandTests() => _command = new AddKarmaCommand(_mockRepo.Object);

[Fact]
public async Task ExecuteAsync_AwardsKarma_PersistsToRepository()
{
_mockRepo.Setup(r => r.AddAsync(It.IsAny<Karma>())).Returns(Task.CompletedTask);

await _command.ExecuteAsync("alice", "bob", "great job", KarmaType.PozzyPoz);

_mockRepo.Verify(r => r.AddAsync(It.Is<Karma>(k =>
k.AwardedToUsername == "alice" &&
k.AwardedByUsername == "bob" &&
k.Reason == "great job" &&
k.KarmaType == KarmaType.PozzyPoz)), Times.Once());
}

[Fact]
public async Task ExecuteAsync_SelfPositiveKarma_ThrowsInvalidOperation() =>
await Should.ThrowAsync<InvalidOperationException>(() =>
_command.ExecuteAsync("alice", "alice", "", KarmaType.PozzyPoz));

[Fact]
public async Task ExecuteAsync_SelfNegativeKarma_DoesNotThrow()
{
_mockRepo.Setup(r => r.AddAsync(It.IsAny<Karma>())).Returns(Task.CompletedTask);

await Should.NotThrowAsync(() => _command.ExecuteAsync("alice", "alice", "", KarmaType.NeggyNeg));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Bottomly.Commands;
using Bottomly.Configuration;
using Microsoft.Extensions.Options;
using Shouldly;

namespace Bottomly.Tests.Commands;

public class GoogleImageSearchCommandTests
{
[Fact]
public async Task ExecuteAsync_EmptyInput_ReturnsNull()
{
var options = Options.Create(new BottomlyOptions { GoogleApiKey = "key", GoogleCseId = "cse" });
var command = new GoogleImageSearchCommand(options);

var result = await command.ExecuteAsync("");

result.ShouldBeNull();
}
}
20 changes: 20 additions & 0 deletions bottomly.net/Bottomly.Tests/Commands/GoogleSearchCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Bottomly.Commands;
using Bottomly.Configuration;
using Microsoft.Extensions.Options;
using Shouldly;

namespace Bottomly.Tests.Commands;

public class GoogleSearchCommandTests
{
[Fact]
public async Task ExecuteAsync_EmptyInput_ReturnsNull()
{
var options = Options.Create(new BottomlyOptions { GoogleApiKey = "key", GoogleCseId = "cse" });
var command = new GoogleSearchCommand(options);

var result = await command.ExecuteAsync("");

result.ShouldBeNull();
}
}
Loading
Loading