Skip to content

Commit

Permalink
fix: namespace repository find async & refactor+reformat (#176)
Browse files Browse the repository at this point in the history
* fix: namespace repository find async

* refactor and reformat
  • Loading branch information
chgl authored Jan 26, 2025
1 parent 10a2348 commit 93a2a7b
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/Vfps.StressTests/StressTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public StressTests()
InitialBackoff = TimeSpan.FromSeconds(5),
MaxBackoff = TimeSpan.FromSeconds(30),
BackoffMultiplier = 2,
RetryableStatusCodes = { StatusCode.Unavailable, StatusCode.Internal }
}
RetryableStatusCodes = { StatusCode.Unavailable, StatusCode.Internal },
},
};

var channel = GrpcChannel.ForAddress(
Expand Down
14 changes: 3 additions & 11 deletions src/Vfps.Tests/ControllerTests/FhirControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,9 @@ public async Task CreatePseudonym_WithExistingNamespaceRequested_ShouldSucceed()
{
Parameter = new List<Parameters.ParameterComponent>
{
new Parameters.ParameterComponent
{
Name = "namespace",
Value = new FhirString("existingNamespace"),
},
new Parameters.ParameterComponent
{
Name = "originalValue",
Value = new FhirString("test"),
},
}
new() { Name = "namespace", Value = new FhirString("existingNamespace") },
new() { Name = "originalValue", Value = new FhirString("test") },
},
};

var response = await sut.CreatePseudonym(p);
Expand Down
2 changes: 1 addition & 1 deletion src/Vfps.Tests/MemoryCacheMetricsBackgroundServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class MemoryCacheMetricsBackgroundServiceTests
public async Task MemoryCacheMetricsBackgroundService_ShouldStartAndStopWithoutException()
{
var memoryCache = new MemoryCache(
new MemoryCacheOptions { TrackStatistics = true, SizeLimit = 32, }
new MemoryCacheOptions { TrackStatistics = true, SizeLimit = 32 }
);

var sut = new MemoryCacheMetricsBackgroundService(memoryCache);
Expand Down
12 changes: 6 additions & 6 deletions src/Vfps.Tests/ServiceTests/NamespaceServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ await sut.Invoking(async s => await s.Create(request, TestServerCallContext.Crea
[Fact]
public async Task Get_WithExistingNamespace_ShouldReturnNamespace()
{
var request = new NamespaceServiceGetRequest { Name = "existingNamespace", };
var request = new NamespaceServiceGetRequest { Name = "existingNamespace" };

var response = await sut.Get(request, TestServerCallContext.Create());

Expand All @@ -51,7 +51,7 @@ public async Task GetAll_ShouldReturnAllNamespace()
[Fact]
public async Task Get_WithNonExistingNamespace_ShouldThrowNotFoundException()
{
var request = new NamespaceServiceGetRequest { Name = "notExisting", };
var request = new NamespaceServiceGetRequest { Name = "notExisting" };

var act = () => sut.Get(request, TestServerCallContext.Create());

Expand Down Expand Up @@ -81,7 +81,7 @@ public async Task Create_WithPseudonymLengthZero_ShouldFail()
var request = new NamespaceServiceCreateRequest
{
Name = nameof(Create_WithPseudonymLengthZero_ShouldFail),
PseudonymLength = 0
PseudonymLength = 0,
};

var act = () => sut.Create(request, TestServerCallContext.Create());
Expand All @@ -101,7 +101,7 @@ public async Task Delete_WithExistingNamespace_ShouldDeleteNamespace()

await sut.Create(createRequest, TestServerCallContext.Create());

var deleteRequest = new NamespaceServiceDeleteRequest { Name = "toBeDeleted", };
var deleteRequest = new NamespaceServiceDeleteRequest { Name = "toBeDeleted" };

await sut.Delete(deleteRequest, TestServerCallContext.Create());

Expand Down Expand Up @@ -150,7 +150,7 @@ public async Task Delete_WithNamespaceContainingPseudonyms_ShouldDeleteNamespace

pseudonymCount.Should().Be(pseudonymsToCreateCount);

var deleteRequest = new NamespaceServiceDeleteRequest { Name = createRequest.Name, };
var deleteRequest = new NamespaceServiceDeleteRequest { Name = createRequest.Name };

await sut.Delete(deleteRequest, TestServerCallContext.Create());

Expand All @@ -171,7 +171,7 @@ public async Task Delete_WithNamespaceContainingPseudonyms_ShouldDeleteNamespace
[Fact]
public async Task Delete_WithNonExistingNamespace_ShouldThrowNotFoundException()
{
var request = new NamespaceServiceDeleteRequest { Name = "notExisting", };
var request = new NamespaceServiceDeleteRequest { Name = "notExisting" };

var act = () => sut.Delete(request, TestServerCallContext.Create());

Expand Down
6 changes: 3 additions & 3 deletions src/Vfps.Tests/ServiceTests/PseudonymServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public async Task Create_WithCachingNamespaceRepositoryAndCachingPseudonymReposi
[Fact]
public async Task List_WithEmptyNamespace_ShouldReturnEmptyList()
{
var request = new PseudonymServiceListRequest { Namespace = "emptyNamespace", };
var request = new PseudonymServiceListRequest { Namespace = "emptyNamespace" };

var response = await sut.List(request, TestServerCallContext.Create());

Expand All @@ -212,7 +212,7 @@ public async Task List_WithEmptyNamespace_ShouldReturnEmptyList()
[Fact]
public async Task List_WithNonExistingNamespace_ShouldThrowNotFoundError()
{
var request = new PseudonymServiceListRequest { Namespace = "nonExistingNamespace", };
var request = new PseudonymServiceListRequest { Namespace = "nonExistingNamespace" };

await sut.Invoking(async s => await s.List(request, TestServerCallContext.Create()))
.Should()
Expand All @@ -223,7 +223,7 @@ await sut.Invoking(async s => await s.List(request, TestServerCallContext.Create
[Fact]
public async Task List_WithExistingNonEmptyNamespace_ShouldReturnAllPseudonyms()
{
var request = new PseudonymServiceListRequest { Namespace = "existingNamespace", };
var request = new PseudonymServiceListRequest { Namespace = "existingNamespace" };

var response = await sut.List(request, TestServerCallContext.Create());

Expand Down
14 changes: 3 additions & 11 deletions src/Vfps.Tests/WebAppTests/HttpEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,9 @@ public async Task CreatePseudonym_ShouldSucceed()
{
Parameter = new List<Parameters.ParameterComponent>
{
new Parameters.ParameterComponent
{
Name = "namespace",
Value = new FhirString("existingNamespace"),
},
new Parameters.ParameterComponent
{
Name = "originalValue",
Value = new FhirString("test"),
},
}
new() { Name = "namespace", Value = new FhirString("existingNamespace") },
new() { Name = "originalValue", Value = new FhirString("test") },
},
};

var response = await fhirClient.WholeSystemOperationAsync("create-pseudonym", p);
Expand Down
5 changes: 4 additions & 1 deletion src/Vfps/Data/NamespaceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ CancellationToken cancellationToken
CancellationToken cancellationToken
)
{
return await context.Namespaces.FindAsync(namespaceName, cancellationToken);
return await context.Namespaces.FindAsync(
[namespaceName],
cancellationToken: cancellationToken
);
}
}
28 changes: 10 additions & 18 deletions src/Vfps/Fhir/FhirController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<ObjectResult> CreatePseudonym(
{
Severity = OperationOutcome.IssueSeverity.Error,
Code = OperationOutcome.IssueType.Processing,
Diagnostics = "Received malformed or missing resource"
Diagnostics = "Received malformed or missing resource",
}
);
logger.LogError("Bad Request: received request body is empty.");
Expand All @@ -64,7 +64,7 @@ public async Task<ObjectResult> CreatePseudonym(
Severity = OperationOutcome.IssueSeverity.Error,
Code = OperationOutcome.IssueType.Processing,
Diagnostics =
"namespace and/or originalValue are missing in the Parameters request object"
"namespace and/or originalValue are missing in the Parameters request object",
}
);
return BadRequest(outcome);
Expand All @@ -79,7 +79,7 @@ public async Task<ObjectResult> CreatePseudonym(
{
Severity = OperationOutcome.IssueSeverity.Error,
Code = OperationOutcome.IssueType.Processing,
Diagnostics = $"the namespace '{namespaceName}' could not be found."
Diagnostics = $"the namespace '{namespaceName}' could not be found.",
}
);
return NotFound(outcome);
Expand Down Expand Up @@ -120,7 +120,7 @@ public async Task<ObjectResult> CreatePseudonym(
{
Severity = OperationOutcome.IssueSeverity.Error,
Code = OperationOutcome.IssueType.Processing,
Diagnostics = "failed to store the pseudonym after several retries"
Diagnostics = "failed to store the pseudonym after several retries",
}
);
return StatusCode(500, outcome);
Expand All @@ -131,22 +131,14 @@ public async Task<ObjectResult> CreatePseudonym(
{
Parameter = new List<Parameters.ParameterComponent>
{
new Parameters.ParameterComponent
{
Name = "namespace",
Value = new FhirString(namespaceName),
},
new Parameters.ParameterComponent
{
Name = "originalValue",
Value = new FhirString(originalValue),
},
new Parameters.ParameterComponent
new() { Name = "namespace", Value = new FhirString(namespaceName) },
new() { Name = "originalValue", Value = new FhirString(originalValue) },
new()
{
Name = "pseudonymValue",
Value = new FhirString(upsertedPseudonym.PseudonymValue),
},
}
},
}
);
}
Expand All @@ -164,12 +156,12 @@ public CapabilityStatement GetMetadata()
Status = PublicationStatus.Active,
Date = DateTime.UtcNow.ToString("s", CultureInfo.InvariantCulture),
Kind = CapabilityStatementKind.Instance,
Software = new CapabilityStatement.SoftwareComponent { Name = "VFPS FHIR API", },
Software = new CapabilityStatement.SoftwareComponent { Name = "VFPS FHIR API" },
FhirVersion = FHIRVersion.N4_0_1,
Format = new[] { "application/fhir+json" },
Rest = new List<CapabilityStatement.RestComponent>
{
new() { Mode = CapabilityStatement.RestfulCapabilityMode.Server }
new() { Mode = CapabilityStatement.RestfulCapabilityMode.Server },
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Vfps/Migrations/20221207170557_InitialCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
name: "last_updated_at",
type: "timestamp with time zone",
nullable: false
)
),
},
constraints: table =>
{
Expand Down Expand Up @@ -82,7 +82,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
name: "last_updated_at",
type: "timestamp with time zone",
nullable: false
)
),
},
constraints: table =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Vfps/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
// there's currently no readiness probes depending on external state,
// but in case we ever add one, this prepares the code for it.
// see https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-7.0#separate-readiness-and-liveness-probes
Predicate = healthCheck => healthCheck.Tags.Contains("ready")
Predicate = healthCheck => healthCheck.Tags.Contains("ready"),
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public PseudonymizationMethodsLookup()
new CryptoRandomBase64UrlEncodedGenerator()
},
{ PseudonymGenerationMethod.Sha256HexEncoded, new HexEncodedSha256HashGenerator() },
{ PseudonymGenerationMethod.Uuid4, new Uuid4Generator() }
{ PseudonymGenerationMethod.Uuid4, new Uuid4Generator() },
};
}

Expand Down
16 changes: 8 additions & 8 deletions src/Vfps/Services/NamespaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ ServerCallContext context
Meta = new Meta
{
CreatedAt = Timestamp.FromDateTimeOffset(@namespace.CreatedAt),
LastUpdatedAt = Timestamp.FromDateTimeOffset(@namespace.LastUpdatedAt)
}
}
LastUpdatedAt = Timestamp.FromDateTimeOffset(@namespace.LastUpdatedAt),
},
},
};
}

Expand Down Expand Up @@ -113,9 +113,9 @@ ServerCallContext context
Meta = new Meta
{
CreatedAt = Timestamp.FromDateTimeOffset(@namespace.CreatedAt),
LastUpdatedAt = Timestamp.FromDateTimeOffset(@namespace.LastUpdatedAt)
}
}
LastUpdatedAt = Timestamp.FromDateTimeOffset(@namespace.LastUpdatedAt),
},
},
};
}

Expand Down Expand Up @@ -170,8 +170,8 @@ ServerCallContext context
Meta = new Meta
{
CreatedAt = Timestamp.FromDateTimeOffset(n.CreatedAt),
LastUpdatedAt = Timestamp.FromDateTimeOffset(n.LastUpdatedAt)
}
LastUpdatedAt = Timestamp.FromDateTimeOffset(n.LastUpdatedAt),
},
})
.ToListAsync(context.CancellationToken);

Expand Down
10 changes: 5 additions & 5 deletions src/Vfps/Services/PseudonymService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ServerCallContext context

if (upsertedPseudonym is null)
{
var metadata = new Metadata { { "Namespace", request.Namespace }, };
var metadata = new Metadata { { "Namespace", request.Namespace } };

throw new RpcException(
new Status(
Expand All @@ -98,7 +98,7 @@ ServerCallContext context
CreatedAt = Timestamp.FromDateTimeOffset(upsertedPseudonym.CreatedAt),
LastUpdatedAt = Timestamp.FromDateTimeOffset(upsertedPseudonym.LastUpdatedAt),
},
}
},
};
}

Expand All @@ -118,7 +118,7 @@ ServerCallContext context
var metadata = new Metadata
{
{ "Namespace", request.Namespace },
{ "Pseudonym", request.PseudonymValue }
{ "Pseudonym", request.PseudonymValue },
};

throw new RpcException(
Expand All @@ -142,7 +142,7 @@ ServerCallContext context
CreatedAt = Timestamp.FromDateTimeOffset(pseudonym.CreatedAt),
LastUpdatedAt = Timestamp.FromDateTimeOffset(pseudonym.LastUpdatedAt),
},
}
},
};
}

Expand All @@ -154,7 +154,7 @@ ServerCallContext context
{
if (!Context.Namespaces.Where(n => n.Name == request.Namespace).Any())
{
var metadata = new Metadata { { "Namespace", request.Namespace }, };
var metadata = new Metadata { { "Namespace", request.Namespace } };

throw new RpcException(
new Status(
Expand Down

0 comments on commit 93a2a7b

Please sign in to comment.