@@ -467,6 +467,69 @@ type Moon {
467
467
}
468
468
" ;
469
469
470
+ public const string CONFIG_FILE_WITH_NO_OPTIONAL_FIELD = @"{
471
+ ""$schema"":""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json"",
472
+ ""data-source"": {
473
+ ""database-type"": ""mssql"",
474
+ ""connection-string"": ""sample-conn-string""
475
+ },
476
+ ""entities"":{ }
477
+ }" ;
478
+
479
+ public const string CONFIG_FILE_WITH_NO_AUTHENTICATION_FIELD = @"{
480
+ // Link for latest draft schema.
481
+ ""$schema"":""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json"",
482
+ ""data-source"": {
483
+ ""database-type"": ""mssql"",
484
+ ""connection-string"": ""sample-conn-string""
485
+ },
486
+ ""runtime"": {
487
+ ""rest"": {
488
+ ""enabled"": true,
489
+ ""path"": ""/api""
490
+ },
491
+ ""graphql"": {
492
+ ""enabled"": true,
493
+ ""path"": ""/graphql"",
494
+ ""allow-introspection"": true
495
+ },
496
+ ""host"": {
497
+ ""cors"": {
498
+ ""origins"": [
499
+ ""http://localhost:5000""
500
+ ],
501
+ ""allow-credentials"": false
502
+ }
503
+ }
504
+ },
505
+ ""entities"":{ }
506
+ }" ;
507
+ public const string CONFIG_FILE_WITH_NO_CORS_FIELD = @"{
508
+ // Link for latest draft schema.
509
+ ""$schema"":""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json"",
510
+ ""data-source"": {
511
+ ""database-type"": ""mssql"",
512
+ ""connection-string"": ""sample-conn-string""
513
+ },
514
+ ""runtime"": {
515
+ ""rest"": {
516
+ ""enabled"": true,
517
+ ""path"": ""/api""
518
+ },
519
+ ""graphql"": {
520
+ ""enabled"": true,
521
+ ""path"": ""/graphql"",
522
+ ""allow-introspection"": true
523
+ },
524
+ ""host"": {
525
+ ""authentication"": {
526
+ ""provider"": ""StaticWebApps""
527
+ }
528
+ }
529
+ },
530
+ ""entities"":{ }
531
+ }" ;
532
+
470
533
[ TestCleanup ]
471
534
public void CleanupAfterEachTest ( )
472
535
{
@@ -1590,6 +1653,64 @@ public async Task TestConfigSchemaIsValid()
1590
1653
Times . Once ) ;
1591
1654
}
1592
1655
1656
+ /// <summary>
1657
+ /// This test method validates a sample DAB runtime config file against DAB's JSON schema definition.
1658
+ /// It asserts that the validation is successful and there are no validation failures when no optional fields are used.
1659
+ /// It also verifies that the expected log message is logged.
1660
+ /// </summary>
1661
+ [ DataTestMethod ]
1662
+ [ DataRow ( CONFIG_FILE_WITH_NO_OPTIONAL_FIELD , DisplayName = "Validates schema of the config file with no optional fields." ) ]
1663
+ [ DataRow ( CONFIG_FILE_WITH_NO_AUTHENTICATION_FIELD , DisplayName = "Validates schema of the config file with no Authentication field." ) ]
1664
+ [ DataRow ( CONFIG_FILE_WITH_NO_CORS_FIELD , DisplayName = "Validates schema of the config file with no Cors field." ) ]
1665
+ public async Task TestBasicConfigSchemaWithNoOptionalFieldsIsValid ( string jsonData )
1666
+ {
1667
+ Mock < ILogger < JsonConfigSchemaValidator > > schemaValidatorLogger = new ( ) ;
1668
+
1669
+ string jsonSchema = File . ReadAllText ( "dab.draft.schema.json" ) ;
1670
+
1671
+ JsonConfigSchemaValidator jsonSchemaValidator = new ( schemaValidatorLogger . Object , new MockFileSystem ( ) ) ;
1672
+
1673
+ JsonSchemaValidationResult result = await jsonSchemaValidator . ValidateJsonConfigWithSchemaAsync ( jsonSchema , jsonData ) ;
1674
+ Assert . IsTrue ( result . IsValid ) ;
1675
+ Assert . IsTrue ( EnumerableUtilities . IsNullOrEmpty ( result . ValidationErrors ) ) ;
1676
+ schemaValidatorLogger . Verify (
1677
+ x => x . Log (
1678
+ LogLevel . Information ,
1679
+ It . IsAny < EventId > ( ) ,
1680
+ It . Is < It . IsAnyType > ( ( o , t ) => o . ToString ( ) ! . Contains ( $ "The config satisfies the schema requirements.") ) ,
1681
+ It . IsAny < Exception > ( ) ,
1682
+ ( Func < It . IsAnyType , Exception , string > ) It . IsAny < object > ( ) ) ,
1683
+ Times . Once ) ;
1684
+ }
1685
+
1686
+ /// <summary>
1687
+ /// The config file does not contain any entity fields, which is expected to be invalid according to the schema.
1688
+ /// The test asserts that the validation fails and there are validation errors.
1689
+ /// It also verifies that the expected error message is logged, indicating that the 'entities' property is required.
1690
+ [ TestMethod ]
1691
+ public async Task TestBasicConfigSchemaWithNoEntityFieldsIsInvalid ( )
1692
+ {
1693
+ string jsonData = @"{
1694
+ ""$schema"":""https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch-alpha/dab.draft.schema.json"",
1695
+ ""data-source"": {
1696
+ ""database-type"": ""mssql"",
1697
+ ""connection-string"": ""sample-conn-string""
1698
+ }
1699
+ }" ;
1700
+
1701
+ Mock < ILogger < JsonConfigSchemaValidator > > schemaValidatorLogger = new ( ) ;
1702
+
1703
+ string jsonSchema = File . ReadAllText ( "dab.draft.schema.json" ) ;
1704
+
1705
+ JsonConfigSchemaValidator jsonSchemaValidator = new ( schemaValidatorLogger . Object , new MockFileSystem ( ) ) ;
1706
+
1707
+ JsonSchemaValidationResult result = await jsonSchemaValidator . ValidateJsonConfigWithSchemaAsync ( jsonSchema , jsonData ) ;
1708
+ Assert . IsFalse ( result . IsValid ) ;
1709
+ Assert . IsFalse ( EnumerableUtilities . IsNullOrEmpty ( result . ValidationErrors ) ) ;
1710
+ Assert . AreEqual ( 1 , result . ErrorCount ) ;
1711
+ Assert . IsTrue ( result . ErrorMessage . Contains ( "Total schema validation errors: 1\n > PropertyRequired: #/entities" ) ) ;
1712
+ }
1713
+
1593
1714
/// <summary>
1594
1715
/// This test tries to validate a runtime config file that is not compliant with the runtime config JSON schema.
1595
1716
/// It validates no additional properties are defined in the config file.
0 commit comments