diff --git a/Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs b/Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs index bd0b69c..2e21c64 100644 --- a/Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs +++ b/Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs @@ -15,10 +15,10 @@ namespace Microservice.Customer.Api.Test.Unit; [TestFixture] public class GetCustomerMediatrTests { - private Mock customerRepositoryMock = new(); - private Mock customerHttpAccessorMock = new(); - private Mock> loggerMock = new(); - private ServiceCollection services = new(); + private readonly Mock customerRepositoryMock = new(); + private readonly Mock customerHttpAccessorMock = new(); + private readonly Mock> loggerMock = new(); + private readonly ServiceCollection services = new(); private ServiceProvider serviceProvider; private IMediator mediator; @@ -60,7 +60,7 @@ public async Task Get_customer_return_customer() customerRepositoryMock .Setup(x => x.ByIdAsync(customerId)) - .Returns(Task.FromResult(customer)); + .Returns(Task.FromResult(customer ?? null)); var getCustomerRequest = new GetCustomerRequest(customerId); diff --git a/Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs b/Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs index 6a42770..d9d510f 100644 --- a/Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs +++ b/Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs @@ -1,3 +1,5 @@ +// Ignore Spelling: Mediatr + using FluentValidation; using MediatR; using Microservice.Customer.Api.Data.Repository.Interfaces; @@ -14,10 +16,10 @@ namespace Microservice.Customer.Api.Test.Unit; [TestFixture] public class UpdateCustomerMediatrTests { - private Mock customerRepositoryMock = new(); - private Mock customerHttpAccessorMock = new(); - private Mock> loggerMock = new(); - private ServiceCollection services = new(); + private readonly Mock customerRepositoryMock = new(); + private readonly Mock customerHttpAccessorMock = new(); + private readonly Mock> loggerMock = new(); + private readonly ServiceCollection services = new(); private ServiceProvider serviceProvider; private IMediator mediator; private Guid customerId; @@ -59,25 +61,28 @@ public async Task Customer_updated_return_success_message() { var customerId = Guid.NewGuid(); - var customer = new Domain.Customer() { Id = customerId, Email = "ValidEmail@hotmail.com", Surname = "TestSurname", FirstName = "TestFirstName" }; + Domain.Customer? customer = new Domain.Customer() { Id = customerId, Email = "ValidEmail@hotmail.com", Surname = "TestSurname", FirstName = "TestFirstName" }; customerHttpAccessorMock.Setup(x => x.CustomerId) .Returns(customerId); customerRepositoryMock .Setup(x => x.ByIdAsync(customerId)) - .Returns(Task.FromResult(customer)); + .Returns(Task.FromResult(customer ?? null)); customerRepositoryMock .Setup(x => x.ExistsAsync(customerId)) .Returns(Task.FromResult(true)); - customer.Surname = "Changed Surname"; - customer.FirstName = "Changed FirstName"; - customer.Email = "Changed Email"; + if (customer is not null) + { + customer.Surname = "Changed Surname"; + customer.FirstName = "Changed FirstName"; + customer.Email = "Changed Email"; - customerRepositoryMock - .Setup(x => x.UpdateAsync(customer)); + customerRepositoryMock + .Setup(x => x.UpdateAsync(customer)); + } var updateCustomerRequest = new UpdateCustomerRequest(customerId, "ValidEmail@hotmail.com", "TestSurname", "TestFirstName"); @@ -142,7 +147,7 @@ public void Customer_not_updated_invalid_email_return_exception_fail_message() } [Test] - public void Customer_not_updated_invalid_surname_firstname_return_exception_fail_message() + public void Customer_not_updated_invalid_surname_first_name_return_exception_fail_message() { customerRepositoryMock .Setup(x => x.ExistsAsync("ValidEmail@hotmail.com")) diff --git a/Microservice.Customer.Api/Data/Repository/CustomerRepository.cs b/Microservice.Customer.Api/Data/Repository/CustomerRepository.cs index 36a036a..dca21d1 100644 --- a/Microservice.Customer.Api/Data/Repository/CustomerRepository.cs +++ b/Microservice.Customer.Api/Data/Repository/CustomerRepository.cs @@ -30,7 +30,7 @@ public async Task UpdateAsync(Domain.Customer entity) return await db.Customer.AsNoTracking().ToListAsync(); } - public async Task ByIdAsync(Guid id) + public async Task ByIdAsync(Guid id) { await using var db = await _dbContextFactory.CreateDbContextAsync(); return await db.Customer.FindAsync(id); diff --git a/Microservice.Customer.Api/Data/Repository/Interfaces/ICustomerRepository.cs b/Microservice.Customer.Api/Data/Repository/Interfaces/ICustomerRepository.cs index 8d09f1d..16b223c 100644 --- a/Microservice.Customer.Api/Data/Repository/Interfaces/ICustomerRepository.cs +++ b/Microservice.Customer.Api/Data/Repository/Interfaces/ICustomerRepository.cs @@ -5,7 +5,7 @@ public interface ICustomerRepository Task AddAsync(Domain.Customer customer); Task UpdateAsync(Domain.Customer entity); Task> AllAsync(); - Task ByIdAsync(Guid id); + Task ByIdAsync(Guid id); Task ExistsAsync(string email); Task ExistsAsync(string email, Guid id); Task ExistsAsync(Guid id); diff --git a/Microservice.Customer.Api/Domain/Customer.cs b/Microservice.Customer.Api/Domain/Customer.cs index 7da3b7b..70259b0 100644 --- a/Microservice.Customer.Api/Domain/Customer.cs +++ b/Microservice.Customer.Api/Domain/Customer.cs @@ -11,15 +11,15 @@ public class Customer [MaxLength(30)] [Required] - public string Surname { get; set; } + public string Surname { get; set; } = string.Empty; [MaxLength(30)] [Required] - public string FirstName { get; set; } + public string FirstName { get; set; } = string.Empty; [EmailAddress] [Required] - public string Email { get; set; } + public string Email { get; set; } = string.Empty; [Required] public DateTime Created { get; set; } = DateTime.Now; diff --git a/Microservice.Customer.Api/Helpers/EnvironmentVariables.cs b/Microservice.Customer.Api/Helpers/EnvironmentVariables.cs index 28a9139..98efe28 100644 --- a/Microservice.Customer.Api/Helpers/EnvironmentVariables.cs +++ b/Microservice.Customer.Api/Helpers/EnvironmentVariables.cs @@ -1,8 +1,10 @@ -namespace Microservice.Customer.Api.Helpers; +// Ignore Spelling: Jwt + +namespace Microservice.Customer.Api.Helpers; public class EnvironmentVariables { - public static string JwtIssuer = Environment.GetEnvironmentVariable(Constants.JwtIssuer); - public static string JwtAudience = Environment.GetEnvironmentVariable(Constants.JwtAudience); - public static string JwtSymmetricSecurityKey = Environment.GetEnvironmentVariable(Constants.JwtSymmetricSecurityKey); + public static string JwtIssuer = Environment.GetEnvironmentVariable(Constants.JwtIssuer) ?? "Environment Variable JwtIssuer not found."; + public static string JwtAudience = Environment.GetEnvironmentVariable(Constants.JwtAudience) ?? "Environment Variable JwtAudience not found."; + public static string JwtSymmetricSecurityKey = Environment.GetEnvironmentVariable(Constants.JwtSymmetricSecurityKey) ?? "Environment Variable JwtSymmetricSecurityKey not found."; } \ No newline at end of file diff --git a/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerQueryHandler.cs b/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerQueryHandler.cs index 86d82b6..9b3e41e 100644 --- a/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerQueryHandler.cs +++ b/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerQueryHandler.cs @@ -18,7 +18,7 @@ public async Task Handle(GetCustomerRequest getCustomerRequ var customer = await _customerRepository.ByIdAsync(getCustomerRequest.Id); if (customer == null) { - _logger.LogError($"Customer not found - {getCustomerRequest.Id}"); + _logger.LogError($"Customer not found - { getCustomerRequest.Id }"); throw new NotFoundException("Customer not found."); } diff --git a/Microservice.Customer.Api/Microservice.Customer.Api.csproj b/Microservice.Customer.Api/Microservice.Customer.Api.csproj index f46a85b..dea68f1 100644 --- a/Microservice.Customer.Api/Microservice.Customer.Api.csproj +++ b/Microservice.Customer.Api/Microservice.Customer.Api.csproj @@ -2,7 +2,7 @@ net8.0 - disable + enable enable false b2c27b2e-bd15-497f-93fa-fd78705e6a39