1212using System . IO ;
1313using System . Text ;
1414using Xunit ;
15+ using static NBB . MultiTenancy . Abstractions . Configuration . TenantConfigurationExtensions ;
1516
1617namespace NBB . MultiTenancy . Abstractions . Tests
1718{
@@ -209,6 +210,7 @@ public void load_connectionString_splitted_without_defaults()
209210 ""Server"": ""server1"",
210211 ""Database"": ""db1"",
211212 ""UserName"": ""web"",
213+ ""Password"": ""pass"",
212214 ""OtherParams"": ""MultipleActiveResultSets=true""
213215 }
214216 }
@@ -242,7 +244,7 @@ public void load_connectionString_splitted_without_defaults()
242244
243245 // Assert
244246 tenantConfig . GetConnectionString ( "ConnectionString_" ) . Should ( ) . BeNull ( ) ;
245- tenantConfig . GetConnectionString ( "Leasing_Database" ) . Should ( ) . Be ( "Server=server1;Database=db1;User Id=web;Password=;MultipleActiveResultSets=true" ) ;
247+ tenantConfig . GetConnectionString ( "Leasing_Database" ) . Should ( ) . Be ( "Server=server1;Database=db1;User Id=web;Password=pass ;MultipleActiveResultSets=true" ) ;
246248 tenantConfig . GetValue < string > ( "TenantId" ) . Should ( ) . Be ( "68a448a2-e7d8-4875-8127-f18668217eb6" ) ;
247249 }
248250
@@ -258,7 +260,8 @@ public void load_connectionString_splitted_from_default()
258260 ""Leasing_Database"": {
259261 ""Server"": ""cf-erp17\\mama"",
260262 ""Database"": ""db1"",
261- ""UserName"": ""web""
263+ ""UserName"": ""web"",
264+ ""Password"": ""pass""
262265 }
263266 }
264267 },
@@ -303,7 +306,7 @@ public void load_connectionString_splitted_from_default()
303306
304307 // Assert
305308 tenantConfig . GetConnectionString ( "ConnectionString_" ) . Should ( ) . BeNull ( ) ;
306- tenantConfig . GetConnectionString ( "Leasing_Database" ) . Should ( ) . Be ( "Server=cf-erp17\\ mama;Database=db1;User Id=web;Password=;" ) ;
309+ tenantConfig . GetConnectionString ( "Leasing_Database" ) . Should ( ) . Be ( "Server=cf-erp17\\ mama;Database=db1;User Id=web;Password=pass ;" ) ;
307310 tenantConfig . GetValue < string > ( "TenantId" ) . Should ( ) . Be ( "ef8d5362-9969-4e02-8794-0d1af56816f6" ) ;
308311 }
309312
@@ -383,6 +386,7 @@ public void load_connectionString_with_concatenated_defaults()
383386 ""Server"": ""server1"",
384387 ""Database"": ""db1"",
385388 ""UserName"": ""web"",
389+ ""Password"": ""pass"",
386390 ""OtherParams"": ""MultipleActiveResultSets=true""
387391 }
388392 }
@@ -416,7 +420,7 @@ public void load_connectionString_with_concatenated_defaults()
416420
417421 // Assert
418422 tenantConfig . GetConnectionString ( "ConnectionString_" ) . Should ( ) . BeNull ( ) ;
419- tenantConfig . GetConnectionString ( "Leasing_Database" ) . Should ( ) . Be ( "Server=server1;Database=db1;User Id=web;Password=;MultipleActiveResultSets=true" ) ;
423+ tenantConfig . GetConnectionString ( "Leasing_Database" ) . Should ( ) . Be ( "Server=server1;Database=db1;User Id=web;Password=pass ;MultipleActiveResultSets=true" ) ;
420424 tenantConfig . GetValue < string > ( "TenantId" ) . Should ( ) . Be ( "68a448a2-e7d8-4875-8127-f18668217eb6" ) ;
421425 }
422426
@@ -476,6 +480,54 @@ public void load_connectionString_concatenated_with_splitted_defaults()
476480 tenantConfig . GetValue < string > ( "TenantId" ) . Should ( ) . Be ( "68a448a2-e7d8-4875-8127-f18668217eb6" ) ;
477481 }
478482
479-
483+ [ Fact ]
484+ public void load_connectionString_splitted_tenant1_missing_database ( )
485+ {
486+ // Arrange
487+ var myConfiguration = @"{
488+ ""MultiTenancy"": {
489+ ""Tenants"": {
490+ ""MBFS"": {
491+ ""TenantId"": ""68a448a2-e7d8-4875-8127-f18668217eb6"",
492+ ""ConnectionStrings"": {
493+ ""Leasing_Database"": {
494+ ""Server"": ""server1"",
495+ ""Database"": """",
496+ ""UserName"": ""web"",
497+ ""OtherParams"": ""MultipleActiveResultSets=true""
498+ }
499+ }
500+ },
501+ ""BCR"": {
502+ ""TenantId"": ""ef8d5362-9969-4e02-8794-0d1af56816f6"",
503+ ""Code"": ""BCR""
504+ },
505+ ""DEV"": {
506+ ""TenantId"": ""da84628a-2925-4b69-9116-a90dd5a72b1f"",
507+ ""Code"": ""DEV""
508+ }
509+ }
510+ }
511+ }" ;
512+
513+ var configuration = new ConfigurationBuilder ( )
514+ . AddInMemoryCollection ( )
515+ . AddJsonStream ( new MemoryStream ( Encoding . UTF8 . GetBytes ( myConfiguration ) ) )
516+ . Build ( ) ;
517+
518+ var tid = Guid . Parse ( "68a448a2-e7d8-4875-8127-f18668217eb6" ) ;
519+ var tca = new TenantContextAccessor ( ) ;
520+
521+ tca . TenantContext = new TenantContext ( new Tenant ( tid , "MBFS" ) ) ;
522+ var tenantConfig = new TenantConfiguration ( configuration ,
523+ new OptionsWrapper < TenancyHostingOptions > ( new TenancyHostingOptions ( )
524+ { TenancyType = TenancyType . MultiTenant } ) , tca ) ;
525+
526+ // Act
527+ Action act = ( ) => tenantConfig . GetConnectionString ( "Leasing_Database" ) ;
528+
529+ // Assert
530+ act . Should ( ) . Throw < TenantConnectionStringBuilderException > ( ) . WithMessage ( "Connection string part Database is not provided!" ) ;
531+ }
480532 }
481533}
0 commit comments