Skip to content
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

Add exception messages for data source and entities #2490

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/Cli.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,21 @@ public static Process ExecuteDabCommand(string command, string flags)
}
";

public const string RUNTIME_SECTION = @"
public const string RUNTIME_SECTION_WITH_EMPTY_ENTITIES = RUNTIME_SECTION + "," + @"""entities"": {}";

/// <summary>
/// Runtime section containing both rest and graphql disabled.
/// This is used for validating config to test that exceptions are thrown when both rest and graphql are disabled.
/// </summary>
public const string RUNTIME_SECTION_WITH_DISABLED_REST_GRAPHQL = @"
""runtime"": {
""rest"": {
""path"": ""/api"",
""enabled"": true,
""request-body-strict"": true
""enabled"": false
},
""graphql"": {
""path"": ""/graphql"",
""enabled"": true,
""enabled"": false,
""allow-introspection"": true
},
""host"": {
Expand All @@ -145,18 +150,17 @@ public static Process ExecuteDabCommand(string command, string flags)
""entities"": {}";

/// <summary>
/// Runtime section containing both rest and graphql disabled.
/// This is used for validating config to test that exceptions are thrown when both rest and graphql are disabled.
/// Only Runtime section containing both rest and graphql enabled.
/// </summary>
public const string RUNTIME_SECTION_WITH_DISABLED_REST_GRAPHQL = @"
public const string RUNTIME_SECTION = @"
""runtime"": {
""rest"": {
""path"": ""/api"",
""enabled"": false
""enabled"": true
},
""graphql"": {
""path"": ""/graphql"",
""enabled"": false,
""enabled"": true,
""allow-introspection"": true
},
""host"": {
Expand All @@ -169,8 +173,7 @@ public static Process ExecuteDabCommand(string command, string flags)
""provider"": ""StaticWebApps""
}
}
},
""entities"": {}";
}";

/// <summary>
/// Configuration with unresolved environment variable references on
Expand Down Expand Up @@ -237,16 +240,16 @@ public static Process ExecuteDabCommand(string command, string flags)
/// <summary>
/// A minimal valid config json without any entities. This config string is used in unit tests.
/// </summary>
public const string INITIAL_CONFIG = $"{{{SAMPLE_SCHEMA_DATA_SOURCE},{RUNTIME_SECTION}}}";
public const string INITIAL_COSMOSDB_NOSQL_CONFIG = $"{{{SAMPLE_SCHEMA_DATA_SOURCE_COSMOSDB_NOSQL},{RUNTIME_SECTION}}}";
public const string INITIAL_CONFIG = $"{{{SAMPLE_SCHEMA_DATA_SOURCE},{RUNTIME_SECTION_WITH_EMPTY_ENTITIES}}}";
public const string INITIAL_COSMOSDB_NOSQL_CONFIG = $"{{{SAMPLE_SCHEMA_DATA_SOURCE_COSMOSDB_NOSQL},{RUNTIME_SECTION_WITH_EMPTY_ENTITIES}}}";

/// <summary>
/// A minimal config json without any entities. This config is invalid as it contains an empty connection
/// string. This config is used in tests to verify validation failures.
/// </summary>
public const string INVALID_INTIAL_CONFIG = $"{{{SAMPLE_SCHEMA_DATA_SOURCE_WITH_INVALID_CONNSTRING},{RUNTIME_SECTION}}}";
public const string INVALID_INTIAL_CONFIG = $"{{{SAMPLE_SCHEMA_DATA_SOURCE_WITH_INVALID_CONNSTRING},{RUNTIME_SECTION_WITH_EMPTY_ENTITIES}}}";

public const string CONFIG_WITH_CUSTOM_PROPERTIES = $"{{{SAMPLE_DATA_SOURCE_WITH_CUSTOM_PROPERTIES},{RUNTIME_SECTION}}}";
public const string CONFIG_WITH_CUSTOM_PROPERTIES = $"{{{SAMPLE_DATA_SOURCE_WITH_CUSTOM_PROPERTIES},{RUNTIME_SECTION_WITH_EMPTY_ENTITIES}}}";

public const string CONFIG_WITH_DISABLED_GLOBAL_REST_GRAPHQL = $"{{{SAMPLE_SCHEMA_DATA_SOURCE},{RUNTIME_SECTION_WITH_DISABLED_REST_GRAPHQL}}}";

Expand Down
46 changes: 46 additions & 0 deletions src/Cli.Tests/ValidateConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,52 @@ public void TestValidateConfigFailsWithInvalidGraphQLDepthLimit(object? depthLim
}
}

/// <summary>
/// This Test is used to verify that the validate command is able to catch when data source field or entities field is missing.
sezal98 marked this conversation as resolved.
Show resolved Hide resolved
sezal98 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
[TestMethod]
public void TestValidateConfigFailsWithNoEntities()
{
string ConfigWithoutEntities = $"{{{SAMPLE_SCHEMA_DATA_SOURCE},{RUNTIME_SECTION}}}";

// create an empty config file
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, ConfigWithoutEntities);

ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);

try
{
Assert.IsFalse(ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!));
}
catch (Exception ex)
{
Assert.Fail($"Unexpected Exception thrown: {ex.Message}");
}
}

/// <summary>
/// This Test is used to verify that the validate command is able to catch when data source field is missing.
/// </summary>
[TestMethod]
public void TestValidateConfigFailsWithNoDataSource()
{
string ConfigWithoutDataSource = $"{{{SCHEMA_PROPERTY},{RUNTIME_SECTION_WITH_EMPTY_ENTITIES}}}";

// create an empty config file
((MockFileSystem)_fileSystem!).AddFile(TEST_RUNTIME_CONFIG_FILE, ConfigWithoutDataSource);

ValidateOptions validateOptions = new(TEST_RUNTIME_CONFIG_FILE);

try
{
Assert.IsFalse(ConfigGenerator.IsConfigValid(validateOptions, _runtimeConfigLoader!, _fileSystem!));
}
catch (Exception ex)
{
Assert.Fail($"Unexpected Exception thrown: {ex.Message}");
}
}

/// <summary>
/// This method implicitly validates that RuntimeConfigValidator::ValidateConfigSchema(...) successfully
/// executes against a config file referencing environment variables.
Expand Down
16 changes: 16 additions & 0 deletions src/Config/ObjectModel/RuntimeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,29 @@ public RuntimeConfig(
this.Entities = Entities;
this.DefaultDataSourceName = Guid.NewGuid().ToString();

if (this.DataSource is null)
{
throw new DataApiBuilderException(
message: "DataSource is a mandatory property in DAB Config",
sezal98 marked this conversation as resolved.
Show resolved Hide resolved
statusCode: HttpStatusCode.UnprocessableEntity,
sezal98 marked this conversation as resolved.
Show resolved Hide resolved
subStatusCode: DataApiBuilderException.SubStatusCodes.ConfigValidationError);
}

// we will set them up with default values
_dataSourceNameToDataSource = new Dictionary<string, DataSource>
{
{ this.DefaultDataSourceName, this.DataSource }
};

_entityNameToDataSourceName = new Dictionary<string, string>();
if (Entities is null)
{
throw new DataApiBuilderException(
message: "Entities is a mandatory property in DAB Config",
sezal98 marked this conversation as resolved.
Show resolved Hide resolved
sezal98 marked this conversation as resolved.
Show resolved Hide resolved
statusCode: HttpStatusCode.UnprocessableEntity,
subStatusCode: DataApiBuilderException.SubStatusCodes.ConfigValidationError);
}

foreach (KeyValuePair<string, Entity> entity in Entities)
{
_entityNameToDataSourceName.TryAdd(entity.Key, this.DefaultDataSourceName);
Expand Down