1919using System . Threading . Tasks ;
2020using System . Web ;
2121using Azure . DataApiBuilder . Config ;
22- using Azure . DataApiBuilder . Config . NamingPolicies ;
2322using Azure . DataApiBuilder . Config . ObjectModel ;
2423using Azure . DataApiBuilder . Core . AuthenticationHelpers ;
2524using Azure . DataApiBuilder . Core . Authorization ;
@@ -432,7 +431,7 @@ type Character {
432431 moons: [Moon],
433432}
434433
435- type Planet @model(name:""Planet "") {
434+ type Planet @model(name:""PlanetAlias "") {
436435 id : ID!,
437436 name : String,
438437 character: Character
@@ -453,7 +452,7 @@ type Character {
453452 moons: Moon,
454453}
455454
456- type Planet @model(name:""Planet "") {
455+ type Planet @model(name:""PlanetAlias "") {
457456 id : ID!,
458457 name : String,
459458 character: Character
@@ -3068,67 +3067,55 @@ public void ValidateGraphQLSchemaForCircularReference(string schema)
30683067 }
30693068
30703069 /// <summary>
3071- /// When you query, DAB loads schema and check for defined entities in the config file which get load during DAB initialization, and
3072- /// it fails during this check if entity is not defined in the config file. In this test case, we are testing the error message is appropriate.
3070+ /// GraphQL Schema types defined -> Character and Planet
3071+ /// DAB runtime config entities defined -> Planet(Not defined: Character)
3072+ /// Mismatch of entities and types between provided GraphQL schema file and DAB config results in actionable error message.
30733073 /// </summary>
3074+ /// <exception cref="ApplicationException"></exception>
30743075 [ TestMethod , TestCategory ( TestCategory . COSMOSDBNOSQL ) ]
3075- public async Task TestErrorMessageWithoutKeyFieldsInConfig ( )
3076+ public void ValidateGraphQLSchemaEntityPresentInConfig ( )
30763077 {
3077- Dictionary < string , object > dbOptions = new ( ) ;
3078- HyphenatedNamingPolicy namingPolicy = new ( ) ;
3079-
3080- dbOptions . Add ( namingPolicy . ConvertName ( nameof ( CosmosDbNoSQLDataSourceOptions . Database ) ) , "graphqldb" ) ;
3081- dbOptions . Add ( namingPolicy . ConvertName ( nameof ( CosmosDbNoSQLDataSourceOptions . Container ) ) , "dummy" ) ;
3082- dbOptions . Add ( namingPolicy . ConvertName ( nameof ( CosmosDbNoSQLDataSourceOptions . Schema ) ) , "custom-schema.gql" ) ;
3083-
3084- DataSource dataSource = new ( DatabaseType . CosmosDB_NoSQL ,
3085- GetConnectionStringFromEnvironmentConfig ( environment : TestCategory . COSMOSDBNOSQL ) , Options : dbOptions ) ;
3086-
3087- // Add a dummy entity in config file just to make sure the config file is valid.
3088- Entity entity = new (
3089- Source : new ( "EntityName" , EntitySourceType . Table , null , null ) ,
3090- Rest : new ( Enabled : false ) ,
3091- GraphQL : new ( "" , "" ) ,
3092- Permissions : new [ ] { GetMinimalPermissionConfig ( AuthorizationResolver . ROLE_ANONYMOUS ) } ,
3093- Relationships : null ,
3094- Mappings : null
3095- ) ;
3096- RuntimeConfig configuration = InitMinimalRuntimeConfig ( dataSource , new ( ) , new ( ) , entity , "EntityName" ) ;
3097-
3098- const string CUSTOM_CONFIG = "custom-config.json" ;
3099-
3100- File . WriteAllText ( CUSTOM_CONFIG , configuration . ToJson ( ) ) ;
3078+ string GRAPHQL_SCHEMA = @"
3079+ type Character {
3080+ id : ID,
3081+ name : String,
3082+ }
31013083
3102- string [ ] args = new [ ]
3084+ type Planet @model(name:""PlanetAlias"") {
3085+ id : ID!,
3086+ name : String,
3087+ characters : [Character]
3088+ }" ;
3089+ // Read the base config from the file system
3090+ TestHelper . SetupDatabaseEnvironment ( TestCategory . COSMOSDBNOSQL ) ;
3091+ FileSystemRuntimeConfigLoader baseLoader = TestHelper . GetRuntimeConfigLoader ( ) ;
3092+ if ( ! baseLoader . TryLoadKnownConfig ( out RuntimeConfig baseConfig ) )
31033093 {
3104- $ "--ConfigFileName= { CUSTOM_CONFIG } "
3105- } ;
3094+ throw new ApplicationException ( "Failed to load the default CosmosDB_NoSQL config and cannot continue with tests." ) ;
3095+ }
31063096
3107- using ( TestServer server = new ( Program . CreateWebHostBuilder ( args ) ) )
3108- using ( HttpClient client = server . CreateClient ( ) )
3109- {
3110- // When you query, DAB loads schema and check for defined entities in the config file and
3111- // it fails during that, since entity is not defined in the config file.
3112- string query = @"{
3113- Planet {
3114- items{
3115- id
3116- }
3117- }
3118- }" ;
3097+ Dictionary < string , Entity > entities = new ( baseConfig . Entities ) ;
3098+ entities . Remove ( "Character" ) ;
31193099
3120- object payload = new { query } ;
3100+ RuntimeConfig runtimeConfig = new ( Schema : baseConfig . Schema ,
3101+ DataSource : baseConfig . DataSource ,
3102+ Runtime : baseConfig . Runtime ,
3103+ Entities : new ( entities ) ) ;
31213104
3122- HttpRequestMessage graphQLRequest = new ( HttpMethod . Post , "/graphql" )
3123- {
3124- Content = JsonContent . Create ( payload )
3125- } ;
3105+ // Setup a mock file system, and use that one with the loader/provider for the config
3106+ MockFileSystem fileSystem = new ( new Dictionary < string , MockFileData > ( )
3107+ {
3108+ { @"../schema.gql" , new MockFileData ( GRAPHQL_SCHEMA ) } ,
3109+ { DEFAULT_CONFIG_FILE_NAME , new MockFileData ( runtimeConfig . ToJson ( ) ) }
3110+ } ) ;
3111+ FileSystemRuntimeConfigLoader loader = new ( fileSystem ) ;
3112+ RuntimeConfigProvider provider = new ( loader ) ;
31263113
3127- DataApiBuilderException ex = await Assert . ThrowsExceptionAsync < DataApiBuilderException > ( async ( ) => await client . SendAsync ( graphQLRequest ) ) ;
3128- Assert . AreEqual ( "The entity 'Planet' was not found in the runtime config." , ex . Message ) ;
3129- Assert . AreEqual ( HttpStatusCode . ServiceUnavailable , ex . StatusCode ) ;
3130- Assert . AreEqual ( DataApiBuilderException . SubStatusCodes . ConfigValidationError , ex . SubStatusCode ) ;
3131- }
3114+ DataApiBuilderException exception =
3115+ Assert . ThrowsException < DataApiBuilderException > ( ( ) => new CosmosSqlMetadataProvider ( provider , fileSystem ) ) ;
3116+ Assert . AreEqual ( "The entity 'Character' was not found in the runtime config." , exception . Message ) ;
3117+ Assert . AreEqual ( HttpStatusCode . ServiceUnavailable , exception . StatusCode ) ;
3118+ Assert . AreEqual ( DataApiBuilderException . SubStatusCodes . ConfigValidationError , exception . SubStatusCode ) ;
31323119 }
31333120
31343121 /// <summary>
0 commit comments