Skip to content

Commit

Permalink
Make project nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
John Miller committed Aug 23, 2024
1 parent 265f4e9 commit 5ed8edf
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 28 deletions.
10 changes: 5 additions & 5 deletions Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ namespace Microservice.Customer.Api.Test.Unit;
[TestFixture]
public class GetCustomerMediatrTests
{
private Mock<ICustomerRepository> customerRepositoryMock = new();
private Mock<ICustomerHttpAccessor> customerHttpAccessorMock = new();
private Mock<ILogger<GetCustomerQueryHandler>> loggerMock = new();
private ServiceCollection services = new();
private readonly Mock<ICustomerRepository> customerRepositoryMock = new();
private readonly Mock<ICustomerHttpAccessor> customerHttpAccessorMock = new();
private readonly Mock<ILogger<GetCustomerQueryHandler>> loggerMock = new();
private readonly ServiceCollection services = new();
private ServiceProvider serviceProvider;
private IMediator mediator;

Expand Down Expand Up @@ -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);

Expand Down
29 changes: 17 additions & 12 deletions Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Ignore Spelling: Mediatr

using FluentValidation;
using MediatR;
using Microservice.Customer.Api.Data.Repository.Interfaces;
Expand All @@ -14,10 +16,10 @@ namespace Microservice.Customer.Api.Test.Unit;
[TestFixture]
public class UpdateCustomerMediatrTests
{
private Mock<ICustomerRepository> customerRepositoryMock = new();
private Mock<ICustomerHttpAccessor> customerHttpAccessorMock = new();
private Mock<ILogger<UpdateCustomerCommandHandler>> loggerMock = new();
private ServiceCollection services = new();
private readonly Mock<ICustomerRepository> customerRepositoryMock = new();
private readonly Mock<ICustomerHttpAccessor> customerHttpAccessorMock = new();
private readonly Mock<ILogger<UpdateCustomerCommandHandler>> loggerMock = new();
private readonly ServiceCollection services = new();
private ServiceProvider serviceProvider;
private IMediator mediator;
private Guid customerId;
Expand Down Expand Up @@ -59,25 +61,28 @@ public async Task Customer_updated_return_success_message()
{
var customerId = Guid.NewGuid();

var customer = new Domain.Customer() { Id = customerId, Email = "[email protected]", Surname = "TestSurname", FirstName = "TestFirstName" };
Domain.Customer? customer = new Domain.Customer() { Id = customerId, Email = "[email protected]", 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, "[email protected]", "TestSurname", "TestFirstName");

Expand Down Expand Up @@ -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("[email protected]"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task UpdateAsync(Domain.Customer entity)
return await db.Customer.AsNoTracking().ToListAsync();
}

public async Task<Domain.Customer> ByIdAsync(Guid id)
public async Task<Domain.Customer?> ByIdAsync(Guid id)
{
await using var db = await _dbContextFactory.CreateDbContextAsync();
return await db.Customer.FindAsync(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public interface ICustomerRepository
Task<Domain.Customer> AddAsync(Domain.Customer customer);
Task UpdateAsync(Domain.Customer entity);
Task<IEnumerable<Domain.Customer>> AllAsync();
Task<Domain.Customer> ByIdAsync(Guid id);
Task<Domain.Customer?> ByIdAsync(Guid id);
Task<bool> ExistsAsync(string email);
Task<bool> ExistsAsync(string email, Guid id);
Task<bool> ExistsAsync(Guid id);
Expand Down
6 changes: 3 additions & 3 deletions Microservice.Customer.Api/Domain/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions Microservice.Customer.Api/Helpers/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
@@ -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.";
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task<GetCustomerResponse> 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.");
}

Expand Down
2 changes: 1 addition & 1 deletion Microservice.Customer.Api/Microservice.Customer.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>false</InvariantGlobalization>
<UserSecretsId>b2c27b2e-bd15-497f-93fa-fd78705e6a39</UserSecretsId>
Expand Down

0 comments on commit 5ed8edf

Please sign in to comment.