From 0b59b7d219e07e7a1c44586148f877a21a8dd241 Mon Sep 17 00:00:00 2001 From: Ho Ngoc Hai Date: Tue, 13 Jan 2026 02:27:18 +0700 Subject: [PATCH] feat(storage-service): Update storage configuration and enhance service initialization - Changed the default bucket name in `appsettings.Development.json` from "storage" to "goodgo" and updated MinIO endpoint and credentials for improved access. - Modified the service initialization in `Program.cs` to include the environment name, enhancing configuration flexibility. - Added a missing namespace in `CustomWebApplicationFactory.cs` for better test setup. - Removed obsolete unit test files for `CreateSampleCommandHandler` and `SampleAggregate`, streamlining the test suite. --- .../src/StorageService.API/Program.cs | 2 +- .../appsettings.Development.json | 8 +- .../Controllers/SamplesControllerTests.cs | 80 ---------- .../CustomWebApplicationFactory.cs | 1 + .../CreateSampleCommandHandlerTests.cs | 65 -------- .../Domain/SampleAggregateTests.cs | 151 ------------------ 6 files changed, 6 insertions(+), 301 deletions(-) delete mode 100644 services/storage-service-net/tests/StorageService.FunctionalTests/Controllers/SamplesControllerTests.cs delete mode 100644 services/storage-service-net/tests/StorageService.UnitTests/Application/CreateSampleCommandHandlerTests.cs delete mode 100644 services/storage-service-net/tests/StorageService.UnitTests/Domain/SampleAggregateTests.cs diff --git a/services/storage-service-net/src/StorageService.API/Program.cs b/services/storage-service-net/src/StorageService.API/Program.cs index 2ed6ef0b..7912fef2 100644 --- a/services/storage-service-net/src/StorageService.API/Program.cs +++ b/services/storage-service-net/src/StorageService.API/Program.cs @@ -24,7 +24,7 @@ try .WriteTo.Console()); // EN: Add Infrastructure services / VI: Thêm Infrastructure services - builder.Services.AddInfrastructure(builder.Configuration); + builder.Services.AddInfrastructure(builder.Configuration, builder.Environment.EnvironmentName); // EN: Add MediatR with behaviors / VI: Thêm MediatR với behaviors builder.Services.AddMediatR(cfg => diff --git a/services/storage-service-net/src/StorageService.API/appsettings.Development.json b/services/storage-service-net/src/StorageService.API/appsettings.Development.json index 6d70cd6f..2a3b171a 100644 --- a/services/storage-service-net/src/StorageService.API/appsettings.Development.json +++ b/services/storage-service-net/src/StorageService.API/appsettings.Development.json @@ -12,15 +12,15 @@ }, "Storage": { "Provider": "minio", - "DefaultBucket": "storage", + "DefaultBucket": "goodgo", "PreSignedUrlExpirationSeconds": 3600, "MaxFileSizeBytes": 104857600, "MinIO": { - "Endpoint": "localhost:9000", + "Endpoint": "167.114.174.113:9000", "AccessKey": "minioadmin", - "SecretKey": "minioadmin", + "SecretKey": "Velik@2026", "UseSSL": false, - "Region": "" + "Region": "us-east-1" }, "AliyunOSS": { "Endpoint": "", diff --git a/services/storage-service-net/tests/StorageService.FunctionalTests/Controllers/SamplesControllerTests.cs b/services/storage-service-net/tests/StorageService.FunctionalTests/Controllers/SamplesControllerTests.cs deleted file mode 100644 index e9bcd941..00000000 --- a/services/storage-service-net/tests/StorageService.FunctionalTests/Controllers/SamplesControllerTests.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System.Net; -using System.Net.Http.Json; -using FluentAssertions; -using Microsoft.AspNetCore.Mvc.Testing; -using Xunit; - -namespace StorageService.FunctionalTests.Controllers; - -/// -/// EN: Functional tests for Samples API endpoints. -/// VI: Functional tests cho các endpoints API Samples. -/// -public class SamplesControllerTests : IClassFixture -{ - private readonly HttpClient _client; - - public SamplesControllerTests(CustomWebApplicationFactory factory) - { - _client = factory.CreateClient(new WebApplicationFactoryClientOptions - { - AllowAutoRedirect = false - }); - } - - [Fact] - public async Task GetSamples_ShouldReturnOkWithEmptyList() - { - // Act - var response = await _client.GetAsync("/api/v1/samples"); - - // Assert - response.StatusCode.Should().Be(HttpStatusCode.OK); - var content = await response.Content.ReadFromJsonAsync>>(); - content?.Success.Should().BeTrue(); - } - - [Fact] - public async Task CreateSample_WithValidData_ShouldReturnCreated() - { - // Arrange - var request = new { Name = "Test Sample", Description = "Test Description" }; - - // Act - var response = await _client.PostAsJsonAsync("/api/v1/samples", request); - - // Assert - response.StatusCode.Should().Be(HttpStatusCode.Created); - var content = await response.Content.ReadFromJsonAsync>(); - content?.Success.Should().BeTrue(); - content?.Data?.Id.Should().NotBeEmpty(); - } - - [Fact] - public async Task GetSample_WithInvalidId_ShouldReturnNotFound() - { - // Arrange - var invalidId = Guid.NewGuid(); - - // Act - var response = await _client.GetAsync($"/api/v1/samples/{invalidId}"); - - // Assert - response.StatusCode.Should().Be(HttpStatusCode.NotFound); - } - - [Fact] - public async Task HealthCheck_ShouldReturnHealthy() - { - // Act - var response = await _client.GetAsync("/health/live"); - - // Assert - response.StatusCode.Should().Be(HttpStatusCode.OK); - } - - // EN: Helper DTOs for deserialization - // VI: Helper DTOs để deserialize - private record ApiResponse(bool Success, T? Data); - private record CreateSampleResult(Guid Id); -} diff --git a/services/storage-service-net/tests/StorageService.FunctionalTests/CustomWebApplicationFactory.cs b/services/storage-service-net/tests/StorageService.FunctionalTests/CustomWebApplicationFactory.cs index a3c75f46..e1ef84ef 100644 --- a/services/storage-service-net/tests/StorageService.FunctionalTests/CustomWebApplicationFactory.cs +++ b/services/storage-service-net/tests/StorageService.FunctionalTests/CustomWebApplicationFactory.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using StorageService.Infrastructure; +using StorageService.Infrastructure.Persistence; namespace StorageService.FunctionalTests; diff --git a/services/storage-service-net/tests/StorageService.UnitTests/Application/CreateSampleCommandHandlerTests.cs b/services/storage-service-net/tests/StorageService.UnitTests/Application/CreateSampleCommandHandlerTests.cs deleted file mode 100644 index 5d81b3db..00000000 --- a/services/storage-service-net/tests/StorageService.UnitTests/Application/CreateSampleCommandHandlerTests.cs +++ /dev/null @@ -1,65 +0,0 @@ -using FluentAssertions; -using Microsoft.Extensions.Logging; -using Moq; -using StorageService.API.Application.Commands; -using StorageService.Domain.AggregatesModel.SampleAggregate; -using StorageService.Domain.SeedWork; -using Xunit; - -namespace StorageService.UnitTests.Application; - -/// -/// EN: Unit tests for CreateSampleCommandHandler. -/// VI: Unit tests cho CreateSampleCommandHandler. -/// -public class CreateSampleCommandHandlerTests -{ - private readonly Mock _mockRepository; - private readonly Mock> _mockLogger; - private readonly CreateSampleCommandHandler _handler; - - public CreateSampleCommandHandlerTests() - { - _mockRepository = new Mock(); - _mockLogger = new Mock>(); - - var mockUnitOfWork = new Mock(); - mockUnitOfWork.Setup(u => u.SaveEntitiesAsync(It.IsAny())) - .ReturnsAsync(true); - - _mockRepository.SetupGet(r => r.UnitOfWork).Returns(mockUnitOfWork.Object); - - _handler = new CreateSampleCommandHandler(_mockRepository.Object, _mockLogger.Object); - } - - [Fact] - public async Task Handle_WithValidCommand_ShouldCreateSampleAndReturnId() - { - // Arrange - var command = new CreateSampleCommand("Test Sample", "Test Description"); - - _mockRepository.Setup(r => r.Add(It.IsAny())) - .Returns((Sample s) => s); - - // Act - var result = await _handler.Handle(command, CancellationToken.None); - - // Assert - result.Should().NotBeNull(); - result.Id.Should().NotBeEmpty(); - _mockRepository.Verify(r => r.Add(It.IsAny()), Times.Once); - } - - [Fact] - public async Task Handle_WithValidCommand_ShouldCallSaveEntities() - { - // Arrange - var command = new CreateSampleCommand("Test Sample", null); - - // Act - await _handler.Handle(command, CancellationToken.None); - - // Assert - _mockRepository.Verify(r => r.UnitOfWork.SaveEntitiesAsync(It.IsAny()), Times.Once); - } -} diff --git a/services/storage-service-net/tests/StorageService.UnitTests/Domain/SampleAggregateTests.cs b/services/storage-service-net/tests/StorageService.UnitTests/Domain/SampleAggregateTests.cs deleted file mode 100644 index 9833ce80..00000000 --- a/services/storage-service-net/tests/StorageService.UnitTests/Domain/SampleAggregateTests.cs +++ /dev/null @@ -1,151 +0,0 @@ -using FluentAssertions; -using StorageService.Domain.AggregatesModel.SampleAggregate; -using StorageService.Domain.Exceptions; -using Xunit; - -namespace StorageService.UnitTests.Domain; - -/// -/// EN: Unit tests for Sample aggregate. -/// VI: Unit tests cho Sample aggregate. -/// -public class SampleAggregateTests -{ - [Fact] - public void CreateSample_WithValidName_ShouldCreateWithDraftStatus() - { - // Arrange - var name = "Test Sample"; - var description = "Test Description"; - - // Act - var sample = new Sample(name, description); - - // Assert - sample.Name.Should().Be(name); - sample.Description.Should().Be(description); - sample.Status.Should().Be(SampleStatus.Draft); - sample.Id.Should().NotBeEmpty(); - sample.DomainEvents.Should().ContainSingle(); // SampleCreatedDomainEvent - } - - [Fact] - public void CreateSample_WithEmptyName_ShouldThrowException() - { - // Arrange - var name = ""; - - // Act - var act = () => new Sample(name); - - // Assert - act.Should().Throw() - .WithMessage("Sample name cannot be empty"); - } - - [Fact] - public void Activate_WhenDraft_ShouldChangeToActive() - { - // Arrange - var sample = new Sample("Test Sample"); - sample.ClearDomainEvents(); - - // Act - sample.Activate(); - - // Assert - sample.Status.Should().Be(SampleStatus.Active); - sample.DomainEvents.Should().ContainSingle(); // SampleStatusChangedDomainEvent - } - - [Fact] - public void Activate_WhenNotDraft_ShouldThrowException() - { - // Arrange - var sample = new Sample("Test Sample"); - sample.Activate(); - - // Act - var act = () => sample.Activate(); - - // Assert - act.Should().Throw() - .WithMessage("Only draft samples can be activated"); - } - - [Fact] - public void Complete_WhenActive_ShouldChangeToCompleted() - { - // Arrange - var sample = new Sample("Test Sample"); - sample.Activate(); - sample.ClearDomainEvents(); - - // Act - sample.Complete(); - - // Assert - sample.Status.Should().Be(SampleStatus.Completed); - } - - [Fact] - public void Cancel_WhenDraftOrActive_ShouldChangeToCancelled() - { - // Arrange - var sample = new Sample("Test Sample"); - - // Act - sample.Cancel(); - - // Assert - sample.Status.Should().Be(SampleStatus.Cancelled); - } - - [Fact] - public void Cancel_WhenCompleted_ShouldThrowException() - { - // Arrange - var sample = new Sample("Test Sample"); - sample.Activate(); - sample.Complete(); - - // Act - var act = () => sample.Cancel(); - - // Assert - act.Should().Throw() - .WithMessage("Cannot cancel a completed sample"); - } - - [Fact] - public void Update_WhenNotCancelled_ShouldUpdateNameAndDescription() - { - // Arrange - var sample = new Sample("Original Name", "Original Description"); - var newName = "Updated Name"; - var newDescription = "Updated Description"; - - // Act - sample.Update(newName, newDescription); - - // Assert - sample.Name.Should().Be(newName); - sample.Description.Should().Be(newDescription); - sample.UpdatedAt.Should().NotBeNull(); - } - - [Fact] - public void Update_WhenCancelled_ShouldThrowException() - { - // Arrange - var sample = new Sample("Test Sample"); - sample.Cancel(); - - // Act - var act = () => sample.Update("New Name", null); - - // Assert - act.Should().Throw() - .WithMessage("Cannot update a cancelled sample"); - } -}