Skip to content

[Health]: Test Case for Rest and GraphQL Requests to DAB during health check #2667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

sezal98
Copy link
Contributor

@sezal98 sezal98 commented Apr 28, 2025

Why make this change?

Resolves #2662

What is this change?

  1. Created the HttpUtilities with HttpClient already instantiated and basic details filled from Startup.cs
  2. Using this httpClient, call the REST and GraphQL requests with relative URI
  3. Created a function in DatabaseObject to access mapping dict as it is an abstract class which was not allowed to be mocked.
  4. Test cases for both REST and GraphQL
  5. Rest Test case: Mock the client to return a 200 response when we receive a GET request with specific URI. Validate the error message from HttpUtilities should be null.
  6. GraphQL Test case: Mock the client to return a 200 response when we get a POST request with /graphql URI and content payload. Mock the Dabase Object with columns in the entity map to only return the db object when a certain entity is called for.

How was this tested?

  • Integration Tests
  • Unit Tests

Sample Request(s)

Requests are running as expected. Test cases added for Rest and Graphql

@Copilot Copilot AI review requested due to automatic review settings April 28, 2025 11:41
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds unit tests for REST and GraphQL health check endpoints while refactoring the HTTP client configuration and health check utilities. Key changes include:

  • Configuring a named HttpClient in Startup to set up base addresses and default headers.
  • Refactoring HttpUtilities methods to use IHttpClientFactory with asynchronous REST calls.
  • Updating test configurations and helper methods to support asynchronous health check processing.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Service/Startup.cs Adds a named HttpClient for context-aware health check requests.
src/Service/HealthCheck/Utilities.cs Updates REST query string formatting for health check endpoints.
src/Service/HealthCheck/HttpUtilities.cs Refactors REST calls to async using IHttpClientFactory and updates GraphQL query handling.
src/Service/HealthCheck/HealthCheckHelper.cs Converts helper methods to async to support non-blocking health check responses.
src/Service/HealthCheck/ComprehensiveHealthReportResponseWriter.cs Updates response writing to be asynchronous.
src/Service.Tests/Configuration/ConfigurationTests.cs Removes unused options in custom configuration file creation and hardcodes GraphQL enablement.
src/Core/Services/MetadataProviders/ISqlMetadataProvider.cs Adds a helper method for accessing database objects by key.
src/Config/DatabasePrimitives/DatabaseObject.cs Allows overriding of the SourceDefinition property for testability.
Comments suppressed due to low confidence (2)

src/Service/HealthCheck/HttpUtilities.cs:122

  • For consistency with the async ExecuteRestQuery, consider converting ExecuteGraphQLQuery to an async method and await the HttpClient call instead of using .Result.
public string? ExecuteGraphQLQuery(string graphqlUriSuffix, string entityName, Entity entity, string incomingRoleHeader, string incomingRoleToken)

src/Service.Tests/Configuration/ConfigurationTests.cs:4457

  • Since the function previously accepted multiple flags for GraphQL and health check enablement, ensure that hardcoding GraphQL as enabled aligns with the intended test scenarios and document the change accordingly.
private static void CreateCustomConfigFile(Dictionary<string, Entity> entityMap, bool enableGlobalRest = true)

@sezal98
Copy link
Contributor Author

sezal98 commented Apr 28, 2025

/azp run

Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@sezal98
Copy link
Contributor Author

sezal98 commented Apr 30, 2025

/azp run

Copy link

Azure Pipelines successfully started running 6 pipeline(s).

{
Assert.Fail();
}
HealthEndpointTests.ValidateBasicDetailsHealthCheckResponse(responseProperties);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: missing tab to align with previous code

@@ -4667,22 +4454,20 @@ public async Task TestNoDepthLimitOnGrahQLInNonHostedMode(int? depthLimit)
/// </summary>
/// <param name="entityMap">Collection of entityName -> Entity object.</param>
/// <param name="enableGlobalRest">flag to enable or disabled REST globally.</param>
private static void CreateCustomConfigFile(Dictionary<string, Entity> entityMap, bool enableGlobalRest = true, bool enableGlobalGraphql = true, bool enableGlobalHealth = true, bool enableDatasourceHealth = true, HostMode hostMode = HostMode.Production)
private static void CreateCustomConfigFile(Dictionary<string, Entity> entityMap, bool enableGlobalRest = true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is enableGlobalGraphQL not used by any of the existing tests?

@@ -270,6 +271,27 @@ public void ConfigureServices(IServiceCollection services)
//Enable accessing HttpContext in RestService to get ClaimsPrincipal.
services.AddHttpContextAccessor();

services.AddHttpClient("ContextConfiguredHealthCheckClient")
.ConfigureHttpClient((serviceProvider, client) =>
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does adding HttpClient to the service provider help with testing?

// Act
string errorMessageFromGraphQL = httpUtilities.ExecuteGraphQLQuery(
graphqlUriSuffix: "/graphql",
entityName: runtimeConfig.Entities.First().Key,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this test have caught the bug reported in this #2645 ?

incomingRoleToken: string.Empty);

// Assert
Assert.IsNull(errorMessageFromGraphQL);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a test for the failure case as well. Mock a failed response, and verify the httputilities returns the error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[HealthEndpoint] Test cases for GraphQL and REST in Health Check Report
2 participants